From e0513e95b0e4f490a64642f5b9981dc60693e1de Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 16 Nov 2021 19:59:42 +0100 Subject: [PATCH] refactor: provide log output directly on debug --- .github/ISSUE_TEMPLATE/bug.yml | 2 +- src/engine.go | 8 +++---- src/environment.go | 42 +++++++++++++++------------------- src/environment_test.go | 23 +++++++++++++------ src/segment_path_test.go | 5 ++++ 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml index 2aab6eec..1ef5aba5 100644 --- a/.github/ISSUE_TEMPLATE/bug.yml +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -29,7 +29,7 @@ body: id: logs attributes: label: Log output - description: Please copy and paste the output generated by `Write-PoshDebug` (PowerShell) or `oh-my-posh --debug --shell uni --config="$POSH_THEME"` for other shells. Please copy/paste the log file's content, formatting is done automatically. + description: Please copy and paste the output generated by `Write-PoshDebug` (PowerShell) or `oh-my-posh --debug --shell uni --config="$POSH_THEME"` for other shells. render: shell validations: required: true diff --git a/src/engine.go b/src/engine.go index f449a787..11adedca 100644 --- a/src/engine.go +++ b/src/engine.go @@ -128,8 +128,8 @@ func (e *engine) renderBlock(block *Block) { func (e *engine) debug() string { var segmentTimings []*SegmentTiming largestSegmentNameLength := 0 - e.write("\n\x1b[1mHere are the timings of segments in your prompt:\x1b[0m\n\n") - + e.write(fmt.Sprintf("\n\x1b[1mVersion:\x1b[0m %s\n", Version)) + e.write("\n\x1b[1mSegments:\x1b[0m\n\n") // console title timing start := time.Now() consoleTitle := e.consoleTitle.getTemplateText() @@ -163,8 +163,8 @@ func (e *engine) debug() string { segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.enabled) e.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.stringValue)) } - e.write(fmt.Sprintf("\n\x1b[1mVersion:\x1b[0m %s\n", Version)) - e.write(fmt.Sprintf("\x1b[1mLog:\x1b[0m %s/oh-my-posh.log\n\n", e.env.homeDir())) + e.write("\n\x1b[1mLogs:\x1b[0m\n\n") + e.write(e.env.logs()) return e.string() } diff --git a/src/environment.go b/src/environment.go index 096d55e6..529526f0 100644 --- a/src/environment.go +++ b/src/environment.go @@ -88,6 +88,7 @@ type environmentInfo interface { getCachePath() string cache() cache close() + logs() string } type commandCache struct { @@ -115,40 +116,32 @@ const ( ) type tracer interface { - init(home string) - close() + init() + string() string trace(start time.Time, function string, args ...string) log(lt logType, function, message string) } -type fileTracer struct { - file *os.File - debug bool +type logTracer struct { + builder strings.Builder + debug bool } -func (t *fileTracer) init(home string) { +func (t *logTracer) init() { if !t.debug { return } - var err error - fileName := home + "/oh-my-posh.log" - t.file, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - log.Fatalf("error opening file: %v", err) - } - log.SetOutput(t.file) - log.Println("#### start oh-my-posh run ####") + log.SetOutput(&t.builder) } -func (t *fileTracer) close() { +func (t *logTracer) string() string { if !t.debug { - return + return "" } - log.Println("#### end oh-my-posh run ####") - _ = t.file.Close() + return t.builder.String() } -func (t *fileTracer) trace(start time.Time, function string, args ...string) { +func (t *logTracer) trace(start time.Time, function string, args ...string) { if !t.debug { return } @@ -157,7 +150,7 @@ func (t *fileTracer) trace(start time.Time, function string, args ...string) { log.Println(trace) } -func (t *fileTracer) log(lt logType, function, message string) { +func (t *logTracer) log(lt logType, function, message string) { if !t.debug { return } @@ -178,10 +171,10 @@ func (env *environment) init(args *args) { env.cmdCache = &commandCache{ commands: newConcurrentMap(), } - tracer := &fileTracer{ + tracer := &logTracer{ debug: *args.Debug, } - tracer.init(env.homeDir()) + tracer.init() env.tracer = tracer env.fileCache = &fileCache{} env.fileCache.init(env.getCachePath()) @@ -531,7 +524,10 @@ func (env *environment) cache() cache { func (env *environment) close() { env.fileCache.close() - env.tracer.close() +} + +func (env *environment) logs() string { + return env.tracer.string() } func cleanHostName(hostName string) string { diff --git a/src/environment_test.go b/src/environment_test.go index 3773085f..db9a310f 100644 --- a/src/environment_test.go +++ b/src/environment_test.go @@ -13,21 +13,30 @@ type MockedTracer struct { mock.Mock } -// close provides a mock function with given fields: -func (_m *MockedTracer) close() { +// init provides a mock function with given fields: +func (_m *MockedTracer) init() { _m.Called() } -// init provides a mock function with given fields: home -func (_m *MockedTracer) init(home string) { - _m.Called(home) -} - // log provides a mock function with given fields: lt, function, message func (_m *MockedTracer) log(lt logType, function, message string) { _m.Called(lt, function, message) } +// string provides a mock function with given fields: +func (_m *MockedTracer) string() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + // trace provides a mock function with given fields: start, function, args func (_m *MockedTracer) trace(start time.Time, function string, args ...string) { _va := make([]interface{}, len(args)) diff --git a/src/segment_path_test.go b/src/segment_path_test.go index 1ac6e4cd..c8a555b9 100644 --- a/src/segment_path_test.go +++ b/src/segment_path_test.go @@ -168,6 +168,11 @@ func (env *MockedEnvironment) close() { _ = env.Called(nil) } +func (env *MockedEnvironment) logs() string { + args := env.Called(nil) + return args.String(0) +} + const ( homeBill = "/home/bill" homeJan = "/usr/home/jan"