-
Notifications
You must be signed in to change notification settings - Fork 62
/
node_log.go
107 lines (89 loc) · 2.53 KB
/
node_log.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
package goroslib
import (
"fmt"
"os"
"time"
"github.com/gookit/color"
"github.com/bluenviron/goroslib/v2/pkg/msgs/rosgraph_msgs"
"github.com/bluenviron/goroslib/v2/pkg/msgs/std_msgs"
)
// LogLevel is the level of a log message.
type LogLevel int
// standard log levels (http://wiki.ros.org/roscpp/Overview/Logging)
const (
LogLevelDebug LogLevel = iota + 1
LogLevelInfo
LogLevelWarn
LogLevelError
LogLevelFatal
)
// LogDestination is the destination of a log message.
type LogDestination int
// available destinations.
const (
LogDestinationConsole LogDestination = 0x01
LogDestinationRosout LogDestination = 0x02
LogDestinationCallback LogDestination = 0x04
)
// Log writes a log message.
// This is implemented like the reference C++ implementation,
// except for the log file.
// (http://wiki.ros.org/roscpp/Overview/Logging)
func (n *Node) Log(level LogLevel, format string, args ...interface{}) {
if level < n.conf.LogLevel {
return
}
msg := fmt.Sprintf(format, args...)
now := time.Now()
if (n.conf.LogDestinations & LogDestinationConsole) != 0 {
formatted := msg
switch level {
case LogLevelDebug:
formatted = "[DEBUG] " + formatted
case LogLevelInfo:
formatted = "[INFO] " + formatted
case LogLevelWarn:
formatted = "[WARN] " + formatted
case LogLevelError:
formatted = "[ERROR] " + formatted
case LogLevelFatal:
formatted = "[FATAL] " + formatted
}
formatted = now.Format("[2006/01/02 15:04:05]") + " " + formatted
switch level {
case LogLevelDebug:
os.Stdout.WriteString(color.RenderString(color.Gray.Code(), formatted) + "\n")
case LogLevelInfo:
os.Stdout.WriteString(formatted + "\n")
case LogLevelWarn:
os.Stderr.WriteString(color.RenderString(color.Yellow.Code(), formatted) + "\n")
case LogLevelError, LogLevelFatal:
os.Stderr.WriteString(color.RenderString(color.Red.Code(), formatted) + "\n")
}
}
if (n.conf.LogDestinations&LogDestinationRosout) != 0 && n.rosoutPublisher != nil {
n.rosoutPublisher.Write(&rosgraph_msgs.Log{
Header: std_msgs.Header{
Stamp: now,
},
Level: func() int8 {
switch level {
case LogLevelDebug:
return rosgraph_msgs.Log_DEBUG
case LogLevelInfo:
return rosgraph_msgs.Log_INFO
case LogLevelWarn:
return rosgraph_msgs.Log_WARN
case LogLevelError:
return rosgraph_msgs.Log_ERROR
}
return rosgraph_msgs.Log_FATAL
}(),
Name: n.absoluteName(),
Msg: msg,
})
}
if (n.conf.LogDestinations&LogDestinationCallback) != 0 && n.conf.OnLog != nil {
n.conf.OnLog(level, msg)
}
}