From 98d3c9e1b820020111aa85853c93ecb3550c627b Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 6 May 2015 03:42:02 +0200 Subject: Support pcilib_log_once calls --- pcilib/debug.c | 4 ++-- pcilib/debug.h | 4 ++-- pcilib/error.c | 37 +++++++++++++++++++++++++++++++++++-- pcilib/error.h | 16 +++++++++++++--- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/pcilib/debug.c b/pcilib/debug.c index 6d7d8af..ca31364 100644 --- a/pcilib/debug.c +++ b/pcilib/debug.c @@ -11,13 +11,13 @@ #define PCILIB_MAX_DEBUG_FILENAME_LENGTH 1023 -void pcilib_debug_message(const char *function, const char *file, int line, const char *format, ...) { +void pcilib_debug_message(const char *function, const char *file, int line, pcilib_log_flags_t flags, const char *format, ...) { va_list va; if (!getenv(function)) return; va_start(va, format); - pcilib_log_vmessage(file, line, PCILIB_LOG_DEBUG, format, va); + pcilib_log_vmessage(file, line, PCILIB_LOG_DEBUG, flags, format, va); va_end(va); } diff --git a/pcilib/debug.h b/pcilib/debug.h index fb0a791..bff3ab0 100644 --- a/pcilib/debug.h +++ b/pcilib/debug.h @@ -28,7 +28,7 @@ #endif /* PCILIB_DEBUG_MISSING_EVENTS */ #define pcilib_debug(function, ...) \ - PCILIB_DEBUG_##function##_MESSAGE(PCILIB_DEBUG_##function, __VA_ARGS__) + PCILIB_DEBUG_##function##_MESSAGE(PCILIB_DEBUG_##function, PCILIB_LOG_DEFAULT, __VA_ARGS__) #define pcilib_debug_buffer(function, ...) \ PCILIB_DEBUG_##function##_BUFFER(PCILIB_DEBUG_##function, __VA_ARGS__) @@ -38,7 +38,7 @@ typedef enum { PCILIB_DEBUG_BUFFER_MKDIR = 2 } pcilib_debug_buffer_flags_t; -void pcilib_debug_message(const char *function, const char *file, int line, const char *format, ...); +void pcilib_debug_message(const char *function, const char *file, int line, pcilib_log_flags_t flags, const char *format, ...); void pcilib_debug_data_buffer(const char *function, size_t size, void *buffer, pcilib_debug_buffer_flags_t flags, const char *file, ...); diff --git a/pcilib/error.c b/pcilib/error.c index ad38622..2c4296e 100644 --- a/pcilib/error.c +++ b/pcilib/error.c @@ -1,10 +1,15 @@ +#define _BSD_SOURCE + #include #include #include +#include #include "export.h" #include "error.h" +#define PCILIB_LOGGER_HISTORY 16 + void pcilib_print_error(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va) { vprintf(msg, va); printf(" [%s:%d]\n", file, line); @@ -13,21 +18,49 @@ void pcilib_print_error(void *arg, const char *file, int line, pcilib_log_priori static void *pcilib_logger_argument = NULL; static pcilib_log_priority_t pcilib_logger_min_prio = PCILIB_LOG_WARNING; static pcilib_logger_t pcilib_logger = pcilib_print_error; +static char *pcilib_logger_history[PCILIB_LOGGER_HISTORY] = {0}; +static int pcilib_logger_history_pointer = 0; + +static int pcilib_log_check_history(pcilib_log_flags_t flags, const char *msg) { + int i; + + if ((flags&PCILIB_LOG_ONCE) == 0) + return 0; + + for (i = 0; ((i < PCILIB_LOGGER_HISTORY)&&(pcilib_logger_history[i])); i++) { + if (!strcmp(msg, pcilib_logger_history[i])) + return -1; + } + if (pcilib_logger_history[pcilib_logger_history_pointer]) + free(pcilib_logger_history[pcilib_logger_history_pointer]); + pcilib_logger_history[pcilib_logger_history_pointer] = strdup(msg); -void pcilib_log_message(const char *file, int line, pcilib_log_priority_t prio, const char *msg, ...) { + if (++pcilib_logger_history_pointer == PCILIB_LOGGER_HISTORY) + pcilib_logger_history_pointer = 0; + + return 0; +} + +void pcilib_log_message(const char *file, int line, pcilib_log_flags_t flags, pcilib_log_priority_t prio, const char *msg, ...) { va_list va; if ((!prio)||(prio >= pcilib_logger_min_prio)) { + if (pcilib_log_check_history(flags, msg)) + return; + va_start(va, msg); pcilib_logger(pcilib_logger_argument, file, line, prio, msg, va); va_end(va); } } -void pcilib_log_vmessage(const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va) { +void pcilib_log_vmessage(const char *file, int line, pcilib_log_flags_t flags, pcilib_log_priority_t prio, const char *msg, va_list va) { if ((!prio)||(prio >= pcilib_logger_min_prio)) { + if (pcilib_log_check_history(flags, msg)) + return; + pcilib_logger(pcilib_logger_argument, file, line, prio, msg, va); } } diff --git a/pcilib/error.h b/pcilib/error.h index 511f43d..e68d102 100644 --- a/pcilib/error.h +++ b/pcilib/error.h @@ -4,6 +4,11 @@ #include #include +typedef enum { + PCILIB_LOG_DEFAULT = 0, + PCILIB_LOG_ONCE = 1 +} pcilib_log_flags_t; + enum { PCILIB_ERROR_SUCCESS = 0, PCILIB_ERROR_MEMORY = ENOMEM, @@ -26,15 +31,20 @@ enum { PCILIB_ERROR_BUSY = EBUSY } pcilib_errot_t; -void pcilib_log_message(const char *file, int line, pcilib_log_priority_t prio, const char *msg, ...); -void pcilib_log_vmessage(const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va); +void pcilib_log_message(const char *file, int line, pcilib_log_flags_t flags, pcilib_log_priority_t prio, const char *msg, ...); +void pcilib_log_vmessage(const char *file, int line, pcilib_log_flags_t flags, pcilib_log_priority_t prio, const char *msg, va_list va); #define pcilib_log(prio, ...) \ - pcilib_log_message(__FILE__, __LINE__, prio, __VA_ARGS__) + pcilib_log_message(__FILE__, __LINE__, PCILIB_LOG_DEFAULT, prio, __VA_ARGS__) + +#define pcilib_log_once(prio, ...) \ + pcilib_log_message(__FILE__, __LINE__, PCILIB_LOG_ONCE, prio, __VA_ARGS__) #define pcilib_error(...) pcilib_log(PCILIB_LOG_ERROR, __VA_ARGS__) #define pcilib_warning(...) pcilib_log(PCILIB_LOG_WARNING, __VA_ARGS__) #define pcilib_info(...) pcilib_log(PCILIB_LOG_INFO, __VA_ARGS__) +#define pcilib_warning_once(...) pcilib_log_once(PCILIB_LOG_WARNING, __VA_ARGS__) +#define pcilib_info_once(...) pcilib_log_once(PCILIB_LOG_INFO, __VA_ARGS__) #endif /* _PCILIB_ERROR_H */ -- cgit v1.2.3