27 #include <sys/types.h> 58 if (log_file_name ==
NULL || file_config ==
NULL)
63 char file_index[10] = {
'\0'};
67 strcat(log_file_name, name);
70 snprintf(file_index, 10,
"%d",idx);
75 unsigned int digit_idx = 0;
77 snprintf(file_index, 10,
"%d",idx);
78 digit_idx = strlen(file_index);
81 strcat(log_file_name,
"0");
85 strcat(log_file_name, file_index);
91 time_t t = time(
NULL);
92 struct tm *tm_info = localtime(&t);
94 "%c%04d%02d%02d-%02d%02d%02d",
96 1900 + tm_info->tm_year,
102 strcat(log_file_name, stamp);
105 strcat(log_file_name,
".dlt");
120 if (head ==
NULL || *head ==
NULL || (*head)->next ==
NULL)
167 if (head ==
NULL || *head ==
NULL || (*head)->next ==
NULL)
172 for (n = *head; n !=
NULL; n = n->
next)
176 if ((n->
idx - n_prev->
idx) != 1)
186 if (wrap_post && wrap_pre)
207 unsigned int idx = -1;
210 unsigned int filename_len = 0 ;
211 unsigned int fileindex_len = 0;
213 if (file_config ==
NULL || file ==
NULL)
220 filename_len = strlen(file) - strlen(filename);
225 fileindex_len = strlen(file) -
230 idx = (int) strtol(&file[strlen(file) -
239 fileindex_len = strlen(file) -
243 idx = (int) strtol(&file[strlen(file)-
245 +fileindex_len)], &endptr, 10);
248 if (endptr == file || idx == 0)
251 "Unable to calculate index from log file name. Reset to 001.\n");
275 struct dirent **files = {0};
276 unsigned int current_idx = 0;
278 if (config ==
NULL ||
279 file_config ==
NULL ||
286 cnt = scandir(path, &files, 0, alphasort);
290 "dlt_logstorage_storage_dir_info: Failed to scan directory\n");
294 for (i = 0; i < cnt; i++)
298 if ((strncmp(files[i]->d_name,
314 "Memory allocation failed\n");
322 while(*(tmp) !=
NULL)
331 "Memory allocation failed\n");
335 (*tmp)->name = strdup(files[i]->d_name);
336 (*tmp)->idx = current_idx;
348 for (i = 0; i < cnt; i++)
379 unsigned int num_log_files = 0;
393 dlt_log(LOG_ERR,
"Mount point path name too long\n");
407 while(*(tmp) !=
NULL)
410 if((*tmp)->next ==
NULL)
417 if (num_log_files == 0)
425 strcat(absolute_file_path, storage_path);
426 strcat(absolute_file_path, file_name);
427 config->
log = fopen(absolute_file_path,
"a+");
434 "Memory allocation for file name failed\n");
437 (*tmp)->name = strdup(file_name);
443 strcat(absolute_file_path, storage_path);
444 strcat(absolute_file_path, (*newest)->name);
446 ret = stat(absolute_file_path, &s);
449 if (ret == 0 && s.st_size + msg_size < (
int)config->
file_size)
451 config->
log = fopen(absolute_file_path,
"a+");
455 unsigned int idx = 0;
475 memset(absolute_file_path,
477 sizeof(absolute_file_path)/
sizeof(
char));
478 strcat(absolute_file_path, storage_path);
479 strcat(absolute_file_path, file_name);
480 config->
log = fopen(absolute_file_path,
"a+");
487 "Memory allocation for file name failed\n");
491 (*tmp)->name = strdup(file_name);
503 memset(absolute_file_path,
505 sizeof(absolute_file_path)/
sizeof(
char));
506 strcat(absolute_file_path, storage_path);
507 strcat(absolute_file_path, (*head)->name);
508 remove(absolute_file_path);
520 "dlt_logstorage_create_log_file: Unable to open log file.\n");
547 if (config ==
NULL || file_config ==
NULL || dev_path ==
NULL)
561 ret = fstat(fileno(config->
log), &s);
564 if (s.st_size + log_msg_size >= (
int)config->
file_size)
581 "dlt_logstorage_prepare_log_file: stat() failed.\n");
603 unsigned char *data1,
605 unsigned char *data2,
607 unsigned char *data3,
617 ret = fwrite(data1, 1, size1, config->
log);
620 dlt_log(LOG_WARNING,
"Wrote less data than specified\n");
622 ret = fwrite(data2, 1, size2, config->
log);
625 dlt_log(LOG_WARNING,
"Wrote less data than specified\n");
627 ret = fwrite(data3, 1, size3, config->
log);
630 dlt_log(LOG_WARNING,
"Wrote less data than specified\n");
633 return ferror(config->
log);
656 ret = fflush(config->
log);
660 dlt_log(LOG_ERR,
"fflush failed\n");
685 if (config ==
NULL || file_config ==
NULL || dev_path ==
NULL)
690 log_msg_size = log_msg_size;
702 "Cannot prepare log file for ON_DAEMON_EXIT sync\n");
716 dlt_log(LOG_ERR,
"Max size of Logstorage Cache already used.");
720 config->
cache = calloc(1,
731 "Cannot allocate memory for filter ring buffer\n");
753 unsigned char *data1,
755 unsigned char *data2,
757 unsigned char *data3,
762 int remain_cache_size;
763 void *curr_write_addr =
NULL;
765 if (config ==
NULL || data1 ==
NULL || size1 < 0 || data2 ==
NULL ||
766 size2 < 0 || data3 ==
NULL || size3 < 0 || config->cache ==
NULL)
774 dlt_log(LOG_ERR,
"Cannot retrieve cache footer. Address is NULL\n");
778 msg_size = size1 + size2 + size3;
781 if (msg_size < remain_cache_size)
783 curr_write_addr = (
void *)(config->
cache + footer->
offset);
784 footer->
offset += msg_size;
786 else if (msg_size > remain_cache_size)
788 if ((
unsigned int) msg_size > config->
file_size)
790 dlt_log(LOG_WARNING,
"Message is larger than cache. Discard.\n");
795 curr_write_addr = config->
cache;
796 footer->
offset = msg_size;
801 curr_write_addr = (
void *)(config->
cache + footer->
offset);
807 memcpy(curr_write_addr, data1, size1);
808 curr_write_addr += size1;
809 memcpy(curr_write_addr, data2, size2);
810 curr_write_addr += size2;
811 memcpy(curr_write_addr, data3, size3);
842 "Cannot copy cache to file. One of both is NULL\n");
853 dlt_log(LOG_ERR,
"Cannot retrieve cache information\n");
863 if (ferror(config->
log) != 0)
865 dlt_log(LOG_CRIT,
"Failed to write cache into log file\n");
871 if (fsync(fileno(config->
log)) != 0)
873 dlt_log(LOG_ERR,
"Failed to sync log file\n");
881 memset(config->
cache,
unsigned int g_logstorage_cache_size
void dlt_logstorage_rearrange_file_name(DltLogStorageFileList **head)
int dlt_logstorage_prepare_msg_cache(DltLogStorageConfigData *config, DltLogStorageUserConfig *file_config, char *dev_path, int log_msg_size)
DltReturnValue dlt_log(int prio, char *s)
int dlt_logstorage_sync_on_msg(DltLogStorageConfigData *config, int status)
#define DLT_OFFLINE_LOGSTORAGE_TIMESTAMP_LEN
unsigned int logfile_maxcounter
int dlt_logstorage_write_on_msg(DltLogStorageConfigData *config, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
DltLogStorageFileList * records
#define DLT_MOUNT_PATH_MAX
int dlt_logstorage_sync_msg_cache(DltLogStorageConfigData *config, int status)
int dlt_logstorage_prepare_on_msg(DltLogStorageConfigData *config, DltLogStorageUserConfig *file_config, char *dev_path, int log_msg_size)
#define DLT_OFFLINE_LOGSTORAGE_IS_STRATEGY_SET(S, s)
void dlt_logstorage_sort_file_name(DltLogStorageFileList **head)
int dlt_logstorage_write_msg_cache(DltLogStorageConfigData *config, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
unsigned int logfile_counteridxlen
int dlt_logstorage_storage_dir_info(DltLogStorageUserConfig *file_config, char *path, DltLogStorageConfigData *config)
void dlt_logstorage_log_file_name(char *log_file_name, DltLogStorageUserConfig *file_config, char *name, int idx)
int dlt_logstorage_open_log_file(DltLogStorageConfigData *config, DltLogStorageUserConfig *file_config, char *dev_path, int msg_size)
unsigned int g_logstorage_cache_max
struct DltLogStorageFileList * next
#define DLT_LOGSTORAGE_SYNC_ON_DAEMON_EXIT
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_DIR_PATH_LEN
unsigned int dlt_logstorage_get_idx_of_log_file(DltLogStorageUserConfig *file_config, char *file)
#define DLT_LOGSTORAGE_SYNC_ON_MSG
#define DLT_OFFLINE_LOGSTORAGE_FILE_EXTENSION_LEN