Commit d7b2719e authored by Benjamin REIS's avatar Benjamin REIS

fix bctbx_dirname and allows different extension for log file

parent 98f88619
...@@ -116,7 +116,7 @@ bctbx_log_handler_t* bctbx_create_log_handler(BctbxLogHandlerFunc func, BctbxLog ...@@ -116,7 +116,7 @@ bctbx_log_handler_t* bctbx_create_log_handler(BctbxLogHandlerFunc func, BctbxLog
bctbx_log_handler_t* bctbx_create_file_log_handler(uint64_t max_size, const char* path, const char* name, FILE* f) { bctbx_log_handler_t* bctbx_create_file_log_handler(uint64_t max_size, const char* path, const char* name, FILE* f) {
bctbx_log_handler_t* handler = (bctbx_log_handler_t*)bctbx_malloc0(sizeof(bctbx_log_handler_t)); bctbx_log_handler_t* handler = (bctbx_log_handler_t*)bctbx_malloc0(sizeof(bctbx_log_handler_t));
bctbx_file_log_handler_t* filehandler = (bctbx_file_log_handler_t*)bctbx_malloc(sizeof(bctbx_file_log_handler_t)); bctbx_file_log_handler_t* filehandler = (bctbx_file_log_handler_t*)bctbx_malloc(sizeof(bctbx_file_log_handler_t));
char *full_name = bctbx_strdup_printf("%s/%s.log", char *full_name = bctbx_strdup_printf("%s/%s",
path, path,
name); name);
struct stat buf; struct stat buf;
...@@ -504,7 +504,7 @@ static int _try_open_log_collection_file(bctbx_file_log_handler_t *filehandler) ...@@ -504,7 +504,7 @@ static int _try_open_log_collection_file(bctbx_file_log_handler_t *filehandler)
struct stat statbuf; struct stat statbuf;
char *log_filename; char *log_filename;
log_filename = bctbx_strdup_printf("%s/%s.log", log_filename = bctbx_strdup_printf("%s/%s",
filehandler->path, filehandler->path,
filehandler->name); filehandler->name);
filehandler->file = fopen(log_filename, "a"); filehandler->file = fopen(log_filename, "a");
...@@ -524,43 +524,53 @@ static int _try_open_log_collection_file(bctbx_file_log_handler_t *filehandler) ...@@ -524,43 +524,53 @@ static int _try_open_log_collection_file(bctbx_file_log_handler_t *filehandler)
static void _rotate_log_collection_files(bctbx_file_log_handler_t *filehandler) { static void _rotate_log_collection_files(bctbx_file_log_handler_t *filehandler) {
char *log_filename; char *log_filename;
char *log_filename2; char *log_filename2;
char *extension = strrchr(filehandler->name, '.');
char *file_no_extension = bctbx_strdup(filehandler->name);
file_no_extension[extension - file_no_extension] = '\0';
int n = 1; int n = 1;
log_filename = bctbx_strdup_printf("%s/%s_1.log", log_filename = bctbx_strdup_printf("%s/%s_1.%s",
filehandler->path, filehandler->path,
filehandler->name); file_no_extension,
extension);
while(access(log_filename, F_OK) != -1) { while(access(log_filename, F_OK) != -1) {
// file exists // file exists
n++; n++;
log_filename = bctbx_strdup_printf("%s/%s_%d.log", log_filename = bctbx_strdup_printf("%s/%s_%d.%s",
filehandler->path, filehandler->path,
filehandler->name, file_no_extension,
n); n,
extension);
} }
while(n > 1) { while(n > 1) {
log_filename = bctbx_strdup_printf("%s/%s_%d.log", log_filename = bctbx_strdup_printf("%s/%s_%d.%s",
filehandler->path, filehandler->path,
filehandler->name, file_no_extension,
n-1); n-1,
log_filename2 = bctbx_strdup_printf("%s/%s_%d.log", extension);
log_filename2 = bctbx_strdup_printf("%s/%s_%d.%s",
filehandler->path, filehandler->path,
filehandler->name, file_no_extension,
n); n,
extension);
n--; n--;
rename(log_filename, log_filename2); rename(log_filename, log_filename2);
} }
log_filename = bctbx_strdup_printf("%s/%s.log", log_filename = bctbx_strdup_printf("%s/%s",
filehandler->path, filehandler->path,
filehandler->name); filehandler->name);
log_filename2 = bctbx_strdup_printf("%s/%s_1.log", log_filename2 = bctbx_strdup_printf("%s/%s_1.%s",
filehandler->path, filehandler->path,
filehandler->name); file_no_extension,
extension);
rename(log_filename, log_filename2); rename(log_filename, log_filename2);
bctbx_free(log_filename); bctbx_free(log_filename);
bctbx_free(log_filename2); bctbx_free(log_filename2);
bctbx_free(extension);
bctbx_free(file_no_extension);
} }
static void _open_log_collection_file(bctbx_file_log_handler_t *filehandler) { static void _open_log_collection_file(bctbx_file_log_handler_t *filehandler) {
......
...@@ -112,14 +112,14 @@ char * bctbx_dirname(const char *path) { ...@@ -112,14 +112,14 @@ char * bctbx_dirname(const char *path) {
char *dname = bctbx_strdup(path); char *dname = bctbx_strdup(path);
bool_t found = FALSE; bool_t found = FALSE;
ptr = strchr(path, '/'); ptr = strrchr(path, '/');
if (ptr != NULL) { if (ptr != NULL) {
dname[ptr - path + 1] = '\0'; dname[ptr - path] = '\0';
found = TRUE; found = TRUE;
} else { } else {
ptr = strchr(path, '\\'); ptr = strrchr(path, '\\');
if (ptr != NULL) { if (ptr != NULL) {
dname[ptr - path + 1] = '\0'; dname[ptr - path] = '\0';
found = TRUE; found = TRUE;
} }
} }
......
...@@ -63,7 +63,7 @@ int bctoolbox_tester_set_log_file(const char *filename) { ...@@ -63,7 +63,7 @@ int bctoolbox_tester_set_log_file(const char *filename) {
if (log_file) { if (log_file) {
fclose(log_file); fclose(log_file);
} }
log_file = fopen(filename, "w"); log_file = fopen(filename, "a");
if (!log_file) { if (!log_file) {
bctbx_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno)); bctbx_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno));
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment