From 2c1ecb86b090ca89cb62cd915dba85c88cc9b5f7 Mon Sep 17 00:00:00 2001 From: Augustin Degomme Date: Fri, 12 Dec 2014 16:46:01 +0100 Subject: [PATCH] Patch by F.Chaix : add two "new" log appender methods : split and roll split will create new files when a specified size is reached roll will overwrite the file when this size is reached example syntax is : --log=root.appender:splitfile:10000:myfilename_%.txt The % is a wildcard that will be replaced by the number of the file. If no % is present, it will be at the end --- include/xbt/log.h | 1 + src/xbt/log.c | 4 ++ src/xbt/xbt_log_appender_file.c | 99 +++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/include/xbt/log.h b/include/xbt/log.h index 073bc8ddbb..a4c4d8ac06 100644 --- a/include/xbt/log.h +++ b/include/xbt/log.h @@ -340,6 +340,7 @@ XBT_PUBLIC(void) xbt_log_additivity_set(xbt_log_category_t cat, XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_simple_new(char *arg); XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_format_new(char *arg); XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(char *arg); +XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender2_file_new(char *arg,int roll); /* ********************************** */ diff --git a/src/xbt/log.c b/src/xbt/log.c index f04e5b5c51..159b85a4f2 100644 --- a/src/xbt/log.c +++ b/src/xbt/log.c @@ -1156,6 +1156,10 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string) if (!strncmp(neweq, "file:", 5)) { set->appender = xbt_log_appender_file_new(neweq + 5); + }else if (!strncmp(neweq, "rollfile:", 9)) { + set->appender = xbt_log_appender2_file_new(neweq + 9,1); + }else if (!strncmp(neweq, "splitfile:", 10)) { + set->appender = xbt_log_appender2_file_new(neweq + 10,0); } else { THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq); } diff --git a/src/xbt/xbt_log_appender_file.c b/src/xbt/xbt_log_appender_file.c index 124c51f956..d06d9b9d00 100644 --- a/src/xbt/xbt_log_appender_file.c +++ b/src/xbt/xbt_log_appender_file.c @@ -49,3 +49,102 @@ xbt_log_appender_t xbt_log_appender_file_new(char *arg) { res->data = (void *) stderr; return res; } + +struct xbt_log_append2_file_s { + FILE* file; + char* filename; + int count; //negative for roll + long int limit; +}; +typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t; + +#define APPEND2_END_TOKEN "\n[End of log]\n" +#define APPEND2_END_TOKEN_CLEAR "\n " + +static void open_append2_file(xbt_log_append2_file_t data){ + if(data->count<0) + { + //Roll + if(!data->file) + data->file= fopen(data->filename, "w"); + else{ + fputs(APPEND2_END_TOKEN_CLEAR,data->file); + fseek(data->file,0,SEEK_SET); + } + } + else{ + //printf("Splitting\n"); + //Split + if(data->file) + fclose(data->file); + char newname[512]; + char* pre=xbt_strdup(data->filename); + char* sep=strchr(pre,'%'); + if(!sep) + sep=pre+strlen(pre); + char* post=sep+1; + *sep='\0'; + snprintf(newname,511,"%s%i%s",pre,data->count,post); + data->count++; + data->file= fopen(newname, "w"); + xbt_assert(data->file); + + } +} + + + + +static void append2_file(xbt_log_appender_t this_, char *str) { + xbt_log_append2_file_t d=(xbt_log_append2_file_t) this_->data; + xbt_assert(d->file); + if(ftell(d->file)>=d->limit) + { + open_append2_file(d); + } + fputs(str, d->file); + if(d->count<0){ + fputs(APPEND2_END_TOKEN,d->file); + fseek(d->file,-strlen(APPEND2_END_TOKEN),SEEK_CUR); + } +} + +static void smpi_append2_file(xbt_log_appender_t this_, char *str) { + append2_file(this_,str); +} + +static void free_append2_(xbt_log_appender_t this_) { + FILE* f=((xbt_log_append2_file_t)(this_->data))->file; + if (f) + fclose(f); +} + + +//syntax is : +//If roll is 0, use split files, otherwise, use roll file +//For split, replace % in the file by the current count +xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) { + + xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1); + if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition + res->do_append = smpi_append2_file; + else + res->do_append = append2_file; + res->free_ = free_append2_; + xbt_log_append2_file_t data = xbt_new0(struct xbt_log_append2_file_s, 1); + xbt_assert(arg); + char* buf=xbt_strdup(arg); + char* sep=strchr(buf,':'); + xbt_assert(sep>0); + data->filename=xbt_strdup(sep+1); + *sep='\0'; + data->limit=atol(buf); + if(roll) + data->count=-1; + else + data->count=0; + open_append2_file(data); + res->data = data; + return res; +} + -- 2.20.1