Skip to content

Commit

Permalink
Allow to set AbsoluteFilePath through the env
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaylor Bosson committed Oct 9, 2019
1 parent c4c8c70 commit 8af1d5b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// The log-package also takes into account the following environment-variables:
// DEBUG_LVL // will act like SetDebugVisible
// DEBUG_TIME // if 'true' it will print the date and time
// DEBUG_FILEPATH // if 'true' it will print the absolute filepath
// DEBUG_COLOR // if 'false' it will not use colors
// DEBUG_PADDING // if 'false' it will not use padding
// But for this the function ParseEnv() or AddFlags() has to be called.
Expand Down
15 changes: 15 additions & 0 deletions log/lvl.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var NamePadding = 40

// LinePadding of line-numbers for a nice debug-output - used in the same way as
// NamePadding.
// Deprecated: the caller is merged thus no line padding is necessary anymore.
var LinePadding = 3

// StaticMsg - if this variable is set, it will be outputted between the
Expand Down Expand Up @@ -385,6 +386,7 @@ func ParseEnv() {
Error("Couldn't convert", dv, "to debug-level")
}
}

dt := os.Getenv("DEBUG_TIME")
if dt != "" {
dtInt, err := strconv.ParseBool(dt)
Expand All @@ -394,6 +396,17 @@ func ParseEnv() {
Error("Couldn't convert", dt, "to boolean")
}
}

dfp := os.Getenv("DEBUG_FILEPATH")
if dfp != "" {
dfpInt, err := strconv.ParseBool(dfp)
Lvl3("Setting absoluteFilePath to", dfp, dfpInt, err)
SetAbsoluteFilePath(dfpInt)
if err != nil {
Error("Couldn't convert", dfp, "to boolean")
}
}

dc := os.Getenv("DEBUG_COLOR")
if dc != "" {
ucInt, err := strconv.ParseBool(dc)
Expand All @@ -403,6 +416,7 @@ func ParseEnv() {
Error("Couldn't convert", dc, "to boolean")
}
}

dp := os.Getenv("DEBUG_PADDING")
if dp != "" {
dpBool, err := strconv.ParseBool(dp)
Expand All @@ -428,6 +442,7 @@ func RegisterFlags() {
flag.BoolVar(&loggers[0].GetLoggerInfo().ShowTime, "debug-time", defaultShowTime, "Shows the time of each message")
flag.BoolVar(&loggers[0].GetLoggerInfo().UseColors, "debug-color", defaultUseColors, "Colors each message")
flag.BoolVar(&loggers[0].GetLoggerInfo().Padding, "debug-padding", defaultPadding, "Pads each message nicely")
flag.BoolVar(&loggers[0].GetLoggerInfo().AbsoluteFilePath, "debug-filepath", DefaultStdAbsoluteFilePath, "extends the filename with the absolute path")
}

var timeoutFlagMutex sync.Mutex
Expand Down
7 changes: 7 additions & 0 deletions log/lvl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func TestFlags(t *testing.T) {
if ShowTime() {
t.Fatal("ShowTime should be false")
}
if AbsoluteFilePath() {
t.Fatal("AbsoluteFilePath should be false")
}
if UseColors() {
t.Fatal("UseColors should be false")
}
Expand All @@ -63,6 +66,7 @@ func TestFlags(t *testing.T) {

os.Setenv("DEBUG_LVL", "3")
os.Setenv("DEBUG_TIME", "true")
os.Setenv("DEBUG_FILEPATH", "true")
os.Setenv("DEBUG_COLOR", "false")
os.Setenv("DEBUG_PADDING", "false")
ParseEnv()
Expand All @@ -72,6 +76,9 @@ func TestFlags(t *testing.T) {
if !ShowTime() {
t.Fatal("ShowTime should be true")
}
if !AbsoluteFilePath() {
t.Fatal("AbsoluteFilePath sgould be true")
}
if UseColors() {
t.Fatal("UseColors should be false")
}
Expand Down

0 comments on commit 8af1d5b

Please sign in to comment.