2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 var errL, warnL, infoL *log.Logger
26 var stdErr, stdWarn, stdInfo *log.Logger
28 // Init will be called by sms.go before any other packages use it
29 func Init(filePath string) {
31 stdErr = log.New(os.Stderr, "ERROR: ", log.Lshortfile|log.LstdFlags)
32 stdWarn = log.New(os.Stdout, "WARNING: ", log.Lshortfile|log.LstdFlags)
33 stdInfo = log.New(os.Stdout, "INFO: ", log.Lshortfile|log.LstdFlags)
36 // We will just to std streams
40 f, err := os.Create(filePath)
42 stdErr.Println("Unable to create log file: " + err.Error())
46 errL = log.New(f, "ERROR: ", log.Lshortfile|log.LstdFlags)
47 warnL = log.New(f, "WARNING: ", log.Lshortfile|log.LstdFlags)
48 infoL = log.New(f, "INFO: ", log.Lshortfile|log.LstdFlags)
51 // WriteError writes output to the writer we have
52 // defined during its creation with ERROR prefix
53 func WriteError(msg string) {
55 errL.Output(2, fmt.Sprintln(msg))
58 stdErr.Output(2, fmt.Sprintln(msg))
62 // WriteWarn writes output to the writer we have
63 // defined during its creation with WARNING prefix
64 func WriteWarn(msg string) {
66 warnL.Output(2, fmt.Sprintln(msg))
69 stdWarn.Output(2, fmt.Sprintln(msg))
73 // WriteInfo writes output to the writer we have
74 // defined during its creation with INFO prefix
75 func WriteInfo(msg string) {
77 infoL.Output(2, fmt.Sprintln(msg))
80 stdInfo.Output(2, fmt.Sprintln(msg))
84 //CheckError is a helper function to reduce
85 //repetition of error checking blocks of code
86 func CheckError(err error, topic string) error {
88 msg := topic + ": " + err.Error()
90 errL.Output(2, fmt.Sprintln(msg))
93 stdErr.Output(2, fmt.Sprintln(msg))