Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not display file position at INFO level (reduce the amount of false positive in...
[simgrid.git] / src / xbt / log_default_appender.c
index d385a71..51e4e15 100644 (file)
@@ -1,73 +1,80 @@
-// $Id$
-// Copyright (c) 2001, Bit Farm, Inc. All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-// 1. Redistributions of source code must retain the above copyright
-//    notice, this list of conditions and the following disclaimer.
-// 2. Redistributions in binary form must reproduce the above copyright
-//    notice, this list of conditions and the following disclaimer in the
-//    documentation and/or other materials provided with the distribution.
-// 3. The name of the author may not be used to endorse or promote products
-//    derived from this software without specific prior written permission.
-// 
-// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-#include <gras.h>
+/* $Id$ */
+
+/* file_appender - a dumb log appender which simply prints to stdout        */
+
+/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "xbt/sysdep.h"
+#include "xbt/log.h"
 #include <stdio.h>
+#include "gras/virtu.h"
 
 /**
  * The root category's default logging function.
  */
 
-extern const char *gras_log_priority_names[7];
+extern const char *xbt_log_priority_names[7];
 
-static void append_file(gras_log_appender_t* this, gras_log_event_t* ev);
+static void append_file(xbt_log_appender_t this_appender, xbt_log_event_t ev,
+                       const char *fmt);
 
 /*
-struct gras_log_appender_file_s {
-  gras_log_appender_t* appender;
+struct xbt_log_appender_file_s {
+  xbt_log_appender_t* appender;
   FILE *file;
 };
 */
 
-static gras_log_appender_t gras_log_appender_file = { append_file, NULL } ;
+static s_xbt_log_appender_t xbt_log_appender_file = { append_file, NULL } ;
 /* appender_data=FILE* */
 
-gras_log_appender_t* gras_log_default_appender  = &gras_log_appender_file;
+xbt_log_appender_t xbt_log_default_appender  = &xbt_log_appender_file;
 
-static void append_file(gras_log_appender_t* this, gras_log_event_t* ev) {
+static const char* xbt_logappender_verbose_information(void) {
+  static char buffer[256];
+  static double begin_of_time = -1;
+  
+  if (begin_of_time<0) 
+    begin_of_time=gras_os_time();
 
-    // TODO: define a format field in struct for timestamp, etc.
-    const char *pn;
-    char buf[20];
-    //    struct DefaultLogAppender* this = (struct DefaultLogAppender*)this0;
-    
-    if ((FILE*)(this->appender_data) == NULL)
-      this->appender_data = (void*)stderr;
+  if(strlen(xbt_procname()))
+    sprintf(buffer,"%s:%s:(%ld) %f", gras_os_myname(),
+           xbt_procname(),gras_os_getpid(),gras_os_time()-begin_of_time);
+  else 
+    sprintf(buffer,"%f", gras_os_time()-begin_of_time);
+  
+  return buffer;
+}
+
+static void append_file(xbt_log_appender_t this_appender,
+                       xbt_log_event_t ev, 
+                       const char *fmt) {
+
+  /* TODO: define a format field in struct for timestamp, etc.
+     struct DefaultLogAppender* this = (struct DefaultLogAppender*)this0;*/
+
+  char *procname = (char*)xbt_logappender_verbose_information();
+  if (!procname) 
+     procname = (char*)"";
+   
+    if ((FILE*)(this_appender->appender_data) == NULL)
+      this_appender->appender_data = (void*)stderr;
     
-    if (ev->priority < 0) {
-        pn = "Negative Priority NOT ALLOWED!!";
-    }
-    else if (ev->priority < sizeof(gras_log_priority_names)) {
-        pn = gras_log_priority_names[ev->priority];
-    } else {
-        sprintf(buf, "%s+%d",
-                gras_log_priority_names[sizeof(gras_log_priority_names)-1],
-                ev->priority - sizeof(gras_log_priority_names) + 1);
-    }
-    fprintf(stderr, "%s:%d: ", ev->fileName, ev->lineNum);
-    fprintf(stderr, "[%s/%s] ", ev->cat->name,pn);
-    vfprintf(stderr, ev->fmt, ev->ap);
+    xbt_assert0(ev->priority>=0,
+                "Negative logging priority naturally forbidden");
+    xbt_assert1(ev->priority<sizeof(xbt_log_priority_names),
+                "Priority %d is greater than the biggest allowed value",
+                ev->priority);
+
+    if (ev->priority != xbt_log_priority_info)
+     fprintf(stderr, "[%s] %s:%d: ", procname, ev->fileName, ev->lineNum);
+    else
+     fprintf(stderr, "[%s] ", procname);
+    fprintf(stderr, "[%s/%s] ", 
+           ev->cat->name, xbt_log_priority_names[ev->priority] );
+    vfprintf(stderr, fmt, ev->ap);
     fprintf(stderr, "\n");
 }