log: cleanup

parent ac2cecc4
...@@ -72,9 +72,9 @@ enum lxc_loglevel { ...@@ -72,9 +72,9 @@ enum lxc_loglevel {
/* location information of the logging event */ /* location information of the logging event */
struct lxc_log_locinfo { struct lxc_log_locinfo {
const char *file; const char *file;
const char *func; const char *func;
int line; int line;
}; };
#define LXC_LOG_LOCINFO_INIT \ #define LXC_LOG_LOCINFO_INIT \
...@@ -82,31 +82,31 @@ struct lxc_log_locinfo { ...@@ -82,31 +82,31 @@ struct lxc_log_locinfo {
/* brief logging event object */ /* brief logging event object */
struct lxc_log_event { struct lxc_log_event {
const char* category; const char *category;
int priority; int priority;
struct timespec timestamp; struct timespec timestamp;
struct lxc_log_locinfo *locinfo; struct lxc_log_locinfo *locinfo;
const char *fmt; const char *fmt;
va_list *vap; va_list *vap;
}; };
/* log appender object */ /* log appender object */
struct lxc_log_appender { struct lxc_log_appender {
const char* name; const char *name;
int (*append)(const struct lxc_log_appender *, struct lxc_log_event *); int (*append)(const struct lxc_log_appender *, struct lxc_log_event *);
/* /*
* appenders can be stacked * appenders can be stacked
*/ */
struct lxc_log_appender *next; struct lxc_log_appender *next;
}; };
/* log category object */ /* log category object */
struct lxc_log_category { struct lxc_log_category {
const char *name; const char *name;
int priority; int priority;
struct lxc_log_appender *appender; struct lxc_log_appender *appender;
const struct lxc_log_category *parent; const struct lxc_log_category *parent;
}; };
#ifndef NO_LXC_CONF #ifndef NO_LXC_CONF
...@@ -117,18 +117,16 @@ extern int lxc_log_use_global_fd; ...@@ -117,18 +117,16 @@ extern int lxc_log_use_global_fd;
* Returns true if the chained priority is equal to or higher than * Returns true if the chained priority is equal to or higher than
* given priority. * given priority.
*/ */
static inline int static inline int lxc_log_priority_is_enabled(const struct lxc_log_category *category,
lxc_log_priority_is_enabled(const struct lxc_log_category* category, int priority)
int priority)
{ {
while (category->priority == LXC_LOG_LEVEL_NOTSET && while (category->priority == LXC_LOG_LEVEL_NOTSET && category->parent)
category->parent)
category = category->parent; category = category->parent;
int cmp_prio = category->priority; int cmp_prio = category->priority;
#ifndef NO_LXC_CONF #ifndef NO_LXC_CONF
if (!lxc_log_use_global_fd && current_config && if (!lxc_log_use_global_fd && current_config &&
current_config->loglevel != LXC_LOG_LEVEL_NOTSET) current_config->loglevel != LXC_LOG_LEVEL_NOTSET)
cmp_prio = current_config->loglevel; cmp_prio = current_config->loglevel;
#endif #endif
...@@ -138,79 +136,114 @@ lxc_log_priority_is_enabled(const struct lxc_log_category* category, ...@@ -138,79 +136,114 @@ lxc_log_priority_is_enabled(const struct lxc_log_category* category,
/* /*
* converts a priority to a literal string * converts a priority to a literal string
*/ */
static inline const char* lxc_log_priority_to_string(int priority) static inline const char *lxc_log_priority_to_string(int priority)
{ {
switch (priority) { switch (priority) {
case LXC_LOG_LEVEL_TRACE: return "TRACE"; case LXC_LOG_LEVEL_TRACE:
case LXC_LOG_LEVEL_DEBUG: return "DEBUG"; return "TRACE";
case LXC_LOG_LEVEL_INFO: return "INFO"; case LXC_LOG_LEVEL_DEBUG:
case LXC_LOG_LEVEL_NOTICE: return "NOTICE"; return "DEBUG";
case LXC_LOG_LEVEL_WARN: return "WARN"; case LXC_LOG_LEVEL_INFO:
case LXC_LOG_LEVEL_ERROR: return "ERROR"; return "INFO";
case LXC_LOG_LEVEL_CRIT: return "CRIT"; case LXC_LOG_LEVEL_NOTICE:
case LXC_LOG_LEVEL_ALERT: return "ALERT"; return "NOTICE";
case LXC_LOG_LEVEL_FATAL: return "FATAL"; case LXC_LOG_LEVEL_WARN:
default: return "WARN";
return "NOTSET"; case LXC_LOG_LEVEL_ERROR:
return "ERROR";
case LXC_LOG_LEVEL_CRIT:
return "CRIT";
case LXC_LOG_LEVEL_ALERT:
return "ALERT";
case LXC_LOG_LEVEL_FATAL:
return "FATAL";
} }
return "NOTSET";
} }
static inline const char* lxc_syslog_priority_to_string(int priority) static inline const char *lxc_syslog_priority_to_string(int priority)
{ {
switch (priority) { switch (priority) {
case LOG_DAEMON: return "daemon"; case LOG_DAEMON:
case LOG_LOCAL0: return "local0"; return "daemon";
case LOG_LOCAL1: return "local1"; case LOG_LOCAL0:
case LOG_LOCAL2: return "local2"; return "local0";
case LOG_LOCAL3: return "local3"; case LOG_LOCAL1:
case LOG_LOCAL4: return "local4"; return "local1";
case LOG_LOCAL5: return "local5"; case LOG_LOCAL2:
case LOG_LOCAL6: return "local6"; return "local2";
case LOG_LOCAL7: return "local7"; case LOG_LOCAL3:
default: return "local3";
return "NOTSET"; case LOG_LOCAL4:
return "local4";
case LOG_LOCAL5:
return "local5";
case LOG_LOCAL6:
return "local6";
case LOG_LOCAL7:
return "local7";
} }
return "NOTSET";
} }
/* /*
* converts a literal priority to an int * converts a literal priority to an int
*/ */
static inline int lxc_log_priority_to_int(const char* name) static inline int lxc_log_priority_to_int(const char *name)
{ {
if (!strcasecmp("TRACE", name)) return LXC_LOG_LEVEL_TRACE; if (strcasecmp("TRACE", name) == 0)
if (!strcasecmp("DEBUG", name)) return LXC_LOG_LEVEL_DEBUG; return LXC_LOG_LEVEL_TRACE;
if (!strcasecmp("INFO", name)) return LXC_LOG_LEVEL_INFO; if (strcasecmp("DEBUG", name) == 0)
if (!strcasecmp("NOTICE", name)) return LXC_LOG_LEVEL_NOTICE; return LXC_LOG_LEVEL_DEBUG;
if (!strcasecmp("WARN", name)) return LXC_LOG_LEVEL_WARN; if (strcasecmp("INFO", name) == 0)
if (!strcasecmp("ERROR", name)) return LXC_LOG_LEVEL_ERROR; return LXC_LOG_LEVEL_INFO;
if (!strcasecmp("CRIT", name)) return LXC_LOG_LEVEL_CRIT; if (strcasecmp("NOTICE", name) == 0)
if (!strcasecmp("ALERT", name)) return LXC_LOG_LEVEL_ALERT; return LXC_LOG_LEVEL_NOTICE;
if (!strcasecmp("FATAL", name)) return LXC_LOG_LEVEL_FATAL; if (strcasecmp("WARN", name) == 0)
return LXC_LOG_LEVEL_WARN;
if (strcasecmp("ERROR", name) == 0)
return LXC_LOG_LEVEL_ERROR;
if (strcasecmp("CRIT", name) == 0)
return LXC_LOG_LEVEL_CRIT;
if (strcasecmp("ALERT", name) == 0)
return LXC_LOG_LEVEL_ALERT;
if (strcasecmp("FATAL", name) == 0)
return LXC_LOG_LEVEL_FATAL;
return LXC_LOG_LEVEL_NOTSET; return LXC_LOG_LEVEL_NOTSET;
} }
static inline int lxc_syslog_priority_to_int(const char* name) static inline int lxc_syslog_priority_to_int(const char *name)
{ {
if (!strcasecmp("daemon", name)) return LOG_DAEMON; if (strcasecmp("daemon", name) == 0)
if (!strcasecmp("local0", name)) return LOG_LOCAL0; return LOG_DAEMON;
if (!strcasecmp("local1", name)) return LOG_LOCAL1; if (strcasecmp("local0", name) == 0)
if (!strcasecmp("local2", name)) return LOG_LOCAL2; return LOG_LOCAL0;
if (!strcasecmp("local3", name)) return LOG_LOCAL3; if (strcasecmp("local1", name) == 0)
if (!strcasecmp("local4", name)) return LOG_LOCAL4; return LOG_LOCAL1;
if (!strcasecmp("local5", name)) return LOG_LOCAL5; if (strcasecmp("local2", name) == 0)
if (!strcasecmp("local6", name)) return LOG_LOCAL6; return LOG_LOCAL2;
if (!strcasecmp("local7", name)) return LOG_LOCAL7; if (strcasecmp("local3", name) == 0)
return LOG_LOCAL3;
if (strcasecmp("local4", name) == 0)
return LOG_LOCAL4;
if (strcasecmp("local5", name) == 0)
return LOG_LOCAL5;
if (strcasecmp("local6", name) == 0)
return LOG_LOCAL6;
if (strcasecmp("local7", name) == 0)
return LOG_LOCAL7;
return -EINVAL; return -EINVAL;
} }
static inline void static inline void __lxc_log_append(const struct lxc_log_appender *appender,
__lxc_log_append(const struct lxc_log_appender *appender, struct lxc_log_event *event)
struct lxc_log_event* event)
{ {
va_list va, *va_keep; va_list va;
va_keep = event->vap; va_list *va_keep = event->vap;
while (appender) { while (appender) {
va_copy(va, *va_keep); va_copy(va, *va_keep);
...@@ -221,9 +254,8 @@ __lxc_log_append(const struct lxc_log_appender *appender, ...@@ -221,9 +254,8 @@ __lxc_log_append(const struct lxc_log_appender *appender,
} }
} }
static inline void static inline void __lxc_log(const struct lxc_log_category *category,
__lxc_log(const struct lxc_log_category* category, struct lxc_log_event *event)
struct lxc_log_event* event)
{ {
while (category) { while (category) {
__lxc_log_append(category->appender, event); __lxc_log_append(category->appender, event);
...@@ -234,34 +266,34 @@ __lxc_log(const struct lxc_log_category* category, ...@@ -234,34 +266,34 @@ __lxc_log(const struct lxc_log_category* category,
/* /*
* Helper macro to define log functions. * Helper macro to define log functions.
*/ */
#define lxc_log_priority_define(acategory, LEVEL) \ #define lxc_log_priority_define(acategory, LEVEL) \
\ \
ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo *, \ ATTR_UNUSED __attribute__ ((format (printf, 2, 3))) \
const char *, ...) __attribute__ ((format (printf, 2, 3))); \ static inline void LXC_##LEVEL(struct lxc_log_locinfo *, const char *, ...); \
\ \
ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
const char* format, ...) \ const char* format, ...) \
{ \ { \
if (lxc_log_priority_is_enabled(acategory, \ if (lxc_log_priority_is_enabled(acategory, LXC_LOG_LEVEL_##LEVEL)) { \
LXC_LOG_LEVEL_##LEVEL)) { \ va_list va_ref; \
struct lxc_log_event evt = { \ struct lxc_log_event evt = { \
.category = (acategory)->name, \ .category = (acategory)->name, \
.priority = LXC_LOG_LEVEL_##LEVEL, \ .priority = LXC_LOG_LEVEL_##LEVEL, \
.fmt = format, \ .fmt = format, \
.locinfo = locinfo \ .locinfo = locinfo \
}; \ }; \
va_list va_ref; \ \
\ /* clock_gettime() is explicitly marked as MT-Safe \
/* clock_gettime() is explicitly marked as MT-Safe \ * without restrictions. So let's use it for our \
* without restrictions. So let's use it for our \ * logging stamps. \
* logging stamps. */ \ */ \
clock_gettime(CLOCK_REALTIME, &evt.timestamp); \ (void)clock_gettime(CLOCK_REALTIME, &evt.timestamp); \
\ \
va_start(va_ref, format); \ va_start(va_ref, format); \
evt.vap = &va_ref; \ evt.vap = &va_ref; \
__lxc_log(acategory, &evt); \ __lxc_log(acategory, &evt); \
va_end(va_ref); \ va_end(va_ref); \
} \ } \
} }
/* /*
...@@ -271,7 +303,7 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ ...@@ -271,7 +303,7 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
extern struct lxc_log_category lxc_log_category_##parent; \ extern struct lxc_log_category lxc_log_category_##parent; \
struct lxc_log_category lxc_log_category_##name = { \ struct lxc_log_category lxc_log_category_##name = { \
#name, \ #name, \
LXC_LOG_LEVEL_NOTSET, \ LXC_LOG_LEVEL_NOTSET, \
NULL, \ NULL, \
&lxc_log_category_##parent \ &lxc_log_category_##parent \
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment