Skip to content

Commit

Permalink
Add a sample showcasing VA_OPT for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Aug 18, 2024
1 parent 058e62b commit 682c1bb
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions book/samples/03_variadic_macro.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h> // printf

#define MARKDOWN_HEADER "|Level|File|Function|Line|Message|\n|:-|:-|:-|-:|:-|\n"

#define log_log(LEVEL, MESSAGE, ...) \
printf("|" LEVEL "|`" __FILE__ "`|`%s`|%i|" MESSAGE "\n", __func__, __LINE__ __VA_OPT__(,) __VA_ARGS__)

#define log_debug(MESSAGE, ...) log_log("DEBUG", MESSAGE __VA_OPT__(,) __VA_ARGS__)
#define log_error(MESSAGE, ...) log_log("ERROR", MESSAGE __VA_OPT__(,) __VA_ARGS__)

int main(int ac, char** av)
{
printf(MARKDOWN_HEADER);
log_debug("Hello world !");
log_error("Failed to open %s: %s", "bar.csv", "no such file");
}

0 comments on commit 682c1bb

Please sign in to comment.