Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
MuggleWei committed Sep 23, 2024
2 parents 8fe45d8 + 94d1cb1 commit eaa352c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 45 deletions.
6 changes: 6 additions & 0 deletions doc/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
| 2024-03-22 | v0.2.0 |
| 2024-08-21 | v0.3.0 |
| 2024-09-05 | v0.3.1 |
| 2024-09-23 | v0.4.0 |

---
## v0.4.0
* fix: failed automatically mkdir when log filepath is abs filepath
* add: add haclog_os_fopen

---
## v0.3.1
Expand Down
38 changes: 38 additions & 0 deletions haclog/haclog_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,41 @@ int haclog_os_rename(const char *src, const char *dst)
}

#endif

FILE *haclog_os_fopen(const char *filepath, const char *mode)
{
int ret = 0;
const char *abs_filepath = NULL;
char tmp_path[HACLOG_MAX_PATH];
if (haclog_path_isabs(filepath)) {
abs_filepath = filepath;
} else {
char cur_path[HACLOG_MAX_PATH];
ret = haclog_os_curdir(cur_path, sizeof(cur_path));
if (ret != 0) {
return NULL;
}

ret = haclog_path_join(cur_path, filepath, tmp_path, sizeof(tmp_path));
if (ret != 0) {
return NULL;
}

abs_filepath = tmp_path;
}

char file_dir[HACLOG_MAX_PATH];
ret = haclog_path_dirname(abs_filepath, file_dir, sizeof(file_dir));
if (ret != 0) {
return NULL;
}

if (!haclog_path_exists(file_dir)) {
ret = haclog_os_mkdir(file_dir);
if (ret != 0) {
return NULL;
}
}

return fopen(abs_filepath, mode);
}
14 changes: 14 additions & 0 deletions haclog/haclog_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define HACLOG_OS_H_

#include "haclog/haclog_macro.h"
#include <stdio.h>

HACLOG_EXTERN_C_BEGIN

Expand Down Expand Up @@ -88,6 +89,19 @@ int haclog_os_rmdir(const char *path);
HACLOG_EXPORT
int haclog_os_rename(const char *src, const char *dst);

/**
* @brief open file, if dir is not exists, create dir automatically
*
* @param filepath file path
* @param mode open file mode
*
* @return
* - on success, return FILE pointer
* - on failed, NULL is returned
*/
HACLOG_EXPORT
FILE* haclog_os_fopen(const char *filepath, const char *mode);

HACLOG_EXTERN_C_END

#endif // !HACLOG_OS_H_
15 changes: 1 addition & 14 deletions haclog/handler/haclog_file_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,10 @@ int haclog_file_handler_init(haclog_file_handler_t *handler,
return ret;
}

char log_dir[HACLOG_MAX_PATH];
ret = haclog_path_dirname(log_path, log_dir, sizeof(log_dir));
if (ret != 0) {
return ret;
}

if (!haclog_path_exists(log_dir)) {
ret = haclog_os_mkdir(log_dir);
if (ret != 0) {
return ret;
}
}

abs_filepath = log_path;
}

handler->fp = fopen(abs_filepath, mode);
handler->fp = haclog_os_fopen(abs_filepath, mode);
if (handler->fp == NULL) {
return HACLOG_ERR_SYS_CALL;
}
Expand Down
17 changes: 2 additions & 15 deletions haclog/handler/haclog_file_rotate_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ haclog_file_rotate_handler_after_write(haclog_handler_t *base_handler,

if (handler->offset >= handler->max_bytes) {
if (haclog_file_rotate_handler_rotate(handler) != 0) {
fprintf(stderr, "failed rotate log handler");
fprintf(stderr, "failed rotate log handler\n");
}
}
}
Expand Down Expand Up @@ -155,23 +155,10 @@ int haclog_file_rotate_handler_init(haclog_file_rotate_handler_t *handler,
return ret;
}

char log_dir[HACLOG_MAX_PATH];
ret = haclog_path_dirname(log_path, log_dir, sizeof(log_dir));
if (ret != 0) {
return ret;
}

if (!haclog_path_exists(log_dir)) {
ret = haclog_os_mkdir(log_dir);
if (ret != 0) {
return ret;
}
}

abs_filepath = log_path;
}

handler->fp = fopen(abs_filepath, "ab+");
handler->fp = haclog_os_fopen(abs_filepath, "ab+");
if (handler->fp == NULL) {
return HACLOG_ERR_SYS_CALL;
}
Expand Down
17 changes: 2 additions & 15 deletions haclog/handler/haclog_file_time_rot_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ haclog_file_time_rot_handler_rotate(haclog_file_time_rot_handler_t *handler)
return HACLOG_ERR_ARGUMENTS;
}

handler->fp = fopen(buf, "ab+");
handler->fp = haclog_os_fopen(buf, "ab+");
if (handler->fp == NULL) {
return HACLOG_ERR_SYS_CALL;
}
Expand Down Expand Up @@ -225,24 +225,11 @@ int haclog_file_time_rotate_handler_init(
return ret;
}

char log_dir[HACLOG_MAX_PATH];
ret = haclog_path_dirname(log_path, log_dir, sizeof(log_dir));
if (ret != 0) {
return ret;
}

if (!haclog_path_exists(log_dir)) {
ret = haclog_os_mkdir(log_dir);
if (ret != 0) {
return ret;
}
}

abs_filepath = log_path;
}

strncpy(handler->filepath, abs_filepath, sizeof(handler->filepath) - 1);
// handler already memset, the line below just for get rid of gcc strncpy
// handler already memset, the line below just for get rid of gcc strncpy
// truncated warning
handler->filepath[sizeof(handler->filepath) - 1] = '\0';

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.1
0.4.0

0 comments on commit eaa352c

Please sign in to comment.