From d575f579bcd604910847219c64c9a5e79af1dd17 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 9 Mar 2015 02:36:09 +0100 Subject: [PATCH] Added logging facilities --- Makefile.am | 2 ++ include/criterion/common.h | 6 ++++++ include/criterion/logging.h | 19 +++++++++++++++++++ src/logging.c | 16 ++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 include/criterion/logging.h create mode 100644 src/logging.c diff --git a/Makefile.am b/Makefile.am index 00a090d..e889c93 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,7 @@ subdirinclude_HEADERS = \ include/criterion/criterion.h \ include/criterion/event.h \ include/criterion/hooks.h \ + include/criterion/logging.h \ include/criterion/stats.h libcriterion_la_SOURCES = \ @@ -41,4 +42,5 @@ libcriterion_la_SOURCES = \ src/process.h \ src/stats.c \ src/stats.h \ + src/logging.c \ src/main.c diff --git a/include/criterion/common.h b/include/criterion/common.h index a2ae32a..30995eb 100644 --- a/include/criterion/common.h +++ b/include/criterion/common.h @@ -27,4 +27,10 @@ # define SECTION_(Name) __attribute__((section(Name))) # define UNUSED __attribute__((unused)) +# ifdef __GNUC__ +# define FORMAT(Archetype, Index, Ftc) __attribute__((format(Archetype, Index, Ftc))) +# else +# define FORMAT(Archetype, Index, Ftc) +# endif + #endif /* !CRITERION_COMMON_H_ */ diff --git a/include/criterion/logging.h b/include/criterion/logging.h new file mode 100644 index 0000000..dfc6c53 --- /dev/null +++ b/include/criterion/logging.h @@ -0,0 +1,19 @@ +#ifndef CRITERION_LOGGING_H_ +# define CRITERION_LOGGING_H_ + +#include "common.h" + +enum criterion_logging_level { + CRITERION_INFO = 1, + CRITERION_IMPORTANT, +}; + +extern enum criterion_logging_level logging_threshold; + +FORMAT(printf, 2, 3) +void criterion_log(enum criterion_logging_level level, const char *msg, ...); + +# define criterion_info(...) criterion_log(CRITERION_INFO, __VA_ARGS__) +# define criterion_important(...) criterion_log(CRITERION_IMPORTANT, __VA_ARGS__) + +#endif /* !CRITERION_LOGGING_H_ */ diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..803a717 --- /dev/null +++ b/src/logging.c @@ -0,0 +1,16 @@ +#include +#include +#include "criterion/logging.h" + +enum criterion_logging_level logging_threshold = CRITERION_IMPORTANT; + +void criterion_log(enum criterion_logging_level level, const char *msg, ...) { + va_list args; + + if (level < logging_threshold) + return; + + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); +}