From 23ce66f0f2266774a446a78400019d5a8d904f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 14 Feb 2018 11:44:58 +0100 Subject: [PATCH] optional init parameters; relocated mutexes --- main.go | 151 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 83 insertions(+), 68 deletions(-) diff --git a/main.go b/main.go index 961e649..da1ebf8 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,6 @@ import ( "time" ) -var muEv = &sync.Mutex{} -var muEr = &sync.Mutex{} - const MaxLogFileSize5MB int64 = 5 * 1024 * 1024 const MaxLogFileSize1MB int64 = 1 * 1024 * 1024 const MaxLogFileSize500KB int64 = 500 * 1024 @@ -22,9 +19,14 @@ const ErrDirName = "error-logs" const EvtDirName = "event-logs" type Logger struct { - eventf *os.File + muEv *sync.Mutex + eventf *os.File + eventFileName string - errorf *os.File + + muEr *sync.Mutex + errorf *os.File + errorFileName string } @@ -32,42 +34,44 @@ var logger Logger func Init(eventFileName, errorFileName string) error { timestamp := "_" + time.Now().Format(time.RFC3339) + var err error - // create directories to keep log files - err := os.Mkdir(EvtDirName, os.ModePerm) - if err != nil { - if !os.IsExist(err) { - fmt.Printf("logger: mkdir: couldn't create event log directory\n") - return err + // event file/dir + if eventFileName != "" { + err = os.Mkdir(EvtDirName, os.ModePerm) + if err != nil { + if !os.IsExist(err) { + fmt.Printf("logger: mkdir: couldn't create event log directory\n") + return err + } } - } - err = os.Mkdir(ErrDirName, os.ModePerm) - if err != nil { - if !os.IsExist(err) { - fmt.Printf("logger: new: couldn't create error log directory\n") + logger.eventFileName = eventFileName + path := filepath.Join(EvtDirName, eventFileName+timestamp+".txt") + logger.eventf, err = os.Create(path) + if err != nil { + fmt.Printf("logger: new: couldn't create event log file\n") return err } } - // create files - logger.eventFileName = eventFileName - logger.errorFileName = errorFileName - - path := filepath.Join(EvtDirName, eventFileName+timestamp+".txt") - logger.eventf, err = os.Create(path) - - if err != nil { - fmt.Printf("logger: new: couldn't create event log file\n") - return err - } - - path = filepath.Join(ErrDirName, errorFileName+timestamp+".txt") - logger.errorf, err = os.Create(path) + // error file/dir + if errorFileName != "" { + err = os.Mkdir(ErrDirName, os.ModePerm) + if err != nil { + if !os.IsExist(err) { + fmt.Printf("logger: new: couldn't create error log directory\n") + return err + } + } - if err != nil { - fmt.Printf("logger: new: couldn't create error log file\n") - return err + logger.errorFileName = errorFileName + path := filepath.Join(ErrDirName, errorFileName+timestamp+".txt") + logger.errorf, err = os.Create(path) + if err != nil { + fmt.Printf("logger: new: couldn't create error log file\n") + return err + } } return nil @@ -78,57 +82,68 @@ func Print(s string) { } func LogEvent(event string) { - muEv.Lock() - defer muEv.Unlock() - logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") - logger.splitEventLog() + if logger.eventf != nil { + logger.muEv.Lock() + defer logger.muEv.Unlock() + logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") + logger.splitEventLog() + } } func LogError(comment string, err error) { - muEr.Lock() - defer muEr.Unlock() - logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") - logger.splitErrorLog() + if logger.errorf != nil { + logger.muEr.Lock() + defer logger.muEr.Unlock() + logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") + logger.splitErrorLog() + } } func TraceEvent(event string) { - muEv.Lock() - defer muEv.Unlock() - _, file, line, ok := runtime.Caller(1) - var s string - if ok { - s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) - } else { - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") + if logger.eventf != nil { + logger.muEv.Lock() + defer logger.muEv.Unlock() + _, file, line, ok := runtime.Caller(1) + var s string + if ok { + s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) + } else { + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") + } + logger.eventf.WriteString(s) + logger.splitEventLog() } - logger.eventf.WriteString(s) - logger.splitEventLog() } func TraceError(err error) { - muEr.Lock() - defer muEr.Unlock() - _, file, line, ok := runtime.Caller(1) - var s string - if ok { - s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) - } else { - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") + if logger.errorf != nil { + logger.muEr.Lock() + defer logger.muEr.Unlock() + _, file, line, ok := runtime.Caller(1) + var s string + if ok { + s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) + } else { + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") + } + logger.errorf.WriteString(s) + logger.splitErrorLog() } - logger.errorf.WriteString(s) - logger.splitErrorLog() } func Close() { - // close files - err := logger.eventf.Close() - if err != nil { - fmt.Printf("logger: on exit: couldn't close event log file\n") + if logger.eventf != nil { + err := logger.eventf.Close() + if err != nil { + fmt.Printf("logger: on exit: couldn't close event log file\n") + } } - err = logger.errorf.Close() - if err != nil { - fmt.Printf("logger: on exit: couldn't close error log file\n") + if logger.errorf != nil { + err := logger.errorf.Close() + if err != nil { + fmt.Printf("logger: on exit: couldn't close error log file\n") + } } } -- 1.8.1.2