From 5eaa49d2a41944dece41e209811d56e2959c0371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Mon, 26 Feb 2018 09:46:47 +0100 Subject: [PATCH] new design; allow for multiple instances --- main.go | 57 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 209c85d..3e9b27b 100644 --- a/main.go +++ b/main.go @@ -24,10 +24,10 @@ const ( MaxLogSize512B int64 = 512 ) -const ErrDirName = "error-logs" -const EvtDirName = "event-logs" +const errDirName = "error-logs" +const evtDirName = "event-logs" -type goLogger struct { +type Logger struct { muEv *sync.Mutex eventf *os.File @@ -41,30 +41,29 @@ type goLogger struct { splitSize int64 } -var logger goLogger - -func Init(flags Option, splitSize int64) error { - timestamp := "_" + time.Now().Format(time.RFC3339) - var err error +func New(name string, flags Option, splitSize int64) (logger *Logger, err error) { + logger = &Logger{} logger.splitSize = splitSize + timestamp := "_" + time.Now().Format(time.RFC3339) + // event file/dir if flags&Events > 0 { - err = os.Mkdir(EvtDirName, os.ModePerm) + 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 + return nil, err } } - logger.eventFileName = "events" - path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt") + logger.eventFileName = name + "-events" + path := filepath.Join(evtDirName, logger.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 + return nil, err } logger.muEv = &sync.Mutex{} @@ -72,33 +71,33 @@ func Init(flags Option, splitSize int64) error { // error file/dir if flags&Errors > 0 { - err = os.Mkdir(ErrDirName, os.ModePerm) + 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 + return nil, err } } - logger.errorFileName = "errors" - path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt") + logger.errorFileName = name + "-errors" + path := filepath.Join(errDirName, logger.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, err } logger.muEr = &sync.Mutex{} } - return nil + return logger, nil } -func Print(s string) { +func (logger *Logger) Print(s string) { fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n") } -func LogEvent(event string) { +func (logger *Logger) LogEvent(event string) { if logger.eventf != nil { logger.muEv.Lock() defer logger.muEv.Unlock() @@ -107,7 +106,7 @@ func LogEvent(event string) { } } -func LogError(comment string, err error) { +func (logger *Logger) LogError(comment string, err error) { if logger.errorf != nil { logger.muEr.Lock() defer logger.muEr.Unlock() @@ -116,7 +115,7 @@ func LogError(comment string, err error) { } } -func TraceEvent(event string) { +func (logger *Logger) TraceEvent(event string) { if logger.eventf != nil { logger.muEv.Lock() defer logger.muEv.Unlock() @@ -132,7 +131,7 @@ func TraceEvent(event string) { } } -func TraceError(err error) { +func (logger *Logger) TraceError(err error) { if logger.errorf != nil { logger.muEr.Lock() defer logger.muEr.Unlock() @@ -148,7 +147,7 @@ func TraceError(err error) { } } -func Close() { +func (logger *Logger) Close() { if logger.eventf != nil { err := logger.eventf.Close() if err != nil { @@ -164,7 +163,7 @@ func Close() { } } -func (l *goLogger) splitEventLog() { +func (l *Logger) splitEventLog() { timestamp := "_" + time.Now().Format(time.RFC3339) evfstats, _ := l.eventf.Stat() @@ -177,7 +176,7 @@ func (l *goLogger) splitEventLog() { } // open new file var errnew error - path := filepath.Join(EvtDirName, l.eventFileName+timestamp) + path := filepath.Join(evtDirName, l.eventFileName+timestamp) l.eventf, errnew = os.Create(path) if errnew != nil { @@ -186,7 +185,7 @@ func (l *goLogger) splitEventLog() { } } -func (l *goLogger) splitErrorLog() { +func (l *Logger) splitErrorLog() { timestamp := "_" + time.Now().Format(time.RFC3339) erfstats, _ := l.errorf.Stat() @@ -199,7 +198,7 @@ func (l *goLogger) splitErrorLog() { } // open new file var errnew error - path := filepath.Join(ErrDirName, l.errorFileName+timestamp) + path := filepath.Join(errDirName, l.errorFileName+timestamp) l.errorf, errnew = os.Create(path) if errnew != nil { -- 1.8.1.2