github.com/danbrough/mobile@v0.0.3-beta03/klog/testing.go (about) 1 package klog 2 3 import ( 4 "bytes" 5 "fmt" 6 ) 7 8 type NoopLogSink struct { 9 } 10 11 func (s NoopLogSink) WriteMsg(logName string, 12 level int, 13 message string, 14 functionName string, 15 fileName string, 16 lineNo int) { 17 } 18 19 type NoopLogger struct { 20 Name string 21 Skip int 22 } 23 24 func (l NoopLogger) Trace(s string) {} 25 func (l NoopLogger) Debug(s string) {} 26 func (l NoopLogger) Info(s string) {} 27 func (l NoopLogger) Warn(s string) {} 28 func (l NoopLogger) Error(s string) {} 29 30 func formatMessage(logName string, level int, useColor bool, 31 message string, 32 functionName string, 33 fileName string, 34 lineNo int) string { 35 var color string 36 var levelName string 37 switch level { 38 case LogTrace: 39 levelName = "TRACE" 40 color = "35" 41 case LogDebug: 42 levelName = "DEBUG" 43 color = "36" 44 case LogInfo: 45 levelName = "INFO " 46 color = "32" 47 case LogWarn: 48 levelName = "WARN " 49 color = "33" 50 case LogError: 51 levelName = "ERROR" 52 color = "31" 53 } 54 var buf bytes.Buffer 55 _, err := fmt.Fprintf(&buf, 56 "%s:%s %s[%s:%d]: %s", 57 levelName, logName, functionName, fileName, lineNo, message) 58 if err != nil { 59 return err.Error() 60 } 61 if useColor { 62 return "\u001b[0;" + color + "m" + buf.String() + "\u001b[0m" 63 } else { 64 return buf.String() 65 } 66 } 67 68 type StdoutLogSink struct { 69 UseColor bool 70 } 71 72 func (s StdoutLogSink) WriteMsg( 73 logName string, 74 level int, 75 message string, 76 functionName string, 77 fileName string, 78 lineNo int, 79 ) { 80 println(formatMessage(logName, level, s.UseColor, message, functionName, fileName, lineNo)) 81 } 82 83 var KLog = Logger{Name: "KIPFS", Skip: 2, LogSink: StdoutLogSink{UseColor: true}}