Logo AND Algorithmique Numérique Distribuée

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