* 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);
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;
}
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);
}
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);
}
}
}
}
-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);
}
#! ./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
> [ 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