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 "src/internal_config.h"
10 #include "xbt/sysdep.h"
11 #include "src/xbt/log_private.h"
13 #include "src/smpi/private.h" // to access bench_begin/end. Not ultraclean, I confess
17 static void append_file(xbt_log_appender_t this_, char *str) {
18 fputs(str, (FILE *) this_->data);
21 static void smpi_append_file(xbt_log_appender_t this_, char *str) {
22 fputs(str, (FILE *) this_->data);
25 static void free_(xbt_log_appender_t this_) {
26 if (this_->data != stderr)
31 void __smpi_bench_dont (void); // Stupid prototype
32 void __smpi_bench_dont (void) { /* I'm only a place-holder in case we link without SMPI */; }
33 void smpi_bench_begin(void) __attribute__ ((weak, alias ("__smpi_bench_dont")));
34 void smpi_bench_end(void) __attribute__ ((weak, alias ("__smpi_bench_dont")));
37 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
39 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
41 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
42 if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
43 res->do_append = smpi_append_file;
45 res->do_append = append_file;
48 res->data = (void *) fopen(arg, "w");
50 res->data = (void *) stderr;
54 struct xbt_log_append2_file_s {
57 int count; //negative for roll
60 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
62 #define APPEND2_END_TOKEN "\n[End of log]\n"
63 #define APPEND2_END_TOKEN_CLEAR "\n "
65 static void open_append2_file(xbt_log_append2_file_t data){
70 data->file= fopen(data->filename, "w");
72 fputs(APPEND2_END_TOKEN_CLEAR,data->file);
73 fseek(data->file,0,SEEK_SET);
77 //printf("Splitting\n");
82 char* pre=xbt_strdup(data->filename);
83 char* sep=strchr(pre,'%');
88 snprintf(newname,511,"%s%i%s",pre,data->count,post);
90 data->file= fopen(newname, "w");
91 xbt_assert(data->file);
99 static void append2_file(xbt_log_appender_t this_, char *str) {
100 xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
102 if(ftell(d->file)>=d->limit)
104 open_append2_file(d);
108 fputs(APPEND2_END_TOKEN,d->file);
109 fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
113 static void smpi_append2_file(xbt_log_appender_t this_, char *str) {
114 append2_file(this_,str);
117 static void free_append2_(xbt_log_appender_t this_) {
118 FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
124 //syntax is <maxsize>:<filename>
125 //If roll is 0, use split files, otherwise, use roll file
126 //For split, replace % in the file by the current count
127 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
129 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
130 if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
131 res->do_append = smpi_append2_file;
133 res->do_append = append2_file;
134 res->free_ = free_append2_;
135 xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
137 char* buf=xbt_strdup(arg);
138 char* sep=strchr(buf,':');
140 data->filename=xbt_strdup(sep+1);
143 data->limit=strtol(buf,&endptr,10);
144 xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
150 open_append2_file(data);