From 434e7da258b7cd4dc9faf3da30dd6f601c871553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 14 Feb 2018 12:06:34 +0100 Subject: [PATCH] changed initialization --- main.go | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index da1ebf8..4f89f04 100644 --- a/main.go +++ b/main.go @@ -9,16 +9,25 @@ import ( "time" ) -const MaxLogFileSize5MB int64 = 5 * 1024 * 1024 -const MaxLogFileSize1MB int64 = 1 * 1024 * 1024 -const MaxLogFileSize500KB int64 = 500 * 1024 -const MaxLogFileSize100KB int64 = 100 * 1024 -const MaxLogFileSize512B int64 = 512 +type Option uint8 + +const ( + Events Option = 0x01 + Errors Option = 0x02 +) + +const ( + MaxLogSize5MB int64 = 5 * 1024 * 1024 + MaxLogSize1MB int64 = 1 * 1024 * 1024 + MaxLogSize500KB int64 = 500 * 1024 + MaxLogSize100KB int64 = 100 * 1024 + MaxLogSize512B int64 = 512 +) const ErrDirName = "error-logs" const EvtDirName = "event-logs" -type Logger struct { +type goLogger struct { muEv *sync.Mutex eventf *os.File @@ -28,16 +37,20 @@ type Logger struct { errorf *os.File errorFileName string + + splitSize int64 } -var logger Logger +var logger goLogger -func Init(eventFileName, errorFileName string) error { +func Init(flags Option, splitSize int64) error { timestamp := "_" + time.Now().Format(time.RFC3339) var err error + logger.splitSize = splitSize + // event file/dir - if eventFileName != "" { + if flags&Events > 0 { err = os.Mkdir(EvtDirName, os.ModePerm) if err != nil { if !os.IsExist(err) { @@ -46,8 +59,8 @@ func Init(eventFileName, errorFileName string) error { } } - logger.eventFileName = eventFileName - path := filepath.Join(EvtDirName, eventFileName+timestamp+".txt") + logger.eventFileName = "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") @@ -56,7 +69,7 @@ func Init(eventFileName, errorFileName string) error { } // error file/dir - if errorFileName != "" { + if flags&Errors > 0 { err = os.Mkdir(ErrDirName, os.ModePerm) if err != nil { if !os.IsExist(err) { @@ -65,8 +78,8 @@ func Init(eventFileName, errorFileName string) error { } } - logger.errorFileName = errorFileName - path := filepath.Join(ErrDirName, errorFileName+timestamp+".txt") + logger.errorFileName = "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") @@ -147,11 +160,11 @@ func Close() { } } -func (l *Logger) splitEventLog() { +func (l *goLogger) splitEventLog() { timestamp := "_" + time.Now().Format(time.RFC3339) evfstats, _ := l.eventf.Stat() - if evfstats.Size() >= MaxLogFileSize100KB { + if evfstats.Size() >= l.splitSize { // close old file err := l.eventf.Close() if err != nil { @@ -169,11 +182,11 @@ func (l *Logger) splitEventLog() { } } -func (l *Logger) splitErrorLog() { +func (l *goLogger) splitErrorLog() { timestamp := "_" + time.Now().Format(time.RFC3339) erfstats, _ := l.errorf.Stat() - if erfstats.Size() >= MaxLogFileSize100KB { + if erfstats.Size() >= l.splitSize { // close old file err := l.errorf.Close() if err != nil { -- 1.8.1.2