Logo AND Algorithmique Numérique Distribuée

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