1 /* file_appender - a dumb log appender which simply prints to a file */
3 /* Copyright (c) 2007-2014. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
10 #include "src/xbt/log_private.h"
12 #include "src/smpi/private.h" // to access bench_begin/end. Not ultraclean, I confess
16 static void append_file(xbt_log_appender_t this_, char *str) {
17 fputs(str, (FILE *) this_->data);
20 static void smpi_append_file(xbt_log_appender_t this_, char *str) {
21 fputs(str, (FILE *) this_->data);
24 static void free_(xbt_log_appender_t this_) {
25 if (this_->data != stderr)
30 void __smpi_bench_dont (void); // Stupid prototype
31 void __smpi_bench_dont (void) { /* I'm only a place-holder in case we link without SMPI */; }
32 void smpi_bench_begin(void) __attribute__ ((weak, alias ("__smpi_bench_dont")));
33 void smpi_bench_end(void) __attribute__ ((weak, alias ("__smpi_bench_dont")));
36 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
38 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
40 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
41 if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
42 res->do_append = smpi_append_file;
44 res->do_append = append_file;
47 res->data = (void *) fopen(arg, "w");
49 res->data = (void *) stderr;
53 struct xbt_log_append2_file_s {
56 int count; //negative for roll
59 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
61 #define APPEND2_END_TOKEN "\n[End of log]\n"
62 #define APPEND2_END_TOKEN_CLEAR "\n "
64 static void open_append2_file(xbt_log_append2_file_t data){
69 data->file= fopen(data->filename, "w");
71 fputs(APPEND2_END_TOKEN_CLEAR,data->file);
72 fseek(data->file,0,SEEK_SET);
76 //printf("Splitting\n");
81 char* pre=xbt_strdup(data->filename);
82 char* sep=strchr(pre,'%');
87 snprintf(newname,511,"%s%i%s",pre,data->count,post);
89 data->file= fopen(newname, "w");
90 xbt_assert(data->file);
98 static void append2_file(xbt_log_appender_t this_, char *str) {
99 xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
101 if(ftell(d->file)>=d->limit)
103 open_append2_file(d);
107 fputs(APPEND2_END_TOKEN,d->file);
108 fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
112 static void smpi_append2_file(xbt_log_appender_t this_, char *str) {
113 append2_file(this_,str);
116 static void free_append2_(xbt_log_appender_t this_) {
117 FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
123 //syntax is <maxsize>:<filename>
124 //If roll is 0, use split files, otherwise, use roll file
125 //For split, replace % in the file by the current count
126 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
128 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
129 if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
130 res->do_append = smpi_append2_file;
132 res->do_append = append2_file;
133 res->free_ = free_append2_;
134 xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
136 char* buf=xbt_strdup(arg);
137 char* sep=strchr(buf,':');
139 data->filename=xbt_strdup(sep+1);
141 data->limit=atol(buf);
146 open_append2_file(data);