aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2018-04-12 23:11:47 +0200
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2018-04-12 23:11:47 +0200
commit52ee08352922701278a9204e31f6325325cf69d4 (patch)
treec6fc86b1df4679345c3da65ce754f6ca3fddb41c
parenta6a57b244e9abfc334a3a15dcff61fc10d5376af (diff)
downloadembedlog-52ee08352922701278a9204e31f6325325cf69d4.tar.gz
embedlog-52ee08352922701278a9204e31f6325325cf69d4.tar.bz2
embedlog-52ee08352922701278a9204e31f6325325cf69d4.zip
add: possibility to remove micro seconds from log
-rw-r--r--examples/print-options.c4
-rw-r--r--include/embedlog.h2
-rw-r--r--src/el-options.c6
-rw-r--r--src/el-print.c14
-rw-r--r--tst/test-el-options.c35
-rw-r--r--tst/test-el-print.c67
6 files changed, 101 insertions, 27 deletions
diff --git a/examples/print-options.c b/examples/print-options.c
index 6a0b18f..97f5651 100644
--- a/examples/print-options.c
+++ b/examples/print-options.c
@@ -32,11 +32,15 @@ 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_print(ELF, "and iff you don't need high resolution");
+ el_print(ELF, "you can simply disable microseconds to save space!");
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_FINFO, 1);
el_print(ELF, "log location is very usefull for debuging");
diff --git a/include/embedlog.h b/include/embedlog.h
index 1cc72b6..e4b4f84 100644
--- a/include/embedlog.h
+++ b/include/embedlog.h
@@ -92,6 +92,7 @@ enum el_option
EL_COLORS,
EL_TS,
EL_TS_TM,
+ EL_TS_USEC,
EL_PRINT_LEVEL,
EL_FINFO,
EL_CUSTOM_PUTS,
@@ -134,6 +135,7 @@ struct el_options
int colors;
int timestamp;
int timestamp_timer;
+ int timestamp_useconds;
int print_log_level;
int serial_fd;
diff --git a/src/el-options.c b/src/el-options.c
index 514972e..a427a01 100644
--- a/src/el-options.c
+++ b/src/el-options.c
@@ -212,10 +212,15 @@ static int el_vooption
options->timestamp = value_int;
return 0;
+ case EL_TS_USEC:
+ value_int = va_arg(ap, int);
+ options->timestamp_useconds = value_int;
+
case EL_TS_TM:
value_int = va_arg(ap, int);
VALID(EINVAL, 0 <= value_int && value_int < EL_TS_TM_ERROR);
+
# if ENABLE_REALTIME == 0
VALID(ENODEV, value_int != EL_TS_TM_REALTIME);
# endif
@@ -380,6 +385,7 @@ int el_oinit
memset(options, 0, sizeof(struct el_options));
options->print_log_level = 1;
+ options->timestamp_useconds = 1;
options->level = EL_INFO;
options->serial_fd = -1;
options->file_sync_every = 32768;
diff --git a/src/el-print.c b/src/el-print.c
index 82eb47b..6d4e4f5 100644
--- a/src/el-print.c
+++ b/src/el-print.c
@@ -363,7 +363,19 @@ static size_t el_timestamp
#endif
}
- tl += sprintf(buf + tl, ".%06ld]", us);
+ if (options->timestamp_useconds)
+ {
+ tl += sprintf(buf + tl, ".%06ld]", us);
+ }
+ else
+ {
+ /*
+ * if micro seconds are not printed we simply add ending ']'
+ */
+
+ buf[tl] = ']';
+ tl++;
+ }
return tl;
diff --git a/tst/test-el-options.c b/tst/test-el-options.c
index 8e6976a..af21763 100644
--- a/tst/test-el-options.c
+++ b/tst/test-el-options.c
@@ -93,23 +93,24 @@ static void options_init(void)
memset(&default_options, 0, sizeof(default_options));
- default_options.outputs = 0;
- default_options.level = EL_INFO;
- default_options.colors = 0;
- default_options.timestamp = EL_TS_OFF;
- default_options.timestamp_timer = EL_TS_TM_TIME;
- default_options.print_log_level = 1;
- default_options.custom_puts = NULL;
- default_options.serial_fd = -1;
-
- default_options.finfo = 0;
- default_options.frotate_number = 0;
- default_options.fcurrent_rotate = 0;
- default_options.frotate_size = 0;
- default_options.fpos = 0;
- default_options.file = NULL;
- default_options.file_sync_every = 32768;
- default_options.fname = NULL;
+ default_options.outputs = 0;
+ default_options.level = EL_INFO;
+ 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.print_log_level = 1;
+ default_options.custom_puts = NULL;
+ default_options.serial_fd = -1;
+
+ default_options.finfo = 0;
+ default_options.frotate_number = 0;
+ default_options.fcurrent_rotate = 0;
+ default_options.frotate_size = 0;
+ default_options.fpos = 0;
+ default_options.file = NULL;
+ default_options.file_sync_every = 32768;
+ default_options.fname = NULL;
mt_fail(el_oinit(&options) == 0);
mt_fail(memcmp(&options, &default_options, sizeof(options)) == 0);
diff --git a/tst/test-el-print.c b/tst/test-el-print.c
index e17c786..b08e01b 100644
--- a/tst/test-el-print.c
+++ b/tst/test-el-print.c
@@ -211,29 +211,46 @@ static int print_check(void)
IS_CHAR(':');
IS_DIGIT();
IS_DIGIT();
- IS_CHAR('.');
- for (i = 0; i != 6; ++i)
+ if (g_options.timestamp_useconds)
{
- IS_DIGIT();
+ IS_CHAR('.');
+
+ for (i = 0; i != 6; ++i)
+ {
+ IS_DIGIT();
+ }
}
+
IS_CHAR(']');
}
else if (g_options.timestamp == EL_TS_SHORT)
{
IS_CHAR('[');
- while (*msg != '.' )
+
+ if (g_options.timestamp_useconds)
{
- IS_DIGIT();
- }
+ while (*msg != '.')
+ {
+ IS_DIGIT();
+ }
- ++msg; /* skip the '.' character */
+ ++msg; /* skip the '.' character */
- for (i = 0; i != 6; ++i)
+ for (i = 0; i != 6; ++i)
+ {
+ IS_DIGIT();
+ }
+ }
+ else
{
- IS_DIGIT();
+ while (*msg != ']')
+ {
+ IS_DIGIT();
+ }
}
+
IS_CHAR(']');
}
else if (g_options.timestamp == EL_TS_OFF)
@@ -601,6 +618,34 @@ static void print_timestamp_long(void)
========================================================================== */
+static void print_timestamp_short_no_useconds(void)
+{
+ el_option(EL_TS_USEC, 0);
+ el_option(EL_TS, EL_TS_SHORT);
+ add_log(ELF, "long timestamp without usec first");
+ add_log(ELF, "long timestamp without usec second");
+ mt_fok(print_check());
+}
+
+
+/* ==========================================================================
+ ========================================================================== */
+
+
+static void print_timestamp_long_no_useconds(void)
+{
+ el_option(EL_TS_USEC, 0);
+ el_option(EL_TS, EL_TS_LONG);
+ add_log(ELF, "long timestamp without usec first");
+ add_log(ELF, "long timestamp without usec second");
+ mt_fok(print_check());
+}
+
+
+/* ==========================================================================
+ ========================================================================== */
+
+
static void print_finfo(void)
{
el_option(EL_FINFO, 1);
@@ -644,6 +689,7 @@ static void print_mix_of_everything(void)
int finfo;
int colors;
int prefix;
+ int usec;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@@ -653,6 +699,7 @@ static void print_mix_of_everything(void)
for (finfo = 0; finfo <= 1; ++finfo)
for (colors = 0; colors <= 1; ++colors)
for (prefix = 0; prefix <= 1; ++prefix)
+ for (usec = 0; usec <= 1; ++usec);
{
test_prepare();
el_option(EL_LEVEL, level);
@@ -899,6 +946,8 @@ 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_finfo);
mt_run(print_too_long_print_truncate);
mt_run(print_truncate_with_date);