Logo AND Algorithmique Numérique Distribuée

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