#include <assert.h>
#include <limits.h>
#include <ctype.h>
+#include <syslog.h>
/**
* @addtogroup SHARED_FUNCTIONS
/**
* @brief Translate syslog priority
* @details Maps syslog priority to DLog priority
- * @param[in] prio Syslog priority
+ * @param[in] fac_prio Syslog facility/priority
* @return DLog priority
*/
-static int translate_syslog_priority(int prio)
+static int translate_syslog_priority(int fac_prio)
{
- prio &= 7; // syslog priority is encoded on the three lowest bits.
+ const int prio = LOG_PRI(fac_prio);
+ assert(prio >= 0);
+ assert(prio < NELEMS(syslog_priority_mapping));
return syslog_priority_mapping[prio];
}
-static const char *syslog_facilities[] = {
- "SYSLOG-KERN",
- "SYSLOG-USER",
- "SYSLOG-MAIL",
- "SYSLOG-DAEMON",
- "SYSLOG-AUTH",
- "SYSLOG-SYSLOG",
- "SYSLOG-LPR",
- "SYSLOG-NEWS",
- "SYSLOG-UUCP",
- "SYSLOG-CRON",
- "SYSLOG-AUTHPRIV",
- "SYSLOG-SYSTEM",
- "SYSLOG-SYSTEM",
- "SYSLOG-SYSTEM",
- "SYSLOG-SYSTEM",
- "SYSLOG-SYSTEM",
- "SYSLOG-LOCAL0",
- "SYSLOG-LOCAL1",
- "SYSLOG-LOCAL2",
- "SYSLOG-LOCAL3",
- "SYSLOG-LOCAL4",
- "SYSLOG-LOCAL5",
- "SYSLOG-LOCAL6",
- "SYSLOG-LOCAL7",
+#define STR_FAC(x)[LOG_FAC(x)] = "SYS" #x // [LOG_FOO] = "SYSLOG_FOO"
+static const char *syslog_facilities[LOG_NFACILITIES] = {
+ STR_FAC(LOG_KERN),
+ STR_FAC(LOG_USER),
+ STR_FAC(LOG_MAIL),
+ STR_FAC(LOG_DAEMON),
+ STR_FAC(LOG_AUTH),
+ STR_FAC(LOG_SYSLOG),
+ STR_FAC(LOG_LPR),
+ STR_FAC(LOG_NEWS),
+ STR_FAC(LOG_UUCP),
+ STR_FAC(LOG_CRON),
+ STR_FAC(LOG_AUTHPRIV),
+ [11] = "SYSLOG_SYSTEM_11", // 11-15 are system reserved without any LOG_SYSTEM constants
+ [12] = "SYSLOG_SYSTEM_12",
+ [13] = "SYSLOG_SYSTEM_13",
+ [14] = "SYSLOG_SYSTEM_14",
+ [15] = "SYSLOG_SYSTEM_15",
+ STR_FAC(LOG_LOCAL0),
+ STR_FAC(LOG_LOCAL1),
+ STR_FAC(LOG_LOCAL2),
+ STR_FAC(LOG_LOCAL3),
+ STR_FAC(LOG_LOCAL4),
+ STR_FAC(LOG_LOCAL5),
+ STR_FAC(LOG_LOCAL6),
+ STR_FAC(LOG_LOCAL7),
};
+#undef STR_FAC
/**
* @brief Translate syslog facility
* @details Translates the integer representation of a syslog facility into a string
- * @param[in] facility The facility (as provided by syslog)
+ * @param[in] fac_prio The facility/priority (as provided by syslog)
* @return String representation of the facility name
*/
-static const char *translate_syslog_facility(int facility)
+static const char *translate_syslog_facility(int fac_prio)
{
- facility >>= 3; // first three bits are priority, facility is encoded on the remaining higher bits
+ const int facility = LOG_FAC(fac_prio);
- if (facility < 0 ||
- facility >= NELEMS(syslog_facilities))
+ assert(facility >= 0);
+
+ if (facility >= NELEMS(syslog_facilities))
return "SYSLOG-UNKNOWN";
return syslog_facilities[facility];
*/
static void construct_logger_entry_from_syslog(struct logger_entry *le, int priority, const char *tag, struct tm *timestamp, int pid, const char *msg)
{
+ assert(le);
+ assert(tag);
+ assert(timestamp);
+ assert(msg);
+
const int tag_len = strlen(tag) + 1;
const int msg_len = strlen(msg) + 1;
*/
int parse_syslog_datagram(const char *buffer, int buffer_size, struct logger_entry * le)
{
- int fac_prio, pid = 0;
+ int fac_prio = -1, pid = 0;
struct tm timestamp;
- const char *msg;
+ const char *msg = NULL;
struct parser parsePid[] = {
{ parse_const_char, "[" },
assert(le.pid == 678);
assert(le.priority == DLOG_FATAL);
- assert(!strcmp("SYSLOG-USER", le.tag));
+ assert(!strcmp("SYSLOG_USER", le.tag));
assert(!strcmp("msg", le.message));
// Valid
assert(le.pid == 0);
assert(le.priority == DLOG_FATAL);
- assert(!strcmp("SYSLOG-USER", le.tag));
+ assert(!strcmp("SYSLOG_USER", le.tag));
assert(!strcmp("msg", le.message));
return 0;