forked from apache/mynewt-newtmgr
/
log.go
320 lines (258 loc) · 7.83 KB
/
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 nmp
import (
"fmt"
)
//////////////////////////////////////////////////////////////////////////////
// $defs //
//////////////////////////////////////////////////////////////////////////////
const (
LEVEL_DEBUG int = 0
LEVEL_INFO = 1
LEVEL_WARN = 2
LEVEL_ERROR = 3
LEVEL_CRITICAL = 4
/* Upto 7 custom loglevels */
LEVEL_MAX = 255
)
const (
STREAM_LOG int = 0
MEMORY_LOG = 1
STORAGE_LOG = 2
)
const (
MODULE_DEFAULT int = 0
MODULE_OS = 1
MODULE_NEWTMGR = 2
MODULE_NIMBLE_CTLR = 3
MODULE_NIMBLE_HOST = 4
MODULE_NFFS = 5
MODULE_REBOOT = 6
MODULE_TEST = 8
MODULE_MAX = 255
)
var LogModuleNameMap = map[int]string{
MODULE_DEFAULT: "DEFAULT",
MODULE_OS: "OS",
MODULE_NEWTMGR: "NEWTMGR",
MODULE_NIMBLE_CTLR: "NIMBLE_CTLR",
MODULE_NIMBLE_HOST: "NIMBLE_HOST",
MODULE_NFFS: "NFFS",
MODULE_REBOOT: "REBOOT",
MODULE_TEST: "TEST",
}
var LogLevelNameMap = map[int]string{
LEVEL_DEBUG: "DEBUG",
LEVEL_INFO: "INFO",
LEVEL_WARN: "WARN",
LEVEL_ERROR: "ERROR",
LEVEL_CRITICAL: "CRITICAL",
}
var LogTypeNameMap = map[int]string{
STREAM_LOG: "STREAM",
MEMORY_LOG: "MEMORY",
STORAGE_LOG: "STORAGE",
}
func LogModuleToString(lm int) string {
name := LogModuleNameMap[lm]
if name == "" {
name = "CUSTOM"
}
return name
}
func LogLevelToString(lm int) string {
name := LogLevelNameMap[lm]
if name == "" {
name = "CUSTOM"
}
return name
}
func LogTypeToString(lm int) string {
name := LogTypeNameMap[lm]
if name == "" {
name = "UNDEFINED"
}
return name
}
//////////////////////////////////////////////////////////////////////////////
// $show //
//////////////////////////////////////////////////////////////////////////////
type LogEntryType int
const (
LOG_ENTRY_TYPE_STRING LogEntryType = 0
LOG_ENTRY_TYPE_CBOR = 1
LOG_ENTRY_TYPE_BINARY = 2
)
var LogEntryTypeStringMap = map[LogEntryType]string{
LOG_ENTRY_TYPE_STRING: "str",
LOG_ENTRY_TYPE_CBOR: "cbor",
LOG_ENTRY_TYPE_BINARY: "bin",
}
type LogShowReq struct {
NmpBase `codec:"-"`
Name string `codec:"log_name"`
Timestamp int64 `codec:"ts"`
Index uint32 `codec:"index"`
}
type LogEntry struct {
Index uint32 `codec:"index"`
Timestamp int64 `codec:"ts"`
Module uint8 `codec:"module"`
Level uint8 `codec:"level"`
Type LogEntryType `codec:"type"`
ImgHash []byte `codec:"imghash"`
Msg []byte `codec:"msg"`
}
type LogShowLog struct {
Name string `codec:"name"`
Type int `codec:"type"`
Entries []LogEntry `codec:"entries"`
}
type LogShowRsp struct {
NmpBase
Rc int `codec:"rc" codec:",omitempty"`
NextIndex uint32 `codec:"next_index"`
Logs []LogShowLog `codec:"logs"`
}
func NewLogShowReq() *LogShowReq {
r := &LogShowReq{}
fillNmpReq(r, NMP_OP_READ, NMP_GROUP_LOG, NMP_ID_LOG_SHOW)
return r
}
func (r *LogShowReq) Msg() *NmpMsg { return MsgFromReq(r) }
func NewLogShowRsp() *LogShowRsp {
return &LogShowRsp{}
}
func (r *LogShowRsp) Msg() *NmpMsg { return MsgFromReq(r) }
//////////////////////////////////////////////////////////////////////////////
// $list //
//////////////////////////////////////////////////////////////////////////////
type LogListReq struct {
NmpBase `codec:"-"`
}
type LogListRsp struct {
NmpBase
Rc int `codec:"rc"`
List []string `codec:"log_list"`
}
func NewLogListReq() *LogListReq {
r := &LogListReq{}
fillNmpReq(r, NMP_OP_READ, NMP_GROUP_LOG, NMP_ID_LOG_LIST)
return r
}
func (r *LogListReq) Msg() *NmpMsg { return MsgFromReq(r) }
func NewLogListRsp() *LogListRsp {
return &LogListRsp{}
}
func (r *LogListRsp) Msg() *NmpMsg { return MsgFromReq(r) }
//////////////////////////////////////////////////////////////////////////////
// $module list //
//////////////////////////////////////////////////////////////////////////////
type LogModuleListReq struct {
NmpBase `codec:"-"`
}
type LogModuleListRsp struct {
NmpBase
Rc int `codec:"rc"`
Map map[string]int `codec:"module_map"`
}
func NewLogModuleListReq() *LogModuleListReq {
r := &LogModuleListReq{}
fillNmpReq(r, NMP_OP_READ, NMP_GROUP_LOG, NMP_ID_LOG_MODULE_LIST)
return r
}
func (r *LogModuleListReq) Msg() *NmpMsg { return MsgFromReq(r) }
func NewLogModuleListRsp() *LogModuleListRsp {
return &LogModuleListRsp{}
}
func (r *LogModuleListRsp) Msg() *NmpMsg { return MsgFromReq(r) }
//////////////////////////////////////////////////////////////////////////////
// $level list //
//////////////////////////////////////////////////////////////////////////////
type LogLevelListReq struct {
NmpBase `codec:"-"`
}
type LogLevelListRsp struct {
NmpBase
Rc int `codec:"rc"`
Map map[string]int `codec:"level_map"`
}
func NewLogLevelListReq() *LogLevelListReq {
r := &LogLevelListReq{}
fillNmpReq(r, NMP_OP_READ, NMP_GROUP_LOG, NMP_ID_LOG_LEVEL_LIST)
return r
}
func (r *LogLevelListReq) Msg() *NmpMsg { return MsgFromReq(r) }
func NewLogLevelListRsp() *LogLevelListRsp {
return &LogLevelListRsp{}
}
func (r *LogLevelListRsp) Msg() *NmpMsg { return MsgFromReq(r) }
//////////////////////////////////////////////////////////////////////////////
// $clear //
//////////////////////////////////////////////////////////////////////////////
type LogClearReq struct {
NmpBase `codec:"-"`
}
type LogClearRsp struct {
NmpBase
Rc int `codec:"rc"`
}
func NewLogClearReq() *LogClearReq {
r := &LogClearReq{}
fillNmpReq(r, NMP_OP_WRITE, NMP_GROUP_LOG, NMP_ID_LOG_CLEAR)
return r
}
func (r *LogClearReq) Msg() *NmpMsg { return MsgFromReq(r) }
func NewLogClearRsp() *LogClearRsp {
return &LogClearRsp{}
}
func (r *LogClearRsp) Msg() *NmpMsg { return MsgFromReq(r) }
//////////////////////////////////////////////////////////////////////////////
// $LogType Marshal/Unmarshal //
//////////////////////////////////////////////////////////////////////////////
func LogEntryTypeToString(LogType LogEntryType) string {
s := LogEntryTypeStringMap[LogType]
if s == "" {
return "???"
}
return s
}
func LogEntryTypeFromString(s string) (LogEntryType, error) {
for LogType, name := range LogEntryTypeStringMap {
if s == name {
return LogType, nil
}
}
return LogEntryType(0), fmt.Errorf("Invalid LogEntryType string: %s", s)
}
func (l LogEntryType) String() string {
return LogEntryTypeToString(l)
}
func (l LogEntryType) MarshalBinary() ([]byte, error) {
return []byte(l.String()), nil
}
func (l *LogEntryType) UnmarshalBinary(data []byte) error {
var err error
*l, err = LogEntryTypeFromString(string(data))
if err != nil {
return err
}
return nil
}