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