[Issue #4] Merge branch 'logging' into bleeding

This commit is contained in:
Snaipe 2015-03-09 03:02:01 +01:00
commit 7f1fa41974
6 changed files with 87 additions and 6 deletions

View file

@ -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

View file

@ -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_ */

View file

@ -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_ */

16
src/logging.c Normal file
View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdarg.h>
#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);
}

View file

@ -1,5 +1,34 @@
#define _GNU_SOURCE
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <stdio.h>
#include <getopt.h>
# 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();
}

View file

@ -24,6 +24,7 @@
#include <stdio.h>
#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,