1 /* file_appender - a dumb log appender which simply prints to a file */
3 /* Copyright (c) 2007-2014. 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 smpi_append_file(xbt_log_appender_t this_, char *str) {
19 fputs(str, (FILE *) this_->data);
22 static void free_(xbt_log_appender_t this_) {
23 if (this_->data != stderr)
27 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
29 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
31 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
32 if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
33 res->do_append = &smpi_append_file;
35 res->do_append = &append_file;
38 res->data = (void *) fopen(arg, "w");
40 res->data = (void *) stderr;
44 struct xbt_log_append2_file_s {
47 int count; //negative for roll
50 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
52 #define APPEND2_END_TOKEN "\n[End of log]\n"
53 #define APPEND2_END_TOKEN_CLEAR "\n "
55 static void open_append2_file(xbt_log_append2_file_t data){
59 data->file= fopen(data->filename, "w");
61 fputs(APPEND2_END_TOKEN_CLEAR,data->file);
62 fseek(data->file,0,SEEK_SET);
65 //printf("Splitting\n");
70 char* pre=xbt_strdup(data->filename);
71 char* sep=strchr(pre,'%');
76 snprintf(newname,511,"%s%i%s",pre,data->count,post);
78 data->file= fopen(newname, "w");
79 xbt_assert(data->file);
83 static void append2_file(xbt_log_appender_t this_, char *str) {
84 xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data;
86 if(ftell(d->file)>=d->limit) {
91 fputs(APPEND2_END_TOKEN,d->file);
92 fseek(d->file,-((signed long)strlen(APPEND2_END_TOKEN)),SEEK_CUR);
96 static void smpi_append2_file(xbt_log_appender_t this_, char *str) {
97 append2_file(this_,str);
100 static void free_append2_(xbt_log_appender_t this_) {
101 FILE* f=((xbt_log_append2_file_t)(this_->data))->file;
107 //syntax is <maxsize>:<filename>
108 //If roll is 0, use split files, otherwise, use roll file
109 //For split, replace % in the file by the current count
110 xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
112 xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
113 if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
114 res->do_append = &smpi_append2_file;
116 res->do_append = &append2_file;
117 res->free_ = &free_append2_;
118 xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1);
120 char* buf=xbt_strdup(arg);
121 char* sep=strchr(buf,':');
123 data->filename=xbt_strdup(sep+1);
126 data->limit=strtol(buf,&endptr,10);
127 xbt_assert(endptr[0]=='\0', "Invalid buffer size: %s", buf);
133 open_append2_file(data);