From 2856438a0b1450228b21a9e52680aaf4c20cfb47 Mon Sep 17 00:00:00 2001 From: Dmitry Tatarinov Date: Thu, 17 Mar 2016 15:45:14 +0200 Subject: [PATCH] Add file name and line number to log outputs The changes improve logging by adding __file__ and __line__ to output --- include/libswiftnav/logging.h | 236 +++++++++++++++++----------------- src/logging.c | 7 +- 2 files changed, 124 insertions(+), 119 deletions(-) diff --git a/include/libswiftnav/logging.h b/include/libswiftnav/logging.h index 1b222b05..67707b9a 100644 --- a/include/libswiftnav/logging.h +++ b/include/libswiftnav/logging.h @@ -1,118 +1,118 @@ -/* - * Copyright (C) 2015 Swift Navigation Inc. - * Contact: Fergus Noble - * - * This source is subject to the license found in the file 'LICENSE' which must - * be be distributed together with this source. All other rights reserved. - * - * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, - * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. - */ - -#ifndef LIBSWIFTNAV_LOGGING_H -#define LIBSWIFTNAV_LOGGING_H - -#include - -#include - -/* DEBUG off by default, enable it on a per-file basis. */ -#ifndef DEBUG -#define DEBUG 0 -#endif - -/** \defgroup logging Logging - * Logging - * - * Logging at the `DEBUG` level is turned off by default and should be enabled - * on a per-file basis by adding the following line to the source file *before* - * including `logging.h`: - * - * \#define DEBUG 1 - * - * \{ */ - -extern void log_(u8 level, const char *msg, ...) __attribute__ ((weak)); - -#define LOG_EMERG 0 /* system is unusable */ -#define LOG_ALERT 1 /* action must be taken immediately */ -#define LOG_CRIT 2 /* critical conditions */ -#define LOG_ERROR 3 /* error conditions */ -#define LOG_WARN 4 /* warning conditions */ -#define LOG_NOTICE 5 /* normal but significant condition */ -#define LOG_INFO 6 /* informational */ -#define LOG_DEBUG 7 /* debug-level messages */ - -/** Log an emergency. - * \param args `printf` style format and arguments. - */ -#define log_emerg(args...) log_(LOG_EMERG, args) - -/** Log an alert. - * \param args `printf` style format and arguments. - */ -#define log_alert(args...) log_(LOG_ALERT, args) - -/** Log a critical event. - * \param args `printf` style format and arguments. - */ -#define log_crit(args...) log_(LOG_CRIT, args) - -/** Log an error. - * \param args `printf` style format and arguments. - */ -#define log_error(args...) log_(LOG_ERROR, args) - -/** Log a warning. - * \param args `printf` style format and arguments. - */ -#define log_warn(args...) log_(LOG_WARN, args) - -/** Log a notice. - * \param args `printf` style format and arguments. - */ -#define log_notice(args...) log_(LOG_NOTICE, args) - -/** Log an information message. - * \param args `printf` style format and arguments. - */ -#define log_info(args...) log_(LOG_INFO, args) - -/** Log a debugging message. - * \param args `printf` style format and arguments. - */ -#define log_debug(args...) \ -do { \ - if (DEBUG) { \ - log_(LOG_DEBUG, args); \ - } \ -} while (0) - -/** Log a debug message indicating entry to a function. - * Logs a debug message of the form `\` to indicate entry to a - * function. `function_name` is automatically filled in with the name of the - * current function by GCC magic. - */ -#define DEBUG_ENTRY() \ -do { \ - if (DEBUG) { \ - log_debug("<%s>\n", __func__); \ - } \ -} while (0) - -/** Log a debug message indicating exit to a function. - * Logs a debug message of the form `\` to indicate exit from a - * function. `function_name` is automatically filled in with the name of the - * current function by GCC magic. - */ -#define DEBUG_EXIT() \ -do { \ - if (DEBUG) { \ - log_debug("\n", __func__); \ - } \ -} while (0) - -/** \} */ - -#endif /* LIBSWIFTNAV_LOGGING_H */ +/* + * Copyright (C) 2015 Swift Navigation Inc. + * Contact: Fergus Noble + * + * This source is subject to the license found in the file 'LICENSE' which must + * be be distributed together with this source. All other rights reserved. + * + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + */ + +#ifndef LIBSWIFTNAV_LOGGING_H +#define LIBSWIFTNAV_LOGGING_H + +#include +#include + +#include + +/* DEBUG off by default, enable it on a per-file basis. */ +#ifndef DEBUG +#define DEBUG 0 +#endif + +/** \defgroup logging Logging + * Logging + * + * Logging at the `DEBUG` level is turned off by default and should be enabled + * on a per-file basis by adding the following line to the source file *before* + * including `logging.h`: + * + * \#define DEBUG 1 + * + * \{ */ + +#define __FNAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) + +void log_(u8 level, const char *file, const int line, const char *msg, ...) __attribute__ ((weak)); + +#define LOG_EMERG 0 /* system is unusable */ +#define LOG_ALERT 1 /* action must be taken immediately */ +#define LOG_CRIT 2 /* critical conditions */ +#define LOG_ERROR 3 /* error conditions */ +#define LOG_WARN 4 /* warning conditions */ +#define LOG_NOTICE 5 /* normal but significant condition */ +#define LOG_INFO 6 /* informational */ +#define LOG_DEBUG 7 /* debug-level messages */ + +#define log_emerg(args...) log_(LOG_EMERG, __FNAME__, __LINE__, args) + +/** Log an alert. + * \param args `printf` style format and arguments. + */ +#define log_alert(args...) log_(LOG_ALERT, __FNAME__, __LINE__, args) + +/** Log a critical event. + * \param args `printf` style format and arguments. + */ +#define log_crit(args...) log_(LOG_CRIT, __FNAME__, __LINE__, args) + +/** Log an error. + * \param args `printf` style format and arguments. + */ +#define log_error(args...) log_(LOG_ERROR, __FNAME__, __LINE__, args) + +/** Log a warning. + * \param args `printf` style format and arguments. + */ +#define log_warn(args...) log_(LOG_WARN, __FNAME__, __LINE__, args) + +/** Log a notice. + * \param args `printf` style format and arguments. + */ +#define log_notice(args...) log_(LOG_NOTICE, __FNAME__, __LINE__, args) + +/** Log an information message. + * \param args `printf` style format and arguments. + */ +#define log_info(args...) log_(LOG_INFO, __FNAME__, __LINE__, args) + +/** Log a debugging message. + * \param args `printf` style format and arguments. + */ +#define log_debug(args...) \ +do { \ + if (DEBUG) { \ + log_(LOG_DEBUG, __FNAME__, __LINE__, args); \ + } \ +} while (0) + +/** Log a debug message indicating entry to a function. + * Logs a debug message of the form `\` to indicate entry to a + * function. `function_name` is automatically filled in with the name of the + * current function by GCC magic. + */ +#define DEBUG_ENTRY() \ +do { \ + if (DEBUG) { \ + log_debug("<%s>\n", __func__); \ + } \ +} while (0) + +/** Log a debug message indicating exit to a function. + * Logs a debug message of the form `\` to indicate exit from a + * function. `function_name` is automatically filled in with the name of the + * current function by GCC magic. + */ +#define DEBUG_EXIT() \ +do { \ + if (DEBUG) { \ + log_debug("\n", __func__); \ + } \ +} while (0) + +/** \} */ + +#endif /* LIBSWIFTNAV_LOGGING_H */ diff --git a/src/logging.c b/src/logging.c index bc222eab..ac917ab3 100644 --- a/src/logging.c +++ b/src/logging.c @@ -32,10 +32,15 @@ const char *level_string[] = { /** Log message by level. * * \param level Log level + * \param file file name + * \param line line number * \param msg Log contents */ -void log_(u8 level, const char *msg, ...) +void log_(u8 level, const char *file, const int line, const char *msg, ...) { + (void)file; + (void)line; + va_list ap; printf("%s: ", level_string[level]);