log: new methods to check if a given logging level is enabled

There are certain cases where we perform a lot of data processing just
for logging purposes. Having methods that let us know whether a given
log level will be printed before doing all that data processing is useful.
This commit is contained in:
Aleksander Morgado
2022-09-13 12:09:58 +00:00
parent afb89bb78f
commit 3592d98e5b
2 changed files with 24 additions and 11 deletions

View File

@@ -213,6 +213,12 @@ log_backend_systemd_journal (const char *loc,
} }
#endif #endif
gboolean
mm_log_check_level_enabled (MMLogLevel level)
{
return (log_level & level);
}
void void
_mm_log (gpointer obj, _mm_log (gpointer obj,
const gchar *module, const gchar *module,
@@ -225,7 +231,7 @@ _mm_log (gpointer obj,
va_list args; va_list args;
GTimeVal tv; GTimeVal tv;
if (!(log_level & level)) if (!mm_log_check_level_enabled (level))
return; return;
if (g_once_init_enter (&msgbuf_once)) { if (g_once_init_enter (&msgbuf_once)) {

View File

@@ -50,6 +50,12 @@ typedef enum {
# define mm_dbg(...) mm_obj_dbg (NULL, ## __VA_ARGS__ ) # define mm_dbg(...) mm_obj_dbg (NULL, ## __VA_ARGS__ )
#endif #endif
#define mm_log_err_enabled() mm_log_check_level_enabled (MM_LOG_LEVEL_ERR)
#define mm_log_warn_enabled() mm_log_check_level_enabled (MM_LOG_LEVEL_WARN)
#define mm_log_msg_enabled() mm_log_check_level_enabled (MM_LOG_LEVEL_MSG)
#define mm_log_info_enabled() mm_log_check_level_enabled (MM_LOG_LEVEL_INFO)
#define mm_log_debug_enabled() mm_log_check_level_enabled (MM_LOG_LEVEL_DEBUG)
void _mm_log (gpointer obj, void _mm_log (gpointer obj,
const gchar *module, const gchar *module,
const gchar *loc, const gchar *loc,
@@ -58,16 +64,17 @@ void _mm_log (gpointer obj,
const gchar *fmt, const gchar *fmt,
...) __attribute__((__format__ (__printf__, 6, 7))); ...) __attribute__((__format__ (__printf__, 6, 7)));
gboolean mm_log_set_level (const gchar *level, gboolean mm_log_set_level (const gchar *level,
GError **error); GError **error);
gboolean mm_log_setup (const gchar *level, gboolean mm_log_setup (const gchar *level,
const gchar *log_file, const gchar *log_file,
gboolean log_journal, gboolean log_journal,
gboolean show_ts, gboolean show_ts,
gboolean rel_ts, gboolean rel_ts,
gboolean show_personal_info, gboolean show_personal_info,
GError **error); GError **error);
void mm_log_shutdown (void); gboolean mm_log_check_level_enabled (MMLogLevel level);
void mm_log_shutdown (void);
/* Helper used when printing a string that may be personal /* Helper used when printing a string that may be personal
* info. Depending on the settings, we may print it as-is, * info. Depending on the settings, we may print it as-is,