LCOV - code coverage report
Current view: top level - utils - logging.c (source / functions) Coverage Total Hit
Test: libblockdev Coverage Report Lines: 54.1 % 37 20
Test Date: 2026-01-23 09:12:16 Functions: 80.0 % 5 4
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2019  Red Hat, Inc.
       3              :  *
       4              :  * This library is free software; you can redistribute it and/or
       5              :  * modify it under the terms of the GNU Lesser General Public
       6              :  * License as published by the Free Software Foundation; either
       7              :  * version 2.1 of the License, or (at your option) any later version.
       8              :  *
       9              :  * This library is distributed in the hope that it will be useful,
      10              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12              :  * Lesser General Public License for more details.
      13              :  *
      14              :  * You should have received a copy of the GNU Lesser General Public
      15              :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      16              :  *
      17              :  * Author: Vojtech Trefny <vtrefny@redhat.com>
      18              :  */
      19              : 
      20              : #include <glib.h>
      21              : #include <glib/gprintf.h>
      22              : #include <stdarg.h>
      23              : 
      24              : #include "logging.h"
      25              : 
      26              : static BDUtilsLogFunc log_func = &bd_utils_log_stdout;
      27              : 
      28              : #ifdef DEBUG
      29              : static int log_level = BD_UTILS_LOG_DEBUG;
      30              : #else
      31              : static int log_level = BD_UTILS_LOG_WARNING;
      32              : #endif
      33              : 
      34              : /**
      35              :  * bd_utils_init_logging:
      36              :  * @new_log_func: (nullable) (scope notified): logging function to use or
      37              :  *                                               %NULL to disable logging; use
      38              :  *                                               #bd_utils_log_stdout to reset to
      39              :  *                                               the default behaviour
      40              :  * @error: (out) (optional): place to store error (if any)
      41              :  *
      42              :  * Returns: whether logging was successfully initialized or not
      43              :  */
      44           10 : gboolean bd_utils_init_logging (BDUtilsLogFunc new_log_func, GError **error G_GNUC_UNUSED) {
      45              :     /* XXX: the error attribute will likely be used in the future when this
      46              :        function gets more complicated */
      47              : 
      48           10 :     log_func = new_log_func;
      49              : 
      50           10 :     return TRUE;
      51              : }
      52              : 
      53              : /**
      54              :  * bd_utils_set_log_level:
      55              :  * @level: log level
      56              :  *
      57              :  * Level of messages to log. Only messages with level <= @level will be logged.
      58              :  * For example using with #BD_UTILS_LOG_WARNING (default value) only messages
      59              :  * with log levels #BD_UTILS_LOG_WARNING, #BD_UTILS_LOG_ERR, ..., #BD_UTILS_LOG_EMERG
      60              :  * will be logged.
      61              :  *
      62              :  * Note: #BD_UTILS_LOG_DEBUG level messages are always skipped unless compiled
      63              :  *       with `--enable-debug` configure option.
      64              :  */
      65           17 : void bd_utils_set_log_level (gint level) {
      66           17 :     log_level = level;
      67           17 : }
      68              : 
      69              : /**
      70              :  * bd_utils_log:
      71              :  * @level: log level
      72              :  * @msg: log message
      73              :  */
      74        11850 : void bd_utils_log (gint level, const gchar *msg) {
      75        11850 :     if (log_func && level <= log_level)
      76          133 :         log_func (level, msg);
      77        11850 : }
      78              : 
      79              : /**
      80              :  * bd_utils_log_format:
      81              :  * @level: log level
      82              :  * @format: printf-style format for the log message
      83              :  * @...: arguments for @format
      84              :  */
      85          111 : void bd_utils_log_format (gint level, const gchar *format, ...) {
      86          111 :     gchar *msg = NULL;
      87              :     va_list args;
      88          111 :     gint ret = 0;
      89              : 
      90          111 :     if (log_func && level <= log_level) {
      91            7 :         va_start (args, format);
      92            7 :         ret = g_vasprintf (&msg, format, args);
      93            7 :         va_end (args);
      94              : 
      95            7 :         if (ret < 0) {
      96            0 :             g_free (msg);
      97            0 :             return;
      98              :         }
      99              : 
     100            7 :         log_func (level, msg);
     101              :     }
     102              : 
     103          111 :     g_free (msg);
     104              : }
     105              : 
     106              : /**
     107              :  * bd_utils_log_stdout:
     108              :  * @level: log level
     109              :  * @msg: log message
     110              :  *
     111              :  * Convenient function for logging to stdout. Can be used as #BDUtilsLogFunc.
     112              :  *
     113              :  */
     114            0 : void bd_utils_log_stdout (gint level, const gchar *msg) {
     115            0 :     if (level > log_level)
     116            0 :         return;
     117              : 
     118            0 :     switch (level) {
     119            0 :         case BD_UTILS_LOG_DEBUG:
     120              : #ifdef DEBUG
     121              :             g_debug ("%s", msg);
     122              : #endif
     123            0 :             break;
     124            0 :         case BD_UTILS_LOG_INFO:
     125              :         case BD_UTILS_LOG_NOTICE:
     126            0 :             g_info ("%s", msg);
     127            0 :             break;
     128            0 :         case BD_UTILS_LOG_WARNING:
     129              :         case BD_UTILS_LOG_ERR:
     130            0 :             g_warning ("%s", msg);
     131            0 :             break;
     132            0 :         case BD_UTILS_LOG_EMERG:
     133              :         case BD_UTILS_LOG_ALERT:
     134              :         case BD_UTILS_LOG_CRIT:
     135            0 :             g_critical ("%s", msg);
     136            0 :             break;
     137              :     }
     138              : }
        

Generated by: LCOV version 2.0-1