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