summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/dlt/dlt_offline_trace.h41
-rw-r--r--src/daemon/dlt-daemon.c12
-rw-r--r--src/daemon/dlt-daemon.h1
-rw-r--r--src/daemon/dlt.conf3
-rw-r--r--src/shared/dlt_offline_trace.c174
5 files changed, 219 insertions, 12 deletions
diff --git a/include/dlt/dlt_offline_trace.h b/include/dlt/dlt_offline_trace.h
index 90cfa96..7612b90 100644
--- a/include/dlt/dlt_offline_trace.h
+++ b/include/dlt/dlt_offline_trace.h
@@ -61,12 +61,24 @@
#include "dlt_types.h"
+#define DLT_OFFLINETRACE_FILENAME_BASE "dlt_offlinetrace"
+#define DLT_OFFLINETRACE_FILENAME_DELI "."
+#define DLT_OFFLINETRACE_FILENAME_EXT ".dlt"
+#define DLT_OFFLINETRACE_INDEX_MAX_SIZE 10
+#define DLT_OFFLINETRACE_FILENAME_TO_COMPARE "dlt_offlinetrace_"
+/* "dlt_offlinetrace.4294967295.dlt" -> MAX 32byte include NULL terminate */
+#define DLT_OFFLINETRACE_FILENAME_MAX_SIZE (sizeof(DLT_OFFLINETRACE_FILENAME_BASE) + \
+ sizeof(DLT_OFFLINETRACE_FILENAME_DELI) + \
+ DLT_OFFLINETRACE_INDEX_MAX_SIZE + \
+ sizeof(DLT_OFFLINETRACE_FILENAME_EXT) + 1)
+
typedef struct
{
char directory[NAME_MAX + 1];/**< (String) Store DLT messages to local directory */
char filename[NAME_MAX + 1]; /**< (String) Filename of currently used log file */
int fileSize; /**< (int) Maximum size in bytes of one trace file (Default: 1000000) */
int maxSize; /**< (int) Maximum size of all trace files (Default: 4000000) */
+ int filenameTimestampBased; /**< (int) timestamp based or index based (Default: 1 Timestamp based) */
int ohandle;
} DltOfflineTrace;
@@ -80,9 +92,10 @@ typedef struct
* @param directory directory where to store offline trace files
* @param fileSize maximum size of one offline trace file.
* @param maxSize maximum size of complete offline trace in bytes.
+ *.@param filenameTimestampBased filename to be created on timestamp based or index based
* @return negative value if there was an error
*/
-extern DltReturnValue dlt_offline_trace_init(DltOfflineTrace *trace,const char *directory,int fileSize,int maxSize);
+extern DltReturnValue dlt_offline_trace_init(DltOfflineTrace *trace,const char *directory,int fileSize,int maxSize,int filenameTimestampBased);
/**
* Uninitialise the offline trace
@@ -115,4 +128,30 @@ extern DltReturnValue dlt_offline_trace_write(DltOfflineTrace *trace,unsigned ch
*/
extern unsigned long dlt_offline_trace_get_total_size(DltOfflineTrace *trace);
+/**
+ * Provides info about the offline logs storage directory
+ * @param path of the storage directory
+ * @param filename to search for
+ * @param pointer to store newest filename
+ * @param pointer to store oldest filename
+ * @return num of files in the directory
+ */
+unsigned int dlt_offline_trace_storage_dir_info(char *path, char *file_name, char *newest, char *oldest);
+
+/**
+ * creates filename with index
+ * @param log file name created with index
+ * @param filename base
+ * @param index to be used for file name creation
+ */
+void dlt_offline_trace_file_name(char *log_file_name, char *name, unsigned int idx);
+
+/**
+ * generates index for log file name
+ * @param filename supplied to create index
+ * @return the index to be used for log file name
+ */
+unsigned int dlt_offline_trace_get_idx_of_log_file(char *file);
+
+
#endif /* DLT_OFFLINE_TRACE_H */
diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c
index fb9fe22..4e6b4b4 100644
--- a/src/daemon/dlt-daemon.c
+++ b/src/daemon/dlt-daemon.c
@@ -216,6 +216,7 @@ int option_file_parser(DltDaemonLocal *daemon_local)
daemon_local->flags.offlineTraceDirectory[0] = 0;
daemon_local->flags.offlineTraceFileSize = 1000000;
daemon_local->flags.offlineTraceMaxSize = 0;
+ daemon_local->flags.offlineTraceFilenameTimestampBased = 1;
daemon_local->flags.loggingMode = DLT_LOG_TO_CONSOLE;
daemon_local->flags.loggingLevel = LOG_INFO;
snprintf(daemon_local->flags.loggingFilename, sizeof(daemon_local->flags.loggingFilename)-1, "%s/dlt.log", dltFifoBaseDir);
@@ -394,6 +395,11 @@ int option_file_parser(DltDaemonLocal *daemon_local)
daemon_local->flags.offlineTraceMaxSize = atoi(value);
//printf("Option: %s=%s\n",token,value);
}
+ else if(strcmp(token,"OfflineTraceFileNameTimestampBased")==0)
+ {
+ daemon_local->flags.offlineTraceFilenameTimestampBased = atoi(value);
+ //printf("Option: %s=%s\n",token,value);
+ }
else if(strcmp(token,"SendECUSoftwareVersion")==0)
{
daemon_local->flags.sendECUSoftwareVersion = atoi(value);
@@ -684,7 +690,11 @@ int dlt_daemon_local_init_p2(DltDaemon *daemon, DltDaemonLocal *daemon_local, in
/* init offline trace */
if(((daemon->mode == DLT_USER_MODE_INTERNAL) || (daemon->mode == DLT_USER_MODE_BOTH)) && daemon_local->flags.offlineTraceDirectory[0])
{
- if (dlt_offline_trace_init(&(daemon_local->offlineTrace),daemon_local->flags.offlineTraceDirectory,daemon_local->flags.offlineTraceFileSize,daemon_local->flags.offlineTraceMaxSize)==-1)
+ if (dlt_offline_trace_init(&(daemon_local->offlineTrace),
+ daemon_local->flags.offlineTraceDirectory,
+ daemon_local->flags.offlineTraceFileSize,
+ daemon_local->flags.offlineTraceMaxSize,
+ daemon_local->flags.offlineTraceFilenameTimestampBased)==-1)
{
dlt_log(LOG_ERR,"Could not initialize offline trace\n");
return -1;
diff --git a/src/daemon/dlt-daemon.h b/src/daemon/dlt-daemon.h
index 0096ba6..0e2fe37 100644
--- a/src/daemon/dlt-daemon.h
+++ b/src/daemon/dlt-daemon.h
@@ -105,6 +105,7 @@ typedef struct
char offlineTraceDirectory[256]; /**< (String: Directory) Store DLT messages to local directory (Default: /etc/dlt.conf) */
int offlineTraceFileSize; /**< (int) Maximum size in bytes of one trace file (Default: 1000000) */
int offlineTraceMaxSize; /**< (int) Maximum size of all trace files (Default: 4000000) */
+ int offlineTraceFilenameTimestampBased; /**< (int) timestamp based or index based (Default: 1 Timestamp based) */
int loggingMode; /**< (int) The logging console for internal logging of dlt-daemon (Default: 0) */
int loggingLevel; /**< (int) The logging level for internal logging of dlt-daemon (Default: 6) */
char loggingFilename[256]; /**< (String: Filename) The logging filename if internal logging mode is log to file (Default: /tmp/log) */
diff --git a/src/daemon/dlt.conf b/src/daemon/dlt.conf
index 5976c1f..d9b0987 100644
--- a/src/daemon/dlt.conf
+++ b/src/daemon/dlt.conf
@@ -66,6 +66,9 @@ RingbufferStepSize = 500000
# Maximum size of all trace files (Default: 4000000)
# OfflineTraceMaxSize = 4000000
+# Filename timestamp based or index based (Default:1) (timestamp based=1, index based =0)
+# OfflineTraceFileNameTimestampBased = 1
+
########################################################################
# Local console output configuration #
########################################################################
diff --git a/src/shared/dlt_offline_trace.c b/src/shared/dlt_offline_trace.c
index c4d3f6b..b2e5612 100644
--- a/src/shared/dlt_offline_trace.c
+++ b/src/shared/dlt_offline_trace.c
@@ -65,21 +65,175 @@
#include <dlt_offline_trace.h>
+unsigned int dlt_offline_trace_storage_dir_info(char *path, char *file_name, char *newest, char *oldest)
+{
+ int i = 0;
+ unsigned int num = 0;
+ int cnt = 0;
+ struct dirent **files = {0};
+ char *tmp_old = NULL;
+ char *tmp_new = NULL;
+
+ if (path == NULL || file_name == NULL || newest == NULL || oldest == NULL)
+ {
+ printf("dlt_offline_trace_storage_dir_info: Invalid parameter(s)");
+ return 0;
+ }
+
+ cnt = scandir(path, &files, NULL, alphasort);
+
+ if (cnt < 0)
+ {
+ return 0;
+ }
+
+ for (i = 0; i < cnt; i++)
+ {
+ int len = 0;
+ len = strlen(file_name);
+ if ((strncmp(files[i]->d_name, file_name, len) == 0) && (files[i]->d_name[len] == '.'))
+ {
+ num++;
+
+ if (tmp_old == NULL || (strlen(tmp_old) >= strlen(files[i]->d_name)))
+ {
+ if (tmp_old == NULL)
+ {
+ tmp_old = files[i]->d_name;
+ }
+ /* when file name is smaller, it is older */
+ else if (strlen(tmp_old) > strlen(files[i]->d_name))
+ {
+ tmp_old = files[i]->d_name;
+ }
+ else /* filename is equal, do a string compare */
+ {
+ if (strcmp(tmp_old, files[i]->d_name) > 0)
+ tmp_old = files[i]->d_name;
+ }
+ }
+
+ if (tmp_new == NULL || (strlen(tmp_new) <= strlen(files[i]->d_name)))
+ {
+ if (tmp_new == NULL)
+ {
+ tmp_new = files[i]->d_name;
+ }
+ /* when file name is longer, it is younger */
+ else if (strlen(tmp_new) < strlen(files[i]->d_name))
+ {
+ tmp_new = files[i]->d_name;
+ }
+ else
+ {
+ if (strcmp(tmp_new, files[i]->d_name) < 0)
+ {
+ tmp_new = files[i]->d_name;
+ }
+ }
+ }
+ }
+ }
+
+ if (num > 0)
+ {
+ if (tmp_old != NULL)
+ {
+ if (strlen(tmp_old) < DLT_OFFLINETRACE_FILENAME_MAX_SIZE)
+ {
+ strncpy(oldest, tmp_old, DLT_OFFLINETRACE_FILENAME_MAX_SIZE);
+ }
+ }
+ if (tmp_new != NULL)
+ {
+ if (strlen(tmp_old) < DLT_OFFLINETRACE_FILENAME_MAX_SIZE)
+ {
+ strncpy(newest, tmp_new, DLT_OFFLINETRACE_FILENAME_MAX_SIZE);
+ }
+ }
+ }
+
+ /* free scandir result */
+ for (i = 0; i < cnt; i++)
+ {
+ free(files[i]);
+ }
+ free(files);
+
+ return num;
+}
+
+void dlt_offline_trace_file_name(char *log_file_name, char *name, unsigned int idx)
+{
+ char file_index[11]; // UINT_MAX = 4294967295 -> 10 digits
+ sprintf(file_index, "%010u", idx);
+
+ // create log file name
+ memset(log_file_name, 0, DLT_OFFLINETRACE_FILENAME_MAX_SIZE * sizeof(char));
+ strncat(log_file_name, name,sizeof(DLT_OFFLINETRACE_FILENAME_BASE));
+ strncat(log_file_name, DLT_OFFLINETRACE_FILENAME_DELI,sizeof(DLT_OFFLINETRACE_FILENAME_DELI));
+ strncat(log_file_name, file_index,sizeof(file_index));
+ strncat(log_file_name, DLT_OFFLINETRACE_FILENAME_EXT,sizeof(DLT_OFFLINETRACE_FILENAME_EXT));
+}
+
+unsigned int dlt_offline_trace_get_idx_of_log_file(char *file)
+{
+ const char d[2] = ".";
+ char *token;
+ unsigned int idx = 0;
+
+ if (file[0] == '\0')
+ {
+ return 0;
+ }
+
+ token = strtok(file, d);
+ /* we are interested in 2. token because of log file name */
+ token = strtok(NULL, d);
+
+ if (token != NULL)
+ {
+ idx = strtol(token, NULL, 10);
+ }
+ else
+ {
+ idx = 0;
+ }
+ return idx;
+}
+
+
DltReturnValue dlt_offline_trace_create_new_file(DltOfflineTrace *trace) {
time_t t;
struct tm *tmp;
char outstr[200];
+ char newest[DLT_OFFLINETRACE_FILENAME_MAX_SIZE] = {0};
+ char oldest[DLT_OFFLINETRACE_FILENAME_MAX_SIZE] = {0};
+ unsigned int idx = 0;
/* set filename */
- t = time(NULL);
- tmp = localtime(&t);
- if (NULL == tmp) {
- printf("dlt_offline_trace_create_new_file: pointer to tmp is NULL!");
- return DLT_RETURN_ERROR;
+ if(trace->filenameTimestampBased)
+ {
+ t = time(NULL);
+ tmp = localtime(&t);
+ if (NULL == tmp) {
+ printf("dlt_offline_trace_create_new_file: pointer to tmp is NULL!");
+ return DLT_RETURN_ERROR;
+ }
+ if (strftime(outstr, sizeof(outstr),"%Y%m%d_%H%M%S", tmp) == 0) {
+ }
+ snprintf(trace->filename, NAME_MAX + 1, "%s/dlt_offlinetrace_%s.dlt", trace->directory, outstr);
}
- if (strftime(outstr, sizeof(outstr),"%Y%m%d_%H%M%S", tmp) == 0) {
+ else
+ {
+ /* targeting newest file, ignoring number of files in dir returned */
+ dlt_offline_trace_storage_dir_info(trace->directory, DLT_OFFLINETRACE_FILENAME_BASE, newest, oldest);
+ idx = dlt_offline_trace_get_idx_of_log_file(newest) + 1;
+
+ dlt_offline_trace_file_name(outstr, DLT_OFFLINETRACE_FILENAME_BASE, idx);
+ snprintf(trace->filename, NAME_MAX + 1, "%s/%s", trace->directory, outstr);
}
- snprintf(trace->filename,NAME_MAX + 1,"%s/dlt_offlinetrace_%s.dlt",trace->directory,outstr);
+
/* open DLT output file */
trace->ohandle = open(trace->filename,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
@@ -144,7 +298,7 @@ int dlt_offline_trace_delete_oldest_file(DltOfflineTrace *trace) {
/* go through all dlt files in directory */
DIR *dir = opendir(trace->directory);
while ((dp=readdir(dir)) != NULL) {
- if(strstr(dp->d_name,".dlt")) {
+ if(strstr(dp->d_name,DLT_OFFLINETRACE_FILENAME_TO_COMPARE)) {
int res = snprintf(filename, sizeof(filename), "%s/%s",trace->directory,dp->d_name);
// if the total length of the string is greater than the buffer, silently forget it.
// snprintf: a return value of size or more means that the output was truncated
@@ -197,14 +351,14 @@ DltReturnValue dlt_offline_trace_check_size(DltOfflineTrace *trace) {
return DLT_RETURN_OK; /* OK */
}
-DltReturnValue dlt_offline_trace_init(DltOfflineTrace *trace,const char *directory,int fileSize,int maxSize) {
+DltReturnValue dlt_offline_trace_init(DltOfflineTrace *trace,const char *directory,int fileSize,int maxSize,int filenameTimestampBased) {
/* init parameters */
strncpy(trace->directory,directory,NAME_MAX);
trace->directory[NAME_MAX]=0;
trace->fileSize = fileSize;
trace->maxSize = maxSize;
-
+ trace->filenameTimestampBased = filenameTimestampBased;
/* check complete offlien trace size, remove old logs if needed */
dlt_offline_trace_check_size(trace);