diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 19:24:49 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 19:24:49 +0100 |
| commit | 61ec593c95f3210bdfcd22539d43ef507210981f (patch) | |
| tree | 6a74c720944330505977ebfdcf813b05a33ccade /examples | |
| parent | 7b75ac0962be8ed19a1eca638f600ebee337a18a (diff) | |
| download | nonstd-61ec593c95f3210bdfcd22539d43ef507210981f.tar.gz | |
Add logging
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/.gitignore | 1 | ||||
| -rw-r--r-- | examples/Makefile | 7 | ||||
| -rw-r--r-- | examples/logging.c | 25 |
3 files changed, 32 insertions, 1 deletions
diff --git a/examples/.gitignore b/examples/.gitignore index 0707ee1..dcb0ae9 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -5,4 +5,5 @@ array slice arena files +logging diff --git a/examples/Makefile b/examples/Makefile index a614627..cd83e92 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -5,7 +5,7 @@ CFLAGS = -Wall -Wextra -std=c99 -fsanitize=address -g -O0 LDFLAGS = # Example targets -EXAMPLES = foreach stringv stringb array slice arena files +EXAMPLES = foreach stringv stringb array slice arena files logging # Default target all: $(EXAMPLES) @@ -32,6 +32,9 @@ arena: arena.c files: files.c $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) +logging: logging.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + # Run all examples run: all @echo "\n=== Running stringv ===\n" @@ -48,6 +51,8 @@ run: all @./arena @echo "\n=== Running files ===\n" @./files + @echo "\n=== Running logging ===\n" + @./logging # Clean build artifacts clean: diff --git a/examples/logging.c b/examples/logging.c new file mode 100644 index 0000000..1d2744c --- /dev/null +++ b/examples/logging.c @@ -0,0 +1,25 @@ +#define NONSTD_IMPLEMENTATION +#include "../nonstd.h" + +int main(void) { + // Default level is LOG_INFO + LOG_INFO_MSG("This is an info message: %d", 42); + LOG_DEBUG_MSG("This debug message will NOT be shown by default"); + + // Change level to LOG_DEBUG + set_log_level(LOG_DEBUG); + LOG_DEBUG_MSG("Now debug messages are shown: %s", "hello"); + + // Warnings and Errors + LOG_WARN_MSG("This is a warning!"); + LOG_ERROR_MSG("This is an error!"); + + // Environment variable override test + // You can set LOG_LEVEL=1 (WARN) etc. + LogLevel env_level = get_log_level_from_env(); + if (env_level != LOG_DEBUG) { + printf("Environment overrides level to: %d\n", env_level); + } + + return 0; +} |
