Commit 434e7da258b7cd4dc9faf3da30dd6f601c871553

Authored by Marko Tikvić
1 parent 23ce66f0f2
Exists in master

changed initialization

Showing 1 changed file with 31 additions and 18 deletions   Show diff stats
... ... @@ -9,16 +9,25 @@ import (
9 9 "time"
10 10 )
11 11  
12   -const MaxLogFileSize5MB int64 = 5 * 1024 * 1024
13   -const MaxLogFileSize1MB int64 = 1 * 1024 * 1024
14   -const MaxLogFileSize500KB int64 = 500 * 1024
15   -const MaxLogFileSize100KB int64 = 100 * 1024
16   -const MaxLogFileSize512B int64 = 512
  12 +type Option uint8
  13 +
  14 +const (
  15 + Events Option = 0x01
  16 + Errors Option = 0x02
  17 +)
  18 +
  19 +const (
  20 + MaxLogSize5MB int64 = 5 * 1024 * 1024
  21 + MaxLogSize1MB int64 = 1 * 1024 * 1024
  22 + MaxLogSize500KB int64 = 500 * 1024
  23 + MaxLogSize100KB int64 = 100 * 1024
  24 + MaxLogSize512B int64 = 512
  25 +)
17 26  
18 27 const ErrDirName = "error-logs"
19 28 const EvtDirName = "event-logs"
20 29  
21   -type Logger struct {
  30 +type goLogger struct {
22 31 muEv *sync.Mutex
23 32 eventf *os.File
24 33  
... ... @@ -28,16 +37,20 @@ type Logger struct {
28 37 errorf *os.File
29 38  
30 39 errorFileName string
  40 +
  41 + splitSize int64
31 42 }
32 43  
33   -var logger Logger
  44 +var logger goLogger
34 45  
35   -func Init(eventFileName, errorFileName string) error {
  46 +func Init(flags Option, splitSize int64) error {
36 47 timestamp := "_" + time.Now().Format(time.RFC3339)
37 48 var err error
38 49  
  50 + logger.splitSize = splitSize
  51 +
39 52 // event file/dir
40   - if eventFileName != "" {
  53 + if flags&Events > 0 {
41 54 err = os.Mkdir(EvtDirName, os.ModePerm)
42 55 if err != nil {
43 56 if !os.IsExist(err) {
... ... @@ -46,8 +59,8 @@ func Init(eventFileName, errorFileName string) error {
46 59 }
47 60 }
48 61  
49   - logger.eventFileName = eventFileName
50   - path := filepath.Join(EvtDirName, eventFileName+timestamp+".txt")
  62 + logger.eventFileName = "events"
  63 + path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt")
51 64 logger.eventf, err = os.Create(path)
52 65 if err != nil {
53 66 fmt.Printf("logger: new: couldn't create event log file\n")
... ... @@ -56,7 +69,7 @@ func Init(eventFileName, errorFileName string) error {
56 69 }
57 70  
58 71 // error file/dir
59   - if errorFileName != "" {
  72 + if flags&Errors > 0 {
60 73 err = os.Mkdir(ErrDirName, os.ModePerm)
61 74 if err != nil {
62 75 if !os.IsExist(err) {
... ... @@ -65,8 +78,8 @@ func Init(eventFileName, errorFileName string) error {
65 78 }
66 79 }
67 80  
68   - logger.errorFileName = errorFileName
69   - path := filepath.Join(ErrDirName, errorFileName+timestamp+".txt")
  81 + logger.errorFileName = "errors"
  82 + path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt")
70 83 logger.errorf, err = os.Create(path)
71 84 if err != nil {
72 85 fmt.Printf("logger: new: couldn't create error log file\n")
... ... @@ -147,11 +160,11 @@ func Close() {
147 160 }
148 161 }
149 162  
150   -func (l *Logger) splitEventLog() {
  163 +func (l *goLogger) splitEventLog() {
151 164 timestamp := "_" + time.Now().Format(time.RFC3339)
152 165  
153 166 evfstats, _ := l.eventf.Stat()
154   - if evfstats.Size() >= MaxLogFileSize100KB {
  167 + if evfstats.Size() >= l.splitSize {
155 168 // close old file
156 169 err := l.eventf.Close()
157 170 if err != nil {
... ... @@ -169,11 +182,11 @@ func (l *Logger) splitEventLog() {
169 182 }
170 183 }
171 184  
172   -func (l *Logger) splitErrorLog() {
  185 +func (l *goLogger) splitErrorLog() {
173 186 timestamp := "_" + time.Now().Format(time.RFC3339)
174 187  
175 188 erfstats, _ := l.errorf.Stat()
176   - if erfstats.Size() >= MaxLogFileSize100KB {
  189 + if erfstats.Size() >= l.splitSize {
177 190 // close old file
178 191 err := l.errorf.Close()
179 192 if err != nil {
... ...