diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-04-18 13:44:26 +0200 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-04-18 13:44:26 +0200 |
commit | 82c17ecf967c940ae6763c794594b66bac9f882a (patch) | |
tree | 0e65ce9ea56fa0d9119335dc142612349d9f3f91 | |
parent | 021965eedc03dd17b98d56f2bb45cac9b391b7a2 (diff) | |
download | embedlog-82c17ecf967c940ae6763c794594b66bac9f882a.tar.gz embedlog-82c17ecf967c940ae6763c794594b66bac9f882a.tar.bz2 embedlog-82c17ecf967c940ae6763c794594b66bac9f882a.zip |
add option to disable fractions of seconds in compile time
this saves 10bytes from stack memory for each function call
-rw-r--r-- | configure.ac | 22 | ||||
l--------- | examples/el-decode-number.c | 1 | ||||
l--------- | examples/el-encode-number.c | 1 | ||||
l--------- | examples/el-pbinary.c | 1 | ||||
l--------- | examples/el-ts.c | 1 | ||||
-rw-r--r-- | examples/print-options.c | 12 | ||||
-rw-r--r-- | include/embedlog.h | 14 | ||||
-rw-r--r-- | man/el_option.3 | 20 | ||||
-rw-r--r-- | src/el-options.c | 11 | ||||
-rw-r--r-- | src/el-pbinary.c | 9 | ||||
-rw-r--r-- | src/el-private.h | 44 | ||||
-rw-r--r-- | src/el-ts.c | 120 | ||||
-rw-r--r-- | tst/test-el-options.c | 26 | ||||
-rw-r--r-- | tst/test-el-pbinary.c | 77 | ||||
-rw-r--r-- | tst/test-el-print.c | 144 |
15 files changed, 407 insertions, 96 deletions
diff --git a/configure.ac b/configure.ac index 30b7149..163be32 100644 --- a/configure.ac +++ b/configure.ac @@ -151,12 +151,29 @@ AC_ARG_ENABLE([timestamp], AS_IF([test "x$enable_timestamp" = "xyes"], [ AC_DEFINE([ENABLE_TIMESTAMP], [1], [Enable printing timestamp with log]) - AC_CHECK_FUNCS([clock time],, + AC_CHECK_FUNCS([clock time sprintf],, AC_MSG_ERROR(not found, needed by --enable-timestamp)) ]) ### +# --enable-fractions +# + + +AC_ARG_ENABLE([fractions], + AS_HELP_STRING([--enable-fractions], [Enable printing fractions with log]), + [], [enable_fractions="yes"]) + +AS_IF([test "x$enable_fractions" = "xyes"], +[ + AC_DEFINE([ENABLE_FRACTIONS], [1], [Enable printing fractions with log]) + AC_CHECK_FUNCS([sprintf],, + AC_MSG_ERROR(not found, needed by --enable-fractions)) +]) + + +### # --enable-realtime # @@ -234,7 +251,7 @@ AC_ARG_ENABLE([binary-logs], AM_CONDITIONAL([ENABLE_BINARY_LOGS], [test "x$enable_binary_logs" = "xyes"]) AS_IF([test "x$enable_binary_logs" = "xyes"], [ - AC_DEFINE([ENABLE_PREFIX], [1], [Enable printing binary logs]) + AC_DEFINE([ENABLE_BINARY_LOGS], [1], [Enable printing binary logs]) AC_CHECK_FUNCS([memcpy],, AC_MSG_ERROR(not found, needed by --enable-binary-logs)) ]) @@ -399,6 +416,7 @@ echo "print to file............... : $enable_out_file" echo "print to tty................ : $enable_out_tty" echo "print to custom routine..... : $enable_out_custom" echo "print timestamp enabled..... : $enable_timestamp" +echo "print fractions of seconds.. : $enable_fractions" echo "clock_realtime timestamp.... : $enable_realtime" echo "clock_monotonic timestamp... : $enable_monotonic" echo "clock() timestamp........... : $enable_clock" diff --git a/examples/el-decode-number.c b/examples/el-decode-number.c new file mode 120000 index 0000000..268c6a8 --- /dev/null +++ b/examples/el-decode-number.c @@ -0,0 +1 @@ +../src/el-decode-number.c
\ No newline at end of file diff --git a/examples/el-encode-number.c b/examples/el-encode-number.c new file mode 120000 index 0000000..c90c2c0 --- /dev/null +++ b/examples/el-encode-number.c @@ -0,0 +1 @@ +../src/el-encode-number.c
\ No newline at end of file diff --git a/examples/el-pbinary.c b/examples/el-pbinary.c new file mode 120000 index 0000000..3e46c13 --- /dev/null +++ b/examples/el-pbinary.c @@ -0,0 +1 @@ +../src/el-pbinary.c
\ No newline at end of file diff --git a/examples/el-ts.c b/examples/el-ts.c new file mode 120000 index 0000000..478d0cd --- /dev/null +++ b/examples/el-ts.c @@ -0,0 +1 @@ +../src/el-ts.c
\ No newline at end of file diff --git a/examples/print-options.c b/examples/print-options.c index 0ea88a4..2c619cb 100644 --- a/examples/print-options.c +++ b/examples/print-options.c @@ -32,15 +32,21 @@ int main(void) el_print(ELF, "if higher precision is needed we can use CLOCK_REALTIME"); el_option(EL_TS, EL_TS_SHORT); el_print(ELF, "we can also mix REALTIME with short format"); - el_option(EL_TS_USEC, 0); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); el_print(ELF, "and iff you don't need high resolution"); - el_print(ELF, "you can simply disable microseconds to save space!"); + el_print(ELF, "you can disable fractions of seconds to save space!"); + el_option(EL_TS_FRACT, EL_TS_FRACT_MS); + el_print(ELF, "or enable only millisecond resolution"); + el_option(EL_TS_FRACT, EL_TS_FRACT_US); + el_print(ELF, "or enable only microsecond resolution"); + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); + el_print(ELF, "or enable only nanosecond resolution"); el_option(EL_TS, EL_TS_LONG); el_option(EL_TS_TM, EL_TS_TM_CLOCK); el_print(ELF, "or long with clock() if you desire"); el_option(EL_TS, EL_TS_OFF); el_print(ELF, "no time information, if your heart desire it"); - el_option(EL_TS_USEC, 1); + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); el_option(EL_FINFO, 1); el_print(ELF, "log location is very usefull for debuging"); diff --git a/include/embedlog.h b/include/embedlog.h index ad69196..853e769 100644 --- a/include/embedlog.h +++ b/include/embedlog.h @@ -92,7 +92,7 @@ enum el_option EL_COLORS, EL_TS, EL_TS_TM, - EL_TS_USEC, + EL_TS_FRACT, EL_PRINT_LEVEL, EL_PRINT_NL, EL_FINFO, @@ -128,6 +128,16 @@ enum el_option_timestamp_timer EL_TS_TM_ERROR /* internal use only */ }; +enum el_option_timestamp_fractions +{ + EL_TS_FRACT_OFF, + EL_TS_FRACT_MS, + EL_TS_FRACT_US, + EL_TS_FRACT_NS, + + EL_TS_FRACT_ERROR /* internal use onsly */ +}; + typedef int (*el_custom_puts)(const char *s); struct el_options @@ -136,7 +146,7 @@ struct el_options unsigned int colors:1; unsigned int timestamp:2; unsigned int timestamp_timer:3; - unsigned int timestamp_useconds:1; + unsigned int timestamp_fractions:2; unsigned int print_log_level:1; unsigned int print_newline:1; unsigned int finfo:1; diff --git a/man/el_option.3 b/man/el_option.3 index 29bd814..c94a340 100644 --- a/man/el_option.3 +++ b/man/el_option.3 @@ -316,6 +316,26 @@ Specified timer source was not compiled in and is not available . RE .RE .PP +.BI "EL_TS_FRACT (" enum " " el_option_timestamp_fraction " fraction) +.RS +This options controls how to display fractions of seconds. +If high resolution is not needed or not supported, it's best to set this to +lowest resolution possible. +Table will show exacly what this is about. +Example uses long timestamp, interesting part is after date after dot '.'. +.PP +.EX + +-----------------+-------------------------------+ + | value | resulting timestamp string | + +-----------------+-------------------------------+ + | EL_TS_FRACT_OFF | 2018-04-17 22:02:57 | + | EL_TS_FRACT_MS | 2018-04-17 22:02:57.070 | + | EL_TS_FRACT_US | 2018-04-17 22:02:57.070518 | + | EL_TS_FRACT_NS | 2018-04-17 22:02:57.070518782 | + +-----------------+-------------------------------+ +.EE +.RE +.PP .BI "EL_PRINT_LEVEL (" int " print)" .RS If this is set to 1, each log will have log level information prefix in format diff --git a/src/el-options.c b/src/el-options.c index a4ba538..22b7e1a 100644 --- a/src/el-options.c +++ b/src/el-options.c @@ -217,13 +217,17 @@ static int el_vooption options->timestamp = value_int; return 0; - case EL_TS_USEC: +# if ENABLE_FRACTIONS + + case EL_TS_FRACT: value_int = va_arg(ap, int); - VALID(EINVAL, (value_int & ~1) == 0); + VALID(EINVAL, 0 <= value_int && value_int < EL_TS_FRACT_ERROR); - options->timestamp_useconds = value_int; + options->timestamp_fractions = value_int; return 0; +# endif /* ENABLE_FRACTIONS */ + case EL_TS_TM: value_int = va_arg(ap, int); VALID(EINVAL, 0 <= value_int && value_int < EL_TS_TM_ERROR); @@ -394,7 +398,6 @@ int el_oinit memset(options, 0, sizeof(struct el_options)); options->print_log_level = 1; options->print_newline = 1; - options->timestamp_useconds = 1; options->level = EL_INFO; options->level_current_msg = EL_DBG; options->file_sync_level = EL_FATAL; diff --git a/src/el-pbinary.c b/src/el-pbinary.c index a8f967b..ae636ca 100644 --- a/src/el-pbinary.c +++ b/src/el-pbinary.c @@ -102,19 +102,20 @@ static size_t el_flags { *buf = 0; +#if ENABLE_TIMESTAMP if (options->timestamp != EL_TS_OFF) { *buf |= FLAG_TS; +# if ENABLE_FRACTIONS /* * fraction of seconds can be printed only when timestamp is on */ - if (options->timestamp_useconds) - { - *buf |= FLAG_TS_FRACT_USEC << FLAG_TS_FRACT_SHIFT; - } + *buf |= options->timestamp_fractions << FLAG_TS_FRACT_SHIFT; +# endif } +#endif *buf |= level << FLAG_LEVEL_SHIFT; diff --git a/src/el-private.h b/src/el-private.h index 54c4a96..44e2262 100644 --- a/src/el-private.h +++ b/src/el-private.h @@ -104,30 +104,52 @@ extern struct el_options g_options; /* ========================================================================== length of long timestamp in a single log. Timestamp format is - [yyyy-mm-dd hh:mm:ss.uuuuuu] + [yyyy-mm-dd hh:mm:ss] - which is 28 bytes long + which is 21 bytes long ========================================================================== */ -#if ENABLE_TIMESTAMP -# define EL_PRE_TS_LONG_LEN 28 -#else -# define EL_PRE_TS_LONG_LEN 0 -#endif +#define EL_PRE_TS_LONG_LEN 21 /* ========================================================================== length of short timestamp in a single log. Timestamp format is - [ssssssssss.uuuuuu] + [ssssssssss] - which is 19 bytes long. This is maximum value for short timestamp, as it + which is 12 bytes long. This is maximum value for short timestamp, as it can be shorter. ========================================================================== */ -#define EL_PRE_TS_SHORT_LEN 19 +#define EL_PRE_TS_SHORT_LEN 12 + + +/* ========================================================================== + Size of the fractions of seconds, that is part after seconds like: + + [ssssssssss.fffffffff] + + or + + [yyyy-mm-dd hh:mm:ss.fffffffff] + ========================================================================== */ + + +#define EL_PRE_TS_FRACT_LEN 10 + + +/* ========================================================================== + Calculate what is the minimum needed length to hold longest timestamp + ========================================================================== */ + + +#if ENABLE_TIMESTAMP +# define EL_PRE_TS_MAX (EL_PRE_TS_LONG_LEN + EL_PRE_TS_FRACT_LEN) +#else +# define EL_PRE_TS_MAX 0 +#endif /* ========================================================================== @@ -183,7 +205,7 @@ extern struct el_options g_options; # define EL_PREFIX_LEN 0 #endif -#define EL_PRE_LEN (EL_PRE_TS_LONG_LEN + EL_PRE_FINFO_LEN + EL_PREFIX_LEN + \ +#define EL_PRE_LEN (EL_PRE_TS_MAX + EL_PRE_FINFO_LEN + EL_PREFIX_LEN + \ EL_PRE_LEVEL_LEN) diff --git a/src/el-ts.c b/src/el-ts.c index 949a39d..fc29e93 100644 --- a/src/el-ts.c +++ b/src/el-ts.c @@ -24,7 +24,7 @@ /* ========================================================================== - returns seconds and microseconds calculated from clock() function + returns seconds and nanoseconds calculated from clock() function ========================================================================== */ @@ -33,7 +33,7 @@ static void el_ts_clock ( time_t *s, /* seconds will be stored here */ - long *us /* microseconds will be stored here */ + long *ns /* nanoseconds will be stored here */ ) { clock_t clk; /* clock value */ @@ -42,14 +42,15 @@ static void el_ts_clock clk = clock(); *s = clk / CLOCKS_PER_SEC; - *us = clk % CLOCKS_PER_SEC; - *us *= 1000000 / CLOCKS_PER_SEC; + *ns = clk % CLOCKS_PER_SEC; /* [cps] */ + *ns *= (1000000L / CLOCKS_PER_SEC); /* [ms] */ + *ns *= 1000L; /* [ns] */ } #endif /* ENABLE_CLOCK */ #endif /* ENABLE_TIMESTAMP */ /* ========================================================================== - returns seconds and microseconds calculated from time() function. + returns seconds and nanoseconds calculated from time() function. ========================================================================== */ @@ -57,16 +58,16 @@ static void el_ts_clock static void el_ts_time ( time_t *s, /* seconds will be stored here */ - long *us /* microseconds will be stored here */ + long *ns /* nanoseconds will be stored here */ ) { - *s = (long)time(NULL); - *us = 0; + *s = time(NULL); + *ns = 0; } #endif /* ENABLE_TIMESTAMP */ /* ========================================================================== - returns seconds and microseconds calculated from clock_gettime function + returns seconds and nanoseconds calculated from clock_gettime function ========================================================================== */ @@ -75,7 +76,7 @@ static void el_ts_time static void el_ts_clock_gettime ( time_t *s, /* seconds will be stored here */ - long *us, /* microseconds will be stored here */ + long *ns, /* nanoseconds will be stored here */ clockid_t clkid /* clock id */ ) { @@ -85,7 +86,7 @@ static void el_ts_clock_gettime clock_gettime(clkid, &tp); *s = tp.tv_sec; - *us = tp.tv_nsec / 1000; + *ns = tp.tv_nsec; } #endif /* ENABLE_REALTIME || ENABLE_MONOTONIC */ #endif /* ENABLE_TIMESTAMP */ @@ -107,9 +108,9 @@ size_t el_timestamp ) { #if ENABLE_TIMESTAMP - time_t s; /* timestamp seconds */ - long us; /* timestamp microseconds */ - size_t tl; /* timestamp length */ + time_t s; /* timestamp seconds */ + long ns; /* timestamp nanoseconds */ + size_t tl; /* timestamp length */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ if (options->timestamp == EL_TS_OFF) @@ -122,7 +123,7 @@ size_t el_timestamp } /* - * first we get seconds and microseconds from proper timer + * first we get seconds and nanoseconds from proper timer */ switch (options->timestamp_timer) @@ -130,7 +131,7 @@ size_t el_timestamp # if ENABLE_REALTIME case EL_TS_TM_REALTIME: - el_ts_clock_gettime(&s, &us, CLOCK_REALTIME); + el_ts_clock_gettime(&s, &ns, CLOCK_REALTIME); break; # endif @@ -138,19 +139,19 @@ size_t el_timestamp # if ENABLE_MONOTONIC case EL_TS_TM_MONOTONIC: - el_ts_clock_gettime(&s, &us, CLOCK_MONOTONIC); + el_ts_clock_gettime(&s, &ns, CLOCK_MONOTONIC); break; # endif case EL_TS_TM_TIME: - el_ts_time(&s, &us); + el_ts_time(&s, &ns); break; # if ENABLE_CLOCK case EL_TS_TM_CLOCK: - el_ts_clock(&s, &us); + el_ts_clock(&s, &ns); break; # endif @@ -178,17 +179,41 @@ size_t el_timestamp /* * put encoded nano/micro/milli seconds in buf if enabled */ +# if ENABLE_FRACTIONS - if (options->timestamp_useconds) + switch (options->timestamp_fractions) { -# ifdef LLONG_MAX - tl += el_encode_number((unsigned long long)us, buf + tl); -# else - tl += el_encode_number((unsigned long)us, buf + tl); -# endif - } + case EL_TS_FRACT_OFF: + /* + * we don't add fractions, so simply return what has been + * already stored (only seconds) + */ + + return tl; + case EL_TS_FRACT_MS: + tl += el_encode_number(ns / 1000000, buf + tl); + return tl; + + case EL_TS_FRACT_US: + tl += el_encode_number(ns / 1000, buf + tl); + return tl; + + case EL_TS_FRACT_NS: + tl += el_encode_number(ns, buf + tl); + return tl; + + default: + /* + * something went somehere seriously wrong, act like no + * timestamp has been configured + */ + + return 0; + } +#else /* ENABLE_FRACTIONS */ return tl; +#endif /* ENABLE_FRACTIONS */ } else { @@ -226,24 +251,47 @@ size_t el_timestamp # endif } - if (options->timestamp_useconds) - { - tl += sprintf(buf + tl, ".%06ld]", us); - } - else + /* + * if requested, add proper fractions of seconds + */ +# if ENABLE_FRACTIONS + switch (options->timestamp_fractions) { + case EL_TS_FRACT_OFF: /* - * if micro seconds are not printed we simply add ending ']' + * we don't add fractions, so simply close opening bracker '[' */ - buf[tl] = ']'; - tl++; - } + buf[tl++] = ']'; + return tl; + + case EL_TS_FRACT_MS: + tl += sprintf(buf + tl, ".%03ld]", ns / 1000000); + return tl; + case EL_TS_FRACT_US: + tl += sprintf(buf + tl, ".%06ld]", ns / 1000); + return tl; + + case EL_TS_FRACT_NS: + tl += sprintf(buf + tl, ".%09ld]", ns); + return tl; + + default: + /* + * something went somehere seriously wrong, act like no + * timestamp has been configured + */ + + return 0; + } +# else /* ENABLE_FRACTIONS */ + buf[tl++] = ']'; return tl; +# endif /* ENABLE_FRACTIONS */ } -#else /* ENABLE_TIMESTAMP */ +#else /* ENABLE_TIMESTAMP */ return 0; #endif /* ENABLE_TIMESTAMP */ } diff --git a/tst/test-el-options.c b/tst/test-el-options.c index 47c8fee..44e7668 100644 --- a/tst/test-el-options.c +++ b/tst/test-el-options.c @@ -99,7 +99,7 @@ static void options_init(void) default_options.colors = 0; default_options.timestamp = EL_TS_OFF; default_options.timestamp_timer = EL_TS_TM_TIME; - default_options.timestamp_useconds = 1; + default_options.timestamp_fractions = EL_TS_FRACT_OFF; default_options.print_log_level = 1; default_options.print_newline = 1; default_options.custom_puts = NULL; @@ -402,6 +402,29 @@ static void options_opt_timestamp_timer(void) } +static void options_opt_timestamp_fraction(void) +{ + int i; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + + for (i = EL_TS_FRACT_OFF; i != EL_TS_FRACT_ERROR; ++i) + { +#if ENABLE_FRACTIONS + mt_fok(el_option(EL_TS_FRACT, i)); + mt_fail(g_options.timestamp_fractions == i); +#else + mt_ferr(el_option(EL_TS_FRACT, i), ENOSYS); +#endif + } + +#if ENABLE_FRACTIONS + mt_ferr(el_option(EL_TS_FRACT, i), EINVAL); +#else + mt_ferr(el_option(EL_TS_FRACT, i), ENOSYS); +#endif +} + /* ========================================================================== ========================================================================== */ @@ -475,6 +498,7 @@ void el_options_test_group(void) mt_run(options_opt_colors); mt_run(options_opt_timestamp); mt_run(options_opt_timestamp_timer); + mt_run(options_opt_timestamp_fraction); mt_run(options_ooption_test); mt_run(options_einval); mt_run(options_prefix); diff --git a/tst/test-el-pbinary.c b/tst/test-el-pbinary.c index 23e0620..a193297 100644 --- a/tst/test-el-pbinary.c +++ b/tst/test-el-pbinary.c @@ -189,7 +189,7 @@ static int pbinary_check(void) * fraction of seconds checks makes sense only when ts is set */ - if (g_options.timestamp_useconds) + if (g_options.timestamp_fractions) { unsigned long long usec; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ @@ -371,7 +371,7 @@ static void test_prepare(void) el_option(EL_PREFIX, NULL); el_option(EL_COLORS, 0); el_option(EL_TS, EL_TS_OFF); - el_option(EL_TS_USEC, 0); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); el_option(EL_PRINT_LEVEL, 0); el_option(EL_FROTATE_NUMBER, 0); el_option(EL_FILE_SYNC_EVERY, 1024); @@ -431,7 +431,52 @@ static void pbinary_simple_multiple_message(void) static void pbinary_ts_without_fractions(void) { - el_option(EL_TS_USEC, 1); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); + add_log(EL_FATAL, d1, 1); + add_log(EL_FATAL, d2, 2); + add_log(EL_FATAL, d5, 5); + add_log(EL_FATAL, d8, 8); + mt_fok(pbinary_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void pbinary_ts_fractions_ms(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_MS); + add_log(EL_FATAL, d1, 1); + add_log(EL_FATAL, d2, 2); + add_log(EL_FATAL, d5, 5); + add_log(EL_FATAL, d8, 8); + mt_fok(pbinary_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void pbinary_ts_fractions_us(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_US); + add_log(EL_FATAL, d1, 1); + add_log(EL_FATAL, d2, 2); + add_log(EL_FATAL, d5, 5); + add_log(EL_FATAL, d8, 8); + mt_fok(pbinary_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void pbinary_ts_fractions_ns(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); add_log(EL_FATAL, d1, 1); add_log(EL_FATAL, d2, 2); add_log(EL_FATAL, d5, 5); @@ -472,9 +517,9 @@ static void pbinary_timestamp_long(void) ========================================================================== */ -static void pbinary_timestamp_short_no_useconds(void) +static void pbinary_timestamp_short_no_fractions(void) { - el_option(EL_TS_USEC, 0); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); el_option(EL_TS, EL_TS_SHORT); add_log(EL_FATAL, d2, 2); add_log(EL_FATAL, d5, 5); @@ -487,9 +532,9 @@ static void pbinary_timestamp_short_no_useconds(void) ========================================================================== */ -static void pbinary_timestamp_long_no_useconds(void) +static void pbinary_timestamp_long_no_fractions(void) { - el_option(EL_TS_USEC, 0); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); el_option(EL_TS, EL_TS_LONG); add_log(EL_FATAL, d2, 2); add_log(EL_FATAL, d5, 5); @@ -533,18 +578,18 @@ static void pbinary_mix_of_everything(void) int finfo; int colors; int prefix; - int usec; + int fract; int nl; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + for (fract = EL_TS_FRACT_OFF; fract != EL_TS_FRACT_ERROR; ++fract) for (level = EL_FATAL; level <= EL_DBG; ++level) for (timestamp = EL_TS_OFF; timestamp != EL_TS_ERROR; ++timestamp) for (printlevel = 0; printlevel <= 1; ++printlevel) for (finfo = 0; finfo <= 1; ++finfo) for (colors = 0; colors <= 1; ++colors) for (prefix = 0; prefix <= 1; ++prefix) - for (usec = 0; usec <= 1; ++usec) for (nl = 0; nl <= 1; ++nl) { test_prepare(); @@ -555,6 +600,7 @@ static void pbinary_mix_of_everything(void) el_option(EL_FINFO, finfo); el_option(EL_COLORS, colors); el_option(EL_PREFIX, prefix ? "prefix" : NULL); + el_option(EL_TS_FRACT, fract); add_log(EL_FATAL, d1, 1); add_log(EL_ALERT, d1, 1); @@ -627,7 +673,7 @@ static void pbinary_truncate(void) } el_option(EL_TS, EL_TS_LONG); - el_option(EL_TS_USEC, 1); + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); add_log(EL_INFO, msg, sizeof(msg)); /* @@ -650,6 +696,7 @@ static void pbinary_truncate(void) void el_pbinary_test_group(void) { +#if ENABLE_BINARY_LOGS mt_run(pbinary_different_clocks); mt_run(pbinary_mix_of_everything); @@ -661,10 +708,14 @@ void el_pbinary_test_group(void) mt_run(pbinary_ts_without_fractions); mt_run(pbinary_timestamp_short); mt_run(pbinary_timestamp_long); - mt_run(pbinary_timestamp_short_no_useconds); - mt_run(pbinary_timestamp_long_no_useconds); + mt_run(pbinary_ts_fractions_ms); + mt_run(pbinary_ts_fractions_us); + mt_run(pbinary_ts_fractions_ns); + mt_run(pbinary_timestamp_short_no_fractions); + mt_run(pbinary_timestamp_long_no_fractions); mt_run(pbinary_with_no_output_available); mt_run(pbinary_level_not_high_enough); mt_run(pbinary_null); mt_run(pbinary_truncate); -}
\ No newline at end of file +#endif +} diff --git a/tst/test-el-print.c b/tst/test-el-print.c index f8d23c3..2af6c92 100644 --- a/tst/test-el-print.c +++ b/tst/test-el-print.c @@ -19,6 +19,8 @@ #include <ctype.h> #include <errno.h> #include <libgen.h> +#include <stdint.h> +#include <math.h> #include "mtest.h" #include "stdlib.h" @@ -211,11 +213,11 @@ static int print_check(void) IS_DIGIT(); IS_DIGIT(); - if (g_options.timestamp_useconds) + if (g_options.timestamp_fractions) { IS_CHAR('.'); - for (i = 0; i != 6; ++i) + for (i = 0; i != 3 * g_options.timestamp_fractions; ++i) { IS_DIGIT(); } @@ -228,7 +230,7 @@ static int print_check(void) { IS_CHAR('['); - if (g_options.timestamp_useconds) + if (g_options.timestamp_fractions) { while (*msg != '.') { @@ -237,7 +239,7 @@ static int print_check(void) ++msg; /* skip the '.' character */ - for (i = 0; i != 6; ++i) + for (i = 0; i != 3 * g_options.timestamp_fractions; ++i) { IS_DIGIT(); } @@ -635,12 +637,12 @@ static void print_timestamp_long(void) ========================================================================== */ -static void print_timestamp_short_no_useconds(void) +static void print_timestamp_short_no_fractions(void) { - el_option(EL_TS_USEC, 0); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); el_option(EL_TS, EL_TS_SHORT); - add_log(ELF, "long timestamp without usec first"); - add_log(ELF, "long timestamp without usec second"); + add_log(ELF, "first meaningless message"); + add_log(ELF, "second stupid log"); mt_fok(print_check()); } @@ -649,12 +651,96 @@ static void print_timestamp_short_no_useconds(void) ========================================================================== */ -static void print_timestamp_long_no_useconds(void) +static void print_timestamp_long_no_fractions(void) { - el_option(EL_TS_USEC, 0); + el_option(EL_TS_FRACT, EL_TS_FRACT_OFF); el_option(EL_TS, EL_TS_LONG); - add_log(ELF, "long timestamp without usec first"); - add_log(ELF, "long timestamp without usec second"); + add_log(ELF, "they don't even care"); + add_log(ELF, "what I put in here"); + mt_fok(print_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void print_timestamp_short_fractions_ms(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_MS); + el_option(EL_TS, EL_TS_SHORT); + add_log(ELF, "first meaningless message"); + add_log(ELF, "second stupid log"); + mt_fok(print_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void print_timestamp_long_fractions_ms(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_MS); + el_option(EL_TS, EL_TS_LONG); + add_log(ELF, "they don't even care"); + add_log(ELF, "what I put in here"); + mt_fok(print_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void print_timestamp_short_fractions_us(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_US); + el_option(EL_TS, EL_TS_SHORT); + add_log(ELF, "first meaningless message"); + add_log(ELF, "second stupid log"); + mt_fok(print_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void print_timestamp_long_fractions_us(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_US); + el_option(EL_TS, EL_TS_LONG); + add_log(ELF, "they don't even care"); + add_log(ELF, "what I put in here"); + mt_fok(print_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void print_timestamp_short_fractions_ns(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); + el_option(EL_TS, EL_TS_SHORT); + add_log(ELF, "first meaningless message"); + add_log(ELF, "second stupid log"); + mt_fok(print_check()); +} + + +/* ========================================================================== + ========================================================================== */ + + +static void print_timestamp_long_fractions_ns(void) +{ + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); + el_option(EL_TS, EL_TS_LONG); + add_log(ELF, "they don't even care"); + add_log(ELF, "what I put in here"); mt_fok(print_check()); } @@ -706,18 +792,18 @@ static void print_mix_of_everything(void) int finfo; int colors; int prefix; - int usec; + int fract; int nl; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - for (level = EL_FATAL; level <= EL_DBG; ++level) + for (fract = EL_TS_FRACT_OFF; fract != EL_TS_FRACT_ERROR; ++fract) for (timestamp = EL_TS_OFF; timestamp != EL_TS_ERROR; ++timestamp) + for (level = EL_FATAL; level <= EL_DBG; ++level) for (printlevel = 0; printlevel <= 1; ++printlevel) - for (finfo = 0; finfo <= 1; ++finfo) for (colors = 0; colors <= 1; ++colors) for (prefix = 0; prefix <= 1; ++prefix) - for (usec = 0; usec <= 1; ++usec) + for (finfo = 0; finfo <= 1; ++finfo) for (nl = 0; nl <= 1; ++nl) { test_prepare(); @@ -845,14 +931,26 @@ static void print_truncate_with_date(void) static void print_truncate_with_all_options(void) { - char msg[EL_LOG_MAX + 3]; + char msg[EL_LOG_MAX + 3]; + char finfo[EL_FLEN_MAX + 1]; + char prefix[EL_PREFIX_MAX + 1]; + size_t fline; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ el_option(EL_TS, EL_TS_LONG); + el_option(EL_TS_FRACT, EL_TS_FRACT_NS); + el_option(EL_TS_TM, EL_TS_TM_REALTIME); el_option(EL_FINFO, 1); + el_option(EL_COLORS, 1); + el_option(EL_PREFIX, prefix); el_option(EL_PRINT_LEVEL, 1); memset(msg, 'a', sizeof(msg)); + memset(finfo, 'b', sizeof(finfo)); + memset(prefix, 'c', sizeof(prefix)); + finfo[sizeof(finfo) - 1] = '\0'; + prefix[sizeof(prefix) - 1] = '\0'; + fline = (size_t)pow(10, EL_PRE_FINFO_LINE_MAX_LEN) - 1; msg[sizeof(msg) - 1] = '\0'; msg[sizeof(msg) - 2] = '3'; msg[sizeof(msg) - 3] = '2'; @@ -860,7 +958,7 @@ static void print_truncate_with_all_options(void) msg[sizeof(msg) - 4] = '0'; add_log(ELI, "not truncated"); - add_log(ELI, msg); + add_log(finfo, fline, EL_FATAL, msg); msg[sizeof(msg) - 3] = '\0'; @@ -1001,8 +1099,14 @@ void el_print_test_group(void) mt_run(print_custom_log_level); mt_run(print_timestamp_short); mt_run(print_timestamp_long); - mt_run(print_timestamp_short_no_useconds); - mt_run(print_timestamp_long_no_useconds); + mt_run(print_timestamp_short_no_fractions); + mt_run(print_timestamp_long_no_fractions); + mt_run(print_timestamp_short_fractions_ms); + mt_run(print_timestamp_long_fractions_ms); + mt_run(print_timestamp_short_fractions_us); + mt_run(print_timestamp_long_fractions_us); + mt_run(print_timestamp_short_fractions_ns); + mt_run(print_timestamp_long_fractions_ns); mt_run(print_finfo); mt_run(print_too_long_print_truncate); mt_run(print_too_long_print_truncate_no_newline); |