Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not truncate the output on large messages, but switch to a dynamically allocated...
[simgrid.git] / src / xbt / xbt_log_layout_format.c
index a6df8ef..5e6bb17 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "portable.h" /* execinfo when available */
 #include "xbt/sysdep.h"
+#include "xbt/strbuff.h"
 #include "xbt/log_private.h"
 #include "gras/virtu.h" /* gras_os_myname (KILLME) */
 #include "xbt/synchro.h" /* xbt_thread_self_name */
 
 extern const char *xbt_log_priority_names[7];
 
-/* Get serious about checking buffer overflows during log construction */
+static double begin_of_time = -1;
+
+#define append1(fmt,fmt2,elm)                                         \
+   do {                                                               \
+       if (precision == -1) {                                        \
+          xbt_strbuff_append(buff, tmp=bprintf(fmt,elm));            \
+          free(tmp);                                                 \
+        } else {                                                      \
+          xbt_strbuff_append(buff, tmp=bprintf(fmt2,precision,elm)); \
+          free(tmp);                                                 \
+          precision = -1;                                            \
+       }                                                             \
+   } while (0)
+#define append2(fmt,elm,elm2)                                         \
+   do {                                                               \
+       xbt_strbuff_append(buff, tmp=bprintf(fmt,elm,elm2));          \
+       free(tmp);                                                    \
+       precision = -1;                                               \
+   } while (0)
+
+static void xbt_log_layout_format_dynamic(xbt_log_layout_t l,
+                                         xbt_log_event_t ev,
+                                         const char*fmt,
+                                         xbt_log_appender_t app) {
+  xbt_strbuff_t buff = xbt_strbuff_new();   
+  int precision=-1;
+  char *q = l->data;
+  char *tmp;
+  char *tmp2;
+
+  while (*q != '\0') {
+    if (*q == '%') {
+      q++;
+       handle_modifier:
+      switch (*q) {
+      case '\0':
+       fprintf(stderr,"Layout format (%s) ending with %%\n",(char*)l->data);
+       abort();
+      case '%':
+       xbt_strbuff_append(buff,"%");
+       break;
+      case 'n': /* platform-dependant line separator (LOG4J compliant) */
+       xbt_strbuff_append(buff,"\n");
+       break;
+      case 'e': /* plain space (SimGrid extension) */
+       xbt_strbuff_append(buff," ");
+       break;
+        
+      case '.': /* precision specifyier */
+       q++;
+       q += sscanf(q,"%d",&precision);
+       goto handle_modifier;
+
+      case 'c': /* category name; LOG4J compliant
+                  should accept a precision postfix to show the hierarchy */
+       append1("%s","%.*s",ev->cat->name);
+       break;
+      case 'p': /* priority name; LOG4J compliant */   
+       append1("%s","%.*s",xbt_log_priority_names[ev->priority]);
+       break;
+
+      case 'h': /* host name; SimGrid extension */
+       append1("%s","%.*s",gras_os_myname());
+       break;
+      case 't': /* thread name; LOG4J compliant */
+       append1("%s","%.*s",xbt_thread_self_name());
+       break;
+      case 'P': /* process name; SimGrid extension */
+       append1("%s","%.*s",xbt_procname());
+       break;
+      case 'i': /* process PID name; SimGrid extension */
+       append1("%d","%.*d",(*xbt_getpid)());
+       break;
+   
+      case 'F': /* file name; LOG4J compliant */
+       append1("%s","%.*s",ev->fileName);
+       break;
+      case 'l': /* location; LOG4J compliant */
+       append2("%s:%d",ev->fileName,ev->lineNum);
+       precision = -1; /* Ignored */
+       break;
+      case 'L': /* line number; LOG4J compliant */
+       append1("%d","%.*d",ev->lineNum);
+       break;
+      case 'M': /* method (ie, function) name; LOG4J compliant */
+       append1("%s","%.*s",ev->functionName);
+       break;
+      case 'b': /* backtrace; called %throwable in LOG4J */
+      case 'B': /* short backtrace; called %throwable{short} in LOG4J */
+#if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
+       {
+         xbt_ex_t e;
+         int i;
+         
+         e.used     = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
+         e.bt_strings = NULL;
+         e.msg=NULL;
+         e.remote=0;
+         xbt_backtrace_current(&e);
+         if (*q=='B') {
+            append1("%s","%.*s",e.bt_strings[2]+8);
+         } else {
+           for (i=2; i<e.used; i++)
+             append1("%s\n","%.*s\n",e.bt_strings[i]+8);
+         }
+          
+         xbt_ex_free(e);
+       }
+#else
+       append1("%s","%.*s","(no backtrace on this arch)");
+#endif
+       break;
+
+      case 'd': /* date; LOG4J compliant */
+       append1("%f","%.*f", gras_os_time());
+       break;
+      case 'r': /* application age; LOG4J compliant */
+       append1("%f","%.*f", gras_os_time()-begin_of_time);
+       break;
+       
+      case 'm': /* user-provided message; LOG4J compliant */
+       vasprintf(&tmp2, fmt, ev->ap_copy);
+       append1("%s","%.*s",tmp2);
+       free(tmp2);
+       break;
+
+      default:
+       fprintf(stderr,"Unknown %%%c sequence in layout format (%s)\n",
+               *q,(char*)l->data);
+       abort();
+      }
+      q++;
+    } else {
+      char tmp2[2];
+      tmp2[0] = *(q++);
+      tmp2[1] = '\0';
+      xbt_strbuff_append(buff,tmp2);
+    }
+  }
+  app->do_append(app,buff->data);
+  xbt_strbuff_free(buff);
+}
+
 #define check_overflow \
   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
-     p=ev->buffer + XBT_LOG_BUFF_SIZE - strlen(" >> OUTPUT TRUNCATED <<\n"); \
-     p+=sprintf(p," >> OUTPUT TRUNCATED <<\n");  \
+     xbt_log_layout_format_dynamic(l,ev,msg_fmt,app); \
      return;\
   } 
-
 static void xbt_log_layout_format_doit(xbt_log_layout_t l,
                                       xbt_log_event_t ev, 
-                                      const char *msg_fmt) {
-  static double begin_of_time = -1;
+                                      const char *msg_fmt,
+                                      xbt_log_appender_t app) {
   char *p,*q;
   int precision=-1;
 
@@ -249,6 +390,7 @@ static void xbt_log_layout_format_doit(xbt_log_layout_t l,
     }
   }
   *p = '\0';
+  app->do_append(app,ev->buffer);
 }
 
 static void xbt_log_layout_format_free(xbt_log_layout_t lay) {