From: Arnaud Giersch Date: Thu, 5 Oct 2017 11:36:23 +0000 (+0200) Subject: Add tests for xbt/log and fix some memory leaks. X-Git-Tag: v3_17~12 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/2d9a78430cb77fc81707232b805e68931dbf699c?ds=sidebyside Add tests for xbt/log and fix some memory leaks. --- diff --git a/src/xbt/xbt_log_appender_file.c b/src/xbt/xbt_log_appender_file.c index 472a314d62..e511603ccb 100644 --- a/src/xbt/xbt_log_appender_file.c +++ b/src/xbt/xbt_log_appender_file.c @@ -7,9 +7,11 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "src/internal_config.h" -#include "xbt/sysdep.h" #include "src/xbt/log_private.h" +#include "xbt/sysdep.h" +#include #include +#include static void append_file(xbt_log_appender_t this_, char *str) { fputs(str, (FILE *) this_->data); @@ -25,10 +27,13 @@ xbt_log_appender_t xbt_log_appender_file_new(char *arg) { xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1); res->do_append = &append_file; res->free_ = &free_; - if (arg) + if (arg) { res->data = (void *) fopen(arg, "w"); - else + if (res->data == NULL) + xbt_die("Cannot open file: %s: %s", arg, strerror(errno)); + } else { res->data = (void *) stderr; + } return res; } @@ -46,9 +51,11 @@ typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t; static void open_append2_file(xbt_log_append2_file_t data){ if(data->count<0) { //Roll - if(!data->file) + if (!data->file) { data->file= fopen(data->filename, "w"); - else{ + if (data->file == NULL) + xbt_die("Cannot open file: %s: %s", data->filename, strerror(errno)); + } else { fputs(APPEND2_END_TOKEN_CLEAR,data->file); fseek(data->file,0,SEEK_SET); } @@ -66,7 +73,9 @@ static void open_append2_file(xbt_log_append2_file_t data){ snprintf(newname,511,"%s%i%s",pre,data->count,post); data->count++; data->file= fopen(newname, "w"); - xbt_assert(data->file); + if (data->file == NULL) + xbt_die("Cannot open file: %s: %s", newname, strerror(errno)); + xbt_free(pre); } } @@ -83,10 +92,13 @@ static void append2_file(xbt_log_appender_t this_, char *str) { } } -static void free_append2_(xbt_log_appender_t this_) { - FILE* f=((xbt_log_append2_file_t)(this_->data))->file; - if (f) - fclose(f); +static void free_append2_(xbt_log_appender_t this_) +{ + xbt_log_append2_file_t data = this_->data; + if (data->file) + fclose(data->file); + xbt_free(data->filename); + xbt_free(data); } diff --git a/teshsuite/xbt/CMakeLists.txt b/teshsuite/xbt/CMakeLists.txt index a8fd74320a..70f58b35b7 100644 --- a/teshsuite/xbt/CMakeLists.txt +++ b/teshsuite/xbt/CMakeLists.txt @@ -31,8 +31,10 @@ foreach(x heap_bench log_large parallel_log_crashtest parmap_test) #mallocator p ADD_TESH(tesh-xbt-${x} --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/xbt/${x} --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/${x} ${x}.tesh) endforeach() -if(NOT enable_debug) - ADD_TESH(tesh-xbt-log --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/xbt/log_usage --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/log_usage log_usage_ndebug.tesh) +if(enable_debug) + ADD_TESH(tesh-xbt-log --cd ${CMAKE_BINARY_DIR}/teshsuite/xbt/log_usage ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/log_usage/log_usage.tesh) +else() + ADD_TESH(tesh-xbt-log --cd ${CMAKE_BINARY_DIR}/teshsuite/xbt/log_usage ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/log_usage/log_usage_ndebug.tesh) endif() if(HAVE_MMALLOC) diff --git a/teshsuite/xbt/log_usage/log_usage.c b/teshsuite/xbt/log_usage/log_usage.c index dd9c606771..e417d93c6d 100644 --- a/teshsuite/xbt/log_usage/log_usage.c +++ b/teshsuite/xbt/log_usage/log_usage.c @@ -6,7 +6,8 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "xbt.h" +#include +#include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(test, top, "Logging specific to this test"); XBT_LOG_NEW_CATEGORY(top, "Useless test channel"); @@ -23,7 +24,7 @@ static void dolog(const char *settings) int main(int argc, char **argv) { - xbt_init(&argc, argv); + MSG_init(&argc, argv); dolog(""); dolog(" "); diff --git a/teshsuite/xbt/log_usage/log_usage.tesh b/teshsuite/xbt/log_usage/log_usage.tesh index 0ef0851d11..eace392653 100644 --- a/teshsuite/xbt/log_usage/log_usage.tesh +++ b/teshsuite/xbt/log_usage/log_usage.tesh @@ -1,5 +1,6 @@ #! ./tesh +p Check different log thresholds $ $SG_TEST_EXENV ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%P@%h]%e%m%n" > [ 0.000000] [0:maestro@] Test with the settings '' > [ 0.000000] [0:maestro@] val=2 @@ -19,3 +20,74 @@ $ $SG_TEST_EXENV ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%P@%h]%e%m > [ 0.000000] [0:maestro@] false alarm! > [ 0.000000] [0:maestro@] Test with the settings ' test.thres:critical ' > [ 0.000000] [0:maestro@] false alarm! + +p Check the "file" log appender +$ $SG_TEST_EXENV ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%P@%h]%e%m%n" --log=root.app:file:${bindir:=.}/log_usage.log +$ cat ${bindir:=.}/log_usage.log +> [ 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 +> [ 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! + +p Check the "rollfile" log appender +$ $SG_TEST_EXENV ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%P@%h]%e%m%n" --log=root.app:rollfile:500:${bindir:=.}/log_usage.log +$ cat ${bindir:=.}/log_usage.log +> [ 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! +> +> [End of log] +> ] 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 +> +> + +p Check the "splitfile" log appender +$ $SG_TEST_EXENV ${bindir:=.}/log_usage "--log=root.fmt:[%10.6r]%e[%i:%P@%h]%e%m%n" --log=root.app:splitfile:500:${bindir:=.}/log_usage_%.log +$ cat ${bindir:=.}/log_usage_0.log +> [ 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_1.log +> [ 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 ${bindir:=.}/log_usage_0.log ${bindir:=.}/log_usage_1.log + +# Would be nice for code coverage, but the early exit leads to lots of memory leaks +#! output ignore +#$ $SG_TEST_EXENV ${bindir:=.}/log_usage --help --help-log-categories