/
logging.go
128 lines (110 loc) · 3.11 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2018 Andrew Bates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package insteon
import (
"errors"
"log"
"os"
"path"
"runtime"
)
var (
// Log is the global log object. The default level is set to Info
Log = &Logger{Level: LevelNone, Logger: log.New(os.Stderr, "", log.LstdFlags)}
)
// LogLevel indicates verbosity of logging
type LogLevel int
func (ll LogLevel) String() string {
switch ll {
case LevelNone:
return "NONE"
case LevelInfo:
return "INFO"
case LevelDebug:
return "DEBUG"
case LevelTrace:
return "TRACE"
}
return ""
}
// Set sets the LogLevel to the matching input string. This function
// satisfies the Set function for the flag.Value interface
func (ll *LogLevel) Set(s string) (err error) {
switch s {
case "none":
case "info":
(*ll) = LevelInfo
case "debug":
(*ll) = LevelDebug
case "trace":
(*ll) = LevelTrace
default:
err = errors.New("valid values {none|info|debug|trace}")
}
return err
}
// Get returns the underlying LogLevel value in order to satisfy the
// flag.Value interface
func (ll *LogLevel) Get() interface{} {
return LogLevel(*ll)
}
const (
// LevelNone - log nothing to Stderr
LevelNone = LogLevel(iota)
// LevelInfo - log normal information (warnings) to Stderr
LevelInfo
// LevelDebug - log debug information (used for development and troubleshooting)
LevelDebug
// LevelTrace - log everything, including I/O
LevelTrace
)
// Logger is a struct that keeps track of a log level and only
// prints messages of that level or lower
type Logger struct {
Level LogLevel
Logger *log.Logger
}
func (s *Logger) logf(level LogLevel, format string, v ...interface{}) {
if s.Level >= level {
format = sprintf("%5s %s", level, format)
if level == LevelTrace {
pc := make([]uintptr, 10)
runtime.Callers(3, pc)
frames := runtime.CallersFrames(pc)
frame, _ := frames.Next()
function := path.Base(frame.Function)
format = sprintf("%s:%d %s", function, frame.Line, format)
}
s.Logger.Printf(format, v...)
}
}
// Infof will print a message at the Info level
func (s *Logger) Infof(format string, v ...interface{}) {
s.logf(LevelInfo, format, v...)
}
// Errorf will print a message at the Info level if the supplied error is
// non-nil
func (s *Logger) Errorf(err error, format string, v ...interface{}) {
if err != nil {
s.logf(LevelInfo, format, v...)
}
}
// Debugf will print a message at the Debug level
func (s *Logger) Debugf(format string, v ...interface{}) {
s.logf(LevelDebug, format, v...)
}
// Tracef will print a message at the Trace level
func (s *Logger) Tracef(format string, v ...interface{}) {
s.logf(LevelTrace, format, v...)
}