Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics from patch review
[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 #include <stdio.h>
13
14 static void append_file(xbt_log_appender_t this_, char *str) {
15   fputs(str, (FILE *) this_->data);
16 }
17
18 static void smpi_append_file(xbt_log_appender_t this_, char *str) {
19   fputs(str, (FILE *) this_->data);
20 }
21
22 static void free_(xbt_log_appender_t this_) {
23   if (this_->data != stderr)
24     fclose(this_->data);
25 }
26
27 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
28
29 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
30
31   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
32   if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
33     res->do_append = smpi_append_file;
34   else
35     res->do_append = append_file;
36   res->free_ = free_;
37   if (arg)
38     res->data = (void *) fopen(arg, "w");
39   else
40     res->data = (void *) stderr;
41   return res;
42 }
43
44 struct xbt_log_append2_file_s {
45   FILE* file;
46   char* filename;
47   int count; //negative for roll
48   long  int limit;
49 };
50 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
51
52 #define APPEND2_END_TOKEN             "\n[End of log]\n"
53 #define APPEND2_END_TOKEN_CLEAR "\n                   "
54
55 static void open_append2_file(xbt_log_append2_file_t data){
56   if(data->count<0) {
57     //Roll
58     if(!data->file)
59       data->file= fopen(data->filename, "w");
60     else{
61       fputs(APPEND2_END_TOKEN_CLEAR,data->file);
62       fseek(data->file,0,SEEK_SET);
63     }
64   } else{
65     //printf("Splitting\n");
66     //Split
67     if(data->file)
68       fclose(data->file);
69     char newname[512];
70     char* pre=xbt_strdup(data->filename);
71     char* sep=strchr(pre,'%');
72     if(!sep)
73       sep=pre+strlen(pre);
74     char* post=sep+1;
75     *sep='\0';
76     snprintf(newname,511,"%s%i%s",pre,data->count,post);
77     data->count++;
78     data->file= fopen(newname, "w");
79     xbt_assert(data->file);
80   }
81 }
82
83 static void append2_file(xbt_log_appender_t this_, char *str) {
84    xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
85    xbt_assert(d->file);
86    if(ftell(d->file)>=d->limit) {
87      open_append2_file(d);
88    }
89    fputs(str, d->file);
90    if(d->count<0){
91      fputs(APPEND2_END_TOKEN,d->file);
92      fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
93    }
94 }
95
96 static void smpi_append2_file(xbt_log_appender_t this_, char *str) {
97   append2_file(this_,str);
98 }
99
100 static void free_append2_(xbt_log_appender_t this_) {
101   FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
102   if (f)
103     fclose(f);
104 }
105
106
107 //syntax is  <maxsize>:<filename>
108 //If roll is 0, use split files, otherwise, use roll file
109 //For split, replace %  in the file by the current count
110 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
111
112   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
113   if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
114     res->do_append = smpi_append2_file;
115   else
116     res->do_append = append2_file;
117   res->free_ = free_append2_;
118   xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
119   xbt_assert(arg);
120   char* buf=xbt_strdup(arg);
121   char* sep=strchr(buf,':');
122   xbt_assert(sep>0);
123   data->filename=xbt_strdup(sep+1);
124   *sep='\0';
125   char *endptr;
126   data->limit=strtol(buf,&endptr,10);
127   xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
128   xbt_free(buf);
129   if(roll)
130     data->count=-1;
131   else
132     data->count=0;
133   open_append2_file(data);  
134   res->data = data;
135   return res;
136 }