From d2200fc25e3ae0c04e349a80b979476ee156d215 Mon Sep 17 00:00:00 2001 From: "Narasimhaiah Suprathik (RBEI/ECF3)" Date: Mon, 7 Dec 2020 16:27:57 +0530 Subject: lib: Add MaxFileSize handling Signed-Off By: Saya Sugiura --- src/examples/dlt-example-user.c | 19 ++++++++++-- src/lib/dlt_user.c | 64 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/examples/dlt-example-user.c b/src/examples/dlt-example-user.c index 6ede605..8c79c42 100644 --- a/src/examples/dlt-example-user.c +++ b/src/examples/dlt-example-user.c @@ -98,6 +98,7 @@ void usage() printf("Options:\n"); printf(" -d delay Milliseconds to wait between sending messages (Default: 500)\n"); printf(" -f filename Use local log file instead of sending to daemon\n"); + printf(" -S filesize Set maximum size of local log file (Default: UINT_MAX)\n"); printf(" -n count Number of messages to be generated (Default: 10)\n"); printf(" -g Switch to non-verbose mode (Default: verbose mode)\n"); printf(" -a Enable local printing of DLT messages (Default: disabled)\n"); @@ -130,6 +131,7 @@ int main(int argc, char *argv[]) #endif /* DLT_TEST_ENABLE */ char *dvalue = 0; char *fvalue = 0; + unsigned int filesize = 0; char *nvalue = 0; char *mvalue = 0; char *message = 0; @@ -152,10 +154,10 @@ int main(int argc, char *argv[]) opterr = 0; #ifdef DLT_TEST_ENABLE - while ((c = getopt (argc, argv, "vgakcd:f:n:m:z:r:s:l:t:A:C:")) != -1) + while ((c = getopt (argc, argv, "vgakcd:f:S:n:m:z:r:s:l:t:A:C:")) != -1) #else - while ((c = getopt (argc, argv, "vgakd:f:n:m:l:r:t:A:C:")) != -1) + while ((c = getopt (argc, argv, "vgakd:f:S:n:m:l:r:t:A:C:")) != -1) #endif /* DLT_TEST_ENABLE */ { switch (c) { @@ -201,6 +203,11 @@ int main(int argc, char *argv[]) fvalue = optarg; break; } + case 'S': + { + filesize = atoi(optarg); + break; + } case 'n': { nvalue = optarg; @@ -238,7 +245,8 @@ int main(int argc, char *argv[]) } case '?': { - if ((optopt == 'd') || (optopt == 'f') || (optopt == 'n') || (optopt == 'l') || (optopt == 't')) + if ((optopt == 'd') || (optopt == 'f') || (optopt == 'n') || + (optopt == 'l') || (optopt == 't') || (optopt == 'S')) fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); @@ -279,6 +287,11 @@ int main(int argc, char *argv[]) return -1; } + if (filesize != 0) { + if (dlt_set_filesize_max(filesize) < 0) + return -1; + } + dlt_with_session_id(1); dlt_with_timestamp(1); dlt_with_ecu_id(1); diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index 778ea8f..d2ef613 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -91,6 +91,7 @@ static DltUser dlt_user; static bool dlt_user_initialised = false; static int dlt_user_freeing = 0; +static bool dlt_user_file_reach_max = false; #ifdef DLT_LIB_USE_FIFO_IPC static char dlt_user_dir[DLT_PATH_MAX]; @@ -459,6 +460,9 @@ DltReturnValue dlt_init(void) } dlt_user.dlt_is_file = 0; + dlt_user.filesize_max = UINT_MAX; + dlt_user_file_reach_max = false; + dlt_user.overflow = 0; dlt_user.overflow_counter = 0; #ifdef DLT_SHM_ENABLE @@ -546,16 +550,39 @@ DltReturnValue dlt_init_file(const char *name) dlt_user.dlt_is_file = 1; /* open DLT output file */ - dlt_user.dlt_log_handle = open(name, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */ + dlt_user.dlt_log_handle = open(name, O_WRONLY | O_CREAT, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */ if (dlt_user.dlt_log_handle == -1) { dlt_vnlog(LOG_ERR, DLT_USER_BUFFER_LENGTH, "Log file %s cannot be opened!\n", name); + dlt_user.dlt_is_file = 0; return DLT_RETURN_ERROR; } return DLT_RETURN_OK; } +DltReturnValue dlt_set_filesize_max(unsigned int filesize) +{ + if (dlt_user.dlt_is_file == 0) + { + dlt_vlog(LOG_ERR, "%s: Library is not configured to log to file\n", + __func__); + return DLT_LOG_ERROR; + } + + if (filesize == 0) { + dlt_user.filesize_max = UINT_MAX; + } + else { + dlt_user.filesize_max = filesize; + } + dlt_vlog(LOG_DEBUG, "%s: Defined filesize_max is [%d]\n", __func__, + dlt_user.filesize_max); + + return DLT_RETURN_OK; +} + #ifdef DLT_NETWORK_TRACE_ENABLE DltReturnValue dlt_init_message_queue(void) { @@ -3756,11 +3783,36 @@ DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype) } if (dlt_user.dlt_is_file) { - /* log to file */ - ret = dlt_user_log_out2(dlt_user.dlt_log_handle, msg.headerbuffer, msg.headersize, log->buffer, log->size); - return ret; - } - else { + if (dlt_user_file_reach_max) { + return DLT_RETURN_FILESZERR; + } + else { + /* Get file size */ + struct stat st; + fstat(dlt_user.dlt_log_handle, &st); + dlt_vlog(LOG_DEBUG, "%s: Current file size=[%d]\n", __func__, + st.st_size); + + /* Check filesize */ + /* Return error if the file size has reached to maximum */ + unsigned int msg_size = st.st_size + (unsigned int) msg.headersize + + (unsigned int) log->size; + if (msg_size > dlt_user.filesize_max) { + dlt_user_file_reach_max = true; + dlt_vlog(LOG_ERR, + "%s: File size (%d bytes) reached to defined maximum size (%d bytes)\n", + __func__, st.st_size, dlt_user.filesize_max); + return DLT_RETURN_FILESZERR; + } + else { + /* log to file */ + ret = dlt_user_log_out2(dlt_user.dlt_log_handle, + msg.headerbuffer, msg.headersize, + log->buffer, log->size); + return ret; + } + } + } else { if (dlt_user.overflow_counter) { if (dlt_user_log_send_overflow() == DLT_RETURN_OK) { dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "%u messages discarded!\n", dlt_user.overflow_counter); -- cgit v1.2.1