openExistingOrNew opens the logfile if it exists and if the current write would not put it over MaxSize. If there is no such file or the write would put it over the MaxSize, a new file is created.
(writeLen int)
| 262 | // would not put it over MaxSize. If there is no such file or the write would |
| 263 | // put it over the MaxSize, a new file is created. |
| 264 | func (l *Logger) openExistingOrNew(writeLen int) error { |
| 265 | l.mill() |
| 266 | |
| 267 | filename := l.filename() |
| 268 | info, err := osStat(filename) |
| 269 | if os.IsNotExist(err) { |
| 270 | return l.openNew() |
| 271 | } |
| 272 | if err != nil { |
| 273 | return fmt.Errorf("error getting log file info: %s", err) |
| 274 | } |
| 275 | |
| 276 | if info.Size()+int64(writeLen) >= l.max() { |
| 277 | return l.rotate() |
| 278 | } |
| 279 | |
| 280 | file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644) |
| 281 | if err != nil { |
| 282 | // if we fail to open the old log file for some reason, just ignore |
| 283 | // it and open a new log file. |
| 284 | return l.openNew() |
| 285 | } |
| 286 | l.file = file |
| 287 | l.size = info.Size() |
| 288 | return nil |
| 289 | } |
| 290 | |
| 291 | // filename generates the name of the logfile from the current time. |
| 292 | func (l *Logger) filename() string { |