Hoernchen has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/30610 )


Change subject: logging: rework the macros
......................................................................

logging: rework the macros

For some reason plenty of internal code ended up in the header macros
even though it does not require any macro features, one single logging
func should just do the right thing and log to stderr if logging is not
available yet, or check the level and log properly.

Change-Id: I03efa954cb9e991d2c3da4b61b12aac651e0efa2
---
M include/osmocom/core/logging.h
M src/logging.c
2 files changed, 34 insertions(+), 37 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/10/30610/1

diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index f4201b1..852bd97 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -32,8 +32,6 @@
 #define DEBUGPC(ss, fmt, args...)
 #endif

-int log_initialized(void);
-
 void osmo_vlogp(int subsys, int level, const char *file, int line,
                int cont, const char *format, va_list ap);

@@ -57,12 +55,7 @@
 #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); \
+               logp2_with_check(ss, level, __FILE__, __LINE__, 1, fmt, 
##args); \
        } while(0)
 #else
 #define LOGPC(ss, level, fmt, args...)
@@ -99,19 +92,10 @@
 #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); \
-                       else \
-                               logp2(ss, level, __FILE__, __LINE__, cont, fmt, 
##args); \
-               }\
+               if (caller_file) \
+                       logp2_with_check(ss, level, caller_file, caller_line, 
cont, fmt, ##args); \
+               else \
+                       logp2_with_check(ss, level, __FILE__, __LINE__, cont, 
fmt, ##args); \
        } while(0)
 #else
 #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...)
@@ -396,6 +380,9 @@
 void logp2(int subsys, unsigned int level, const char *file,
           int line, int cont, const char *format, ...)
                                __attribute__ ((format (printf, 6, 7)));
+void logp2_with_check(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);
 void log_fini(void);
diff --git a/src/logging.c b/src/logging.c
index 7e360b7..aba9176 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -84,7 +84,7 @@
 osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct 
log_target*)NULL)->filter_map),
                   enum_logging_filters_fit_in_log_target_filter_map);

-static struct log_info *osmo_log_info;
+struct log_info *osmo_log_info;

 static struct log_context log_context;
 void *tall_log_ctx = NULL;
@@ -762,16 +762,33 @@
        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, ...)
+/*! logging function used by LOGP() macro, checks log level,
+ *  and uses fallback fprintf to stderr if logging system is not ready yet.
+ *  \param[in] subsys Logging sub-system
+ *  \param[in] level Log level
+ *  \param[in] file name of source code file
+ *  \param[in] cont continuation (1) or new line (0)
+ *  \param[in] format format string
+ */
+void logp2_with_check(int subsys, unsigned int level, 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);
+
+       TRACE(LIBOSMOCORE_LOG_START());
+       if (tall_log_ctx == NULL) {
+               if(!cont)
+                       fprintf(stderr, "%s:%d ", file, line);
+               va_start(ap, format);
+               vfprintf(stderr, format, ap);
+               va_end(ap);
+       }
+
+       if (log_check_level(subsys, level)) {
+               va_start(ap, format);
+               osmo_vlogp(subsys, level, file, line, cont, format, ap);
+               va_end(ap);
+       }
+       TRACE(LIBOSMOCORE_LOG_DONE());
 }

 /*! Register a new log target with the logging core
@@ -1418,13 +1435,6 @@
        return rc;
 }

-/*! Check if the the Osmocom logging core is initialized
- *  \returns 1 in case this is true
- */
-int log_initialized(void) {
-       return tall_log_ctx != NULL;
-}
-
 /*! Initialize the Osmocom logging core
  *  \param[in] inf Information regarding logging categories, could be NULL
  *  \param[in] ctx talloc context for logging allocations

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/30610
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I03efa954cb9e991d2c3da4b61b12aac651e0efa2
Gerrit-Change-Number: 30610
Gerrit-PatchSet: 1
Gerrit-Owner: Hoernchen <ew...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to