Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2e938d1afa321069fe841141050a49179be9ef13
[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 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     //printf("Splitting\n");
57     //Split
58     if(data->file)
59       fclose(data->file);
60     char newname[512];
61     char* pre=xbt_strdup(data->filename);
62     char* sep=strchr(pre,'%');
63     if(!sep)
64       sep=pre+strlen(pre);
65     char* post=sep+1;
66     *sep='\0';
67     snprintf(newname,511,"%s%i%s",pre,data->count,post);
68     data->count++;
69     data->file= fopen(newname, "w");
70     xbt_assert(data->file);
71   }
72 }
73
74 static void append2_file(xbt_log_appender_t this_, char *str) {
75    xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
76    xbt_assert(d->file);
77    if(ftell(d->file)>=d->limit) {
78      open_append2_file(d);
79    }
80    fputs(str, d->file);
81    if(d->count<0){
82      fputs(APPEND2_END_TOKEN,d->file);
83      fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
84    }
85 }
86
87 static void free_append2_(xbt_log_appender_t this_) {
88   FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
89   if (f)
90     fclose(f);
91 }
92
93
94 //syntax is  <maxsize>:<filename>
95 //If roll is 0, use split files, otherwise, use roll file
96 //For split, replace %  in the file by the current count
97 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
98
99   xbt_log_appender_t res      = xbt_new0(s_xbt_log_appender_t, 1);
100   res->do_append              = &append2_file;
101   res->free_                  = &free_append2_;
102   xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
103   xbt_assert(arg);
104   char* buf=xbt_strdup(arg);
105   char* sep=strchr(buf,':');
106   xbt_assert(sep>0);
107   data->filename=xbt_strdup(sep+1);
108   *sep='\0';
109   char *endptr;
110   data->limit=strtol(buf,&endptr,10);
111   xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
112   xbt_free(buf);
113   if(roll)
114     data->count=-1;
115   else
116     data->count=0;
117   open_append2_file(data);
118   res->data = data;
119   return res;
120 }