Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where rotating log writer panics with a nil pointer dereference. Closes #67 #68

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 25 additions & 29 deletions logging/rotatinglogwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,35 @@ func NewRotatingLogWriter(directory string, prefix string) *RotatingLogWriter {
prefix: prefix,
}
}

func (w *RotatingLogWriter) Write(p []byte) (n int, err error) {
expectedPath := filepath.Join(w.directory, fmt.Sprintf("%s-%s.log", w.prefix, time.Now().Format(time.DateOnly)))

// the code outside of this IF block should be simple and blazing fast
//
// the code inside the IF block will probably execute once in 24 hours at most
// for an instance, but the code outside is used by every log line
if w.currentPath != expectedPath {
if err := w.rotateLogTarget(expectedPath); err != nil {
return 0, err
}
// update the current path
w.currentPath = expectedPath

// update the writer
w.currentWriter, err = os.OpenFile(w.currentPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
err = fmt.Errorf("failed to open steampipe log file: %s", err.Error())
}
}

return w.currentWriter.Write(p)
}

func (w *RotatingLogWriter) rotateLogTarget(targetPath string) (err error) {
w.rotateLock.Lock()
defer w.rotateLock.Unlock()

// update to the current path
w.currentPath = targetPath

// check if the file actually doesn't exist
if files.FileExists(targetPath) {
// nothing to do here
Expand All @@ -51,31 +73,5 @@ func (w *RotatingLogWriter) rotateLogTarget(targetPath string) (err error) {
if isCloseable {
closeableWriter.Close()
}

// we could be in here because the file exists,
// but we are starting up for the first time
if w.currentWriter == nil {
// create a new one
w.currentWriter, err = os.OpenFile(w.currentPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return fmt.Errorf("failed to open steampipe log file: %s", err.Error())
}
}
return nil
}

func (w *RotatingLogWriter) Write(p []byte) (n int, err error) {
expectedPath := filepath.Join(w.directory, fmt.Sprintf("%s-%s.log", w.prefix, time.Now().Format(time.DateOnly)))

// the code outside of this IF block should be simple and blazing fast
//
// the code inside the IF block will probably execute once in 24 hours at most
// for an instance, but the code outside is used by every log line
if w.currentPath != expectedPath {
if err := w.rotateLogTarget(expectedPath); err != nil {
return 0, err
}
}

return w.currentWriter.Write(p)
}
Loading