1 /* file_appender - a dumb log appender which simply prints to a file */
3 /* Copyright (c) 2007-2017. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "src/internal_config.h"
10 #include "xbt/sysdep.h"
11 #include "src/xbt/log_private.h"
14 static void append_file(xbt_log_appender_t this_, char *str) {
15 fputs(str, (FILE *) this_->data);
18 static void free_(xbt_log_appender_t this_) {
19 if (this_->data != stderr)
23 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
25 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
26 res->do_append = &append_file;
29 res->data = (void *) fopen(arg, "w");
31 res->data = (void *) stderr;
35 struct xbt_log_append2_file_s {
38 int count; //negative for roll
41 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
43 #define APPEND2_END_TOKEN "\n[End of log]\n"
44 #define APPEND2_END_TOKEN_CLEAR "\n "
46 static void open_append2_file(xbt_log_append2_file_t data){
50 data->file= fopen(data->filename, "w");
52 fputs(APPEND2_END_TOKEN_CLEAR,data->file);
53 fseek(data->file,0,SEEK_SET);
60 char* pre=xbt_strdup(data->filename);
61 char* sep=strchr(pre,'%');
66 snprintf(newname,511,"%s%i%s",pre,data->count,post);
68 data->file= fopen(newname, "w");
69 xbt_assert(data->file);
73 static void append2_file(xbt_log_appender_t this_, char *str) {
74 xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
76 if(ftell(d->file)>=d->limit) {
81 fputs(APPEND2_END_TOKEN,d->file);
82 fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
86 static void free_append2_(xbt_log_appender_t this_) {
87 FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
93 //syntax is <maxsize>:<filename>
94 //If roll is 0, use split files, otherwise, use roll file
95 //For split, replace % in the file by the current count
96 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
98 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
99 res->do_append = &append2_file;
100 res->free_ = &free_append2_;
101 xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
103 char* buf=xbt_strdup(arg);
104 char* sep=strchr(buf,':');
106 data->filename=xbt_strdup(sep+1);
109 data->limit=strtol(buf,&endptr,10);
110 xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
116 open_append2_file(data);