Commit 5eaa49d2a41944dece41e209811d56e2959c0371

Authored by Marko Tikvić
1 parent e42ead15ba
Exists in master

new design; allow for multiple instances

Showing 1 changed file with 28 additions and 29 deletions   Show diff stats
... ... @@ -24,10 +24,10 @@ const (
24 24 MaxLogSize512B int64 = 512
25 25 )
26 26  
27   -const ErrDirName = "error-logs"
28   -const EvtDirName = "event-logs"
  27 +const errDirName = "error-logs"
  28 +const evtDirName = "event-logs"
29 29  
30   -type goLogger struct {
  30 +type Logger struct {
31 31 muEv *sync.Mutex
32 32 eventf *os.File
33 33  
... ... @@ -41,30 +41,29 @@ type goLogger struct {
41 41 splitSize int64
42 42 }
43 43  
44   -var logger goLogger
45   -
46   -func Init(flags Option, splitSize int64) error {
47   - timestamp := "_" + time.Now().Format(time.RFC3339)
48   - var err error
  44 +func New(name string, flags Option, splitSize int64) (logger *Logger, err error) {
  45 + logger = &Logger{}
49 46  
50 47 logger.splitSize = splitSize
51 48  
  49 + timestamp := "_" + time.Now().Format(time.RFC3339)
  50 +
52 51 // event file/dir
53 52 if flags&Events > 0 {
54   - err = os.Mkdir(EvtDirName, os.ModePerm)
  53 + err = os.Mkdir(evtDirName, os.ModePerm)
55 54 if err != nil {
56 55 if !os.IsExist(err) {
57 56 fmt.Printf("logger: mkdir: couldn't create event log directory\n")
58   - return err
  57 + return nil, err
59 58 }
60 59 }
61 60  
62   - logger.eventFileName = "events"
63   - path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt")
  61 + logger.eventFileName = name + "-events"
  62 + path := filepath.Join(evtDirName, logger.eventFileName+timestamp+".txt")
64 63 logger.eventf, err = os.Create(path)
65 64 if err != nil {
66 65 fmt.Printf("logger: new: couldn't create event log file\n")
67   - return err
  66 + return nil, err
68 67 }
69 68  
70 69 logger.muEv = &sync.Mutex{}
... ... @@ -72,33 +71,33 @@ func Init(flags Option, splitSize int64) error {
72 71  
73 72 // error file/dir
74 73 if flags&Errors > 0 {
75   - err = os.Mkdir(ErrDirName, os.ModePerm)
  74 + err = os.Mkdir(errDirName, os.ModePerm)
76 75 if err != nil {
77 76 if !os.IsExist(err) {
78 77 fmt.Printf("logger: new: couldn't create error log directory\n")
79   - return err
  78 + return nil, err
80 79 }
81 80 }
82 81  
83   - logger.errorFileName = "errors"
84   - path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt")
  82 + logger.errorFileName = name + "-errors"
  83 + path := filepath.Join(errDirName, logger.errorFileName+timestamp+".txt")
85 84 logger.errorf, err = os.Create(path)
86 85 if err != nil {
87 86 fmt.Printf("logger: new: couldn't create error log file\n")
88   - return err
  87 + return nil, err
89 88 }
90 89  
91 90 logger.muEr = &sync.Mutex{}
92 91 }
93 92  
94   - return nil
  93 + return logger, nil
95 94 }
96 95  
97   -func Print(s string) {
  96 +func (logger *Logger) Print(s string) {
98 97 fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n")
99 98 }
100 99  
101   -func LogEvent(event string) {
  100 +func (logger *Logger) LogEvent(event string) {
102 101 if logger.eventf != nil {
103 102 logger.muEv.Lock()
104 103 defer logger.muEv.Unlock()
... ... @@ -107,7 +106,7 @@ func LogEvent(event string) {
107 106 }
108 107 }
109 108  
110   -func LogError(comment string, err error) {
  109 +func (logger *Logger) LogError(comment string, err error) {
111 110 if logger.errorf != nil {
112 111 logger.muEr.Lock()
113 112 defer logger.muEr.Unlock()
... ... @@ -116,7 +115,7 @@ func LogError(comment string, err error) {
116 115 }
117 116 }
118 117  
119   -func TraceEvent(event string) {
  118 +func (logger *Logger) TraceEvent(event string) {
120 119 if logger.eventf != nil {
121 120 logger.muEv.Lock()
122 121 defer logger.muEv.Unlock()
... ... @@ -132,7 +131,7 @@ func TraceEvent(event string) {
132 131 }
133 132 }
134 133  
135   -func TraceError(err error) {
  134 +func (logger *Logger) TraceError(err error) {
136 135 if logger.errorf != nil {
137 136 logger.muEr.Lock()
138 137 defer logger.muEr.Unlock()
... ... @@ -148,7 +147,7 @@ func TraceError(err error) {
148 147 }
149 148 }
150 149  
151   -func Close() {
  150 +func (logger *Logger) Close() {
152 151 if logger.eventf != nil {
153 152 err := logger.eventf.Close()
154 153 if err != nil {
... ... @@ -164,7 +163,7 @@ func Close() {
164 163 }
165 164 }
166 165  
167   -func (l *goLogger) splitEventLog() {
  166 +func (l *Logger) splitEventLog() {
168 167 timestamp := "_" + time.Now().Format(time.RFC3339)
169 168  
170 169 evfstats, _ := l.eventf.Stat()
... ... @@ -177,7 +176,7 @@ func (l *goLogger) splitEventLog() {
177 176 }
178 177 // open new file
179 178 var errnew error
180   - path := filepath.Join(EvtDirName, l.eventFileName+timestamp)
  179 + path := filepath.Join(evtDirName, l.eventFileName+timestamp)
181 180 l.eventf, errnew = os.Create(path)
182 181  
183 182 if errnew != nil {
... ... @@ -186,7 +185,7 @@ func (l *goLogger) splitEventLog() {
186 185 }
187 186 }
188 187  
189   -func (l *goLogger) splitErrorLog() {
  188 +func (l *Logger) splitErrorLog() {
190 189 timestamp := "_" + time.Now().Format(time.RFC3339)
191 190  
192 191 erfstats, _ := l.errorf.Stat()
... ... @@ -199,7 +198,7 @@ func (l *goLogger) splitErrorLog() {
199 198 }
200 199 // open new file
201 200 var errnew error
202   - path := filepath.Join(ErrDirName, l.errorFileName+timestamp)
  201 + path := filepath.Join(errDirName, l.errorFileName+timestamp)
203 202 l.errorf, errnew = os.Create(path)
204 203  
205 204 if errnew != nil {
... ...