log: Refactor evaluation of log level

Starting with adding a typed enum type for the log level named
MMLogLevel.

Suggested-By: Aleksander Morgado <aleksander@aleksander.es>

The level was checked twice in _mm_log. Once at the beginning and
again when turning the level into both the syslog priority and some
descriptive text which was added to the log level. Removing the check
at the second location as it was redundant.

Also adding a helper function to turn the MMLogLevel into the syslog
equivalent. This will become handy when adding support for systemd
journal.
This commit is contained in:
Torsten Hilbrich
2017-06-21 12:54:35 +02:00
committed by Aleksander Morgado
parent 0e854644db
commit b4ad1e8a21
2 changed files with 47 additions and 29 deletions

View File

@@ -19,31 +19,31 @@
#include <glib.h>
/* Log levels */
enum {
LOGL_ERR = 0x00000001,
LOGL_WARN = 0x00000002,
LOGL_INFO = 0x00000004,
LOGL_DEBUG = 0x00000008
};
typedef enum {
MM_LOG_LEVEL_ERR = 0x00000001,
MM_LOG_LEVEL_WARN = 0x00000002,
MM_LOG_LEVEL_INFO = 0x00000004,
MM_LOG_LEVEL_DEBUG = 0x00000008
} MMLogLevel;
#define mm_err(...) \
_mm_log (G_STRLOC, G_STRFUNC, LOGL_ERR, ## __VA_ARGS__ )
_mm_log (G_STRLOC, G_STRFUNC, MM_LOG_LEVEL_ERR, ## __VA_ARGS__ )
#define mm_warn(...) \
_mm_log (G_STRLOC, G_STRFUNC, LOGL_WARN, ## __VA_ARGS__ )
_mm_log (G_STRLOC, G_STRFUNC, MM_LOG_LEVEL_WARN, ## __VA_ARGS__ )
#define mm_info(...) \
_mm_log (G_STRLOC, G_STRFUNC, LOGL_INFO, ## __VA_ARGS__ )
_mm_log (G_STRLOC, G_STRFUNC, MM_LOG_LEVEL_INFO, ## __VA_ARGS__ )
#define mm_dbg(...) \
_mm_log (G_STRLOC, G_STRFUNC, LOGL_DEBUG, ## __VA_ARGS__ )
_mm_log (G_STRLOC, G_STRFUNC, MM_LOG_LEVEL_DEBUG, ## __VA_ARGS__ )
#define mm_log(level, ...) \
_mm_log (G_STRLOC, G_STRFUNC, level, ## __VA_ARGS__ )
void _mm_log (const char *loc,
const char *func,
guint32 level,
MMLogLevel level,
const char *fmt,
...) __attribute__((__format__ (__printf__, 4, 5)));