]> git.ipfire.org Git - pakfire.git/commitdiff
logging: Add convenience functions to call the logger without having a context
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 10 Aug 2024 11:40:22 +0000 (11:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 10 Aug 2024 11:40:22 +0000 (11:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/logging.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/pakfire.c

index 643b58d9c68301de44eb4d05fbb9ac53b38fb6f9..b85b00213c50e4bef1e98dec3696ff7422b47d6a 100644 (file)
@@ -57,29 +57,36 @@ static inline void __attribute__((always_inline, format(printf, 2, 3)))
 #      define CTX_DEBUG pakfire_ctx_log_null
 #endif
 
-// Below is the legacy logging mechanism
+#define PAKFIRE_INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, ## arg)
+#define PAKFIRE_ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, ## arg)
 
-#ifdef PAKFIRE_LEGACY_LOGGING
+#define pakfire_log_condition(pakfire, level, arg...) \
+       do { \
+               if (pakfire_get_log_level(pakfire) >= level) \
+                       pakfire_log(pakfire, level, __FILE__, __LINE__, __FUNCTION__, ## arg); \
+       } while (0)
 
 // This function does absolutely nothing
 static inline void __attribute__((always_inline, format(printf, 2, 3)))
-       pakfire_log_null(struct pakfire* pakfire, const char *format, ...) {}
+       pakfire_log_null(struct pakfire_ctx* ctx, const char *format, ...) {}
+
+#ifdef ENABLE_DEBUG
+#      define PAKFIRE_DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg)
+#else
+#      define PAKFIRE_DEBUG pakfire_log_null
+#endif
 
-#define pakfire_log_condition(pakfire, prio, r, arg...) \
-       pakfire_log(pakfire, prio, r, __FILE__, __LINE__, __FUNCTION__, ## arg)
+// Below is the legacy logging mechanism
 
-#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, 0, ## arg)
-#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, 0, ## arg)
+#ifdef PAKFIRE_LEGACY_LOGGING
 
-#define INFO_ERRNO(pakfire, r, arg...) pakfire_log_condition(pakfire, LOG_INFO, r, ## arg)
-#define ERROR_ERRNO(pakfire, r, arg...) pakfire_log_condition(pakfire, LOG_ERR, r, ## arg)
+#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, ## arg)
+#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, ## arg)
 
 #ifdef ENABLE_DEBUG
-#      define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, 0, ## arg)
-#      define DEBUG_ERRNO(pakfire, r, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, r, ## arg)
+#      define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg)
 #else
 #      define DEBUG pakfire_log_null
-#      define DEBUG_ERRNO pakfire_log_null
 #endif
 
 #endif /* PAKFIRE_LEGACY_LOGGING */
index fde9ac212aec811888438914f5f30ac3eb51a51f..bae86bd893271830aeac915a0a897e235a5fe49a 100644 (file)
@@ -109,9 +109,12 @@ gid_t pakfire_gid(struct pakfire* pakfire);
 const struct pakfire_subid* pakfire_subuid(struct pakfire* pakfire);
 const struct pakfire_subid* pakfire_subgid(struct pakfire* pakfire);
 
-void pakfire_log(struct pakfire* pakfire, int priority, int r, const char *file,
-       int line, const char *fn, const char *format, ...)
-       __attribute__((format(printf, 7, 8)));
+// Logging
+
+int pakfire_get_log_level(struct pakfire* pakfire);
+
+void pakfire_log(struct pakfire* pakfire, int level, const char* file, int line,
+       const char* fn, const char* format, ...) __attribute__((format(printf, 6, 7)));
 
 // Locking
 int pakfire_acquire_lock(struct pakfire* pakfire);
index 99cc684a1b9db165810260fc8a631b81e0609d4b..9478e4cb4b702124d57bd44e9956604a19a10025 100644 (file)
@@ -1487,24 +1487,20 @@ PAKFIRE_EXPORT int pakfire_search(struct pakfire* pakfire, const char* what, int
 
 // Logging
 
-// XXX This function is deprecated and needs to be removed
-void pakfire_log(struct pakfire* pakfire, int priority, int r,
+int pakfire_get_log_level(struct pakfire* pakfire) {
+       return pakfire_ctx_get_log_level(pakfire->ctx);
+}
+
+void pakfire_log(struct pakfire* pakfire, int priority,
                const char* file, int line, const char* fn, const char* format, ...) {
        char* buffer = NULL;
        va_list args;
-
-       // Save errno
-       int saved_errno = errno;
-       if (r)
-               errno = r;
+       int r;
 
        va_start(args, format);
        r = vasprintf(&buffer, format, args);
        va_end(args);
 
-       // Restore errno
-       errno = saved_errno;
-
        if (r < 0)
                return;