From deacdaa6675f03b14160136d7d3bcace5780009e Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 14 Nov 2016 18:02:29 +0100 Subject: [PATCH 1/3] Fixes the filename and lineNo in debug log message --- apiapp.go | 2 +- g.go | 2 +- g_appcode.go | 2 +- logger.go | 15 ++++++--------- migrate.go | 2 +- run.go | 2 +- test.go | 2 +- util.go | 12 ++++++++++++ watch.go | 5 +++-- 9 files changed, 27 insertions(+), 17 deletions(-) diff --git a/apiapp.go b/apiapp.go index 6ecd3ae..a1442d7 100644 --- a/apiapp.go +++ b/apiapp.go @@ -658,7 +658,7 @@ func checkEnv(appname string) (apppath, packpath string, err error) { gopath := gps[0] logger.Warn("You current workdir is not inside $GOPATH/src") - logger.Debugf("GOPATH: %s", gopath) + logger.Debugf("GOPATH: %s", __FILE__(), __LINE__(), gopath) gosrcpath := path.Join(gopath, "src") apppath = path.Join(gosrcpath, appname) diff --git a/g.go b/g.go index e3a18d2..b8589b4 100644 --- a/g.go +++ b/g.go @@ -91,7 +91,7 @@ func generateCode(cmd *Command, args []string) int { gopath := gps[0] - logger.Debugf("GOPATH: %s", gopath) + logger.Debugf("GOPATH: %s", __FILE__(), __LINE__(), gopath) gcmd := args[0] switch gcmd { diff --git a/g_appcode.go b/g_appcode.go index 7778c82..6d9d5a7 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -954,7 +954,7 @@ func getPackagePath(curpath string) (packpath string) { logger.Fatal("GOPATH environment variable is not set or empty") } - logger.Debugf("GOPATH: %s", gopath) + logger.Debugf("GOPATH: %s", __FILE__(), __LINE__(), gopath) appsrcpath := "" haspath := false diff --git a/logger.go b/logger.go index 290264b..ded3e9e 100644 --- a/logger.go +++ b/logger.go @@ -19,7 +19,6 @@ import ( "io" "os" "path/filepath" - "runtime" "sync" "sync/atomic" "text/template" @@ -155,7 +154,7 @@ func (l *BeeLogger) mustLog(level int, message string, args ...interface{}) { // mustLogDebug logs a debug message only if debug mode // is enabled. i.e. DEBUG_ENABLED="1" -func (l *BeeLogger) mustLogDebug(message string, args ...interface{}) { +func (l *BeeLogger) mustLogDebug(message string, file string, line int, args ...interface{}) { if !IsDebugEnabled() { return } @@ -163,9 +162,7 @@ func (l *BeeLogger) mustLogDebug(message string, args ...interface{}) { // Change the output to Stderr l.SetOutput(os.Stderr) - // Create the log record and Get the filename - // and the line number of the caller - _, file, line, _ := runtime.Caller(1) + // Create the log record record := LogRecord{ ID: fmt.Sprintf("%04d", atomic.AddUint64(&sequenceNo, 1)), Level: l.getColorLevel(levelDebug), @@ -178,13 +175,13 @@ func (l *BeeLogger) mustLogDebug(message string, args ...interface{}) { } // Debug outputs a debug log message -func (l *BeeLogger) Debug(message string) { - l.mustLogDebug(message) +func (l *BeeLogger) Debug(message string, file string, line int) { + l.mustLogDebug(message, file, line) } // Debugf outputs a formatted debug log message -func (l *BeeLogger) Debugf(message string, vars ...interface{}) { - l.mustLogDebug(message, vars...) +func (l *BeeLogger) Debugf(message string, file string, line int, vars ...interface{}) { + l.mustLogDebug(message, file, line, vars...) } // Info outputs an information log message diff --git a/migrate.go b/migrate.go index 873fbb5..b4fde98 100644 --- a/migrate.go +++ b/migrate.go @@ -73,7 +73,7 @@ func runMigration(cmd *Command, args []string) int { gopath := gps[0] - logger.Debugf("GOPATH: %s", gopath) + logger.Debugf("GOPATH: %s", __FILE__(), __LINE__(), gopath) // Load the configuration err := loadConfig() diff --git a/run.go b/run.go index 88797ab..bdee001 100644 --- a/run.go +++ b/run.go @@ -99,7 +99,7 @@ func runApp(cmd *Command, args []string) int { logger.Infof("Using '%s' as 'appname'", appname) - logger.Debugf("Current path: %s", currpath) + logger.Debugf("Current path: %s", __FILE__(), __LINE__(), currpath) if runmode == "prod" || runmode == "dev" { os.Setenv("BEEGO_RUNMODE", runmode) diff --git a/test.go b/test.go index b05c10b..2dcf070 100644 --- a/test.go +++ b/test.go @@ -56,7 +56,7 @@ func testApp(cmd *Command, args []string) int { currpath, _ := os.Getwd() - logger.Debugf("Current path: %s", currpath) + logger.Debugf("Current path: %s", __FILE__(), __LINE__(), currpath) err := loadConfig() if err != nil { diff --git a/util.go b/util.go index 7229161..c3cc754 100644 --- a/util.go +++ b/util.go @@ -262,3 +262,15 @@ func IsDebugEnabled() bool { debugMode := os.Getenv("DEBUG_ENABLED") return map[string]bool{"1": true, "0": false}[debugMode] } + +// __FILE__ returns the file name in which the function was invoked +func __FILE__() string { + _, file, _, _ := runtime.Caller(1) + return file +} + +// __LINE__ returns the line number at which the function was invoked +func __LINE__() int { + _, _, line, _ := runtime.Caller(1) + return line +} diff --git a/watch.go b/watch.go index 34c03b3..a030c3b 100644 --- a/watch.go +++ b/watch.go @@ -16,7 +16,6 @@ package main import ( "bytes" - "github.com/howeyc/fsnotify" "os" "os/exec" "regexp" @@ -24,6 +23,8 @@ import ( "strings" "sync" "time" + + "github.com/howeyc/fsnotify" ) var ( @@ -209,7 +210,7 @@ func Kill() { // Restart kills the running command process and starts it again func Restart(appname string) { - logger.Debugf("Kill running process") + logger.Debugf("Kill running process", __FILE__(), __LINE__()) Kill() go Start(appname) } From 21fc1775514586a796e1a4f4ded8c567c442a605 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Tue, 15 Nov 2016 12:25:37 +0100 Subject: [PATCH 2/3] Acquire lock when logging --- apiapp.go | 3 +-- logger.go | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/apiapp.go b/apiapp.go index a1442d7..ca8cf22 100644 --- a/apiapp.go +++ b/apiapp.go @@ -558,8 +558,7 @@ func createapi(cmd *Command, args []string) int { apppath, packpath, err := checkEnv(args[0]) if err != nil { - fmt.Println(err) - os.Exit(2) + logger.Fatalf("%s", err) } if driver == "" { driver = "mysql" diff --git a/logger.go b/logger.go index ded3e9e..8189a52 100644 --- a/logger.go +++ b/logger.go @@ -61,7 +61,6 @@ type LogRecord struct { var ( logRecordTemplate *template.Template debugLogRecordTemplate *template.Template - debugLogFormat string ) func init() { @@ -141,6 +140,10 @@ func (l *BeeLogger) getColorLevel(level int) string { // mustLog logs the message according to the specified level and arguments. // It panics in case of an error. func (l *BeeLogger) mustLog(level int, message string, args ...interface{}) { + // Acquire the lock + l.mu.Lock() + defer l.mu.Unlock() + // Create the logging record and pass into the output record := LogRecord{ ID: fmt.Sprintf("%04d", atomic.AddUint64(&sequenceNo, 1)), From 6b27ef186af6958793336947098035fb8052c89a Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Wed, 16 Nov 2016 12:33:01 +0100 Subject: [PATCH 3/3] This makes the logger instance a singleton --- bee.go | 2 ++ logger.go | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bee.go b/bee.go index 4ae5111..645a979 100644 --- a/bee.go +++ b/bee.go @@ -90,6 +90,8 @@ var commands = []*Command{ cmdFix, } +var logger = GetBeeLogger(os.Stdout) + func main() { flag.Usage = usage flag.Parse() diff --git a/logger.go b/logger.go index 8189a52..9689a00 100644 --- a/logger.go +++ b/logger.go @@ -39,7 +39,8 @@ const ( var ( sequenceNo uint64 - logger *BeeLogger + instance *BeeLogger + once sync.Once ) // BeeLogger logs logging records to the specified io.Writer @@ -79,9 +80,15 @@ func init() { MustCheck(err) debugLogRecordTemplate, err = template.New("dbgLogRecordTemplate").Funcs(funcs).Parse(debugLogFormat) MustCheck(err) +} - // Initialize the logger instance with a NewColorWriter output - logger = &BeeLogger{output: NewColorWriter(os.Stdout)} +// GetBeeLogger initializes the logger instance with a NewColorWriter output +// and returns a singleton +func GetBeeLogger(w io.Writer) *BeeLogger { + once.Do(func() { + instance = &BeeLogger{output: NewColorWriter(w)} + }) + return instance } // SetOutput sets the logger output destination