Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix HAVE_FOOBAR flags handling
[simgrid.git] / src / xbt / xbt_log_appender_file.c
1 /* file_appender - a dumb log appender which simply prints to a file        */
2
3 /* Copyright (c) 2007-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "src/internal_config.h"
10 #include "xbt/sysdep.h"
11 #include "src/xbt/log_private.h"
12 #if HAVE_SMPI
13 #include "src/smpi/private.h" // to access bench_begin/end. Not ultraclean, I confess
14 #endif
15 #include <stdio.h>
16
17 static void append_file(xbt_log_appender_t this_, char *str) {
18   fputs(str, (FILE *) this_->data);
19 }
20
21 static void smpi_append_file(xbt_log_appender_t this_, char *str) {
22   fputs(str, (FILE *) this_->data);
23 }
24
25 static void free_(xbt_log_appender_t this_) {
26   if (this_->data != stderr)
27     fclose(this_->data);
28 }
29
30 #if HAVE_SMPI
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")));
35 #endif
36
37 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
38
39 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
40
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;
44   else
45     res->do_append = append_file;
46   res->free_ = free_;
47   if (arg)
48     res->data = (void *) fopen(arg, "w");
49   else
50     res->data = (void *) stderr;
51   return res;
52 }
53
54 struct xbt_log_append2_file_s {
55   FILE* file;
56   char* filename;
57   int count; //negative for roll
58   long  int limit;
59 };
60 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
61
62 #define APPEND2_END_TOKEN             "\n[End of log]\n"
63 #define APPEND2_END_TOKEN_CLEAR "\n                   "
64
65 static void open_append2_file(xbt_log_append2_file_t data){
66   if(data->count<0)
67   {
68     //Roll
69     if(!data->file)
70       data->file= fopen(data->filename, "w");
71     else{
72       fputs(APPEND2_END_TOKEN_CLEAR,data->file);
73       fseek(data->file,0,SEEK_SET);
74     }
75   }
76   else{
77     //printf("Splitting\n");
78     //Split
79     if(data->file)
80       fclose(data->file);
81     char newname[512];
82     char* pre=xbt_strdup(data->filename);
83     char* sep=strchr(pre,'%');
84     if(!sep)
85       sep=pre+strlen(pre);
86     char* post=sep+1;
87     *sep='\0';
88     snprintf(newname,511,"%s%i%s",pre,data->count,post);
89     data->count++;
90     data->file= fopen(newname, "w");
91     xbt_assert(data->file);
92
93   }
94 }
95   
96
97
98
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;
101    xbt_assert(d->file);
102    if(ftell(d->file)>=d->limit)
103    {
104      open_append2_file(d);
105    }
106    fputs(str, d->file);
107    if(d->count<0){
108           fputs(APPEND2_END_TOKEN,d->file);
109           fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
110    }
111 }
112
113 static void smpi_append2_file(xbt_log_appender_t this_, char *str) {
114   append2_file(this_,str);
115 }
116
117 static void free_append2_(xbt_log_appender_t this_) {
118   FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
119   if (f)
120     fclose(f);
121 }
122
123
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) {
128
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;
132   else
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);
136   xbt_assert(arg);
137   char* buf=xbt_strdup(arg);
138   char* sep=strchr(buf,':');
139   xbt_assert(sep>0);
140   data->filename=xbt_strdup(sep+1);
141   *sep='\0';
142   char *endptr;
143   data->limit=strtol(buf,&endptr,10);
144   xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
145   xbt_free(buf);
146   if(roll)
147     data->count=-1;
148   else
149     data->count=0;
150   open_append2_file(data);  
151   res->data = data;
152   return res;
153 }
154