diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2017-07-26 19:01:28 +0200 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2017-07-26 19:01:28 +0200 |
commit | 173ed710dc1bdd41bb401dc01eb748d2b8709e91 (patch) | |
tree | c2d930c90d7202c93561a2d83c4daca54f864362 | |
parent | 9f81c9672bd1755e24b6ce55cc9f04f18792db7d (diff) | |
download | embedlog-173ed710dc1bdd41bb401dc01eb748d2b8709e91.tar.gz embedlog-173ed710dc1bdd41bb401dc01eb748d2b8709e91.tar.bz2 embedlog-173ed710dc1bdd41bb401dc01eb748d2b8709e91.zip |
Added option to print to many different places)
-rw-r--r-- | include/embedlog.h | 26 | ||||
-rw-r--r-- | src/el-print.c | 372 | ||||
-rw-r--r-- | src/options.c | 207 | ||||
-rw-r--r-- | src/options.h | 99 |
4 files changed, 471 insertions, 233 deletions
diff --git a/include/embedlog.h b/include/embedlog.h index 6c9bf4c..4204099 100644 --- a/include/embedlog.h +++ b/include/embedlog.h @@ -8,6 +8,7 @@ #define EMBEDLOG_H 1 #include <stddef.h> +#include <stdarg.h> #define ELE EL_LEVEL_ERR, __FILE__, __LINE__ #define ELW EL_LEVEL_WRN, __FILE__, __LINE__ @@ -56,14 +57,35 @@ enum el_option_timestamp_timer EL_OPT_TS_TM_MONOTONIC }; +struct options +{ + int outputs; + int level; + int colors; + int timestamp; + int timestamp_timer; + int finfo; +}; +int el_options_init(struct options *); int el_level_set(enum el_level); +int el_olevel_set(struct options *, enum el_level); int el_output_enable(enum el_output); +int el_ooutput_enable(struct options *, enum el_output); int el_output_disable(enum el_output); +int el_ooutput_disable(struct options *, enum el_output); int el_option(enum el_option, int); +int el_ooption(struct options *, enum el_option, int); int el_print(enum el_level, const char *, size_t, const char *, ...); -int el_print_mem(enum el_level, const char *, size_t, const void *, size_t); -int el_print_error(enum el_level, const char *, size_t, const char *, ...); +int el_oprint(enum el_level, const char *, size_t, struct options *, + const char *, ...); +int el_vprint(enum el_level, const char *, size_t, const char *, va_list); +int el_voprint(enum el_level, const char *, size_t, struct options *, + const char *, va_list); +int el_mprint(enum el_level, const char *, size_t, const void *, size_t); +int el_perror(enum el_level, const char *, size_t, const char *, ...); +int el_operror(enum el_level, const char *, size_t, struct options *, + const char *, ...); #endif diff --git a/src/el-print.c b/src/el-print.c index 3850be0..2e17878 100644 --- a/src/el-print.c +++ b/src/el-print.c @@ -1,10 +1,45 @@ /* ========================================================================== - Licensed under BSD 2clause license. See LICENSE file for more information + Licensed under BSD 2clause license See LICENSE file for more information Author: Michał Łyszczek <michal.lyszczek@bofc.pl> + ========================================================================== + + ------------------------------------------------------------ + / This module handles all el_print family functions, this is \ + | the place where message log is constructed and being send | + \ to apropriate output depending on the options / + ------------------------------------------------------------ + \ ,+*^^*+___+++_ + \ ,*^^^^ ) + \ _+* ^**+_ + \ +^ _ _++*+_+++_, ) + _+^^*+_ ( ,+*^ ^ \+_ ) + { ) ( ,( ,_+--+--, ^) ^\ + { (@) } f ,( ,+-^ __*_*_ ^^\_ ^\ ) + {:;-/ (_+*-+^^^^^+*+*<_ _++_)_ ) ) / + ( / ( ( ,___ ^*+_+* ) < < \ + U _/ ) *--< ) ^\-----++__) ) ) ) + ( ) _(^)^^)) ) )\^^^^^))^*+/ / / + ( / (_))_^)) ) ) ))^^^^^))^^^)__/ +^^ + ( ,/ (^))^)) ) ) ))^^^^^^^))^^) _) + *+__+* (_))^) ) ) ))^^^^^^))^^^^^)____*^ + \ \_)^)_)) ))^^^^^^^^^^))^^^^) + (_ ^\__^^^^^^^^^^^^))^^^^^^^) + ^\___ ^\__^^^^^^))^^^^^^^^)\\ + ^^^^^\uuu/^^\uuu/^^^^\^\^\^\^\^\^\^\ + ___) >____) >___ ^\_\_\_\_\_\_\) + ^^^//\\_^^//\\_^ ^(\_\_\_\) + ^^^ ^^ ^^^ ^ ========================================================================== */ -/* ==== include files ======================================================= */ +/* ========================================================================== + _ __ __ ____ _ __ + (_)____ _____ / /__ __ ____/ /___ / __/(_)/ /___ _____ + / // __ \ / ___// // / / // __ // _ \ / /_ / // // _ \ / ___/ + / // / / // /__ / // /_/ // /_/ // __/ / __// // // __/(__ ) + /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/ /_/ /_//_/ \___//____/ + + ========================================================================== */ #include "embedlog.h" @@ -23,7 +58,20 @@ #include <string.h> -/* ==== private variables =================================================== */ +/* ========================================================================== + _ __ + ____ _____ (_)_ __ ____ _ / /_ ___ + / __ \ / ___// /| | / // __ `// __// _ \ + / /_/ // / / / | |/ // /_/ // /_ / __/ + / .___//_/ /_/ |___/ \__,_/ \__/ \___/ + /_/ + _ __ __ + _ __ ____ _ _____ (_)____ _ / /_ / /___ _____ + | | / // __ `// ___// // __ `// __ \ / // _ \ / ___/ + | |/ // /_/ // / / // /_/ // /_/ // // __/(__ ) + |___/ \__,_//_/ /_/ \__,_//_.___//_/ \___//____/ + + ========================================================================== */ static const char char_level[4] = { 'e', 'w', 'i', 'd' }; @@ -56,7 +104,20 @@ static const char *color[] = #endif -/* ==== private functions =================================================== */ +/* ========================================================================== + _ __ + ____ _____ (_)_ __ ____ _ / /_ ___ + / __ \ / ___// /| | / // __ `// __// _ \ + / /_/ // / / / | |/ // /_/ // /_ / __/ + / .___//_/ /_/ |___/ \__,_/ \__/ \___/ + /_/ + ____ __ _ + / __/__ __ ____ _____ / /_ (_)____ ____ _____ + / /_ / / / // __ \ / ___// __// // __ \ / __ \ / ___/ + / __// /_/ // / / // /__ / /_ / // /_/ // / / /(__ ) + /_/ \__,_//_/ /_/ \___/ \__//_/ \____//_/ /_//____/ + + ========================================================================== */ /* ========================================================================== @@ -71,12 +132,13 @@ static const char *color[] = static size_t el_color ( - char *buf, /* buffer where to store color info */ - int level /* log level or COLOR_RESET */ + struct options *options, /* options defining printing style */ + char *buf, /* buffer where to store color info */ + int level /* log level or COLOR_RESET */ ) { #if ENABLE_COLORS - if (g_options.colors == 0) + if (options->colors == 0) { /* * no colors, you got it! @@ -172,16 +234,17 @@ static void el_ts_clock_gettime static size_t el_timestamp ( - char *buf /* buffer where timestamp will be stored */ + struct options *options, /* options defining printing style */ + char *buf /* buffer where timestamp will be stored */ ) { #if ENABLE_TIMESTAMP - long s; /* timestamp seconds */ - long us; /* timestamp microseconds */ - size_t tl; /* timestamp length */ + long s; /* timestamp seconds */ + long us; /* timestamp microseconds */ + size_t tl; /* timestamp length */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - if (g_options.timestamp == EL_OPT_TS_OFF) + if (options->timestamp == EL_OPT_TS_OFF) { /* * user doesn't want us to print timestamp, that's fine @@ -194,7 +257,7 @@ static size_t el_timestamp * first we get seconds and microseconds from proper timer */ - switch (g_options.timestamp_timer) + switch (options->timestamp_timer) { #if ENABLE_REALTIME @@ -232,7 +295,7 @@ static size_t el_timestamp * then convert retrieved time into string timestamp */ - if (g_options.timestamp == EL_OPT_TS_LONG) + if (options->timestamp == EL_OPT_TS_LONG) { struct tm tm; /* timestamp splitted */ struct tm *tmp; /* timestamp splitted pointer */ @@ -307,17 +370,18 @@ static const char *el_basename static size_t el_finfo ( - char *buf, /* location whre to store file information */ - const char *file, /* path to file - will be basenamed */ - int num /* line number (max 99999) */ + struct options *options, /* options defining printing style */ + char *buf, /* location whre to store file information */ + const char *file, /* path to file - will be basenamed */ + int num /* line number (max 99999) */ ) { #if ENABLE_FINFO - const char *base; /* basenem of the 'file' */ - size_t fl; /* number of bytes stored in 'buf' */ + const char *base; /* basenem of the 'file' */ + size_t fl; /* number of bytes stored in 'buf' */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - if (g_options.finfo == 0) + if (options->finfo == 0) { /* * no file info for this caller @@ -349,33 +413,34 @@ static size_t el_finfo static void el_puts ( - const char *s /* string to put into output */ + struct options *options, /* options defining printing style */ + const char *s /* string to put into output */ ) { #if ENABLE_OUT_STDERR - if (g_options.outputs & EL_OUT_STDERR) + if (options->outputs & EL_OUT_STDERR) { fputs(s, stderr); } #endif #if 0 /* TODO */ - if (g_options.outputs & EL_OUT_SYSLOG) + if (options->outputs & EL_OUT_SYSLOG) { el_puts_syslog(s); } - if (g_options.outputs & EL_OUT_FILE) + if (options->outputs & EL_OUT_FILE) { el_puts_file(s); } - if (g_options.outputs & EL_OUT_NET) + if (options->outputs & EL_OUT_NET) { el_puts_net(s); } - if (g_options.outputs & EL_OUT_TTY) + if (options->outputs & EL_OUT_TTY) { el_puts_tty(s); } @@ -384,11 +449,163 @@ static void el_puts /* ========================================================================== + __ __ _ + ____ __ __ / /_ / /(_)_____ + / __ \ / / / // __ \ / // // ___/ + / /_/ // /_/ // /_/ // // // /__ + / .___/ \__,_//_.___//_//_/ \___/ + /_/ + ____ __ _ + / __/__ __ ____ _____ / /_ (_)____ ____ _____ + / /_ / / / // __ \ / ___// __// // __ \ / __ \ / ___/ + / __// /_/ // / / // /__ / /_ / // /_/ // / / /(__ ) + /_/ \__,_//_/ /_/ \___/ \__//_/ \____//_/ /_//____/ + + ========================================================================== */ + + +/* ========================================================================== + calls el_print with 'fmt' and '...' parameters, but additionaly prints + information about errno. Functionaly it is similar to perror function + ========================================================================== */ + + +int el_perror +( + enum el_level level, /* log level to print message with */ + const char *file, /* file name where log is printed */ + size_t num, /* line number where log is printed */ + const char *fmt, /* message format (see printf (3)) */ + ... /* additional parameters for fmt */ +) +{ + va_list ap; /* arguments '...' for 'fmt' */ + int rc; /* return code from el_print() */ + unsigned long e; /* errno from upper layer */ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + + e = errno; + + va_start(ap, fmt); + rc = el_voprint(level, file, num, &g_options, fmt, ap); + rc |= el_oprint(level, file, num, &g_options, + "errno num: %lu, strerror: %s", e, strerror(e)); + va_end(ap); +} + + +/* ========================================================================== + el_perror function with custom options + ========================================================================== */ + + +int el_operror +( + enum el_level level, /* log level to print message with */ + const char *file, /* file name where log is printed */ + size_t num, /* line number where log is printed*/ + struct options *options, /* options defining printing style */ + const char *fmt, /* message format (see printf (3)) */ + ... /* additional parameters for fmt */ +) +{ + va_list ap; /* arguments '...' for 'fmt' */ + int rc; /* return code from el_print() */ + unsigned long e; /* errno from upper layer */ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + + e = errno; + + va_start(ap, fmt); + rc = el_voprint(level, file, num, options, fmt, ap); + rc |= el_oprint(level, file, num, options, + "errno num: %lu, strerror: %s", e, strerror(e)); + va_end(ap); +} + + +/* ========================================================================== + simply calls el_printv with '...' converted to 'va_list' + ========================================================================== */ + + +int el_print +( + enum el_level level, /* log level to print message with */ + const char *file, /* file name where log is printed */ + size_t num, /* line number where log is printed */ + const char *fmt, /* message format (see printf (3)) */ + ... /* additional parameters for fmt */ +) +{ + va_list ap; /* arguments '...' for 'fmt' */ + int rc; /* return code from el_printfv() */ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + + va_start(ap, fmt); + rc = el_voprint(level, file, num, &g_options, fmt, ap); + va_end(ap); + + return rc; +} + + +/* ========================================================================== + el_print but with custom options + ========================================================================== */ + + +int el_oprint +( + enum el_level level, /* log level to print message with */ + const char *file, /* file name where log is printed */ + size_t num, /* line number where log is printed */ + struct options *options, /* options defining printing style */ + const char *fmt, /* message format (see printf (3)) */ + ... /* additional parameters for fmt */ +) +{ + va_list ap; /* arguments '...' for 'fmt' */ + int rc; /* return code from el_printfv() */ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + + va_start(ap, fmt); + rc = el_voprint(level, file, num, options, fmt, ap); + va_end(ap); + + return rc; +} + + +/* ========================================================================== + el_print but accepts variadic argument list object instead of '...' + ========================================================================== */ + + +int el_vprint +( + enum el_level level, /* log level to print message with */ + const char *file, /* file name where log is printed */ + size_t num, /* line number where log is printed */ + const char *fmt, /* message format (see printf (3)) */ + va_list ap /* additional parameters for fmt */ +) +{ + return el_voprint(level, file, num, &g_options, fmt, ap); +} + + +/* ========================================================================== Prints message formated by 'fmt' and 'ap' with timestamp, 'file' and line number 'num' information, with specified 'level' into configured - outputs. Function allocates on callers heap EL_BUF_MAX of memory. If - log message is longer than available buffer, it will be truncated and - part of message will be lost. + outputs. Function allocates on callers stack EL_BUF_MAX of memory. + If log message is longer than available buffer, it will be truncated + and part of message will be lost. Additionally options may be passed to + tune printing style in runtime errno: EINVAL level is invalid @@ -398,25 +615,26 @@ static void el_puts ========================================================================== */ -static int el_printv +int el_voprint ( - enum el_level level, /* log level to print message with */ - const char *file, /* file name where log is printed */ - size_t num, /* line number where log is printed */ - const char *fmt, /* message format (see printf (3)) */ - va_list ap /* additional parameters for fmt */ + enum el_level level, /* log level to print message with */ + const char *file, /* file name where log is printed */ + size_t num, /* line number where log is printed*/ + struct options *options, /* options defining printing style */ + const char *fmt, /* message format (see printf (3)) */ + va_list ap /* additional parameters for fmt */ ) { - char buf[EL_BUF_MAX + 2]; /* buffer for message to print */ - size_t w; /* bytes written to buf */ - size_t flen; /* length of the parsed fmt output */ - int e; /* error code */ + char buf[EL_BUF_MAX + 2]; /* buffer for message to print */ + size_t w; /* bytes written to buf */ + size_t flen; /* length of the parsed fmt output */ + int e; /* error code */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ VALID(EINVAL, 0 <= level && level <= EL_LEVEL_DBG); VALID(EINVAL, fmt); - VALID(ECHRNG, el_log_allowed(level)); + VALID(ECHRNG, el_log_allowed(options, level)); e = 0; @@ -424,9 +642,9 @@ static int el_printv * add preamble and colors to log line buf */ - w = el_color(buf, level); - w += el_timestamp(buf + w); - w += el_finfo(buf + w, file, num); + w = el_color(options, buf, level); + w += el_timestamp(options, buf + w); + w += el_finfo(options, buf + w, file, num); if (w != 0 && buf[w - 1] == ']') { @@ -465,7 +683,7 @@ static int el_printv * add terminal formatting reset sequence */ - w += el_color(buf + w, COLOR_RESET); + w += el_color(options, buf + w, COLOR_RESET); /* * make sure buf is always null terminated and contains new line character @@ -474,7 +692,7 @@ static int el_printv buf[w++] = '\n'; buf[w++] = '\0'; - el_puts(buf); + el_puts(options, buf); if (e) { @@ -484,67 +702,3 @@ static int el_printv return 0; } - - -/* ==== public functions ==================================================== */ - - -/* ========================================================================== - calls el_print with 'fmt' and '...' parameters, but additionaly prints - information about errno. Functionaly it is similar to perror function - ========================================================================== */ - - -int el_print_error -( - enum el_level level, /* log level to print message with */ - const char *file, /* file name where log is printed */ - size_t num, /* line number where log is printed */ - const char *fmt, /* message format (see printf (3)) */ - ... /* additional parameters for fmt */ -) -{ - va_list ap; /* arguments '...' for 'fmt' */ - int rc; /* return code from el_printfv() */ - unsigned long e; /* errno from upper layer */ - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - - - e = errno; - - va_start(ap, fmt); - rc = el_printv(level, file, num, fmt, ap); - rc |= el_print(level, file, num, - "errno num: %lu, strerror: %s", e, strerror(e)); - va_end(ap); -} - - -/* ========================================================================== - simply calls el_printv with '...' converted to 'va_list' - ========================================================================== */ - - -int el_print -( - enum el_level level, /* log level to print message with */ - const char *file, /* file name where log is printed */ - size_t num, /* line number where log is printed */ - const char *fmt, /* message format (see printf (3)) */ - ... /* additional parameters for fmt */ -) -{ - va_list ap; /* arguments '...' for 'fmt' */ - int rc; /* return code from el_printfv() */ - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - - - va_start(ap, fmt); - rc = el_printv(level, file, num, fmt, ap); - va_end(ap); - - return rc; -} - - - diff --git a/src/options.c b/src/options.c index a9b34ae..d266269 100644 --- a/src/options.c +++ b/src/options.c @@ -1,26 +1,82 @@ /* ========================================================================== Licensed under BSD 2clause license. See LICENSE file for more information Author: Michał Łyszczek <michal.lyszczek@bofc.pl> + ========================================================================== + + ------------------------------------------------------------- + / This is option module, here we parse and manage all options \ + | regarding logging. Also this is home for global default | + \ option object / + ------------------------------------------------------------- + \ . . + \ / `. .' " + \ .---. < > < > .---. + \ | \ \ - ~ ~ - / / | + _____ ..-~ ~-..-~ + | | \~~~\.' `./~~~/ + --------- \__/ \__/ + .' O \ / / \ " + (_____, `._.' | } \/~~~/ + `----. / } | / \__/ + `-. | / | / `. ,~~| + ~-.__| /_ - ~ ^| /- _ `..-' + | / | / ~-. `-. _ _ _ + |_____| |_____| ~ - . _ _ _ _ _> ========================================================================== */ -/* ==== include files ======================================================= */ +/* ========================================================================== + _ __ __ ____ _ __ + (_)____ _____ / /__ __ ____/ /___ / __/(_)/ /___ _____ + / // __ \ / ___// // / / // __ // _ \ / /_ / // // _ \ / ___/ + / // / / // /__ / // /_/ // /_/ // __/ / __// // // __/(__ ) + /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/ /_/ /_//_/ \___//____/ + + ========================================================================== */ -#include "embedlog.h" #include "config.h" +#include "embedlog.h" #include "options.h" #include "valid.h" #include <errno.h> +#include <string.h> -/* ==== global variables ==================================================== */ + +/* ========================================================================== + __ __ __ + ____ _ / /____ / /_ ____ _ / / + / __ `// // __ \ / __ \ / __ `// / + / /_/ // // /_/ // /_/ // /_/ // / + \__, //_/ \____//_.___/ \__,_//_/ + /____/ + _ __ __ + _ __ ____ _ _____ (_)____ _ / /_ / /___ _____ + | | / // __ `// ___// // __ `// __ \ / // _ \ / ___/ + | |/ // /_/ // / / // /_/ // /_/ // // __/(__ ) + |___/ \__,_//_/ /_/ \__,_//_.___//_/ \___//____/ + + ========================================================================== */ struct options g_options; -/* ==== private variables =================================================== */ +/* ========================================================================== + _ __ + ____ _____ (_)_ __ ____ _ / /_ ___ + / __ \ / ___// /| | / // __ `// __// _ \ + / /_/ // / / / | |/ // /_/ // /_ / __/ + / .___//_/ /_/ |___/ \__,_/ \__/ \___/ + /_/ + _ __ __ + _ __ ____ _ _____ (_)____ _ / /_ / /___ _____ + | | / // __ `// ___// // __ `// __ \ / // _ \ / ___/ + | |/ // /_/ // / / // /_/ // /_/ // // __/(__ ) + |___/ \__,_//_/ /_/ \__,_//_.___//_/ \___//____/ + + ========================================================================== */ static const int VALID_OUTS = 0 @@ -47,73 +103,151 @@ static const int VALID_OUTS = 0 ; -/* ==== public functions ==================================================== */ +/* ========================================================================== + __ __ _ + ____ __ __ / /_ / /(_)_____ + / __ \ / / / // __ \ / // // ___/ + / /_/ // /_/ // /_/ // // // /__ + / .___/ \__,_//_.___//_//_/ \___/ + /_/ + ____ __ _ + / __/__ __ ____ _____ / /_ (_)____ ____ _____ + / /_ / / / // __ \ / ___// __// // __ \ / __ \ / ___/ + / __// /_/ // / / // /__ / /_ / // /_/ // / / /(__ ) + /_/ \__,_//_/ /_/ \___/ \__//_/ \____//_/ /_//____/ + + ========================================================================== */ + + +/* ========================================================================== + Sets options object to sane values + + errno + EINVAL options is invalid (null) + ========================================================================== */ + + +int el_options_init +( + struct options *options /* options object */ +) +{ + VALID(EINVAL, options); + + memset(options, 0, sizeof(*options)); + return 0; +} + + +/* ========================================================================== + Sets current log level for default options + ========================================================================== */ + + +int el_level_set +( + enum el_level level /* log level to set */ +) +{ + return el_olevel_set(&g_options, level); +} /* ========================================================================== Sets current log level - errno: + errno EINVAL passed level is not a valid log level ========================================================================== */ -int el_level_set +int el_olevel_set ( - enum el_level level /* log level to set */ + struct options *options, /* options object to set option to */ + enum el_level level /* log level to set */ ) { VALID(EINVAL, 0 <= level && level <= EL_LEVEL_DBG); - g_options.level = level; + options->level = level; return 0; } /* ========================================================================== + enables default output + ========================================================================== */ + + +int el_output_enable +( + enum el_output output /* output to enable */ +) +{ + return el_ooutput_enable(&g_options, output); +} + + +/* ========================================================================== Enables specified output for prints - errno: + errno EINVAL passwd output is invalid ENOSYS specified output is not implemented, ie. was not enabled during compilation ========================================================================== */ -int el_output_enable +int el_ooutput_enable ( - enum el_output output /* output to enable */ + struct options *options, /* options object to set option to */ + enum el_output output /* output to enable */ ) { VALID(EINVAL, (output & ~EL_OUT_ALL) == 0x00); VALID(ENOSYS, (output & ~VALID_OUTS) == 0x00); - g_options.outputs |= output; + options->outputs |= output; return 0; } /* ========================================================================== + disable default output + ========================================================================== */ + + +int el_output_disable +( + enum el_output output /* output to disable */ +) +{ + return el_ooutput_disable(&g_options, output); +} + + +/* ========================================================================== Disables specified output from prints - errno: + errno EINVAL passed output is invalid ENOSYS specified output is not implemented, ie. was not enabled during compilation ========================================================================== */ -int el_output_disable +int el_ooutput_disable ( - enum el_output output /* output to disable */ + struct options *options, /* options object to set option to */ + enum el_output output /* output to disable */ ) { VALID(EINVAL, (output & ~EL_OUT_ALL) == 0x00); VALID(ENOSYS, (output & ~VALID_OUTS) == 0x00); - g_options.outputs &= ~output; + options->outputs &= ~output; return 0; } @@ -127,27 +261,44 @@ int el_output_disable int el_log_allowed ( - enum el_level level /* log level to check */ + struct options *options, /* options object */ + enum el_level level /* log level to check */ ) { - return g_options.level >= level && g_options.outputs; + return options->level >= level && options->outputs; } /* ========================================================================== - sets 'option' with 'value' + el_ooptions but for default g_options object + ========================================================================== */ - errno: + +int el_option +( + enum el_option option, /* option to set */ + int value /* option value */ +) +{ + return el_ooption(&g_options, option, value); +} + + +/* ========================================================================== + sets 'option' with 'value' in 'options' object. + + errno EINVAL option is invalid EINVAL value for specific option is invalid ENOSYS option was disabled during compilation ========================================================================== */ -int el_option +int el_ooption ( - enum el_option option, - int value + struct options *options, /* options object to set option to */ + enum el_option option, /* option to set */ + int value /* option value */ ) { VALID(EINVAL, 0 <= option && option <= EL_OPT_FINFO); @@ -163,7 +314,7 @@ int el_option VALID(EINVAL, (value & ~1) == 0); - g_options.colors = value; + options->colors = value; return 0; #endif /* ENABLE_COLORS */ @@ -173,13 +324,13 @@ int el_option case EL_OPT_TS: VALID(EINVAL, 0 <= value && value <= EL_OPT_TS_OFF); - g_options.timestamp = value; + options->timestamp = value; return 0; case EL_OPT_TS_TM: VALID(EINVAL, 0 <= value && value <= EL_OPT_TS_TM_MONOTONIC); - g_options.timestamp_timer = value; + options->timestamp_timer = value; return 0; #endif /* ENABLE_TIMESTAMP */ @@ -189,7 +340,7 @@ int el_option case EL_OPT_FINFO: VALID(EINVAL, (value & ~1) == 0); - g_options.finfo = value; + options->finfo = value; return 0; #endif /* ENABLE_FINFO */ diff --git a/src/options.h b/src/options.h index 47141ce..5b96a14 100644 --- a/src/options.h +++ b/src/options.h @@ -4,105 +4,16 @@ ========================================================================== */ -/* ==== include files ======================================================= */ +#ifndef EL_OPTIONS_H +#define EL_OPTIONS_H 1 #include "config.h" - - -/* ==== public types ======================================================== */ - - -/* ========================================================================== - options used in library - ========================================================================== */ - - -struct options -{ - /* - * list of enabled outputs - */ - - int outputs; - - /* - * current log level - */ - - int level; - -#if ENABLE_COLORS - /* - * if set, output will be colored (on supported terminals only) - */ - - int colors; -#endif - -#if ENABLE_TIMESTAMP - /* - * if enabled, timestamp will be added to the log - * - * EL_OPT_TS_OFF - * no timestamp will be added - * - * EL_OPT_TS_SHORT - * short timestamp will be added in format seconds.microseconds - * - * EL_OPT_TS_LONG - * full date will added to each log - */ - - int timestamp; - - /* - * timer to use to calculate timestamp - * - * EL_OPT_TS_TM_REALTIME - * It usually measures wall clock, it doesn't wrap, but can - * move move forward or backward without a warning. Available - * since POSIX 199309L - * - * EL_OPT_TS_TM_MONOTONIC - * Clock that measures time from unspecified time since boot. - * Changes every boot, but doesn't jump forward of backward. - * This clock is an optional POSIX clock. - * - * EL_OPT_TS_TM_TIME - * Measures wall clock - just like REALTIME but its resolution - * is only to 1 second. Use only when you build for non-posix. - * This is available where C89 is available. - * - * EL_OPT_TS_TM_CLOCK - * Measures cpu clocks. Similar to MONOTONIC, but wraps pretty - * quickly, on systems where CLOCKS_PER_SEC is 1000 (embedded - * systems) clock will wrap in around 24 days (on 32bits), and - * this is high implementation specific. This timer is - * available where C89 is available. - */ - - int timestamp_timer; -#endif - -#if ENABLE_FINFO - /* - * if enabled, information about location from where log is printed will be - * added - */ - - int finfo; -#endif -}; - - -/* ==== global variables ==================================================== */ +#include "embedlog.h" extern struct options g_options; +int el_log_allowed(struct options *, enum el_level); -/* ==== public functions ==================================================== */ - - -int el_log_allowed(enum el_level); +#endif |