]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/syslog-util.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / basic / syslog-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <string.h>
9 #include <syslog.h>
10
11 #include "hexdecoct.h"
12 #include "macro.h"
13 #include "string-table.h"
14 #include "syslog-util.h"
15
16 int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
17 int a = 0, b = 0, c = 0;
18 int k;
19
20 assert(p);
21 assert(*p);
22 assert(priority);
23
24 if ((*p)[0] != '<')
25 return 0;
26
27 if (!strchr(*p, '>'))
28 return 0;
29
30 if ((*p)[2] == '>') {
31 c = undecchar((*p)[1]);
32 k = 3;
33 } else if ((*p)[3] == '>') {
34 b = undecchar((*p)[1]);
35 c = undecchar((*p)[2]);
36 k = 4;
37 } else if ((*p)[4] == '>') {
38 a = undecchar((*p)[1]);
39 b = undecchar((*p)[2]);
40 c = undecchar((*p)[3]);
41 k = 5;
42 } else
43 return 0;
44
45 if (a < 0 || b < 0 || c < 0 ||
46 (!with_facility && (a || b || c > 7)))
47 return 0;
48
49 if (with_facility)
50 *priority = a*100 + b*10 + c;
51 else
52 *priority = (*priority & LOG_FACMASK) | c;
53
54 *p += k;
55 return 1;
56 }
57
58 static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
59 [LOG_FAC(LOG_KERN)] = "kern",
60 [LOG_FAC(LOG_USER)] = "user",
61 [LOG_FAC(LOG_MAIL)] = "mail",
62 [LOG_FAC(LOG_DAEMON)] = "daemon",
63 [LOG_FAC(LOG_AUTH)] = "auth",
64 [LOG_FAC(LOG_SYSLOG)] = "syslog",
65 [LOG_FAC(LOG_LPR)] = "lpr",
66 [LOG_FAC(LOG_NEWS)] = "news",
67 [LOG_FAC(LOG_UUCP)] = "uucp",
68 [LOG_FAC(LOG_CRON)] = "cron",
69 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
70 [LOG_FAC(LOG_FTP)] = "ftp",
71 [LOG_FAC(LOG_LOCAL0)] = "local0",
72 [LOG_FAC(LOG_LOCAL1)] = "local1",
73 [LOG_FAC(LOG_LOCAL2)] = "local2",
74 [LOG_FAC(LOG_LOCAL3)] = "local3",
75 [LOG_FAC(LOG_LOCAL4)] = "local4",
76 [LOG_FAC(LOG_LOCAL5)] = "local5",
77 [LOG_FAC(LOG_LOCAL6)] = "local6",
78 [LOG_FAC(LOG_LOCAL7)] = "local7"
79 };
80
81 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
82
83 bool log_facility_unshifted_is_valid(int facility) {
84 return facility >= 0 && facility <= LOG_FAC(~0);
85 }
86
87 static const char *const log_level_table[] = {
88 [LOG_EMERG] = "emerg",
89 [LOG_ALERT] = "alert",
90 [LOG_CRIT] = "crit",
91 [LOG_ERR] = "err",
92 [LOG_WARNING] = "warning",
93 [LOG_NOTICE] = "notice",
94 [LOG_INFO] = "info",
95 [LOG_DEBUG] = "debug"
96 };
97
98 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
99
100 bool log_level_is_valid(int level) {
101 return level >= 0 && level <= LOG_DEBUG;
102 }