summaryrefslogtreecommitdiffstats
path: root/sms-service/src/sms/log/logger.go
blob: 15207fe7f8e3473d2dded3a69a5f9fb428a0888a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright 2018 Intel Corporation, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package log

import (
	"fmt"
	"log"
	"os"
)

var errL, warnL, infoL *log.Logger
var stdErr, stdWarn, stdInfo *log.Logger

// Init will be called by sms.go before any other packages use it
func Init(filePath string) {

	stdErr = log.New(os.Stderr, "ERROR: ", log.Lshortfile|log.LstdFlags)
	stdWarn = log.New(os.Stdout, "WARNING: ", log.Lshortfile|log.LstdFlags)
	stdInfo = log.New(os.Stdout, "INFO: ", log.Lshortfile|log.LstdFlags)

	if filePath == "" {
		// We will just to std streams
		return
	}

	f, err := os.Create(filePath)
	if err != nil {
		stdErr.Println("Unable to create log file: " + err.Error())
		return
	}

	errL = log.New(f, "ERROR: ", log.Lshortfile|log.LstdFlags)
	warnL = log.New(f, "WARNING: ", log.Lshortfile|log.LstdFlags)
	infoL = log.New(f, "INFO: ", log.Lshortfile|log.LstdFlags)
}

// WriteError writes output to the writer we have
// defined during its creation with ERROR prefix
func WriteError(msg string) {
	if errL != nil {
		errL.Output(2, fmt.Sprintln(msg))
	}
	if stdErr != nil {
		stdErr.Output(2, fmt.Sprintln(msg))
	}
}

// WriteWarn writes output to the writer we have
// defined during its creation with WARNING prefix
func WriteWarn(msg string) {
	if warnL != nil {
		warnL.Output(2, fmt.Sprintln(msg))
	}
	if stdWarn != nil {
		stdWarn.Output(2, fmt.Sprintln(msg))
	}
}

// WriteInfo writes output to the writer we have
// defined during its creation with INFO prefix
func WriteInfo(msg string) {
	if infoL != nil {
		infoL.Output(2, fmt.Sprintln(msg))
	}
	if stdInfo != nil {
		stdInfo.Output(2, fmt.Sprintln(msg))
	}
}

//CheckError is a helper function to reduce
//repetition of error checking blocks of code
func CheckError(err error, topic string) error {
	if err != nil {
		msg := topic + ": " + err.Error()
		if errL != nil {
			errL.Output(2, fmt.Sprintln(msg))
		}
		if stdErr != nil {
			stdErr.Output(2, fmt.Sprintln(msg))
		}
		return err
	}
	return nil
}