-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #4] Added a logging interface
- Loading branch information
Showing
6 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters