Skip to content

Commit

Permalink
Add support for a glog logger
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Oct 17, 2019
1 parent f0db6df commit 72789e0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
8 changes: 7 additions & 1 deletion examples/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
76 changes: 76 additions & 0 deletions pkg/logger/glog.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 72789e0

Please sign in to comment.