dexter has uploaded this change for review.
logging: log to stdout 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
printf.
Change-Id: I9b1b0988e02322e3e44fd4ceea3e1bc2d4df3c45
---
M include/osmocom/core/logging.h
M src/logging.c
2 files changed, 33 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/50/26950/1
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 4a34c7d..fea6b56 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -56,6 +56,10 @@
#ifndef LIBOSMOCORE_NO_LOGGING
#define LOGPC(ss, level, fmt, args...) \
do { \
+ if (!log_initialized()) { \
+ logp_stub(__FILE__, __LINE__, 1, fmt, ##args); \
+ break; \
+ } \
if (log_check_level(ss, level)) \
logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
} while(0)
@@ -94,6 +98,13 @@
#ifndef LIBOSMOCORE_NO_LOGGING
#define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
do { \
+ if (!log_initialized()) { \
+ 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 (caller_file) \
logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
@@ -380,7 +391,9 @@
void logp2(int subsys, unsigned int level, const char *file,
int line, int cont, const char *format, ...)
__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_initialized(void);
void log_fini(void);
int log_check_level(int subsys, unsigned int level);
diff --git a/src/logging.c b/src/logging.c
index 9d2a065..03b06a8 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -312,6 +312,14 @@
}
}
+/* Check whether the logging framework is initialized (osmo_log_info exists) */
+int log_initialized(void)
+{
+ if (!osmo_log_info)
+ return 0;
+ return 1;
+}
+
/* special magic for negative (library-internal) log subsystem numbers */
static int subsys_lib2index(int subsys)
{
@@ -748,6 +756,18 @@
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)
+ printf("%s:%d ", file, line);
+ va_start(ap, format);
+ vprintf(format, ap);
+ va_end(ap);
+}
+
/*! Register a new log target with the logging core
* \param[in] target Log target to be registered
*/
To view, visit change 26950. To unsubscribe, or for help writing mail filters, visit settings.