Commit e42ead15ba63ced439a0b8d5798011538ddd5665

Authored by Marko Tikvić
1 parent 434e7da258
Exists in master

mutex initialization

Showing 1 changed file with 4 additions and 0 deletions   Show diff stats
1 package gologger 1 package gologger
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "os" 5 "os"
6 "path/filepath" 6 "path/filepath"
7 "runtime" 7 "runtime"
8 "sync" 8 "sync"
9 "time" 9 "time"
10 ) 10 )
11 11
12 type Option uint8 12 type Option uint8
13 13
14 const ( 14 const (
15 Events Option = 0x01 15 Events Option = 0x01
16 Errors Option = 0x02 16 Errors Option = 0x02
17 ) 17 )
18 18
19 const ( 19 const (
20 MaxLogSize5MB int64 = 5 * 1024 * 1024 20 MaxLogSize5MB int64 = 5 * 1024 * 1024
21 MaxLogSize1MB int64 = 1 * 1024 * 1024 21 MaxLogSize1MB int64 = 1 * 1024 * 1024
22 MaxLogSize500KB int64 = 500 * 1024 22 MaxLogSize500KB int64 = 500 * 1024
23 MaxLogSize100KB int64 = 100 * 1024 23 MaxLogSize100KB int64 = 100 * 1024
24 MaxLogSize512B int64 = 512 24 MaxLogSize512B int64 = 512
25 ) 25 )
26 26
27 const ErrDirName = "error-logs" 27 const ErrDirName = "error-logs"
28 const EvtDirName = "event-logs" 28 const EvtDirName = "event-logs"
29 29
30 type goLogger struct { 30 type goLogger struct {
31 muEv *sync.Mutex 31 muEv *sync.Mutex
32 eventf *os.File 32 eventf *os.File
33 33
34 eventFileName string 34 eventFileName string
35 35
36 muEr *sync.Mutex 36 muEr *sync.Mutex
37 errorf *os.File 37 errorf *os.File
38 38
39 errorFileName string 39 errorFileName string
40 40
41 splitSize int64 41 splitSize int64
42 } 42 }
43 43
44 var logger goLogger 44 var logger goLogger
45 45
46 func Init(flags Option, splitSize int64) error { 46 func Init(flags Option, splitSize int64) error {
47 timestamp := "_" + time.Now().Format(time.RFC3339) 47 timestamp := "_" + time.Now().Format(time.RFC3339)
48 var err error 48 var err error
49 49
50 logger.splitSize = splitSize 50 logger.splitSize = splitSize
51 51
52 // event file/dir 52 // event file/dir
53 if flags&Events > 0 { 53 if flags&Events > 0 {
54 err = os.Mkdir(EvtDirName, os.ModePerm) 54 err = os.Mkdir(EvtDirName, os.ModePerm)
55 if err != nil { 55 if err != nil {
56 if !os.IsExist(err) { 56 if !os.IsExist(err) {
57 fmt.Printf("logger: mkdir: couldn't create event log directory\n") 57 fmt.Printf("logger: mkdir: couldn't create event log directory\n")
58 return err 58 return err
59 } 59 }
60 } 60 }
61 61
62 logger.eventFileName = "events" 62 logger.eventFileName = "events"
63 path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt") 63 path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt")
64 logger.eventf, err = os.Create(path) 64 logger.eventf, err = os.Create(path)
65 if err != nil { 65 if err != nil {
66 fmt.Printf("logger: new: couldn't create event log file\n") 66 fmt.Printf("logger: new: couldn't create event log file\n")
67 return err 67 return err
68 } 68 }
69
70 logger.muEv = &sync.Mutex{}
69 } 71 }
70 72
71 // error file/dir 73 // error file/dir
72 if flags&Errors > 0 { 74 if flags&Errors > 0 {
73 err = os.Mkdir(ErrDirName, os.ModePerm) 75 err = os.Mkdir(ErrDirName, os.ModePerm)
74 if err != nil { 76 if err != nil {
75 if !os.IsExist(err) { 77 if !os.IsExist(err) {
76 fmt.Printf("logger: new: couldn't create error log directory\n") 78 fmt.Printf("logger: new: couldn't create error log directory\n")
77 return err 79 return err
78 } 80 }
79 } 81 }
80 82
81 logger.errorFileName = "errors" 83 logger.errorFileName = "errors"
82 path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt") 84 path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt")
83 logger.errorf, err = os.Create(path) 85 logger.errorf, err = os.Create(path)
84 if err != nil { 86 if err != nil {
85 fmt.Printf("logger: new: couldn't create error log file\n") 87 fmt.Printf("logger: new: couldn't create error log file\n")
86 return err 88 return err
87 } 89 }
90
91 logger.muEr = &sync.Mutex{}
88 } 92 }
89 93
90 return nil 94 return nil
91 } 95 }
92 96
93 func Print(s string) { 97 func Print(s string) {
94 fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n") 98 fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n")
95 } 99 }
96 100
97 func LogEvent(event string) { 101 func LogEvent(event string) {
98 if logger.eventf != nil { 102 if logger.eventf != nil {
99 logger.muEv.Lock() 103 logger.muEv.Lock()
100 defer logger.muEv.Unlock() 104 defer logger.muEv.Unlock()
101 logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") 105 logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n")
102 logger.splitEventLog() 106 logger.splitEventLog()
103 } 107 }
104 } 108 }
105 109
106 func LogError(comment string, err error) { 110 func LogError(comment string, err error) {
107 if logger.errorf != nil { 111 if logger.errorf != nil {
108 logger.muEr.Lock() 112 logger.muEr.Lock()
109 defer logger.muEr.Unlock() 113 defer logger.muEr.Unlock()
110 logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") 114 logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n")
111 logger.splitErrorLog() 115 logger.splitErrorLog()
112 } 116 }
113 } 117 }
114 118
115 func TraceEvent(event string) { 119 func TraceEvent(event string) {
116 if logger.eventf != nil { 120 if logger.eventf != nil {
117 logger.muEv.Lock() 121 logger.muEv.Lock()
118 defer logger.muEv.Unlock() 122 defer logger.muEv.Unlock()
119 _, file, line, ok := runtime.Caller(1) 123 _, file, line, ok := runtime.Caller(1)
120 var s string 124 var s string
121 if ok { 125 if ok {
122 s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) 126 s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event)
123 } else { 127 } else {
124 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") 128 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n")
125 } 129 }
126 logger.eventf.WriteString(s) 130 logger.eventf.WriteString(s)
127 logger.splitEventLog() 131 logger.splitEventLog()
128 } 132 }
129 } 133 }
130 134
131 func TraceError(err error) { 135 func TraceError(err error) {
132 if logger.errorf != nil { 136 if logger.errorf != nil {
133 logger.muEr.Lock() 137 logger.muEr.Lock()
134 defer logger.muEr.Unlock() 138 defer logger.muEr.Unlock()
135 _, file, line, ok := runtime.Caller(1) 139 _, file, line, ok := runtime.Caller(1)
136 var s string 140 var s string
137 if ok { 141 if ok {
138 s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) 142 s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error())
139 } else { 143 } else {
140 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") 144 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n")
141 } 145 }
142 logger.errorf.WriteString(s) 146 logger.errorf.WriteString(s)
143 logger.splitErrorLog() 147 logger.splitErrorLog()
144 } 148 }
145 } 149 }
146 150
147 func Close() { 151 func Close() {
148 if logger.eventf != nil { 152 if logger.eventf != nil {
149 err := logger.eventf.Close() 153 err := logger.eventf.Close()
150 if err != nil { 154 if err != nil {
151 fmt.Printf("logger: on exit: couldn't close event log file\n") 155 fmt.Printf("logger: on exit: couldn't close event log file\n")
152 } 156 }
153 } 157 }
154 158
155 if logger.errorf != nil { 159 if logger.errorf != nil {
156 err := logger.errorf.Close() 160 err := logger.errorf.Close()
157 if err != nil { 161 if err != nil {
158 fmt.Printf("logger: on exit: couldn't close error log file\n") 162 fmt.Printf("logger: on exit: couldn't close error log file\n")
159 } 163 }
160 } 164 }
161 } 165 }
162 166
163 func (l *goLogger) splitEventLog() { 167 func (l *goLogger) splitEventLog() {
164 timestamp := "_" + time.Now().Format(time.RFC3339) 168 timestamp := "_" + time.Now().Format(time.RFC3339)
165 169
166 evfstats, _ := l.eventf.Stat() 170 evfstats, _ := l.eventf.Stat()
167 if evfstats.Size() >= l.splitSize { 171 if evfstats.Size() >= l.splitSize {
168 // close old file 172 // close old file
169 err := l.eventf.Close() 173 err := l.eventf.Close()
170 if err != nil { 174 if err != nil {
171 fmt.Printf("logger: split: couldn't close event file\n") 175 fmt.Printf("logger: split: couldn't close event file\n")
172 return 176 return
173 } 177 }
174 // open new file 178 // open new file
175 var errnew error 179 var errnew error
176 path := filepath.Join(EvtDirName, l.eventFileName+timestamp) 180 path := filepath.Join(EvtDirName, l.eventFileName+timestamp)
177 l.eventf, errnew = os.Create(path) 181 l.eventf, errnew = os.Create(path)
178 182
179 if errnew != nil { 183 if errnew != nil {
180 fmt.Printf("logger: split: couldn't create event log file\n") 184 fmt.Printf("logger: split: couldn't create event log file\n")
181 } 185 }
182 } 186 }
183 } 187 }
184 188
185 func (l *goLogger) splitErrorLog() { 189 func (l *goLogger) splitErrorLog() {
186 timestamp := "_" + time.Now().Format(time.RFC3339) 190 timestamp := "_" + time.Now().Format(time.RFC3339)
187 191
188 erfstats, _ := l.errorf.Stat() 192 erfstats, _ := l.errorf.Stat()
189 if erfstats.Size() >= l.splitSize { 193 if erfstats.Size() >= l.splitSize {
190 // close old file 194 // close old file
191 err := l.errorf.Close() 195 err := l.errorf.Close()
192 if err != nil { 196 if err != nil {
193 fmt.Printf("logger: split: couldn't close error file\n") 197 fmt.Printf("logger: split: couldn't close error file\n")
194 return 198 return
195 } 199 }
196 // open new file 200 // open new file
197 var errnew error 201 var errnew error
198 path := filepath.Join(ErrDirName, l.errorFileName+timestamp) 202 path := filepath.Join(ErrDirName, l.errorFileName+timestamp)
199 l.errorf, errnew = os.Create(path) 203 l.errorf, errnew = os.Create(path)
200 204
201 if errnew != nil { 205 if errnew != nil {
202 fmt.Printf("logger: split: couldn't create error log file\n") 206 fmt.Printf("logger: split: couldn't create error log file\n")
203 } 207 }
204 } 208 }
205 } 209 }
206 210