diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2019-03-27 22:09:24 +0100 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2019-03-27 22:09:24 +0100 |
commit | fba549fc8b5980d8f52014a5961013b45d7868c4 (patch) | |
tree | 92f85f3dcbfa9d6ee968e72c13386e6d7b129027 | |
parent | 0c6382ba7ed8e04d36a28c942a2db4a391fef563 (diff) | |
download | embedlog-fba549fc8b5980d8f52014a5961013b45d7868c4.tar.gz embedlog-fba549fc8b5980d8f52014a5961013b45d7868c4.tar.bz2 embedlog-fba549fc8b5980d8f52014a5961013b45d7868c4.zip |
embedlog.h: add user's pointer to el_custom_puts
el_custom_puts now also accepts "void *user" pointer beside standard
"const char *s". It's usefull when you need to pass pointer to logger
object that is not global.
Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r-- | include/embedlog.h | 3 | ||||
-rw-r--r-- | man/el_option.3 | 10 | ||||
-rw-r--r-- | src/el-options.c | 1 | ||||
-rw-r--r-- | src/el-puts.c | 2 | ||||
-rw-r--r-- | tst/test-el-perror.c | 9 | ||||
-rw-r--r-- | tst/test-el-pmemory.c | 7 | ||||
-rw-r--r-- | tst/test-el-print.c | 7 |
7 files changed, 25 insertions, 14 deletions
diff --git a/include/embedlog.h b/include/embedlog.h index 8f79ab2..5ecf1af 100644 --- a/include/embedlog.h +++ b/include/embedlog.h @@ -144,7 +144,7 @@ enum el_option_timestamp_fractions EL_TS_FRACT_ERROR /* internal use onsly */ }; -typedef int (*el_custom_puts)(const char *s); +typedef int (*el_custom_puts)(const char *s, void *user); struct el_options { @@ -173,6 +173,7 @@ struct el_options const char *fname; const char *prefix; el_custom_puts custom_puts; + void *custom_puts_user; }; int el_init(void); diff --git a/man/el_option.3 b/man/el_option.3 index e74f6d2..9cb8b53 100644 --- a/man/el_option.3 +++ b/man/el_option.3 @@ -407,13 +407,19 @@ Input argument is different than 1 or 0 .RE .PP .B EL_CUSTOM_PUTS ( -.I int (*el_custom_puts)(const char *s) +.IB "int (*" el_custom_puts ")(const char *s, void *user)" , +.IB void\ *user .B ) .RS Sets function pointer for custom message print. Function will receive complete messsage to print, just as it would be printed to ie. stderr or another facility. -Function cannot fail, if NULL is passed, custom function won't be called. +.I user +pointer can be used to pass own logger object where data shall be sent. +For example it may contain pointer to +.B el_options +struct, if you don't use global options struct. +Function cannot fail. If NULL is passed, custom function won't be called. It is still mandatory to enable custom printing with . BR el_option (3) .RE diff --git a/src/el-options.c b/src/el-options.c index a84259d..6d91616 100644 --- a/src/el-options.c +++ b/src/el-options.c @@ -354,6 +354,7 @@ static int el_vooption case EL_CUSTOM_PUTS: options->custom_puts = va_arg(ap, el_custom_puts); + options->custom_puts_user = va_arg(ap, void *); return 0; # endif /* ENABLE_OUT_CUSTOM */ diff --git a/src/el-puts.c b/src/el-puts.c index bfc5b8a..026db91 100644 --- a/src/el-puts.c +++ b/src/el-puts.c @@ -133,7 +133,7 @@ int el_oputs #if ENABLE_OUT_CUSTOM if (options->outputs & EL_OUT_CUSTOM && options->custom_puts) { - rv |= options->custom_puts(s); + rv |= options->custom_puts(s, options->custom_puts_user); } #endif diff --git a/tst/test-el-perror.c b/tst/test-el-perror.c index 691b6e5..a7fd5d3 100644 --- a/tst/test-el-perror.c +++ b/tst/test-el-perror.c @@ -68,10 +68,11 @@ static char logbuf[1024 * 1024]; /* output simulation */ static int print_to_buffer ( - const char *s + const char *s, + void *user ) { - strcat(logbuf, s); + strcat(user, s); return 0; } @@ -83,7 +84,7 @@ static int print_to_buffer static void test_prepare(void) { el_init(); - el_option(EL_CUSTOM_PUTS, print_to_buffer); + el_option(EL_CUSTOM_PUTS, print_to_buffer, logbuf); el_option(EL_PRINT_LEVEL, 0); el_option(EL_OUT, EL_OUT_CUSTOM); logbuf[0] = '\0'; @@ -149,7 +150,7 @@ static void perror_custom_options_user_message(void) el_oinit(&opts); - el_ooption(&opts, EL_CUSTOM_PUTS, print_to_buffer); + el_ooption(&opts, EL_CUSTOM_PUTS, print_to_buffer, logbuf); el_ooption(&opts, EL_PRINT_LEVEL, 0); el_ooption(&opts, EL_OUT, EL_OUT_CUSTOM); diff --git a/tst/test-el-pmemory.c b/tst/test-el-pmemory.c index f2632d8..f813d10 100644 --- a/tst/test-el-pmemory.c +++ b/tst/test-el-pmemory.c @@ -68,10 +68,11 @@ static char logbuf[1024 * 1024]; /* output simulation */ static int print_to_buffer ( - const char *s + const char *s, + void *user ) { - strcat(logbuf, s); + strcat(user, s); return 0; } @@ -83,7 +84,7 @@ static int print_to_buffer static void test_prepare(void) { el_init(); - el_option(EL_CUSTOM_PUTS, print_to_buffer); + el_option(EL_CUSTOM_PUTS, print_to_buffer, logbuf); el_option(EL_PRINT_LEVEL, 0); el_option(EL_OUT, EL_OUT_CUSTOM); logbuf[0] = '\0'; diff --git a/tst/test-el-print.c b/tst/test-el-print.c index 55d5020..61e8ba1 100644 --- a/tst/test-el-print.c +++ b/tst/test-el-print.c @@ -96,10 +96,11 @@ static struct rb *expected_logs; /* array of expected logs */ static int print_to_buffer ( - const char *s + const char *s, + void *user ) { - strcat(logbuf, s); + strcat(user, s); return 0; } @@ -554,7 +555,7 @@ static void add_log static void test_prepare(void) { el_init(); - el_option(EL_CUSTOM_PUTS, print_to_buffer); + el_option(EL_CUSTOM_PUTS, print_to_buffer, logbuf); el_option(EL_PRINT_LEVEL, 0); el_option(EL_OUT, EL_OUT_CUSTOM); memset(logbuf, 0, sizeof(logbuf)); |