Skip to content

Commit

Permalink
[Issue #4] Added a logging interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Snaipe committed Mar 9, 2015
2 parents 9894a06 + e0eb661 commit 850f681
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
Expand All @@ -41,4 +42,5 @@ libcriterion_la_SOURCES = \
src/process.h \
src/stats.c \
src/stats.h \
src/logging.c \
src/main.c
6 changes: 6 additions & 0 deletions include/criterion/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
19 changes: 19 additions & 0 deletions include/criterion/logging.h
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions src/logging.c
Original file line number Diff line number Diff line change
@@ -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);
}
31 changes: 30 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 14 additions & 5 deletions src/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -49,32 +50,40 @@ 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)() {}
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);
}
}

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,
Expand Down

0 comments on commit 850f681

Please sign in to comment.