Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Constify pointer and reference parameters in src/xbt/log.cpp.
[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(const s_xbt_log_appender_t* this_, const char* str)
17 {
18   fputs(str, (FILE *) this_->data);
19 }
20
21 static void free_(const s_xbt_log_appender_t* this_)
22 {
23   fclose(static_cast<FILE*>(this_->data));
24 }
25
26 xbt_log_appender_t xbt_log_appender_stream(FILE* f)
27 {
28   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
29   res->do_append         = &append_file;
30   res->free_             = nullptr;
31   res->data              = static_cast<void*>(f);
32   return res;
33 }
34
35 xbt_log_appender_t xbt_log_appender_file_new(const char* arg)
36 {
37   if (arg == nullptr)
38     return xbt_log_appender_stream(stderr);
39   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
40   res->do_append         = &append_file;
41   res->free_             = &free_;
42   res->data              = static_cast<void*>(fopen(arg, "w"));
43   if (res->data == nullptr)
44     xbt_die("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 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
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 (!data->file) {
63       data->file= fopen(data->filename, "w");
64       if (data->file == nullptr)
65         xbt_die("Cannot open file: %s: %s", data->filename, strerror(errno));
66     } else {
67       fputs(APPEND2_END_TOKEN_CLEAR,data->file);
68       fseek(data->file,0,SEEK_SET);
69     }
70   } else{
71     //Split
72     if(data->file)
73       fclose(data->file);
74     char newname[512];
75     char* pre=xbt_strdup(data->filename);
76     char* sep=strchr(pre,'%');
77     if(!sep)
78       sep=pre+strlen(pre);
79     char* post=sep+1;
80     *sep='\0';
81     snprintf(newname,511,"%s%i%s",pre,data->count,post);
82     data->count++;
83     data->file= fopen(newname, "w");
84     if (data->file == nullptr)
85       xbt_die("Cannot open file: %s: %s", newname, strerror(errno));
86     xbt_free(pre);
87   }
88 }
89
90 static void append2_file(const s_xbt_log_appender_t* this_, const char* str)
91 {
92   xbt_log_append2_file_t d = (xbt_log_append2_file_t)this_->data;
93   xbt_assert(d->file);
94   if (ftell(d->file) >= d->limit) {
95     open_append2_file(d);
96   }
97   fputs(str, d->file);
98   if (d->count < 0) {
99     fputs(APPEND2_END_TOKEN, d->file);
100     fseek(d->file, -((signed long)strlen(APPEND2_END_TOKEN)), SEEK_CUR);
101   }
102 }
103
104 static void free_append2_(const s_xbt_log_appender_t* this_)
105 {
106   xbt_log_append2_file_t data = static_cast<xbt_log_append2_file_t>(this_->data);
107   if (data->file)
108     fclose(data->file);
109   xbt_free(data->filename);
110   xbt_free(data);
111 }
112
113
114 //syntax is  <maxsize>:<filename>
115 //If roll is 0, use split files, otherwise, use roll file
116 //For split, replace %  in the file by the current count
117 xbt_log_appender_t xbt_log_appender2_file_new(const char* arg, int roll)
118 {
119   xbt_log_appender_t res      = xbt_new0(s_xbt_log_appender_t, 1);
120   res->do_append              = &append2_file;
121   res->free_                  = &free_append2_;
122   xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
123   xbt_assert(arg);
124   char* buf=xbt_strdup(arg);
125   char* sep=strchr(buf,':');
126   xbt_assert(sep != nullptr);
127   data->filename=xbt_strdup(sep+1);
128   *sep='\0';
129   char *endptr;
130   data->limit=strtol(buf,&endptr,10);
131   xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
132   xbt_free(buf);
133   if(roll)
134     data->count=-1;
135   else
136     data->count=0;
137   open_append2_file(data);
138   res->data = data;
139   return res;
140 }