Commit 23ce66f0f2266774a446a78400019d5a8d904f54
1 parent
327c98634f
Exists in
master
optional init parameters; relocated mutexes
Showing
1 changed file
with
83 additions
and
68 deletions
Show diff stats
main.go
... | ... | @@ -9,9 +9,6 @@ import ( |
9 | 9 | "time" |
10 | 10 | ) |
11 | 11 | |
12 | -var muEv = &sync.Mutex{} | |
13 | -var muEr = &sync.Mutex{} | |
14 | - | |
15 | 12 | const MaxLogFileSize5MB int64 = 5 * 1024 * 1024 |
16 | 13 | const MaxLogFileSize1MB int64 = 1 * 1024 * 1024 |
17 | 14 | const MaxLogFileSize500KB int64 = 500 * 1024 |
... | ... | @@ -22,9 +19,14 @@ const ErrDirName = "error-logs" |
22 | 19 | const EvtDirName = "event-logs" |
23 | 20 | |
24 | 21 | type Logger struct { |
25 | - eventf *os.File | |
22 | + muEv *sync.Mutex | |
23 | + eventf *os.File | |
24 | + | |
26 | 25 | eventFileName string |
27 | - errorf *os.File | |
26 | + | |
27 | + muEr *sync.Mutex | |
28 | + errorf *os.File | |
29 | + | |
28 | 30 | errorFileName string |
29 | 31 | } |
30 | 32 | |
... | ... | @@ -32,42 +34,44 @@ var logger Logger |
32 | 34 | |
33 | 35 | func Init(eventFileName, errorFileName string) error { |
34 | 36 | timestamp := "_" + time.Now().Format(time.RFC3339) |
37 | + var err error | |
35 | 38 | |
36 | - // create directories to keep log files | |
37 | - err := os.Mkdir(EvtDirName, os.ModePerm) | |
38 | - if err != nil { | |
39 | - if !os.IsExist(err) { | |
40 | - fmt.Printf("logger: mkdir: couldn't create event log directory\n") | |
41 | - return err | |
39 | + // event file/dir | |
40 | + if eventFileName != "" { | |
41 | + err = os.Mkdir(EvtDirName, os.ModePerm) | |
42 | + if err != nil { | |
43 | + if !os.IsExist(err) { | |
44 | + fmt.Printf("logger: mkdir: couldn't create event log directory\n") | |
45 | + return err | |
46 | + } | |
42 | 47 | } |
43 | - } | |
44 | 48 | |
45 | - err = os.Mkdir(ErrDirName, os.ModePerm) | |
46 | - if err != nil { | |
47 | - if !os.IsExist(err) { | |
48 | - fmt.Printf("logger: new: couldn't create error log directory\n") | |
49 | + logger.eventFileName = eventFileName | |
50 | + path := filepath.Join(EvtDirName, eventFileName+timestamp+".txt") | |
51 | + logger.eventf, err = os.Create(path) | |
52 | + if err != nil { | |
53 | + fmt.Printf("logger: new: couldn't create event log file\n") | |
49 | 54 | return err |
50 | 55 | } |
51 | 56 | } |
52 | 57 | |
53 | - // create files | |
54 | - logger.eventFileName = eventFileName | |
55 | - logger.errorFileName = errorFileName | |
56 | - | |
57 | - path := filepath.Join(EvtDirName, eventFileName+timestamp+".txt") | |
58 | - logger.eventf, err = os.Create(path) | |
59 | - | |
60 | - if err != nil { | |
61 | - fmt.Printf("logger: new: couldn't create event log file\n") | |
62 | - return err | |
63 | - } | |
64 | - | |
65 | - path = filepath.Join(ErrDirName, errorFileName+timestamp+".txt") | |
66 | - logger.errorf, err = os.Create(path) | |
58 | + // error file/dir | |
59 | + if errorFileName != "" { | |
60 | + err = os.Mkdir(ErrDirName, os.ModePerm) | |
61 | + if err != nil { | |
62 | + if !os.IsExist(err) { | |
63 | + fmt.Printf("logger: new: couldn't create error log directory\n") | |
64 | + return err | |
65 | + } | |
66 | + } | |
67 | 67 | |
68 | - if err != nil { | |
69 | - fmt.Printf("logger: new: couldn't create error log file\n") | |
70 | - return err | |
68 | + logger.errorFileName = errorFileName | |
69 | + path := filepath.Join(ErrDirName, errorFileName+timestamp+".txt") | |
70 | + logger.errorf, err = os.Create(path) | |
71 | + if err != nil { | |
72 | + fmt.Printf("logger: new: couldn't create error log file\n") | |
73 | + return err | |
74 | + } | |
71 | 75 | } |
72 | 76 | |
73 | 77 | return nil |
... | ... | @@ -78,57 +82,68 @@ func Print(s string) { |
78 | 82 | } |
79 | 83 | |
80 | 84 | func LogEvent(event string) { |
81 | - muEv.Lock() | |
82 | - defer muEv.Unlock() | |
83 | - logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") | |
84 | - logger.splitEventLog() | |
85 | + if logger.eventf != nil { | |
86 | + logger.muEv.Lock() | |
87 | + defer logger.muEv.Unlock() | |
88 | + logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") | |
89 | + logger.splitEventLog() | |
90 | + } | |
85 | 91 | } |
86 | 92 | |
87 | 93 | func LogError(comment string, err error) { |
88 | - muEr.Lock() | |
89 | - defer muEr.Unlock() | |
90 | - logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") | |
91 | - logger.splitErrorLog() | |
94 | + if logger.errorf != nil { | |
95 | + logger.muEr.Lock() | |
96 | + defer logger.muEr.Unlock() | |
97 | + logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") | |
98 | + logger.splitErrorLog() | |
99 | + } | |
92 | 100 | } |
93 | 101 | |
94 | 102 | func TraceEvent(event string) { |
95 | - muEv.Lock() | |
96 | - defer muEv.Unlock() | |
97 | - _, file, line, ok := runtime.Caller(1) | |
98 | - var s string | |
99 | - if ok { | |
100 | - s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) | |
101 | - } else { | |
102 | - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") | |
103 | + if logger.eventf != nil { | |
104 | + logger.muEv.Lock() | |
105 | + defer logger.muEv.Unlock() | |
106 | + _, file, line, ok := runtime.Caller(1) | |
107 | + var s string | |
108 | + if ok { | |
109 | + s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) | |
110 | + } else { | |
111 | + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") | |
112 | + } | |
113 | + logger.eventf.WriteString(s) | |
114 | + logger.splitEventLog() | |
103 | 115 | } |
104 | - logger.eventf.WriteString(s) | |
105 | - logger.splitEventLog() | |
106 | 116 | } |
107 | 117 | |
108 | 118 | func TraceError(err error) { |
109 | - muEr.Lock() | |
110 | - defer muEr.Unlock() | |
111 | - _, file, line, ok := runtime.Caller(1) | |
112 | - var s string | |
113 | - if ok { | |
114 | - s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) | |
115 | - } else { | |
116 | - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") | |
119 | + if logger.errorf != nil { | |
120 | + logger.muEr.Lock() | |
121 | + defer logger.muEr.Unlock() | |
122 | + _, file, line, ok := runtime.Caller(1) | |
123 | + var s string | |
124 | + if ok { | |
125 | + s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) | |
126 | + } else { | |
127 | + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") | |
128 | + } | |
129 | + logger.errorf.WriteString(s) | |
130 | + logger.splitErrorLog() | |
117 | 131 | } |
118 | - logger.errorf.WriteString(s) | |
119 | - logger.splitErrorLog() | |
120 | 132 | } |
121 | 133 | |
122 | 134 | func Close() { |
123 | - // close files | |
124 | - err := logger.eventf.Close() | |
125 | - if err != nil { | |
126 | - fmt.Printf("logger: on exit: couldn't close event log file\n") | |
135 | + if logger.eventf != nil { | |
136 | + err := logger.eventf.Close() | |
137 | + if err != nil { | |
138 | + fmt.Printf("logger: on exit: couldn't close event log file\n") | |
139 | + } | |
127 | 140 | } |
128 | 141 | |
129 | - err = logger.errorf.Close() | |
130 | - if err != nil { | |
131 | - fmt.Printf("logger: on exit: couldn't close error log file\n") | |
142 | + if logger.errorf != nil { | |
143 | + err := logger.errorf.Close() | |
144 | + if err != nil { | |
145 | + fmt.Printf("logger: on exit: couldn't close error log file\n") | |
146 | + } | |
132 | 147 | } |
133 | 148 | } |
134 | 149 | ... | ... |