]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Enhance logging to pass custom errno
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Jul 2023 11:33:31 +0000 (11:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Jul 2023 12:58:49 +0000 (12:58 +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 a4357f44ae957171926aa06d807db6e427e36578..d9d3271d618bb0cdd2662237cae0f21cf4396639 100644 (file)
@@ -36,19 +36,24 @@ void pakfire_log_syslog(void* data, int priority, const char* file,
 static inline void __attribute__((always_inline, format(printf, 2, 3)))
        pakfire_log_null(struct pakfire* pakfire, const char *format, ...) {}
 
-#define pakfire_log_condition(pakfire, prio, arg...) \
+#define pakfire_log_condition(pakfire, prio, r, arg...) \
        do { \
                if (pakfire_log_get_priority(pakfire) >= prio) \
-                       pakfire_log(pakfire, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
+                       pakfire_log(pakfire, prio, r, __FILE__, __LINE__, __FUNCTION__, ## arg); \
        } while (0)
 
-#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, ## arg)
-#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, ## arg)
+#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, 0, ## arg)
+#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, 0, ## arg)
+
+#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)
 
 #ifdef ENABLE_DEBUG
-#      define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg)
+#      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)
 #else
 #      define DEBUG pakfire_log_null
+#      define DEBUG_ERRNO pakfire_log_null
 #endif
 
 #endif /* PAKFIRE_PRIVATE */
index 9950161efcf9e0f4d6dff09fca27aa9f4070c82b..88589eca3ead5040412550c6d008a9578bf0da8d 100644 (file)
@@ -134,9 +134,9 @@ 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, const char *file,
+void pakfire_log(struct pakfire* pakfire, int priority, int r, const char *file,
        int line, const char *fn, const char *format, ...)
-       __attribute__((format(printf, 6, 7)));
+       __attribute__((format(printf, 7, 8)));
 
 int pakfire_has_flag(struct pakfire* pakfire, int flag);
 
index b1022e925e8ec29a56a5c55b2ed819b6070e6fbf..8c3c3fd679cf275927f6bcc0260399f854ee9ae3 100644 (file)
@@ -1625,8 +1625,8 @@ PAKFIRE_EXPORT void pakfire_set_log_callback(struct pakfire* pakfire,
        pakfire->callbacks.log_data = data;
 }
 
-void pakfire_log(struct pakfire* pakfire, int priority, const char* file, int line,
-               const char* fn, const char* format, ...) {
+void pakfire_log(struct pakfire* pakfire, int priority, int r,
+               const char* file, int line, const char* fn, const char* format, ...) {
        va_list args;
 
        // Do not do anything if callback isn't set
@@ -1635,6 +1635,8 @@ void pakfire_log(struct pakfire* pakfire, int priority, const char* file, int li
 
        // Save errno
        int saved_errno = errno;
+       if (r)
+               errno = r;
 
        va_start(args, format);
        pakfire->callbacks.log(pakfire->callbacks.log_data,