]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/syslog-util.c
Add SPDX license identifiers to source files under the LGPL
[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 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <string.h>
22 #include <syslog.h>
23
24 #include "hexdecoct.h"
25 #include "macro.h"
26 #include "string-table.h"
27 #include "syslog-util.h"
28
29 int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
30 int a = 0, b = 0, c = 0;
31 int k;
32
33 assert(p);
34 assert(*p);
35 assert(priority);
36
37 if ((*p)[0] != '<')
38 return 0;
39
40 if (!strchr(*p, '>'))
41 return 0;
42
43 if ((*p)[2] == '>') {
44 c = undecchar((*p)[1]);
45 k = 3;
46 } else if ((*p)[3] == '>') {
47 b = undecchar((*p)[1]);
48 c = undecchar((*p)[2]);
49 k = 4;
50 } else if ((*p)[4] == '>') {
51 a = undecchar((*p)[1]);
52 b = undecchar((*p)[2]);
53 c = undecchar((*p)[3]);
54 k = 5;
55 } else
56 return 0;
57
58 if (a < 0 || b < 0 || c < 0 ||
59 (!with_facility && (a || b || c > 7)))
60 return 0;
61
62 if (with_facility)
63 *priority = a*100 + b*10 + c;
64 else
65 *priority = (*priority & LOG_FACMASK) | c;
66
67 *p += k;
68 return 1;
69 }
70
71 static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
72 [LOG_FAC(LOG_KERN)] = "kern",
73 [LOG_FAC(LOG_USER)] = "user",
74 [LOG_FAC(LOG_MAIL)] = "mail",
75 [LOG_FAC(LOG_DAEMON)] = "daemon",
76 [LOG_FAC(LOG_AUTH)] = "auth",
77 [LOG_FAC(LOG_SYSLOG)] = "syslog",
78 [LOG_FAC(LOG_LPR)] = "lpr",
79 [LOG_FAC(LOG_NEWS)] = "news",
80 [LOG_FAC(LOG_UUCP)] = "uucp",
81 [LOG_FAC(LOG_CRON)] = "cron",
82 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
83 [LOG_FAC(LOG_FTP)] = "ftp",
84 [LOG_FAC(LOG_LOCAL0)] = "local0",
85 [LOG_FAC(LOG_LOCAL1)] = "local1",
86 [LOG_FAC(LOG_LOCAL2)] = "local2",
87 [LOG_FAC(LOG_LOCAL3)] = "local3",
88 [LOG_FAC(LOG_LOCAL4)] = "local4",
89 [LOG_FAC(LOG_LOCAL5)] = "local5",
90 [LOG_FAC(LOG_LOCAL6)] = "local6",
91 [LOG_FAC(LOG_LOCAL7)] = "local7"
92 };
93
94 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
95
96 bool log_facility_unshifted_is_valid(int facility) {
97 return facility >= 0 && facility <= LOG_FAC(~0);
98 }
99
100 static const char *const log_level_table[] = {
101 [LOG_EMERG] = "emerg",
102 [LOG_ALERT] = "alert",
103 [LOG_CRIT] = "crit",
104 [LOG_ERR] = "err",
105 [LOG_WARNING] = "warning",
106 [LOG_NOTICE] = "notice",
107 [LOG_INFO] = "info",
108 [LOG_DEBUG] = "debug"
109 };
110
111 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
112
113 bool log_level_is_valid(int level) {
114 return level >= 0 && level <= LOG_DEBUG;
115 }