From 72789e0c14eac68d8947a07c432122f84ca47e08 Mon Sep 17 00:00:00 2001 From: KnicKnic Date: Wed, 16 Oct 2019 23:54:04 -0700 Subject: [PATCH] Add support for a glog logger --- examples/cmd/main.go | 8 ++++- pkg/logger/glog.go | 76 ++++++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 pkg/logger/glog.go diff --git a/examples/cmd/main.go b/examples/cmd/main.go index cd49ab0..d9fcd48 100644 --- a/examples/cmd/main.go +++ b/examples/cmd/main.go @@ -7,6 +7,7 @@ import ( "github.com/KnicKnic/go-powershell/pkg/logger" "github.com/KnicKnic/go-powershell/pkg/powershell" + "github.com/golang/glog" ) type callbackTest struct{} @@ -49,9 +50,12 @@ func PrintAndExecuteCommand(runspace powershell.Runspace, command string, useLoc // Example on how to use powershell wrappers func Example() { - runspace := powershell.CreateRunspace(logger.SimpleFmtPrint{}, callbackTest{}) + runspace := powershell.CreateRunspace(logger.Glog{VerboseLevel: 1, DebugLevel: 2}, callbackTest{}) defer runspace.Close() + if len(commandFlags) == 0 { + glog.Exit("Did not specify a \"-command\" to run") + } for i := 0; i < len(commandFlags); i++ { command := strings.ReplaceAll(commandFlags[i], "\\", "\\\\") PrintAndExecuteCommand(runspace, command, *useLocalScope) @@ -75,6 +79,8 @@ var useLocalScope = flag.Bool("useLocalScope", false, "True if should execute sc func main() { flag.Var(&commandFlags, "command", "Command to run in powershell") + flag.Set("logtostderr", "true") flag.Parse() Example() + glog.Flush() } diff --git a/pkg/logger/glog.go b/pkg/logger/glog.go new file mode 100644 index 0000000..711ef86 --- /dev/null +++ b/pkg/logger/glog.go @@ -0,0 +1,76 @@ +package logger + +import ( + "github.com/golang/glog" +) + +// Glog a type that can be used to log messages to glog +type Glog struct { + ErrorLevel glog.Level + WarningLevel glog.Level + InformationLevel glog.Level + VerboseLevel glog.Level + DebugLevel glog.Level +} + +func (log Glog) Warning(arg string) { + if glog.V(log.WarningLevel) { + glog.Warning(arg) + } +} +func (log Glog) Information(arg string) { + if glog.V(log.InformationLevel) { + glog.Info(arg) + } +} +func (log Glog) Verbose(arg string) { + if glog.V(log.VerboseLevel) { + glog.Info(arg) + } +} +func (log Glog) Debug(arg string) { + if glog.V(log.DebugLevel) { + glog.Info(arg) + } +} +func (log Glog) Error(arg string) { + if glog.V(log.ErrorLevel) { + glog.Error(arg) + } +} +func (log Glog) Write(arg string) { + if glog.V(log.InformationLevel) { + glog.Info(arg) + } +} + +func (log Glog) Warningln(arg string) { + if glog.V(log.WarningLevel) { + glog.Warningln(arg) + } +} +func (log Glog) Informationln(arg string) { + if glog.V(log.InformationLevel) { + glog.Infoln(arg) + } +} +func (log Glog) Verboseln(arg string) { + if glog.V(log.VerboseLevel) { + glog.Infoln(arg) + } +} +func (log Glog) Debugln(arg string) { + if glog.V(log.DebugLevel) { + glog.Infoln(arg) + } +} +func (log Glog) Errorln(arg string) { + if glog.V(log.ErrorLevel) { + glog.Errorln(arg) + } +} +func (log Glog) Writeln(arg string) { + if glog.V(log.InformationLevel) { + glog.Infoln(arg) + } +} diff --git a/readme.md b/readme.md index 785cf92..d21c0d5 100644 --- a/readme.md +++ b/readme.md @@ -35,7 +35,7 @@ This project is not api stable, however I believe it will be simple if you do us - [ ] example / helper classes around exception - [x] a doc overview - [x] plumb through callback handler for each specific logging type (verbose, debug, warning, ...) -- [ ] support for default loggers, like glog or log (in separate package) +- [x] support for default loggers, like glog or log (in separate package) # Usage