From: Arnaud Giersch Date: Fri, 18 Feb 2022 09:25:24 +0000 (+0100) Subject: Fix xbt_log_appender when there is no percent in template. X-Git-Tag: v3.31~392 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/4f2a045034b355355ed07cb0e39984ec467ac58d?ds=sidebyside Fix xbt_log_appender when there is no percent in template. --- diff --git a/src/xbt/xbt_log_appender_file.cpp b/src/xbt/xbt_log_appender_file.cpp index 82df026715..02df6f51dd 100644 --- a/src/xbt/xbt_log_appender_file.cpp +++ b/src/xbt/xbt_log_appender_file.cpp @@ -70,17 +70,12 @@ static void open_append2_file(xbt_log_append2_file_t data){ //Split if(data->file) fclose(data->file); - char* pre=xbt_strdup(data->filename); - char* sep=strchr(pre,'%'); - if (not sep) - sep=pre+strlen(pre); - const char* post = sep + 1; - *sep = '\0'; - std::string newname = pre + std::to_string(data->count) + post; + std::string newname = data->filename; + size_t sep = std::min(newname.find_first_of('%'), newname.size()); + newname.replace(sep, 1, std::to_string(data->count)); data->count++; data->file = fopen(newname.c_str(), "w"); xbt_assert(data->file != nullptr, "Cannot open file: %s: %s", newname.c_str(), strerror(errno)); - xbt_free(pre); } } diff --git a/teshsuite/xbt/log_usage/log_usage.tesh b/teshsuite/xbt/log_usage/log_usage.tesh index 67385830e5..d97286796b 100644 --- a/teshsuite/xbt/log_usage/log_usage.tesh +++ b/teshsuite/xbt/log_usage/log_usage.tesh @@ -62,6 +62,8 @@ $ cat ${bindir:=.}/log_usage.log > > +$ rm -f ${bindir:=.}/log_usage.log + p Check the "splitfile" log appender $ ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%a@%h]%e%m%n" --log=root.app:splitfile:500:${bindir:=.}/log_usage_%.log $ cat ${bindir:=.}/log_usage_0.log @@ -86,4 +88,30 @@ $ cat ${bindir:=.}/log_usage_1.log > [ 0.000000] [0:maestro@] Test with the settings ' test.thres:critical ' > [ 0.000000] [0:maestro@] false alarm! -$ rm -f ${bindir:=.}/log_usage.log ${bindir:=.}/log_usage_0.log ${bindir:=.}/log_usage_1.log +$ rm -f ${bindir:=.}/log_usage_0.log ${bindir:=.}/log_usage_1.log + +p Check the "splitfile" log appender without percent in template +$ ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%a@%h]%e%m%n" --log=root.app:splitfile:500:${bindir:=.}/log_usage.log. +$ cat ${bindir:=.}/log_usage.log.0 +> [ 0.000000] [0:maestro@] Test with the settings '' +> [ 0.000000] [0:maestro@] val=2 +> [ 0.000000] [0:maestro@] false alarm! +> [ 0.000000] [0:maestro@] Test with the settings ' ' +> [ 0.000000] [0:maestro@] val=2 +> [ 0.000000] [0:maestro@] false alarm! +> [ 0.000000] [0:maestro@] Test with the settings ' test.thres:info root.thres:info ' +> [ 0.000000] [0:maestro@] val=2 +> [ 0.000000] [0:maestro@] false alarm! +> [ 0.000000] [0:maestro@] Test with the settings ' test.thres:debug ' +> [ 0.000000] [0:maestro@] val=1 + +$ cat ${bindir:=.}/log_usage.log.1 +> [ 0.000000] [0:maestro@] val=2 +> [ 0.000000] [0:maestro@] false alarm! +> [ 0.000000] [0:maestro@] Test with the settings ' test.thres:verbose root.thres:error ' +> [ 0.000000] [0:maestro@] val=2 +> [ 0.000000] [0:maestro@] false alarm! +> [ 0.000000] [0:maestro@] Test with the settings ' test.thres:critical ' +> [ 0.000000] [0:maestro@] false alarm! + +$ rm -f ${bindir:=.}/log_usage.log.0 ${bindir:=.}/log_usage.log.1