Skip to content

Commit

Permalink
Multi log (#5)
Browse files Browse the repository at this point in the history
* enable full log to be passed in
  • Loading branch information
KnicKnic authored Oct 17, 2019
1 parent 551a650 commit f0db6df
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 44 deletions.
2 changes: 1 addition & 1 deletion native-powershell
9 changes: 7 additions & 2 deletions pkg/logger/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
154 changes: 123 additions & 31 deletions pkg/powershell/higherops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/powershell/runspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0db6df

Please sign in to comment.