Skip to content

Commit

Permalink
Merge pull request #2 from mediocregopher/gslog-changes
Browse files Browse the repository at this point in the history
Gslog changes
  • Loading branch information
mediocreshark committed Apr 1, 2014
2 parents 4ed8568 + fcd9110 commit 8eeefe2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 15 additions & 0 deletions gslog/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,40 @@ func Debug(msg string, params ...interface{}) {
log(DEBUG, &msg, params)
}

// Alias of Debug
var Debugf = Debug

// Info writes a message to the log with info severity
func Info(msg string, params ...interface{}) {
log(INFO, &msg, params)
}

// Alias of Info
var Infof = Info

// Warn writes a message to the log with warn severity
func Warn(msg string, params ...interface{}) {
log(WARN, &msg, params)
}

// Alias of Warn
var Warnf = Warn

// Error writes a message to the log with error serverity
func Error(msg string, params ...interface{}) {
log(ERROR, &msg, params)
}

// Alias of Error
var Errorf = Error

// Fatal writes a message to the log with fatal severity, flushes any
// messages waiting to be written, and exits with a non-zero status
func Fatal(msg string, params ...interface{}) {
log(FATAL, &msg, params)
Flush()
os.Exit(1)
}

// Alias of Fatal
var Fatalf = Fatal
20 changes: 12 additions & 8 deletions gslog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type logger struct {
sync.RWMutex
fileHandle *os.File
messages chan *message
flushCh chan chan bool
flushCh chan struct{}
minLevel LogLevel
}

Expand All @@ -26,7 +26,7 @@ func init() {
l = &logger{}
l.fileHandle = os.Stderr
l.messages = make(chan *message, 1024)
l.flushCh = make(chan chan bool)
l.flushCh = make(chan struct{})

go handleMessages()
}
Expand Down Expand Up @@ -56,7 +56,9 @@ func SetMinimumLevel(level string) error {
return nil
}

// SetLogFile sets the file to which messages will be logged to. Default is STDOUT.
// SetLogFile sets the file to which messages will be logged to. Can take
// "stdout" or "stderr" to log to os.Stdout and os.Stderr, respectively. If
// unset defaults to os.Stderr
func SetLogFile(path string) error {
l.RWMutex.Lock()
defer l.RWMutex.Unlock()
Expand All @@ -69,6 +71,11 @@ func SetLogFile(path string) error {
return nil
}
fh = os.Stderr
} else if path == "stdout" {
if l.fileHandle == os.Stdout {
return nil
}
fh = os.Stdout
} else {
flags := os.O_APPEND | os.O_CREATE | os.O_WRONLY
fh, err = os.OpenFile(path, flags, 0644)
Expand All @@ -87,9 +94,7 @@ func SetLogFile(path string) error {

// Flushes the log of at least 100 milliseconds worth of entries
func Flush() {
retCh := make(chan bool)
l.flushCh <- retCh
<-retCh
l.flushCh <- struct{}{}
}

// logString generates the string which will be written to the logfile
Expand Down Expand Up @@ -146,8 +151,7 @@ func handleMessages() {

case <-time.After(100 * time.Millisecond):
select {
case flushret := <-l.flushCh:
flushret <- true
case <-l.flushCh:
default: // Oh well
}

Expand Down

0 comments on commit 8eeefe2

Please sign in to comment.