diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2015-05-06 03:42:02 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2015-05-06 03:42:02 +0200 |
commit | 98d3c9e1b820020111aa85853c93ecb3550c627b (patch) | |
tree | caea2ad19be18d716f47091c23b4fd01e37776a4 /pcilib/error.c | |
parent | 9ccacea308f336d10c8a94d393b261539a6970bf (diff) | |
download | pcitool-98d3c9e1b820020111aa85853c93ecb3550c627b.tar.gz pcitool-98d3c9e1b820020111aa85853c93ecb3550c627b.tar.bz2 pcitool-98d3c9e1b820020111aa85853c93ecb3550c627b.tar.xz pcitool-98d3c9e1b820020111aa85853c93ecb3550c627b.zip |
Support pcilib_log_once calls
Diffstat (limited to 'pcilib/error.c')
-rw-r--r-- | pcilib/error.c | 37 |
1 files changed, 35 insertions, 2 deletions
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 <stdio.h> #include <stdlib.h> #include <stdarg.h> +#include <string.h> #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); } } |