Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add tests for xbt/log and fix some memory leaks.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 5 Oct 2017 11:36:23 +0000 (13:36 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 5 Oct 2017 15:16:49 +0000 (17:16 +0200)
src/xbt/xbt_log_appender_file.c
teshsuite/xbt/CMakeLists.txt
teshsuite/xbt/log_usage/log_usage.c
teshsuite/xbt/log_usage/log_usage.tesh

index 472a314..e511603 100644 (file)
@@ -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 <errno.h>
 #include <stdio.h>
+#include <string.h>
 
 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);
 }
 
 
index a8fd743..70f58b3 100644 (file)
@@ -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)
index dd9c606..e417d93 100644 (file)
@@ -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 <simgrid/msg.h>
+#include <xbt.h>
 
 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(" ");
index 0ef0851..eace392 100644 (file)
@@ -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