forked from digitalocean/captainslog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.go
More file actions
321 lines (263 loc) · 6.05 KB
/
priority.go
File metadata and controls
321 lines (263 loc) · 6.05 KB
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
321
package captainslog
import (
"errors"
"fmt"
)
var (
//ErrBadPriority is returned when the priority of a message is malformed.
ErrBadPriority = errors.New("Priority not found")
//ErrBadFacility is returned when a facility is not within allowed values.
ErrBadFacility = errors.New("Facility not found")
//ErrBadSeverity is returned when a severity is not within allowed values.
ErrBadSeverity = errors.New("Severity not found")
)
// Severity represents a syslog severity code
type Severity int
const (
// Emerg is an emergency rfc3164 severity
Emerg Severity = 0
// Alert is an alert rfc3164 severity
Alert Severity = 1
// Crit is a critical level rfc3164 severity
Crit Severity = 2
// Err is an error level rfc3164 severity
Err Severity = 3
// Warning is a warning level rfc3164 severity
Warning Severity = 4
// Notice is a notice level rfc3164 severity
Notice Severity = 5
// Info is an info level rfc3164 severity
Info Severity = 6
// Debug is a debug level rfc3164 severity
Debug Severity = 7
)
func (s Severity) String() string {
var severityText string
switch s {
case Emerg:
severityText = "emerg"
case Alert:
severityText = "alert"
case Crit:
severityText = "crit"
case Err:
severityText = "err"
case Warning:
severityText = "warning"
case Notice:
severityText = "notice"
case Info:
severityText = "info"
case Debug:
severityText = "debug"
default:
}
return severityText
}
// FromString loads a syslog severity from a string representation
func (s *Severity) FromString(v string) error {
switch v {
case Emerg.String():
*s = Emerg
case Alert.String():
*s = Alert
case Crit.String():
*s = Crit
case Err.String():
*s = Err
case Warning.String():
*s = Warning
case Notice.String():
*s = Notice
case Info.String():
*s = Info
case Debug.String():
*s = Debug
default:
return fmt.Errorf("Failed to load syslog severity from string: %s", v)
}
return nil
}
// Facility represents a syslog facility code
type Facility int
const (
//Kern is the kernel rfc3164 facility.
Kern Facility = 0
//User is the user rfc3164 facility.
User Facility = 1
// Mail is the mail rfc3164 facility.
Mail Facility = 2
// Daemon is the daemon rfc3164 facility.
Daemon Facility = 3
// Auth is the auth rfc3164 facility.
Auth Facility = 4
// Syslog is the syslog rfc3164 facility.
Syslog Facility = 5
// LPR is the printer rfc3164 facility.
LPR Facility = 6
// News is a news rfc3164 facility.
News Facility = 7
// UUCP is the UUCP rfc3164 facility.
UUCP Facility = 8
// Cron is the cron rfc3164 facility.
Cron Facility = 9
//AuthPriv is the authpriv rfc3164 facility.
AuthPriv Facility = 10
// FTP is the ftp rfc3164 facility.
FTP Facility = 11
// Local0 is the local0 rfc3164 facility.
Local0 Facility = 16
// Local1 is the local1 rfc3164 facility.
Local1 Facility = 17
// Local2 is the local2 rfc3164 facility.
Local2 Facility = 18
// Local3 is the local3 rfc3164 facility.
Local3 Facility = 19
// Local4 is the local4 rfc3164 facility.
Local4 Facility = 20
// Local5 is the local5 rfc3164 facility.
Local5 Facility = 21
// Local6 is the local6 rfc3164 facility.
Local6 Facility = 22
// Local7 is the local7 rfc3164 facility.
Local7 Facility = 23
)
func (f Facility) String() string {
var faciliyText string
switch f {
case Kern:
faciliyText = "kern"
case User:
faciliyText = "user"
case Mail:
faciliyText = "mail"
case Daemon:
faciliyText = "daemon"
case Auth:
faciliyText = "auth"
case Syslog:
faciliyText = "syslog"
case LPR:
faciliyText = "lpr"
case News:
faciliyText = "news"
case UUCP:
faciliyText = "uucp"
case Cron:
faciliyText = "cron"
case AuthPriv:
faciliyText = "authpriv"
case FTP:
faciliyText = "ftp"
case Local0:
faciliyText = "local0"
case Local1:
faciliyText = "local1"
case Local2:
faciliyText = "local2"
case Local3:
faciliyText = "local3"
case Local4:
faciliyText = "local4"
case Local5:
faciliyText = "local5"
case Local6:
faciliyText = "local6"
case Local7:
faciliyText = "local7"
default:
}
return faciliyText
}
// FromString sets the Facility from a string representation.
func (f *Facility) FromString(v string) error {
switch v {
case Kern.String():
*f = Kern
case User.String():
*f = User
case Mail.String():
*f = Mail
case Daemon.String():
*f = Daemon
case Auth.String():
*f = Auth
case Syslog.String():
*f = Syslog
case LPR.String():
*f = LPR
case News.String():
*f = News
case UUCP.String():
*f = UUCP
case Cron.String():
*f = Cron
case AuthPriv.String():
*f = AuthPriv
case FTP.String():
*f = FTP
case Local0.String():
*f = Local0
case Local1.String():
*f = Local1
case Local2.String():
*f = Local2
case Local3.String():
*f = Local3
case Local4.String():
*f = Local4
case Local5.String():
*f = Local5
case Local6.String():
*f = Local6
case Local7.String():
*f = Local7
default:
return fmt.Errorf("Failed to load syslog facility from string: %s", v)
}
return nil
}
// Priority represents the PRI of a rfc3164 message.
type Priority struct {
Priority int
Facility Facility
Severity Severity
}
// String converts the given Priority to a string.
func (p Priority) String() string {
return fmt.Sprintf("%d", p.Priority)
}
// SetFacility sets the Facility component of the Priority.
func (p *Priority) SetFacility(f Facility) error {
if int(f) < 0 || int(f) > 23 {
return ErrBadFacility
}
p.Facility = f
p.calculatePriority()
return nil
}
// SetSeverity sets the Severity component of the Priority.
func (p *Priority) SetSeverity(s Severity) error {
if int(s) < 0 || int(s) > 7 {
return ErrBadSeverity
}
p.Severity = s
p.calculatePriority()
return nil
}
func (p *Priority) calculatePriority() {
p.Priority = int(p.Facility)*8 + int(p.Severity)
}
// NewPriority calculates a Priority from a Facility
// and Severity.
func NewPriority(f Facility, s Severity) (*Priority, error) {
p := &Priority{}
if err := p.SetFacility(f); err != nil {
return nil, err
}
if err := p.SetSeverity(s); err != nil {
return nil, err
}
p.calculatePriority()
return p, nil
}