diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2017-11-27 20:59:17 +0100 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2017-11-27 20:59:17 +0100 |
commit | d5e6d8b3e40a22a3a13356f0028ed5784bbc840b (patch) | |
tree | f6f63cf4e2c3eca5851515f0857d6aba6f2f4b67 /examples | |
parent | 0978be2fdbb57b0e8d293ac13cc3a95ac225c403 (diff) | |
download | embedlog-d5e6d8b3e40a22a3a13356f0028ed5784bbc840b.tar.gz embedlog-d5e6d8b3e40a22a3a13356f0028ed5784bbc840b.tar.bz2 embedlog-d5e6d8b3e40a22a3a13356f0028ed5784bbc840b.zip |
Added usage examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 7 | ||||
-rw-r--r-- | examples/print-memory.c | 28 | ||||
-rw-r--r-- | examples/print-options.c | 58 | ||||
-rw-r--r-- | examples/print-simple.c | 49 | ||||
-rw-r--r-- | examples/print-to-file.c | 75 |
5 files changed, 217 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..ef61e57 --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,7 @@ +bin_PROGRAMS = print_to_file print_options print_simple print_memory +LDADD = -lembedlog + +print_to_file_SOURCES = print-to-file.c +print_options_SOURCES = print-options.c +print_simple_SOURCES = print-simple.c +print_memory_SOURCES = print-memory.c diff --git a/examples/print-memory.c b/examples/print-memory.c new file mode 100644 index 0000000..44e8093 --- /dev/null +++ b/examples/print-memory.c @@ -0,0 +1,28 @@ +/* ========================================================================== + Licensed under BSD 2clause license See LICENSE file for more information + Author: Michał Łyszczek <michal.lyszczek@bofc.pl> + ========================================================================== */ + +#include <embedlog.h> + +int main(void) +{ + char s[] = "some message\0that contains\0null characters"; + char ascii[128]; + int i; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + + for (i = 0; i != 128; ++i) + { + ascii[i] = (char)i; + } + + el_init(); + el_option(EL_OPT_OUTPUT, EL_OPT_OUT_STDERR); + + el_pmemory(ELI, ascii, sizeof(ascii)); + el_pmemory(ELI, s, sizeof(s)); + + return 0; +} diff --git a/examples/print-options.c b/examples/print-options.c new file mode 100644 index 0000000..9ca873a --- /dev/null +++ b/examples/print-options.c @@ -0,0 +1,58 @@ +/* ========================================================================== + Licensed under BSD 2clause license See LICENSE file for more information + Author: Michał Łyszczek <michal.lyszczek@bofc.pl> + ========================================================================== */ + +#include <embedlog.h> + +int main(void) +{ + el_init(); + el_option(EL_OPT_OUTPUT, EL_OPT_OUT_STDERR); + + el_option(EL_OPT_PRINT_LEVEL, 0); + el_print(ELI, "We can disable information about log level"); + el_print(ELF, "message still will be filtered by log level"); + el_print(ELA, "but there is no way to tell what level message is"); + el_print(ELD, "like this message will not be printed"); + + el_option(EL_OPT_TS, EL_OPT_TS_SHORT); + el_print(ELF, "As every respected logger, we also have timestamps"); + el_print(ELF, "which work well with time from clock()"); + el_option(EL_OPT_TS_TM, EL_OPT_TS_TM_MONOTONIC); + el_print(ELF, "or CLOCK_MONOTONIC from POSIX"); + el_option(EL_OPT_TS, EL_OPT_TS_LONG); + el_option(EL_OPT_TS_TM, EL_OPT_TS_TM_TIME); + el_print(ELF, "we also have long format that works well with time()"); + el_option(EL_OPT_TS_TM, EL_OPT_TS_TM_REALTIME); + el_print(ELF, "if higher precision is needed we can use CLOCK_REALTIME"); + el_option(EL_OPT_TS, EL_OPT_TS_SHORT); + el_print(ELF, "we can also mix REALTIME with short format"); + el_option(EL_OPT_TS, EL_OPT_TS_LONG); + el_option(EL_OPT_TS_TM, EL_OPT_TS_TM_CLOCK); + el_print(ELF, "or long with clock() if you desire"); + el_option(EL_OPT_TS, EL_OPT_TS_OFF); + el_print(ELF, "no time information, if your heart desire it"); + + el_option(EL_OPT_FINFO, 1); + el_print(ELF, "log location is very usefull for debuging"); + + el_option(EL_OPT_TS, EL_OPT_TS_LONG); + el_option(EL_OPT_TS_TM, EL_OPT_TS_TM_REALTIME); + el_option(EL_OPT_PRINT_LEVEL, 1); + el_print(ELF, "Different scenarios need different options"); + el_print(ELA, "So we can mix options however we want"); + + el_option(EL_OPT_COLORS, 1); + el_option(EL_OPT_LEVEL, EL_DBG); + el_print(ELD, "And if we have"); + el_print(ELI, "modern terminal"); + el_print(ELN, "we can enable colors"); + el_print(ELW, "to spot warnings"); + el_print(ELE, "or errors"); + el_print(ELC, "with a quick"); + el_print(ELA, "glance into"); + el_print(ELF, "log file"); + + el_cleanup(); +} diff --git a/examples/print-simple.c b/examples/print-simple.c new file mode 100644 index 0000000..9fd8e5f --- /dev/null +++ b/examples/print-simple.c @@ -0,0 +1,49 @@ +/* ========================================================================== + Licensed under BSD 2clause license See LICENSE file for more information + Author: Michał Łyszczek <michal.lyszczek@bofc.pl> + ========================================================================== */ + +#include <embedlog.h> + +int main(void) +{ + /* + * first we nned to initialize logger to known state + */ + + el_init(); + + /* + * to use logger you need to enable at least one output, without it logs + * will be printed to /dev/null + */ + + el_option(EL_OPT_OUTPUT, EL_OPT_OUT_STDERR); + + /* + * now we can simply print messages like we would do it with ordinary + * printf - we just need to log level macro as a first argument + */ + + el_print(ELI, "Info message"); + el_print(ELF, "Fatal message with additional argument %d", 42); + el_print(ELD, "Debug message that won't be printed due to log level"); + + /* + * we can change log level in runtime as we see fit, now enable debug + * prints + */ + + el_option(EL_OPT_LEVEL, EL_DBG); + el_print(ELD, "But now debug will be printed"); + + /* + * altough embedlog does not use dynamic allocation by itself, system may + * allocate some resources (like opened file descriptors when printing to + * file), with el_cleanup, we can make sure all resources are freed. + */ + + el_cleanup(); + + return 0; +} diff --git a/examples/print-to-file.c b/examples/print-to-file.c new file mode 100644 index 0000000..0afcac5 --- /dev/null +++ b/examples/print-to-file.c @@ -0,0 +1,75 @@ +/* ========================================================================== + Licensed under BSD 2clause license See LICENSE file for more information + Author: Michał Łyszczek <michal.lyszczek@bofc.pl> + ========================================================================== */ + +#include <embedlog.h> +#include <errno.h> +#include <string.h> +#include <sys/stat.h> + +#define WORKDIR "/tmp/embedlog-example" + +int main(void) +{ + el_init(); + el_option(EL_OPT_TS, EL_OPT_TS_LONG); + el_option(EL_OPT_TS_TM, EL_OPT_TS_TM_REALTIME); + el_option(EL_OPT_FINFO, 1); + + if (mkdir(WORKDIR, 0755) != 0 && errno != EEXIST) + { + fprintf(stderr, "Couldn't create %s, error %s\n", + WORKDIR, strerror(errno)); + + goto error; + } + + if (el_option(EL_OPT_FNAME, WORKDIR"/log") != 0) + { + /* + * embedlog will try to open file now, this may fail for various of + * reasons, if opening fails, we cannot log to file - all el_print + * to files will return error + */ + + fprintf(stderr, "Couldn't open file for logging %s\n", + strerror(errno)); + + goto error; + } + + /* + * instruct logger to print into both file and standard error + */ + + el_option(EL_OPT_OUTPUT, EL_OPT_OUT_FILE | EL_OPT_OUT_STDERR); + + el_print(ELI, "This message will appear both in standard error"); + el_print(ELI, "and in file %s", WORKDIR"/log"); + el_print(ELI, "in this file there might be another logs like this " + "from consecutive execution if this code, as embedlog " + "does not truncate logs but append to file"); + + /* + * enable file rotation and set file size to small enough value to + * present how rotation works, in real life, rotate size should be much + * higher to prevent unnecessary rotation + */ + + el_option(EL_OPT_FROTATE_NUMBER, 5); + el_option(EL_OPT_FROTATE_SIZE, 512); + el_print(ELI, "Now we enabled log rotation"); + el_print(ELI, "If log cannot fit in current file"); + el_print(ELI, "it will be stored in new file"); + el_print(ELI, "and if library creates EL_OPT_FROTATE_NUMBER files"); + el_print(ELI, "oldest file will be deleted and new file will be created"); + el_print(ELI, "run this program multiple times and see how it works"); + + el_cleanup(); + return 0; + +error: + el_cleanup(); + return 1; +} |