syslog-util.c revision 11c3a36649e5e5e77db499c92f3cdcbd619efd3a
3955N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3955N/A
402N/A/***
402N/A This file is part of systemd.
402N/A
402N/A Copyright 2010 Lennart Poettering
402N/A
402N/A systemd is free software; you can redistribute it and/or modify it
402N/A under the terms of the GNU Lesser General Public License as published by
402N/A the Free Software Foundation; either version 2.1 of the License, or
402N/A (at your option) any later version.
3955N/A
402N/A systemd is distributed in the hope that it will be useful, but
402N/A WITHOUT ANY WARRANTY; without even the implied warranty of
402N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
402N/A Lesser General Public License for more details.
3955N/A
3955N/A You should have received a copy of the GNU Lesser General Public License
3955N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
4496N/A***/
3955N/A
4496N/A#include <string.h>
3955N/A#include <syslog.h>
3955N/A
402N/A#include "hexdecoct.h"
402N/A#include "macro.h"
402N/A#include "string-table.h"
402N/A#include "syslog-util.h"
402N/A
402N/Aint syslog_parse_priority(const char **p, int *priority, bool with_facility) {
402N/A int a = 0, b = 0, c = 0;
402N/A int k;
3955N/A
402N/A assert(p);
402N/A assert(*p);
402N/A assert(priority);
402N/A
402N/A if ((*p)[0] != '<')
402N/A return 0;
402N/A
402N/A if (!strchr(*p, '>'))
return 0;
if ((*p)[2] == '>') {
c = undecchar((*p)[1]);
k = 3;
} else if ((*p)[3] == '>') {
b = undecchar((*p)[1]);
c = undecchar((*p)[2]);
k = 4;
} else if ((*p)[4] == '>') {
a = undecchar((*p)[1]);
b = undecchar((*p)[2]);
c = undecchar((*p)[3]);
k = 5;
} else
return 0;
if (a < 0 || b < 0 || c < 0 ||
(!with_facility && (a || b || c > 7)))
return 0;
if (with_facility)
*priority = a*100 + b*10 + c;
else
*priority = (*priority & LOG_FACMASK) | c;
*p += k;
return 1;
}
static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
[LOG_FAC(LOG_KERN)] = "kern",
[LOG_FAC(LOG_USER)] = "user",
[LOG_FAC(LOG_MAIL)] = "mail",
[LOG_FAC(LOG_DAEMON)] = "daemon",
[LOG_FAC(LOG_AUTH)] = "auth",
[LOG_FAC(LOG_SYSLOG)] = "syslog",
[LOG_FAC(LOG_LPR)] = "lpr",
[LOG_FAC(LOG_NEWS)] = "news",
[LOG_FAC(LOG_UUCP)] = "uucp",
[LOG_FAC(LOG_CRON)] = "cron",
[LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
[LOG_FAC(LOG_FTP)] = "ftp",
[LOG_FAC(LOG_LOCAL0)] = "local0",
[LOG_FAC(LOG_LOCAL1)] = "local1",
[LOG_FAC(LOG_LOCAL2)] = "local2",
[LOG_FAC(LOG_LOCAL3)] = "local3",
[LOG_FAC(LOG_LOCAL4)] = "local4",
[LOG_FAC(LOG_LOCAL5)] = "local5",
[LOG_FAC(LOG_LOCAL6)] = "local6",
[LOG_FAC(LOG_LOCAL7)] = "local7"
};
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
bool log_facility_unshifted_is_valid(int facility) {
return facility >= 0 && facility <= LOG_FAC(~0);
}
static const char *const log_level_table[] = {
[LOG_EMERG] = "emerg",
[LOG_ALERT] = "alert",
[LOG_CRIT] = "crit",
[LOG_ERR] = "err",
[LOG_WARNING] = "warning",
[LOG_NOTICE] = "notice",
[LOG_INFO] = "info",
[LOG_DEBUG] = "debug"
};
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
bool log_level_is_valid(int level) {
return level >= 0 && level <= LOG_DEBUG;
}