Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[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-2022. 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 #include <string>
16
17 static void append_file(const s_xbt_log_appender_t* this_, const char* str)
18 {
19   fputs(str, (FILE *) this_->data);
20 }
21
22 static void free_(const s_xbt_log_appender_t* this_)
23 {
24   fclose(static_cast<FILE*>(this_->data));
25 }
26
27 xbt_log_appender_t xbt_log_appender_stream(FILE* f)
28 {
29   auto* res      = xbt_new0(s_xbt_log_appender_t, 1);
30   res->do_append = &append_file;
31   res->free_     = nullptr;
32   res->data      = static_cast<void*>(f);
33   return res;
34 }
35
36 xbt_log_appender_t xbt_log_appender_file_new(const char* arg)
37 {
38   if (arg == nullptr)
39     return xbt_log_appender_stream(stderr);
40   auto* res      = xbt_new0(s_xbt_log_appender_t, 1);
41   res->do_append = &append_file;
42   res->free_     = &free_;
43   res->data      = static_cast<void*>(fopen(arg, "w"));
44   xbt_assert(res->data != nullptr, "Cannot open file: %s: %s", arg, strerror(errno));
45   return res;
46 }
47
48 struct xbt_log_append2_file_s {
49   FILE* file;
50   char* filename;
51   int count; //negative for roll
52   long  int limit;
53 };
54 using xbt_log_append2_file_t = xbt_log_append2_file_s*;
55
56 static constexpr const char* APPEND2_END_TOKEN       = "\n[End of log]\n";
57 static constexpr const char* APPEND2_END_TOKEN_CLEAR = "\n                   ";
58
59 static void open_append2_file(xbt_log_append2_file_t data){
60   if(data->count<0) {
61     //Roll
62     if (not data->file) {
63       data->file= fopen(data->filename, "w");
64       xbt_assert(data->file != nullptr, "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* pre=xbt_strdup(data->filename);
74     char* sep=strchr(pre,'%');
75     if (not sep)
76       sep=pre+strlen(pre);
77     const char* post    = sep + 1;
78     *sep                = '\0';
79     std::string newname = pre + std::to_string(data->count) + post;
80     data->count++;
81     data->file = fopen(newname.c_str(), "w");
82     xbt_assert(data->file != nullptr, "Cannot open file: %s: %s", newname.c_str(), strerror(errno));
83     xbt_free(pre);
84   }
85 }
86
87 static void append2_file(const s_xbt_log_appender_t* this_, const char* str)
88 {
89   auto* d = static_cast<xbt_log_append2_file_t>(this_->data);
90   xbt_assert(d->file);
91   if (ftell(d->file) >= d->limit) {
92     open_append2_file(d);
93   }
94   fputs(str, d->file);
95   if (d->count < 0) {
96     fputs(APPEND2_END_TOKEN, d->file);
97     fseek(d->file, -((signed long)strlen(APPEND2_END_TOKEN)), SEEK_CUR);
98   }
99 }
100
101 static void free_append2_(const s_xbt_log_appender_t* this_)
102 {
103   auto* data = static_cast<xbt_log_append2_file_t>(this_->data);
104   if (data->file)
105     fclose(data->file);
106   xbt_free(data->filename);
107   xbt_free(data);
108 }
109
110
111 //syntax is  <maxsize>:<filename>
112 //If roll is 0, use split files, otherwise, use roll file
113 //For split, replace %  in the file by the current count
114 xbt_log_appender_t xbt_log_appender2_file_new(const char* arg, int roll)
115 {
116   auto* res      = xbt_new0(s_xbt_log_appender_t, 1);
117   res->do_append = &append2_file;
118   res->free_     = &free_append2_;
119   auto* data     = xbt_new0(struct xbt_log_append2_file_s, 1);
120   xbt_assert(arg);
121   char* buf=xbt_strdup(arg);
122   char* sep=strchr(buf,':');
123   xbt_assert(sep != nullptr);
124   data->filename=xbt_strdup(sep+1);
125   *sep='\0';
126   char *endptr;
127   data->limit=strtol(buf,&endptr,10);
128   xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
129   xbt_free(buf);
130   if(roll)
131     data->count=-1;
132   else
133     data->count=0;
134   open_append2_file(data);
135   res->data = data;
136   return res;
137 }