Logo AND Algorithmique Numérique Distribuée

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