logging: log to stderr when logging is not initialized

When the logging framework is not initialized we get an error:

"ERROR: osmo_log_info == NULL! You must call log_init() before
using logging in ..."

There are sometimes situations where some code tries to log before
logging was initialied. This is a problem because the actual log
line with the debug info we need is covered up by the error message
from the logging framework.

Lets introduce a fallback logging function that is called when the
the logging framework is not available. This function can just use
fprintf to output to stderr.

Change-Id: I9b1b0988e02322e3e44fd4ceea3e1bc2d4df3c45
This commit is contained in:
Philipp Maier 2022-01-19 11:43:05 +01:00
parent fca15ebf99
commit 4c44d46308
2 changed files with 27 additions and 0 deletions

View File

@ -11,6 +11,8 @@
#include <osmocom/core/defs.h> #include <osmocom/core/defs.h>
#include <osmocom/core/linuxlist.h> #include <osmocom/core/linuxlist.h>
extern struct log_info *osmo_log_info;
#ifndef DEBUG #ifndef DEBUG
#define DEBUG #define DEBUG
#endif #endif
@ -56,6 +58,10 @@ void logp(int subsys, const char *file, int line, int cont, const char *format,
#ifndef LIBOSMOCORE_NO_LOGGING #ifndef LIBOSMOCORE_NO_LOGGING
#define LOGPC(ss, level, fmt, args...) \ #define LOGPC(ss, level, fmt, args...) \
do { \ do { \
if (!osmo_log_info) { \
logp_stub(__FILE__, __LINE__, 1, fmt, ##args); \
break; \
} \
if (log_check_level(ss, level)) \ if (log_check_level(ss, level)) \
logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \ logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
} while(0) } while(0)
@ -94,6 +100,13 @@ void logp(int subsys, const char *file, int line, int cont, const char *format,
#ifndef LIBOSMOCORE_NO_LOGGING #ifndef LIBOSMOCORE_NO_LOGGING
#define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \ #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
do { \ do { \
if (!osmo_log_info) { \
if (caller_file) \
logp_stub(caller_file, caller_line, cont, fmt, ##args); \
else \
logp_stub(__FILE__, __LINE__, cont, fmt, ##args); \
break; \
} \
if (log_check_level(ss, level)) {\ if (log_check_level(ss, level)) {\
if (caller_file) \ if (caller_file) \
logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \ logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
@ -381,7 +394,9 @@ struct log_target {
void logp2(int subsys, unsigned int level, const char *file, void logp2(int subsys, unsigned int level, const char *file,
int line, int cont, const char *format, ...) int line, int cont, const char *format, ...)
__attribute__ ((format (printf, 6, 7))); __attribute__ ((format (printf, 6, 7)));
void logp_stub(const char *file, int line, int cont, const char *format, ...);
int log_init(const struct log_info *inf, void *talloc_ctx); int log_init(const struct log_info *inf, void *talloc_ctx);
int log_initialized(void);
void log_fini(void); void log_fini(void);
int log_check_level(int subsys, unsigned int level); int log_check_level(int subsys, unsigned int level);

View File

@ -754,6 +754,18 @@ void logp2(int subsys, unsigned int level, const char *file, int line, int cont,
TRACE(LIBOSMOCORE_LOG_DONE()); TRACE(LIBOSMOCORE_LOG_DONE());
} }
/* This logging function is used as a fallback when the logging framework is
* not is not properly initialized. */
void logp_stub(const char *file, int line, int cont, const char *format, ...)
{
va_list ap;
if (!cont)
fprintf(stderr, "%s:%d ", file, line);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/*! Register a new log target with the logging core /*! Register a new log target with the logging core
* \param[in] target Log target to be registered * \param[in] target Log target to be registered
*/ */