nm-logging.h 13.6 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 3 4 5 6 7 8 9 10 11 12 13
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
14 15 16
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *
18
 * Copyright (C) 2006 - 2012 Red Hat, Inc.
19
 * Copyright (C) 2006 - 2008 Novell, Inc.
20 21
 */

22 23
#ifndef __NETWORKMANAGER_LOGGING_H__
#define __NETWORKMANAGER_LOGGING_H__
24

25 26 27 28
#ifdef __NM_TEST_UTILS_H__
#error nm-test-utils.h must be included as last header
#endif

29
#include "nm-glib-aux/nm-logging-fwd.h"
30

31 32 33 34
#define NM_LOG_CONFIG_BACKEND_DEBUG   "debug"
#define NM_LOG_CONFIG_BACKEND_SYSLOG  "syslog"
#define NM_LOG_CONFIG_BACKEND_JOURNAL "journal"

35 36 37 38 39 40 41 42 43 44
static inline NMLogDomain
LOGD_IP_from_af (int addr_family)
{
	switch (addr_family) {
	case AF_INET:  return LOGD_IP4;
	case AF_INET6: return LOGD_IP6;
	}
	g_return_val_if_reached (LOGD_NONE);
}

45 46 47 48 49
#define nm_log_err(domain, ...)     nm_log (LOGL_ERR,   (domain),  NULL, NULL, __VA_ARGS__)
#define nm_log_warn(domain, ...)    nm_log (LOGL_WARN,  (domain),  NULL, NULL, __VA_ARGS__)
#define nm_log_info(domain, ...)    nm_log (LOGL_INFO,  (domain),  NULL, NULL, __VA_ARGS__)
#define nm_log_dbg(domain, ...)     nm_log (LOGL_DEBUG, (domain),  NULL, NULL, __VA_ARGS__)
#define nm_log_trace(domain, ...)   nm_log (LOGL_TRACE, (domain),  NULL, NULL, __VA_ARGS__)
50

51 52 53
//#define _NM_LOG_FUNC G_STRFUNC
#define _NM_LOG_FUNC NULL

54 55 56
/* A wrapper for the _nm_log_impl() function that adds call site information.
 * Contrary to nm_log(), it unconditionally calls the function without
 * checking whether logging for the given level and domain is enabled. */
57
#define _nm_log_mt(mt_require_locking, level, domain, error, ifname, con_uuid, ...) \
58
    G_STMT_START { \
59 60
        _nm_log_impl (__FILE__, \
                      __LINE__, \
61
                      _NM_LOG_FUNC, \
62
                      (mt_require_locking), \
63 64 65
                      (level), \
                      (domain), \
                      (error), \
66 67
                      (ifname), \
                      (con_uuid), \
68
                      ""__VA_ARGS__); \
69 70
    } G_STMT_END

71 72 73
#define _nm_log(level, domain, error, ifname, con_uuid, ...) \
	_nm_log_mt (!(NM_THREAD_SAFE_ON_MAIN_THREAD), level, domain, error, ifname, con_uuid, __VA_ARGS__)

74
/* nm_log() only evaluates its argument list after checking
75
 * whether logging for the given level/domain is enabled.  */
76
#define nm_log(level, domain, ifname, con_uuid, ...) \
77 78
    G_STMT_START { \
        if (nm_logging_enabled ((level), (domain))) { \
79
            _nm_log (level, domain, 0, ifname, con_uuid, __VA_ARGS__); \
80 81
        } \
    } G_STMT_END
82

83 84 85 86 87 88 89 90
#define _nm_log_ptr(level, domain, ifname, con_uuid, self, prefix, ...) \
   nm_log ((level), \
           (domain), \
           (ifname), \
           (con_uuid), \
           "%s[%p] " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
           (prefix) ?: "", \
           self _NM_UTILS_MACRO_REST(__VA_ARGS__))
91

92 93 94 95 96 97
static inline gboolean
_nm_log_ptr_is_debug (NMLogLevel level)
{
	return level <= LOGL_DEBUG;
}

