From d575f579bcd604910847219c64c9a5e79af1dd17 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 9 Mar 2015 02:36:09 +0100 Subject: [PATCH 1/3] 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); +} From 728f75cd0be9a4b5a558de4b8794d220296e0a9c Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 9 Mar 2015 02:37:21 +0100 Subject: [PATCH 2/3] Modified report hooks to use the logging interface --- src/report.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/report.c b/src/report.c index 06e1854..dcbd5b1 100644 --- a/src/report.c +++ b/src/report.c @@ -24,6 +24,7 @@ #include #include "criterion/criterion.h" #include "criterion/stats.h" +#include "criterion/logging.h" #include "report.h" #define IMPL_CALL_REPORT_HOOKS(Kind) \ @@ -49,11 +50,15 @@ IMPL_CALL_REPORT_HOOKS(POST_FINI); IMPL_CALL_REPORT_HOOKS(POST_EVERYTHING); ReportHook(PRE_INIT)(struct criterion_test *test) { - fprintf(stderr, "%s::%s: RUNNING\n", test->category, test->name); + criterion_info("%s::%s: RUNNING\n", test->category, test->name); } ReportHook(POST_TEST)(struct criterion_test_stats *stats) { - fprintf(stderr, "%s::%s: %s\n", stats->test->category, stats->test->name, stats->failed ? "FAILURE" : "SUCCESS"); + criterion_log(stats->failed ? CRITERION_IMPORTANT : CRITERION_INFO, + "%s::%s: %s\n", + stats->test->category, + stats->test->name, + stats->failed ? "FAILURE" : "SUCCESS"); } ReportHook(PRE_TEST)() {} @@ -61,12 +66,16 @@ ReportHook(POST_FINI)() {} ReportHook(PRE_EVERYTHING)() {} ReportHook(POST_EVERYTHING)(struct criterion_global_stats *stats) { - fprintf(stderr, "Synthesis: %lu tests were run. %lu passed, %lu failed (with %lu crashes)\n", stats->nb_tests, stats->tests_passed, stats->tests_failed, stats->tests_crashed); + criterion_important("Synthesis: %lu tests were run. %lu passed, %lu failed (with %lu crashes)\n", + stats->nb_tests, + stats->tests_passed, + stats->tests_failed, + stats->tests_crashed); } ReportHook(ASSERT)(struct criterion_assert_stats *stats) { if (!stats->passed) { - fprintf(stderr, "\t%s:%d: Assertion failed: %s\n", + criterion_important("%s:%d: Assertion failed: %s\n", stats->file, stats->line, *stats->message ? stats->message : stats->condition); @@ -74,7 +83,7 @@ ReportHook(ASSERT)(struct criterion_assert_stats *stats) { } ReportHook(TEST_CRASH)(struct criterion_test_stats *stats) { - fprintf(stderr, "\tUnexpected signal after %s:%u!\n%s::%s: FAILURE (CRASH!)\n", + criterion_important("Unexpected signal after %s:%u!\n%s::%s: FAILURE (CRASH!)\n", stats->file, stats->progress, stats->test->category, From 35f3eeef6e18f2c76d112298e52ce3c662ce0591 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 9 Mar 2015 02:37:55 +0100 Subject: [PATCH 3/3] Added command-line options to change the verbosity level --- src/main.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index bcaac01..789f190 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,34 @@ +#define _GNU_SOURCE #include +#include +#include +#include + +# define USAGE \ + "usage: %s OPTIONS\n" \ + "options: \n" \ + " -h or --help: prints this message\n" \ + " --verbose [level]: sets verbosity to level\n" + +int print_usage(char *progname) { + fprintf(stderr, USAGE, progname); + return 0; +} + +int main(int argc, char *argv[]) { + static struct option opts[] = { + {"verbose", optional_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0 } + }; + + for (int c; (c = getopt_long(argc, argv, "h", opts, NULL)) != -1;) { + switch (c) { + case 'v': logging_threshold = atoi(optarg ?: "1"); break; + case 'h': + default : return print_usage(argv[0]); + } + } -int main(void) { return criterion_run_all_tests(); }