Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tweak things here and there so that we can propagate exceptions across the network...
[simgrid.git] / src / xbt / log_default_appender.c
1 /* $Id$ */
2
3 /* file_appender - a dumb log appender which simply prints to stdout        */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include <stdio.h>
13 #include "gras/virtu.h"
14
15 /**
16  * The root category's default logging function.
17  */
18
19 extern const char *xbt_log_priority_names[7];
20
21 static void append_file(xbt_log_appender_t this_appender, xbt_log_event_t ev,
22                         const char *fmt);
23
24 /*
25 struct xbt_log_appender_file_s {
26   xbt_log_appender_t* appender;
27   FILE *file;
28 };
29 */
30
31 static s_xbt_log_appender_t xbt_log_appender_file = { append_file, NULL } ;
32 /* appender_data=FILE* */
33
34 xbt_log_appender_t xbt_log_default_appender  = &xbt_log_appender_file;
35
36 static const char* xbt_logappender_verbose_information(void) {
37   static char buffer[256];
38   static double begin_of_time = -1;
39   
40   if (begin_of_time<0) 
41     begin_of_time=gras_os_time();
42
43   if(strlen(xbt_procname()))
44     sprintf(buffer,"%s:%s:(%d) %f", gras_os_myname(),
45             xbt_procname(),gras_os_getpid(),gras_os_time()-begin_of_time);
46   else 
47     buffer[0]=0;
48   
49   return buffer;
50 }
51
52 static void append_file(xbt_log_appender_t this_appender,
53                         xbt_log_event_t ev, 
54                         const char *fmt) {
55
56   /* TODO: define a format field in struct for timestamp, etc.
57      struct DefaultLogAppender* this = (struct DefaultLogAppender*)this0;*/
58
59   char *procname = (char*)xbt_logappender_verbose_information();
60   if (!procname) 
61      procname = (char*)"";
62    
63     if ((FILE*)(this_appender->appender_data) == NULL)
64       this_appender->appender_data = (void*)stderr;
65     
66     xbt_assert0(ev->priority>=0,
67                  "Negative logging priority naturally forbidden");
68     xbt_assert1(ev->priority<sizeof(xbt_log_priority_names),
69                  "Priority %d is greater than the biggest allowed value",
70                  ev->priority);
71
72     fprintf(stderr, "[%s] %s:%d: ", procname, ev->fileName, ev->lineNum);
73     fprintf(stderr, "[%s/%s] ", 
74             ev->cat->name, xbt_log_priority_names[ev->priority] );
75     vfprintf(stderr, fmt, ev->ap);
76     fprintf(stderr, "\n");
77 }