Debian Codegraph @

src/utils/logging.c

raw
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 <[email protected]>
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 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 log_func = new_log_func;
49
50 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 void bd_utils_set_log_level (gint level) {
66 log_level = level;
67 }
68
69 /**
70 * bd_utils_log:
71 * @level: log level
72 * @msg: log message
73 */
74 void bd_utils_log (gint level, const gchar *msg) {
75 if (log_func && level <= log_level)
76 log_func (level, msg);
77 }
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 void bd_utils_log_format (gint level, const gchar *format, ...) {
86 gchar *msg = NULL;
87 va_list args;
88 gint ret = 0;
89
90 if (log_func && level <= log_level) {
91 va_start (args, format);
92 ret = g_vasprintf (&msg, format, args);
93 va_end (args);
94
95 if (ret < 0) {
96 g_free (msg);
97 return;
98 }
99
100 log_func (level, msg);
101 }
102
103 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 * Note: This function uses GLib logging functions like %g_info or %g_warning
114 * for the printing to stdout/stderr so these messages can be redirected
115 * when a custom log handler is set using e.g. %g_log_set_handler.
116 *
117 */
118 void bd_utils_log_stdout (gint level, const gchar *msg) {
119 if (level > log_level)
120 return;
121
122 switch (level) {
123 case BD_UTILS_LOG_DEBUG:
124 #ifdef DEBUG
125 g_debug ("%s", msg);
126 #endif
127 break;
128 case BD_UTILS_LOG_INFO:
129 case BD_UTILS_LOG_NOTICE:
130 g_info ("%s", msg);
131 break;
132 case BD_UTILS_LOG_WARNING:
133 case BD_UTILS_LOG_ERR:
134 g_warning ("%s", msg);
135 break;
136 case BD_UTILS_LOG_EMERG:
137 case BD_UTILS_LOG_ALERT:
138 case BD_UTILS_LOG_CRIT:
139 g_critical ("%s", msg);
140 break;
141 }
142 }