treewide: improve logging

Provide multi-priority syslog() based logging to improve user and
developer experience. Add dawnlog_* functions and macros to convert
printf() family and perror() logging to syslog() family. Removed
unnecessary sprintf() for building log strings (embed format directly).
Add local config settings for log level. Add command line parameters for
log level and destination. Set default log level to suppress a lot of
previously noisy messages. Restore some previously removed noisy
messages as DEBUG level in case they help in future. Eliminate DAWN_NO_OUTPUT static code checks which are no longer used.

Signed-off-by: Ian Clowes <clowes_ian@hotmail.com>
This commit is contained in:
Ian Clowes 2022-01-06 09:08:17 +01:00 committed by Nick Hainke
parent ddc007e32c
commit 4df0c986f1
18 changed files with 922 additions and 396 deletions

View file

@ -2,6 +2,7 @@
#define __DAWN_UTILS_H
#include <stdint.h>
#include <syslog.h>
/**
* Check if a string is greater than another one.
@ -11,4 +12,91 @@
*/
int string_is_greater(char *str, char *str_2);
/*
** Log handling for dawn process
*/
#define DAWNLOG_DEST_SYSLOG 0 // Send log output to syslog...
#define DAWNLOG_DEST_STDIO 1 // ... or stdout / stderr as appropriate
#define DAWNLOG_PERROR 0x08 // Bit flag to signal inclusion of errno from system calls
#define DAWNLOG_PRIMASK 0x07 // Bitmask to obtain only priority value
#define DAWNLOG_ERROR 5 // Serious malfunction / unexpected behaviour - eg: OS resource exhaustion
#define DAWNLOG_WARNING 4 // Something appears wrong, but recoverable - eg: data structures inconsistent
#define DAWNLOG_ALWAYS 3 // Standard behaviour always worth reporting - should be very low frequency messages
#define DAWNLOG_INFO 2 // Reporting on standard behaviour - should be comprehensible to user
#define DAWNLOG_TRACE 1 // More info to help trace where algorithms may be going wrong
#define DAWNLOG_DEBUG 0 // Deeper tracing to fix bugs
#define DAWNLOG_COMPILE_MIN DAWNLOG_DEBUG // Messages lower than this priority are not compiled
#define DAWNLOG_COMPILING(level) (level >= DAWNLOG_COMPILE_MIN)
#define dawnlog_perror(s, ...) dawnlog(DAWNLOG_ERROR|DAWNLOG_PERROR, "%s()=%s@%d %s - " s, __func__, dawnlog_basename(__FILE__), __LINE__, dawnlog_pbuf, ##__VA_ARGS__)
#define dawnlog_error(fmt, ...) dawnlog(DAWNLOG_ERROR, "%s()=%s@%d " fmt, __func__, dawnlog_basename(__FILE__), __LINE__, ##__VA_ARGS__)
#define dawnlog_warning(fmt, ...) dawnlog(DAWNLOG_WARNING, fmt, ##__VA_ARGS__)
#define dawnlog_always(fmt, ...) dawnlog(DAWNLOG_ALWAYS, fmt, ##__VA_ARGS__)
#if DAWNLOG_COMPILING(DAWNLOG_INFO)
#define dawnlog_info(fmt, ...) dawnlog(DAWNLOG_INFO, fmt, ##__VA_ARGS__)
#else
#define dawnlog_info(fmt, ...)
#endif
// Use the ..._func variants to get source code position added automatically: function, filename, line
#if DAWNLOG_COMPILING(DAWNLOG_TRACE)
#define dawnlog_trace(fmt, ...) dawnlog(DAWNLOG_TRACE, fmt, ##__VA_ARGS__)
#define dawnlog_trace_func(fmt, ...) dawnlog(DAWNLOG_TRACE, "%s()=%s@%d " fmt, __func__, dawnlog_basename(__FILE__), __LINE__, ##__VA_ARGS__)
#else
#define dawnlog_trace(fmt, ...)
#define dawnlog_trace_func(fmt, ...)
#endif
#if DAWNLOG_COMPILING(DAWNLOG_DEBUG)
#define dawnlog_debug(fmt, ...) dawnlog(DAWNLOG_DEBUG, fmt, ##__VA_ARGS__)
#define dawnlog_debug_func(fmt, ...) dawnlog(DAWNLOG_DEBUG, "%s()=%s@%d " fmt, __func__, dawnlog_basename(__FILE__), __LINE__, ##__VA_ARGS__)
#else
#define dawnlog_debug(fmt, ...)
#define dawnlog_debug_func(fmt, ...)
#endif
extern char dawnlog_pbuf[]; // Buffer for errno conversion for dawnlog_perror()
/**
* Set the output target for dawnlog()
* @param logdest: DAWNLOG_DEST_*
*/
void dawnlog_dest(int logdest);
/**
* Minimum priority level to be logged
* @param level: A priority level
*/
void dawnlog_minlevel(int level);
/**
* Check whether a priority level would be actually logged to allow callers
* to skip "expensive" preparation such as string manipulation or loops
* @param level
* @return TRUE if the priority level would be logged
*/
int dawnlog_showing(int level);
/**
* Log a message.
* @param level
* @param fmt
* @return
*/
void dawnlog(int level, const char* fmt, ...);
/**
* Return pointer to filename part of full path.
* @param file
* @return
*/
const char* dawnlog_basename(const char* file);
#endif