Commit a58576e6c575546dea8df36ab3d951e509d3ee6e

Authored by Marko Tikvić
1 parent 3ba7b56957
Exists in master

fixed stupid thread lock

Showing 1 changed file with 0 additions and 6 deletions   Show diff stats
1 // @TODO: Implement mutex lock for files access. 1 // @TODO: Implement mutex lock for files access.
2 package gologger 2 package gologger
3 3
4 import ( 4 import (
5 "fmt" 5 "fmt"
6 "os" 6 "os"
7 "time" 7 "time"
8 "strconv" 8 "strconv"
9 "sync" 9 "sync"
10 ) 10 )
11 11
12 var muEv = &sync.Mutex{} 12 var muEv = &sync.Mutex{}
13 var muEr = &sync.Mutex{} 13 var muEr = &sync.Mutex{}
14 14
15 const MaxLogFileSize5MB int64 = 5*1024*1024 15 const MaxLogFileSize5MB int64 = 5*1024*1024
16 const MaxLogFileSize1MB int64 = 1*1024*1024 16 const MaxLogFileSize1MB int64 = 1*1024*1024
17 const MaxLogFileSize500KB int64 = 500*1024 17 const MaxLogFileSize500KB int64 = 500*1024
18 const MaxLogFileSize100KB int64 = 100*1024 18 const MaxLogFileSize100KB int64 = 100*1024
19 const MaxLogFileSize512B int64 = 512 19 const MaxLogFileSize512B int64 = 512
20 20
21 const ErrDirName = "error-logs" 21 const ErrDirName = "error-logs"
22 const EvtDirName = "event-logs" 22 const EvtDirName = "event-logs"
23 23
24 type Logger struct { 24 type Logger struct {
25 eventf *os.File 25 eventf *os.File
26 eventFileName string 26 eventFileName string
27 errorf *os.File 27 errorf *os.File
28 errorFileName string 28 errorFileName string
29 } 29 }
30 30
31 func NewLogger(eventFileName, errorFileName string) (*Logger, error) { 31 func NewLogger(eventFileName, errorFileName string) (*Logger, error) {
32 var l Logger 32 var l Logger
33 var err error 33 var err error
34 34
35 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) 35 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10)
36 36
37 // create files 37 // create files
38 l.eventFileName = eventFileName 38 l.eventFileName = eventFileName
39 l.errorFileName = errorFileName 39 l.errorFileName = errorFileName
40 40
41 l.eventf, err = os.Create("./" + EvtDirName + "/" + 41 l.eventf, err = os.Create("./" + EvtDirName + "/" +
42 eventFileName + timestamp + ".txt") 42 eventFileName + timestamp + ".txt")
43 43
44 if err != nil { 44 if err != nil {
45 fmt.Printf("logger: new: couldn't create event log file\n") 45 fmt.Printf("logger: new: couldn't create event log file\n")
46 return &l, err 46 return &l, err
47 } 47 }
48 48
49 l.errorf, err = os.Create("./" + ErrDirName + "/" + 49 l.errorf, err = os.Create("./" + ErrDirName + "/" +
50 errorFileName + timestamp + ".txt") 50 errorFileName + timestamp + ".txt")
51 51
52 if err != nil { 52 if err != nil {
53 fmt.Printf("logger: new: couldn't create error log file\n") 53 fmt.Printf("logger: new: couldn't create error log file\n")
54 return &l, err 54 return &l, err
55 } 55 }
56 56
57 // create directories to keep log files 57 // create directories to keep log files
58 err = os.Mkdir(EvtDirName, os.ModePerm) 58 err = os.Mkdir(EvtDirName, os.ModePerm)
59 if err != nil { 59 if err != nil {
60 if !os.IsExist(err) { 60 if !os.IsExist(err) {
61 fmt.Printf("logger: mkdir: couldn't create event log directory\n") 61 fmt.Printf("logger: mkdir: couldn't create event log directory\n")
62 return &l, err 62 return &l, err
63 } 63 }
64 } 64 }
65 65
66 err = os.Mkdir(ErrDirName, os.ModePerm) 66 err = os.Mkdir(ErrDirName, os.ModePerm)
67 if err != nil { 67 if err != nil {
68 if !os.IsExist(err) { 68 if !os.IsExist(err) {
69 fmt.Printf("logger: new: couldn't create error log directory\n") 69 fmt.Printf("logger: new: couldn't create error log directory\n")
70 return &l, err 70 return &l, err
71 } 71 }
72 } 72 }
73 73
74 return &l, nil 74 return &l, nil
75 } 75 }
76 76
77 func (l *Logger) Close() { 77 func (l *Logger) Close() {
78 // close files 78 // close files
79 err := l.eventf.Close() 79 err := l.eventf.Close()
80 if err != nil { 80 if err != nil {
81 fmt.Printf("logger: on exit: couldn't close event log file\n") 81 fmt.Printf("logger: on exit: couldn't close event log file\n")
82 } 82 }
83 83
84 err = l.errorf.Close() 84 err = l.errorf.Close()
85 if err != nil { 85 if err != nil {
86 fmt.Printf("logger: on exit: couldn't close error log file\n") 86 fmt.Printf("logger: on exit: couldn't close error log file\n")
87 } 87 }
88 } 88 }
89 89
90 func (l *Logger) splitEventLog() { 90 func (l *Logger) splitEventLog() {
91 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) 91 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10)
92 92
93 muEv.Lock()
94 defer muEv.Unlock()
95
96 evfstats, _ := l.eventf.Stat() 93 evfstats, _ := l.eventf.Stat()
97 if evfstats.Size() >= MaxLogFileSize100KB { 94 if evfstats.Size() >= MaxLogFileSize100KB {
98 // close old file 95 // close old file
99 err := l.eventf.Close() 96 err := l.eventf.Close()
100 if err != nil { 97 if err != nil {
101 fmt.Printf("logger: split: couldn't close event file\n") 98 fmt.Printf("logger: split: couldn't close event file\n")
102 return 99 return
103 } 100 }
104 // open new file 101 // open new file
105 var errnew error 102 var errnew error
106 l.eventf, errnew = os.Create("./" + EvtDirName + "/" + 103 l.eventf, errnew = os.Create("./" + EvtDirName + "/" +
107 l.eventFileName + timestamp + ".txt") 104 l.eventFileName + timestamp + ".txt")
108 105
109 if errnew != nil { 106 if errnew != nil {
110 fmt.Printf("logger: split: couldn't create event log file\n") 107 fmt.Printf("logger: split: couldn't create event log file\n")
111 } 108 }
112 } 109 }
113 } 110 }
114 111
115 func (l *Logger) splitErrorLog() { 112 func (l *Logger) splitErrorLog() {
116 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) 113 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10)
117 114
118 muEr.Lock()
119 defer muEr.Unlock()
120
121 erfstats, _ := l.errorf.Stat() 115 erfstats, _ := l.errorf.Stat()
122 if erfstats.Size() >= MaxLogFileSize100KB { 116 if erfstats.Size() >= MaxLogFileSize100KB {
123 // close old file 117 // close old file
124 err := l.errorf.Close() 118 err := l.errorf.Close()
125 if err != nil { 119 if err != nil {
126 fmt.Printf("logger: split: couldn't close error file\n") 120 fmt.Printf("logger: split: couldn't close error file\n")
127 return 121 return
128 } 122 }
129 // open new file 123 // open new file
130 var errnew error 124 var errnew error
131 l.errorf, errnew = os.Create("./" + ErrDirName + "/" + 125 l.errorf, errnew = os.Create("./" + ErrDirName + "/" +
132 l.errorFileName + timestamp + ".txt") 126 l.errorFileName + timestamp + ".txt")
133 127
134 if errnew != nil { 128 if errnew != nil {
135 fmt.Printf("logger: split: couldn't create error log file\n") 129 fmt.Printf("logger: split: couldn't create error log file\n")
136 } 130 }
137 } 131 }
138 } 132 }
139 133
140 func (l *Logger) LogEvent(event string) { 134 func (l *Logger) LogEvent(event string) {
141 muEv.Lock() 135 muEv.Lock()
142 defer muEv.Unlock() 136 defer muEv.Unlock()
143 l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n") 137 l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n")
144 l.splitEventLog() 138 l.splitEventLog()
145 } 139 }
146 140
147 func (l *Logger) LogError(err error) { 141 func (l *Logger) LogError(err error) {
148 muEr.Lock() 142 muEr.Lock()
149 defer muEr.Unlock() 143 defer muEr.Unlock()
150 l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + err.Error() + "\n") 144 l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + err.Error() + "\n")
151 l.splitErrorLog() 145 l.splitErrorLog()
152 } 146 }
153 147