diff --git a/native-powershell b/native-powershell index e3f4bc8..60ee25e 160000 --- a/native-powershell +++ b/native-powershell @@ -1 +1 @@ -Subproject commit e3f4bc87039c47d31c08eb5aec3eb8573617c8a9 +Subproject commit 60ee25e186f8c704d7be99aba3c2cc1ce04827dd diff --git a/pkg/logger/logging.go b/pkg/logger/logging.go index cf20f99..edb8fa8 100644 --- a/pkg/logger/logging.go +++ b/pkg/logger/logging.go @@ -56,14 +56,19 @@ func (holder SimpleFuncPtr) Write(arg string) { // MakeLoggerFull returns a wrapper class that provides Full semantics, // utilizing a simple Simple.write() function -func MakeLoggerFull(logger Simple) Full { +func MakeLoggerFull(logger interface{}) Full { if logger == nil { return nil } if p, ok := logger.(Full); ok { return p } - return simpleToFull{logger} + + if simple, ok := logger.(Simple); ok{ + return simpleToFull{simple} + } + + panic("Expected either logger.Simple or logger.Full") } func (log simpleToFull) Warning(arg string) { diff --git a/pkg/powershell/higherops_test.go b/pkg/powershell/higherops_test.go index fb86cc2..6473374 100644 --- a/pkg/powershell/higherops_test.go +++ b/pkg/powershell/higherops_test.go @@ -95,9 +95,9 @@ write-Information "calling write-Information" write-verbose "calling Write-Verbose" Send-HostCommand -message "Sending 0 objects to host" | out-null "1","2" | Send-HostCommand -message "Sending 2 objects to host and returning them" - In Logging : Debug: calling Write-Host + In Logging : calling Write-Host In Logging : Debug: callng Write-Debug - In Logging : Information: calling write-Information + In Logging : calling write-Information In Logging : Verbose: calling Write-Verbose In callback: Sending 0 objects to host In callback: Sending 2 objects to host and returning them @@ -118,6 +118,98 @@ Command returned 3 objects validate(t, expected, record.lines) } +type fullLogger struct { +} + +func (_ fullLogger) Warning(arg string) { + + record.Print(" In Logging : Warning: ", arg) +} +func (_ fullLogger) Information(arg string) { + record.Print(" In Logging : Information: ", arg) +} +func (_ fullLogger) Verbose(arg string) { + record.Print(" In Logging : Verbose: ", arg) +} +func (_ fullLogger) Debug(arg string) { + record.Print(" In Logging : Debug: ", arg) +} +func (_ fullLogger) Error(arg string) { + record.Print(" In Logging : Error: ", arg) +} +func (_ fullLogger) Write(arg string) { + record.Print(" In Logging : Write: ", arg) +} + +func (_ fullLogger) Warningln(arg string) { + arg = arg + "\n" + record.Print(" In Logging : Line Warning: ", arg) +} +func (_ fullLogger) Informationln(arg string) { + arg = arg + "\n" + record.Print(" In Logging : Line Information: ", arg) +} +func (_ fullLogger) Verboseln(arg string) { + arg = arg + "\n" + record.Print(" In Logging : Line Verbose: ", arg) +} +func (_ fullLogger) Debugln(arg string) { + arg = arg + "\n" + record.Print(" In Logging : Line Debug: ", arg) +} +func (_ fullLogger) Errorln(arg string) { + arg = arg + "\n" + record.Print(" In Logging : Line Error: ", arg) +} +func (_ fullLogger) Writeln(arg string) { + arg = arg + "\n" + record.Print(" In Logging : Line Write: ", arg) +} + +func TestCcreateRunspaceWitLoggerWithFullCallback(t *testing.T) { + record.Reset() + runspace := CreateRunspace(fullLogger{}, callbackTest{}) + // runspace := CreateRunspaceSimple() + defer runspace.Close() + inputStr := ` +write-host "calling Write-Host" +write-debug "callng Write-Debug" +write-Information "calling write-Information" +write-verbose "calling Write-Verbose" +Send-HostCommand -message "Sending 0 objects to host" | out-null +"1","2" | Send-HostCommand -message "Sending 2 objects to host and returning them"` + PrintAndExecuteCommand(runspace, inputStr, false) + // Output: + expected := `Executing powershell command: +write-host "calling Write-Host" +write-debug "callng Write-Debug" +write-Information "calling write-Information" +write-verbose "calling Write-Verbose" +Send-HostCommand -message "Sending 0 objects to host" | out-null +"1","2" | Send-HostCommand -message "Sending 2 objects to host and returning them" + In Logging : Line Write: calling Write-Host + In Logging : Line Debug: callng Write-Debug + In Logging : Line Write: calling write-Information + In Logging : Line Verbose: calling Write-Verbose + In callback: Sending 0 objects to host + In callback: Sending 2 objects to host and returning them + In callback: index 0 type: System.String with value: 1 + In callback: index 1 type: System.String with value: 2 +Completed Executing powershell command: +write-host "calling Write-Host" +write-debug "callng Write-Debug" +write-Information "calling write-Information" +write-verbose "calling Write-Verbose" +Send-HostCommand -message "Sending 0 objects to host" | out-null +"1","2" | Send-HostCommand -message "Sending 2 objects to host and returning them" +Command returned 3 objects + Object 0 is of type System.String and ToString Sending 2 objects to host and returning them + Object 1 is of type System.String and ToString 1 + Object 2 is of type System.String and ToString 2 +` + validate(t, expected, record.lines) +} + func TestCcreateRunspaceSimple(t *testing.T) { record.Reset() runspace := CreateRunspaceSimple() @@ -158,29 +250,29 @@ func TestCcreateRunspaceUsingCommand(t *testing.T) { results2 := runspace.ExecCommand(`..\\..\\tests\\t2.ps1`, false, nil) defer results2.Close() // Output: - expected := ` In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba + expected := ` In Logging : ab ba + In Logging : ab ba + In Logging : ab ba In callback: asdfasdf In callback: index 0 type: System.Int32 with value: 1 In callback: index 1 type: System.Int32 with value: 2 In callback: index 2 type: System.Int32 with value: 3 - In Logging : Debug: asdfasdf 1 2 3 + In Logging : asdfasdf 1 2 3 In callback: two - In Logging : Debug: two + In Logging : two In callback: three - In Logging : Debug: three + In Logging : three In callback: four In callback: index 0 Object Is Null In callback: index 0 type: nullptr with value: nullptr In callback: index 1 Object Is Null In callback: index 1 type: nullptr with value: nullptr - In Logging : Debug: ab four ba + In Logging : ab four ba In Logging : Error: someerror - In Logging : Debug: start t2 - In Logging : Debug: 5 - In Logging : Debug: 5 - In Logging : Debug: asdf + In Logging : start t2 + In Logging : 5 + In Logging : 5 + In Logging : asdf ` validate(t, expected, record.lines) } @@ -195,14 +287,14 @@ func TestCglobalScope(t *testing.T) { results2 := runspace.ExecCommand(`..\\..\\tests\\test_scope.ps1`, false, nil) defer results2.Close() // Output: - expected := ` In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab 1 ba - In Logging : Debug: ab 2 ba - In Logging : Debug: ab 3 ba - In Logging : Debug: ab 4 ba + expected := ` In Logging : ab ba + In Logging : ab ba + In Logging : ab ba + In Logging : ab ba + In Logging : ab 1 ba + In Logging : ab 2 ba + In Logging : ab 3 ba + In Logging : ab 4 ba ` validate(t, expected, record.lines) } @@ -217,14 +309,14 @@ func TestClocalScope(t *testing.T) { results2 := runspace.ExecCommand(`..\\..\\tests\\test_scope.ps1`, true, nil) defer results2.Close() - expected := ` In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab ba - In Logging : Debug: ab 3 ba - In Logging : Debug: ab ba + expected := ` In Logging : ab ba + In Logging : ab ba + In Logging : ab ba + In Logging : ab ba + In Logging : ab ba + In Logging : ab ba + In Logging : ab 3 ba + In Logging : ab ba ` validate(t, expected, record.lines) } @@ -284,7 +376,7 @@ func TestCcallbackSaveObject(t *testing.T) { object.Close() } // Output: In Logging : Debug: 1 - expected := ` In Logging : Debug: 1 + expected := ` In Logging : 1 ` validate(t, expected, record.lines) } @@ -410,7 +502,7 @@ func TestCpowershellJsonMarshal(t *testing.T) { paramResults := runspace.ExecCommandJSONMarshalUnknown(`Write-Host`, true, map[string]interface{}{"Object": 17}) defer paramResults.Close() - expected := ` In Logging : Debug: 17 + expected := ` In Logging : 17 ` validate(t, expected, record.lines) } diff --git a/pkg/powershell/runspace_test.go b/pkg/powershell/runspace_test.go index 4f3c214..7d49051 100644 --- a/pkg/powershell/runspace_test.go +++ b/pkg/powershell/runspace_test.go @@ -2,8 +2,9 @@ package powershell import ( "fmt" - "github.com/KnicKnic/go-powershell/pkg/logger" "strconv" + + "github.com/KnicKnic/go-powershell/pkg/logger" ) func ExampleRunspace_ExecScript() { @@ -108,13 +109,13 @@ func ExampleRunspace_ExecCommandJSONMarshalUnknown() { defer results.Close() // OUTPUT: - // Debug: 1 2 false test string [1,2,3] {"fruit":"apple","vegetable":"celery"} - // Debug: 1 - // Debug: 2 - // Debug: false - // Debug: test string - // Debug: [1,2,3] - // Debug: {"fruit":"apple","vegetable":"celery"} + // 1 2 false test string [1,2,3] {"fruit":"apple","vegetable":"celery"} + // 1 + // 2 + // false + // test string + // [1,2,3] + // {"fruit":"apple","vegetable":"celery"} } type person struct { @@ -183,7 +184,7 @@ func ExampleCallbackHolder() { func ExampleRunspace_customSimpleLogger() { // create a custom logger object customLogger := logger.SimpleFuncPtr{func(str string) { - fmt.Print("Custom: " + str); + fmt.Print("Custom: " + str) }} // create a runspace (where you run your powershell statements in) runspace := CreateRunspace(customLogger, nil) diff --git a/readme.md b/readme.md index 364997b..785cf92 100644 --- a/readme.md +++ b/readme.md @@ -34,7 +34,7 @@ This project is not api stable, however I believe it will be simple if you do us - [X] more examples / tests - [ ] example / helper classes around exception - [x] a doc overview -- [ ] plumb through callback handler for each specific logging type (verbose, debug, warning, ...) +- [x] plumb through callback handler for each specific logging type (verbose, debug, warning, ...) - [ ] support for default loggers, like glog or log (in separate package) # Usage