Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This seems to be useless since ... a long time, now (and it breaks on macos)
[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   {
58     //Roll
59     if(!data->file)
60       data->file= fopen(data->filename, "w");
61     else{
62       fputs(APPEND2_END_TOKEN_CLEAR,data->file);
63       fseek(data->file,0,SEEK_SET);
64     }
65   }
66   else{
67     //printf("Splitting\n");
68     //Split
69     if(data->file)
70       fclose(data->file);
71     char newname[512];
72     char* pre=xbt_strdup(data->filename);
73     char* sep=strchr(pre,'%');
74     if(!sep)
75       sep=pre+strlen(pre);
76     char* post=sep+1;
77     *sep='\0';
78     snprintf(newname,511,"%s%i%s",pre,data->count,post);
79     data->count++;
80     data->file= fopen(newname, "w");
81     xbt_assert(data->file);
82
83   }
84 }
85   
86
87
88
89 static void append2_file(xbt_log_appender_t this_, char *str) {
90    xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
91    xbt_assert(d->file);
92    if(ftell(d->file)>=d->limit)
93    {
94      open_append2_file(d);
95    }
96    fputs(str, d->file);
97    if(d->count<0){
98           fputs(APPEND2_END_TOKEN,d->file);
99           fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
100    }
101 }
102
103 static void smpi_append2_file(xbt_log_appender_t this_, char *str) {
104   append2_file(this_,str);
105 }
106
107 static void free_append2_(xbt_log_appender_t this_) {
108   FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
109   if (f)
110     fclose(f);
111 }
112
113
114 //syntax is  <maxsize>:<filename>
115 //If roll is 0, use split files, otherwise, use roll file
116 //For split, replace %  in the file by the current count
117 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
118
119   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
120   if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
121     res->do_append = smpi_append2_file;
122   else
123     res->do_append = append2_file;
124   res->free_ = free_append2_;
125   xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
126   xbt_assert(arg);
127   char* buf=xbt_strdup(arg);
128   char* sep=strchr(buf,':');
129   xbt_assert(sep>0);
130   data->filename=xbt_strdup(sep+1);
131   *sep='\0';
132   char *endptr;
133   data->limit=strtol(buf,&endptr,10);
134   xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
135   xbt_free(buf);
136   if(roll)
137     data->count=-1;
138   else
139     data->count=0;
140   open_append2_file(data);  
141   res->data = data;
142   return res;
143 }
144