98
/* log a message for an object (with providing a generic @self pointer) */
99
#define nm_log_ptr(level, domain, ifname, con_uuid, self, prefix, ...) \
100
    G_STMT_START { \
101
        if (_nm_log_ptr_is_debug (level)) { \
102 103 104 105 106 107 108
            _nm_log_ptr ((level), \
                         (domain), \
                         (ifname), \
                         (con_uuid), \
                         (self), \
                         (prefix), \
                         __VA_ARGS__); \
109
        } else { \
110 111
            const char *__prefix = (prefix); \
            \
112 113 114 115 116 117 118
            nm_log ((level), \
                    (domain), \
                    (ifname), \
                    (con_uuid), \
                    "%s%s" _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
                    __prefix ?: "", \
                    __prefix ? " " : "" _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
119 120 121
        } \
    } G_STMT_END

122 123 124 125 126 127 128 129
#define _nm_log_obj(level, domain, ifname, con_uuid, self, prefix, ...) \
    _nm_log_ptr ((level), \
                 (domain), \
                 (ifname), \
                 (con_uuid), \
                 (self), \
                 prefix, \
                 __VA_ARGS__)
130 131 132 133

/* log a message for an object (with providing a @self pointer to a GObject).
 * Contrary to nm_log_ptr(), @self must be a GObject type (or %NULL).
 * As of now, nm_log_obj() is identical to nm_log_ptr(), but we might change that */
134 135 136 137 138 139 140 141
#define nm_log_obj(level, domain, ifname, con_uuid, self, prefix, ...) \
    nm_log_ptr ((level), \
                (domain), \
                (ifname), \
                (con_uuid), \
                (self), \
                prefix, \
                __VA_ARGS__)
142

143
const char *nm_logging_level_to_string (void);
144
const char *nm_logging_domains_to_string (void);
145

146 147
/*****************************************************************************/

148
extern NMLogDomain _nm_logging_enabled_state[_LOGL_N_REAL];
149

150
static inline gboolean
151
_nm_logging_enabled_lockfree (NMLogLevel level, NMLogDomain domain)
152 153
{
	nm_assert (((guint) level) < G_N_ELEMENTS (_nm_logging_enabled_state));
154 155
	return    (((guint) level) < G_N_ELEMENTS (_nm_logging_enabled_state))
	       && !!(_nm_logging_enabled_state[level] & domain);
156
}
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
gboolean _nm_logging_enabled_locking (NMLogLevel level, NMLogDomain domain);

static inline gboolean
nm_logging_enabled_mt (gboolean mt_require_locking, NMLogLevel level, NMLogDomain domain)
{
	if (mt_require_locking)
		return _nm_logging_enabled_locking (level, domain);

	NM_ASSERT_ON_MAIN_THREAD ();
	return _nm_logging_enabled_lockfree (level, domain);
}

#define nm_logging_enabled(level, domain) \
	nm_logging_enabled_mt (!(NM_THREAD_SAFE_ON_MAIN_THREAD), level, domain)

/*****************************************************************************/

175 176
NMLogLevel nm_logging_get_level (NMLogDomain domain);

177 178 179
const char *nm_logging_all_levels_to_string (void);
const char *nm_logging_all_domains_to_string (void);

180 181 182 183
gboolean nm_logging_setup (const char  *level,
                           const char  *domains,
                           char       **bad_domains,
                           GError     **error);
184

185 186 187 188
void nm_logging_init_pre (const char *syslog_identifier,
                          char *prefix_take);

void     nm_logging_init (const char *logging_backend, gboolean debug);
189

190
gboolean nm_logging_syslog_enabled (void);
191

192 193 194 195 196 197
/*****************************************************************************/

/* This is the default definition of _NMLOG_ENABLED(). Special implementations
 * might want to undef this and redefine it. */
#define _NMLOG_ENABLED(level) ( nm_logging_enabled ((level), (_NMLOG_DOMAIN)) )

198
#define _LOGT(...)          _NMLOG (LOGL_TRACE, __VA_ARGS__)
199 200 201 202 203
#define _LOGD(...)          _NMLOG (LOGL_DEBUG, __VA_ARGS__)
#define _LOGI(...)          _NMLOG (LOGL_INFO , __VA_ARGS__)
#define _LOGW(...)          _NMLOG (LOGL_WARN , __VA_ARGS__)
#define _LOGE(...)          _NMLOG (LOGL_ERR  , __VA_ARGS__)

204
#define _LOGT_ENABLED(...)  _NMLOG_ENABLED (LOGL_TRACE, ##__VA_ARGS__)
205 206 207 208 209
#define _LOGD_ENABLED(...)  _NMLOG_ENABLED (LOGL_DEBUG, ##__VA_ARGS__)
#define _LOGI_ENABLED(...)  _NMLOG_ENABLED (LOGL_INFO , ##__VA_ARGS__)
#define _LOGW_ENABLED(...)  _NMLOG_ENABLED (LOGL_WARN , ##__VA_ARGS__)
#define _LOGE_ENABLED(...)  _NMLOG_ENABLED (LOGL_ERR  , ##__VA_ARGS__)

210 211 212 213 214 215
#define _LOGT_err(errsv, ...) _NMLOG_err (errsv, LOGL_TRACE, __VA_ARGS__)
#define _LOGD_err(errsv, ...) _NMLOG_err (errsv, LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_err(errsv, ...) _NMLOG_err (errsv, LOGL_INFO , __VA_ARGS__)
#define _LOGW_err(errsv, ...) _NMLOG_err (errsv, LOGL_WARN , __VA_ARGS__)
#define _LOGE_err(errsv, ...) _NMLOG_err (errsv, LOGL_ERR  , __VA_ARGS__)

216
/* _LOGT() and _LOGt() both log with level TRACE, but the latter is disabled by default,
217
 * unless building with --with-more-logging. */
218
#if NM_MORE_LOGGING
219 220 221
#define _LOGt_ENABLED(...)    _NMLOG_ENABLED (LOGL_TRACE, ##__VA_ARGS__)
#define _LOGt(...)            _NMLOG (LOGL_TRACE, __VA_ARGS__)
#define _LOGt_err(errsv, ...) _NMLOG_err (errsv, LOGL_TRACE, __VA_ARGS__)
222
#else
223
/* still call the logging macros to get compile time checks, but they will be optimized out. */
224 225 226
#define _LOGt_ENABLED(...)    ( FALSE && (_NMLOG_ENABLED (LOGL_TRACE, ##__VA_ARGS__)) )
#define _LOGt(...)            G_STMT_START { if (FALSE) { _NMLOG (LOGL_TRACE, __VA_ARGS__); } } G_STMT_END
#define _LOGt_err(errsv, ...) G_STMT_START { if (FALSE) { _NMLOG_err (errsv, LOGL_TRACE, __VA_ARGS__); } } G_STMT_END
227 228 229 230
#endif

/*****************************************************************************/

231
/* Some implementation define a second set of logging macros, for a separate
232
 * use. As with the _LOGD() macro family above, the exact implementation
233 234
 * depends on the file that uses them.
 * Still, it encourages a common pattern to have the common set of macros
235
 * like _LOG2D(), _LOG2I(), etc. and have _LOG2t() which by default
236 237 238 239
 * is disabled at compile time. */

#define _NMLOG2_ENABLED(level) ( nm_logging_enabled ((level), (_NMLOG2_DOMAIN)) )

240
#define _LOG2T(...)          _NMLOG2 (LOGL_TRACE, __VA_ARGS__)
241 242 243 244 245
#define _LOG2D(...)          _NMLOG2 (LOGL_DEBUG, __VA_ARGS__)
#define _LOG2I(...)          _NMLOG2 (LOGL_INFO , __VA_ARGS__)
#define _LOG2W(...)          _NMLOG2 (LOGL_WARN , __VA_ARGS__)
#define _LOG2E(...)          _NMLOG2 (LOGL_ERR  , __VA_ARGS__)

246
#define _LOG2T_ENABLED(...)  _NMLOG2_ENABLED (LOGL_TRACE, ##__VA_ARGS__)
247 248 249 250 251
#define _LOG2D_ENABLED(...)  _NMLOG2_ENABLED (LOGL_DEBUG, ##__VA_ARGS__)
#define _LOG2I_ENABLED(...)  _NMLOG2_ENABLED (LOGL_INFO , ##__VA_ARGS__)
#define _LOG2W_ENABLED(...)  _NMLOG2_ENABLED (LOGL_WARN , ##__VA_ARGS__)
#define _LOG2E_ENABLED(...)  _NMLOG2_ENABLED (LOGL_ERR  , ##__VA_ARGS__)

252 253 254 255 256 257
#define _LOG2T_err(errsv, ...) _NMLOG2_err (errsv, LOGL_TRACE, __VA_ARGS__)
#define _LOG2D_err(errsv, ...) _NMLOG2_err (errsv, LOGL_DEBUG, __VA_ARGS__)
#define _LOG2I_err(errsv, ...) _NMLOG2_err (errsv, LOGL_INFO , __VA_ARGS__)
#define _LOG2W_err(errsv, ...) _NMLOG2_err (errsv, LOGL_WARN , __VA_ARGS__)
#define _LOG2E_err(errsv, ...) _NMLOG2_err (errsv, LOGL_ERR  , __VA_ARGS__)

258
#if NM_MORE_LOGGING
259 260 261
#define _LOG2t_ENABLED(...)    _NMLOG2_ENABLED (LOGL_TRACE, ##__VA_ARGS__)
#define _LOG2t(...)            _NMLOG2 (LOGL_TRACE, __VA_ARGS__)
#define _LOG2t_err(errsv, ...) _NMLOG2_err (errsv, LOGL_TRACE, __VA_ARGS__)
262 263
#else
/* still call the logging macros to get compile time checks, but they will be optimized out. */
264 265 266
#define _LOG2t_ENABLED(...)    ( FALSE && (_NMLOG2_ENABLED (LOGL_TRACE, ##__VA_ARGS__)) )
#define _LOG2t(...)            G_STMT_START { if (FALSE) { _NMLOG2 (LOGL_TRACE, __VA_ARGS__); } } G_STMT_END
#define _LOG2t_err(errsv, ...) G_STMT_START { if (FALSE) { _NMLOG2_err (errsv, LOGL_TRACE, __VA_ARGS__); } } G_STMT_END
267 268
#endif

Thomas Haller's avatar
Thomas Haller committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#define _NMLOG3_ENABLED(level) ( nm_logging_enabled ((level), (_NMLOG3_DOMAIN)) )

#define _LOG3T(...)          _NMLOG3 (LOGL_TRACE, __VA_ARGS__)
#define _LOG3D(...)          _NMLOG3 (LOGL_DEBUG, __VA_ARGS__)
#define _LOG3I(...)          _NMLOG3 (LOGL_INFO , __VA_ARGS__)
#define _LOG3W(...)          _NMLOG3 (LOGL_WARN , __VA_ARGS__)
#define _LOG3E(...)          _NMLOG3 (LOGL_ERR  , __VA_ARGS__)

#define _LOG3T_ENABLED(...)  _NMLOG3_ENABLED (LOGL_TRACE, ##__VA_ARGS__)
#define _LOG3D_ENABLED(...)  _NMLOG3_ENABLED (LOGL_DEBUG, ##__VA_ARGS__)
#define _LOG3I_ENABLED(...)  _NMLOG3_ENABLED (LOGL_INFO , ##__VA_ARGS__)
#define _LOG3W_ENABLED(...)  _NMLOG3_ENABLED (LOGL_WARN , ##__VA_ARGS__)
#define _LOG3E_ENABLED(...)  _NMLOG3_ENABLED (LOGL_ERR  , ##__VA_ARGS__)

#define _LOG3T_err(errsv, ...) _NMLOG3_err (errsv, LOGL_TRACE, __VA_ARGS__)
#define _LOG3D_err(errsv, ...) _NMLOG3_err (errsv, LOGL_DEBUG, __VA_ARGS__)
#define _LOG3I_err(errsv, ...) _NMLOG3_err (errsv, LOGL_INFO , __VA_ARGS__)
#define _LOG3W_err(errsv, ...) _NMLOG3_err (errsv, LOGL_WARN , __VA_ARGS__)
#define _LOG3E_err(errsv, ...) _NMLOG3_err (errsv, LOGL_ERR  , __VA_ARGS__)

289
#if NM_MORE_LOGGING
Thomas Haller's avatar
Thomas Haller committed
290 291 292 293 294 295 296 297 298 299
#define _LOG3t_ENABLED(...)    _NMLOG3_ENABLED (LOGL_TRACE, ##__VA_ARGS__)
#define _LOG3t(...)            _NMLOG3 (LOGL_TRACE, __VA_ARGS__)
#define _LOG3t_err(errsv, ...) _NMLOG3_err (errsv, LOGL_TRACE, __VA_ARGS__)
#else
/* still call the logging macros to get compile time checks, but they will be optimized out. */
#define _LOG3t_ENABLED(...)    ( FALSE && (_NMLOG3_ENABLED (LOGL_TRACE, ##__VA_ARGS__)) )
#define _LOG3t(...)            G_STMT_START { if (FALSE) { _NMLOG3 (LOGL_TRACE, __VA_ARGS__); } } G_STMT_END
#define _LOG3t_err(errsv, ...) G_STMT_START { if (FALSE) { _NMLOG3_err (errsv, LOGL_TRACE, __VA_ARGS__); } } G_STMT_END
#endif

300 301
/*****************************************************************************/

302 303
#define __NMLOG_DEFAULT(level, domain, prefix, ...) \
	G_STMT_START { \
304
		nm_log ((level), (domain), NULL, NULL, \
305 306 307 308 309 310 311
		        "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
		        (prefix) \
		        _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
	} G_STMT_END

#define __NMLOG_DEFAULT_WITH_ADDR(level, domain, prefix, ...) \
	G_STMT_START { \
312
		nm_log ((level), (domain), NULL, NULL, \
313 314 315 316 317 318
		        "%s[%p]: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
		        (prefix), \
		        (self) \
		        _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
	} G_STMT_END

319 320 321 322
/*****************************************************************************/

extern void _nm_logging_clear_platform_logging_cache (void);

323
#endif /* __NETWORKMANAGER_LOGGING_H__ */