diff --git a/core/include/dap_common.h b/core/include/dap_common.h index d08bf97a6661e275864152f76d228743814d87de..4dc1f451bd37f8c1be95b62397a803b670c0e1eb 100755 --- a/core/include/dap_common.h +++ b/core/include/dap_common.h @@ -1120,4 +1120,4 @@ DAP_STATIC_INLINE bool dap_stream_node_addr_is_blank(dap_stream_node_addr_t *a_a const char *dap_stream_node_addr_to_str_static(dap_stream_node_addr_t a_address); -void dap_common_enable_cleaner_log(size_t a_timeout, size_t *a_max_size); +void dap_common_enable_cleaner_log(size_t a_timeout, size_t a_max_size); diff --git a/core/src/dap_common.c b/core/src/dap_common.c index 3e2563e0e8d4f67f8f4d12f3f3cf58dd97d8aeaa..d0abb298fe31c7da38c24dc9da13ec4464b075ea 100755 --- a/core/src/dap_common.c +++ b/core/src/dap_common.c @@ -379,8 +379,8 @@ int dap_deserialize_multy(const uint8_t *a_data, uint64_t a_size, int a_count, . return 0; } -int s_dap_log_open(const char *a_log_file_path) { - if (! (s_log_file = s_log_file ? freopen(a_log_file_path, "w", s_log_file) : fopen( a_log_file_path , "a" )) ) +static int s_dap_log_open(const char *a_log_file_path, bool a_new) { + if (! (s_log_file = s_log_file ? freopen(a_log_file_path, a_new ? "w" : "a", s_log_file) : fopen( a_log_file_path, a_new ? "w" : "a" )) ) return fprintf( stderr, "Can't open log file %s \n", a_log_file_path ), -1; static char s_buf_file[LOG_BUF_SIZE]; return setvbuf(s_log_file, s_buf_file, _IOLBF, LOG_BUF_SIZE); @@ -402,7 +402,7 @@ int dap_common_init( const char *a_console_title, const char *a_log_file_path, c for (int i = 0; i < 16; ++i) s_ansi_seq_color_len[i] =(unsigned int) strlen(s_ansi_seq_color[i]); if ( a_log_file_path && a_log_file_path[0] ) { - if (s_dap_log_open(a_log_file_path)) + if (s_dap_log_open(a_log_file_path, false)) return -1; if (a_log_dirpath != s_log_dir_path) dap_stpcpy(s_log_dir_path, a_log_dirpath); @@ -1563,26 +1563,19 @@ ssize_t dap_writev(dap_file_handle_t a_hf, const char* a_filename, iovec_t const } static void s_dap_common_log_cleanner_interval(void *a_max_size) { - size_t l_max_size = *((size_t*)a_max_size); - size_t l_log_size = ftell(s_log_file); - if (l_log_size == 0){ - log_it(L_ERROR, "The size of the log file could not be determined; cleaning is impossible."); - } else { - size_t l_size_mb = l_log_size / 1048576; - if (l_size_mb > l_max_size) { - char *l_new_file = dap_strdup_printf("%s.old", s_log_file_path); - rename(s_log_file_path, l_new_file); - if (s_dap_log_open(s_log_file_path)) { - log_it(L_CRITICAL, "An error occurred The logging thread was not reopened."); - } - log_it(L_NOTICE, "log file overwritten."); - remove(l_new_file); - DAP_DELETE(l_new_file); - } + size_t l_max_size = DAP_POINTER_TO_SIZE(a_max_size), + l_log_size = ftello(s_log_file); + switch (l_log_size) { + case -1: return log_it(L_ERROR, "Can't tell log file size, error %d :\"%s\"", errno, dap_strerror(errno)); + case 0: return log_it(L_ERROR, "Log file is empty"); + default: + if ( l_log_size / 1048576 > l_max_size && s_dap_log_open(s_log_file_path, true) ) + return log_it(L_ERROR, "Can't reopen log file \"%s\"", s_log_file_path); } + } -void dap_common_enable_cleaner_log(size_t a_timeout, size_t *a_max_size){ - dap_interval_timer_create(a_timeout, s_dap_common_log_cleanner_interval, a_max_size); +void dap_common_enable_cleaner_log(size_t a_timeout, size_t a_max_size){ + dap_interval_timer_create(a_timeout, s_dap_common_log_cleanner_interval, DAP_SIZE_TO_POINTER(a_max_size)); } #ifdef __cplusplus