refactor: provide log output directly on debug

This commit is contained in:
Jan De Dobbeleer 2021-11-16 19:59:42 +01:00 committed by Jan De Dobbeleer
parent 8837c127ed
commit e0513e95b0
5 changed files with 45 additions and 35 deletions

View file

@ -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

View file

@ -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()
}

View file

@ -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
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 {

View file

@ -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))

View file

@ -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"