Commit 02eed16b authored by Dimitris Lampridis's avatar Dimitris Lampridis Committed by Dimitris Lampridis

sw: various smaller fixes to logging

parent 2d0a7df8
......@@ -57,7 +57,8 @@ enum {
`define WRTD_ACTION_LOG 'h20
typedef enum uint32_t {
WRTD_LOG_MSG_EV_GENERATED = 1,
WRTD_LOG_MSG_EV_NONE = 0,
WRTD_LOG_MSG_EV_GENERATED,
WRTD_LOG_MSG_EV_CONSUMED,
WRTD_LOG_MSG_EV_DISCARDED,
WRTD_LOG_MSG_EV_NETWORK
......
......@@ -875,7 +875,10 @@ wrtd_status wrtd_get_attr_tstamp(wrtd_dev *wrtd,
* the buffer_size < 0 case, which produces an error instead of allowing a potential
* buffer overflow.
*
* Event log entries use the format `log tstamp|event tstamp|log type|log reason`, where:
* Event log entries use the format `id|seq|log tstamp|event tstamp|log type|log reason`, where:
* - `id` is the Event ID.
* - `seq` is a sequence number for the rule that generated this Event. Each Event generated by
* the Rule causes the sequence number to increase by one. Limited to 4 digits.
* - `log tstamp` is the timestamp of when the event was logged. This can be zero (1/1/1970)
* for some time-critical events which favour speed over logging information.
* - `event tstamp` is the timestamp of when the event happened.
......@@ -921,6 +924,7 @@ wrtd_status wrtd_get_next_event_log_entry(wrtd_dev *wrtd,
struct wrtd_log_entry log;
wrtd_status status;
struct tm *tm;
time_t t;
if(wrtd == NULL){
return WRTD_ERROR_NOT_INITIALIZED;
......@@ -961,66 +965,87 @@ wrtd_status wrtd_get_next_event_log_entry(wrtd_dev *wrtd,
char *sptr = log_entry;
tm = gmtime((const time_t *)&log.ts.seconds);
int ret;
ret = snprintf(sptr, count, "Id:%-16s|Seq:%04d|",
log.event.id, log.event.seq % 10000);
if (ret < 0)
return wrtd_return_error(
wrtd, WRTD_ERROR_UNEXPECTED_RESPONSE,
"%s: Unexpected response from device",
__func__);
if (ret > count)
return WRTD_LOG_ENTRY_SIZE;
count -= ret;
sptr += ret;
t = log.ts.seconds;
tm = gmtime(&t);
if (tm == NULL)
return wrtd_return_error(
wrtd, WRTD_ERROR_UNEXPECTED_RESPONSE,
"%s: Unexpected response from device",
__func__);
/* Needs 20 characters */
strftime(sptr, count, "%F,%T.", tm);
count -= 20;
if (count < 0)
ret = strftime(sptr, count, "%F,%T.", tm);
if (ret == 0)
return WRTD_LOG_ENTRY_SIZE;
sptr += 20;
/* Needs 16 characters */
snprintf(sptr, count, "%03lu.%03lu.%03lu+%03d|",
(log.ts.ns / (1000L * 1000)),
(log.ts.ns / 1000L) % 1000,
log.ts.ns % 1000UL,
log.ts.frac >> (32 - 9));
count -= 16;
if (count < 0)
count -= ret;
sptr += ret;
ret = snprintf(sptr, count, "%03lu.%03lu.%03lu+%03d|",
(log.ts.ns / (1000L * 1000)),
(log.ts.ns / 1000L) % 1000,
log.ts.ns % 1000UL,
log.ts.frac >> (32 - 9));
if (ret < 0)
return wrtd_return_error(
wrtd, WRTD_ERROR_UNEXPECTED_RESPONSE,
"%s: Unexpected response from device",
__func__);
if (ret > count)
return WRTD_LOG_ENTRY_SIZE;
sptr += 16;
count -= ret;
sptr += ret;
tm = gmtime((const time_t *)&log.event.ts.seconds);
t = log.event.ts.seconds;
tm = gmtime(&t);
if (tm == NULL)
return wrtd_return_error(
wrtd, WRTD_ERROR_UNEXPECTED_RESPONSE,
"%s: Unexpected response from device",
__func__);
/* Needs 20 characters */
strftime(sptr, count, "%F,%T.", tm);
count -= 20;
if (count < 0)
ret = strftime(sptr, count, "%F,%T.", tm);
if (ret == 0)
return WRTD_LOG_ENTRY_SIZE;
sptr += 20;
/* Needs 16 characters */
snprintf(sptr, count, "%03lu.%03lu.%03lu+%03d|",
(log.event.ts.ns / (1000L * 1000)),
(log.event.ts.ns / 1000L) % 1000,
log.event.ts.ns % 1000UL,
log.event.ts.frac >> (32 - 9));
count -= 16;
if (count < 0)
count -= ret;
sptr += ret;
ret = snprintf(sptr, count, "%03lu.%03lu.%03lu+%03d|",
(log.event.ts.ns / (1000L * 1000)),
(log.event.ts.ns / 1000L) % 1000,
log.event.ts.ns % 1000UL,
log.event.ts.frac >> (32 - 9));
if (ret < 0)
return wrtd_return_error(
wrtd, WRTD_ERROR_UNEXPECTED_RESPONSE,
"%s: Unexpected response from device",
__func__);
if (ret > count)
return WRTD_LOG_ENTRY_SIZE;
sptr += 16;
count -= ret;
sptr += ret;
/* Needs 18 characters */
switch(log.type) {
case WRTD_LOG_MSG_EV_GENERATED:
if (log.reason == WRTD_LOG_GENERATED_ALARM)
snprintf(sptr, count, "GENERATED|ALARM ");
ret = snprintf(sptr, count, "GENERATED|ALARM ");
else if ((log.reason >= WRTD_LOG_GENERATED_DEVICE_0) &&
(log.reason <= WRTD_LOG_GENERATED_DEVICE_7) &&
(log.reason % 8 == 0))
snprintf(sptr, count, "GENERATED|DEVICE_%d",
(log.reason - 8) / 8);
ret = snprintf(sptr, count, "GENERATED|DEVICE_%d",
(log.reason - 8) / 8);
else
return wrtd_return_error(
wrtd, WRTD_ERROR_UNKNOWN_LOG_TYPE,
......@@ -1029,9 +1054,9 @@ wrtd_status wrtd_get_next_event_log_entry(wrtd_dev *wrtd,
break;
case WRTD_LOG_MSG_EV_CONSUMED:
if (log.reason == WRTD_LOG_CONSUMED_START)
snprintf(sptr, count, "CONSUMED |START ");
ret = snprintf(sptr, count, "CONSUMED |START ");
else if (log.reason == WRTD_LOG_CONSUMED_DONE)
snprintf(sptr, count, "CONSUMED |DONE ");
ret = snprintf(sptr, count, "CONSUMED |DONE ");
else
return wrtd_return_error(
wrtd, WRTD_ERROR_UNKNOWN_LOG_TYPE,
......@@ -1040,13 +1065,13 @@ wrtd_status wrtd_get_next_event_log_entry(wrtd_dev *wrtd,
break;
case WRTD_LOG_MSG_EV_DISCARDED:
if (log.reason == WRTD_LOG_DISCARD_NO_SYNC)
snprintf(sptr, count, "DISCARDED|NO SYNC ");
ret = snprintf(sptr, count, "DISCARDED|NO SYNC ");
else if (log.reason == WRTD_LOG_DISCARD_HOLDOFF)
snprintf(sptr, count, "DISCARDED|HOLD OFF");
ret = snprintf(sptr, count, "DISCARDED|HOLD OFF");
else if (log.reason == WRTD_LOG_DISCARD_TIMEOUT)
snprintf(sptr, count, "DISCARDED|TIME OUT");
ret = snprintf(sptr, count, "DISCARDED|TIME OUT");
else if (log.reason == WRTD_LOG_DISCARD_OVERFLOW)
snprintf(sptr, count, "DISCARDED|OVERFLOW");
ret = snprintf(sptr, count, "DISCARDED|OVERFLOW");
else
return wrtd_return_error(
wrtd, WRTD_ERROR_UNKNOWN_LOG_TYPE,
......@@ -1055,9 +1080,9 @@ wrtd_status wrtd_get_next_event_log_entry(wrtd_dev *wrtd,
break;
case WRTD_LOG_MSG_EV_NETWORK:
if (log.reason == WRTD_LOG_NETWORK_TX)
snprintf(sptr, count, "NETWORK |TX ");
ret = snprintf(sptr, count, "NETWORK |TX ");
else if (log.reason == WRTD_LOG_NETWORK_RX)
snprintf(sptr, count, "NETWORK |RX ");
ret = snprintf(sptr, count, "NETWORK |RX ");
else
return wrtd_return_error(
wrtd, WRTD_ERROR_UNKNOWN_LOG_TYPE,
......@@ -1072,9 +1097,12 @@ wrtd_status wrtd_get_next_event_log_entry(wrtd_dev *wrtd,
return WRTD_ERROR_UNKNOWN_LOG_TYPE;
}
/* Last check, therefore also account for null termination */
count -= 19;
if (count < 0)
if (ret < 0)
return wrtd_return_error(
wrtd, WRTD_ERROR_UNEXPECTED_RESPONSE,
"%s: Unexpected response from device",
__func__);
if (ret > count)
return WRTD_LOG_ENTRY_SIZE;
return WRTD_SUCCESS;
......
......@@ -225,7 +225,7 @@ typedef enum wrtd_attr {
#define WRTD_GLOBAL_REP_CAP_ID "WGRCI"
/** Size (in characters, including null termination) of an event log enty. */
#define WRTD_LOG_ENTRY_SIZE 91
#define WRTD_LOG_ENTRY_SIZE 116
/* ------------------------------------------------------------------- */
/* Function prototypes for the official WRTD API. Documented in wrtd.c */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment