diff options
| author | root <[email protected]> | 2024-08-07 08:47:15 +0000 |
|---|---|---|
| committer | root <[email protected]> | 2024-08-07 08:47:15 +0000 |
| commit | d114221ebe3bb5aacc9039aa7b124ae3386bf641 (patch) | |
| tree | 0c3ee10f477c224b395efe1ef051536e82f2d606 /deps | |
| parent | 2fd93a16481cad5e801ec399cb19c79f0310bb68 (diff) | |
TSG-22082: support set split log file by size
Diffstat (limited to 'deps')
| -rw-r--r-- | deps/log/log.c | 35 | ||||
| -rw-r--r-- | deps/log/log.h | 1 |
2 files changed, 34 insertions, 2 deletions
diff --git a/deps/log/log.c b/deps/log/log.c index 6e192d0..46d9c1f 100644 --- a/deps/log/log.c +++ b/deps/log/log.c @@ -41,6 +41,9 @@ typedef enum { struct log_handle { int level; int enable; + int split_file_by_size; + size_t max_file_size_mb; + int file_index; FILE *fp; va_list ap; char defined_log_fn[1024]; @@ -145,9 +148,29 @@ int log_create_log_file(struct log_handle *handle) if (NULL == (localtime_r(&t, &local_time))) { return 0; } - snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d", + snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d-%03d", handle->defined_log_fn, local_time.tm_year + 1900, - local_time.tm_mon + 1, local_time.tm_mday); + local_time.tm_mon + 1, local_time.tm_mday, handle->file_index); + + if (handle->split_file_by_size) { + struct stat file_stat; + + if (0 != memcmp(tmp_log_file_name, handle->runtime_log_fn, strlen(tmp_log_file_name))) {//new timestamp, reset file index + handle->file_index = 0; + snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d-%03d", + handle->defined_log_fn, local_time.tm_year + 1900, + local_time.tm_mon + 1, local_time.tm_mday, handle->file_index); + } + + if (stat(tmp_log_file_name, &file_stat) == 0) { + if (file_stat.st_size >= (handle->max_file_size_mb * 1024 * 1024)) { + handle->file_index++; + snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d-%03d", + handle->defined_log_fn, local_time.tm_year + 1900, + local_time.tm_mon + 1, local_time.tm_mday, handle->file_index); + } + } + } if (handle->fp == NULL) { if (0 != log_open_file(tmp_log_file_name, handle)) @@ -219,6 +242,14 @@ void log_handle_set_enable(struct log_handle * handle, int enable) } } +void log_handle_set_file_max_size(struct log_handle *handle, size_t max_file_size_mb) +{ + if (handle != NULL) { + handle->split_file_by_size = 1; + handle->max_file_size_mb = max_file_size_mb; + } +} + struct log_handle *log_handle_create(const char *file_path, int level) { struct log_handle *handle = ALLOC(struct log_handle, 1); diff --git a/deps/log/log.h b/deps/log/log.h index 681fb66..c125f77 100644 --- a/deps/log/log.h +++ b/deps/log/log.h @@ -41,6 +41,7 @@ enum { void log_print(struct log_handle *, int level, const char *module, const char *fmt, ...); void log_handle_set_enable(struct log_handle *, int enable); void log_handle_set_level(struct log_handle *, int level); +void log_handle_set_file_max_size(struct log_handle *, size_t max_file_size_mb); struct log_handle * log_handle_create(const char *file_path, int level); void log_handle_destroy(struct log_handle *); |
