Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Patch by F.Chaix : add two "new" log appender methods : split and roll
authorAugustin Degomme <augustin.degomme@imag.fr>
Fri, 12 Dec 2014 15:46:01 +0000 (16:46 +0100)
committerAugustin Degomme <augustin.degomme@imag.fr>
Fri, 12 Dec 2014 16:29:32 +0000 (17:29 +0100)
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
src/xbt/log.c
src/xbt/xbt_log_appender_file.c

index 073bc8d..a4c4d8a 100644 (file)
@@ -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_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);
 
 
 /* ********************************** */
 
 
 /* ********************************** */
index f04e5b5..159b85a 100644 (file)
@@ -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);
 
     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);
     }
     } else {
       THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
     }
index 124c51f..d06d9b9 100644 (file)
@@ -49,3 +49,102 @@ xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
     res->data = (void *) stderr;
   return res;
 }
     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  <maxsize>:<filename>
+//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;
+}
+