Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
moved to %f instead of %g. It's usefull when values get large...
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log_app,log,"default logging handler");
16
17 /**
18  * The root category's default logging function.
19  */
20
21 extern const char *xbt_log_priority_names[7];
22
23 static void append_file(xbt_log_appender_t this, xbt_log_event_t ev,
24                         const char *fmt);
25
26 /*
27 struct xbt_log_appender_file_s {
28   xbt_log_appender_t* appender;
29   FILE *file;
30 };
31 */
32
33 static s_xbt_log_appender_t xbt_log_appender_file = { append_file, NULL } ;
34 /* appender_data=FILE* */
35
36 xbt_log_appender_t xbt_log_default_appender  = &xbt_log_appender_file;
37
38 static const char* xbt_logappender_verbose_information(void) {
39   static char buffer[256];
40   static double begin_of_time = -1;
41   
42   if (begin_of_time<0) 
43     begin_of_time=gras_os_time();
44
45   if(strlen(xbt_procname()))
46     sprintf(buffer,"%s:%s:(%d) %f", gras_os_myname(),
47             xbt_procname(),gras_os_getpid(),gras_os_time()-begin_of_time);
48   else 
49     buffer[0]=0;
50   
51   return buffer;
52 }
53
54 static void append_file(xbt_log_appender_t this,
55                         xbt_log_event_t ev, 
56                         const char *fmt) {
57
58   /* TODO: define a format field in struct for timestamp, etc.
59      struct DefaultLogAppender* this = (struct DefaultLogAppender*)this0;*/
60
61   char *procname = (char*)xbt_logappender_verbose_information();
62   if (!procname) 
63      procname = (char*)"";
64    
65     if ((FILE*)(this->appender_data) == NULL)
66       this->appender_data = (void*)stderr;
67     
68     xbt_assert0(ev->priority>=0,
69                  "Negative logging priority naturally forbidden");
70     xbt_assert1(ev->priority<sizeof(xbt_log_priority_names),
71                  "Priority %d is greater than the biggest allowed value",
72                  ev->priority);
73
74     fprintf(stderr, "[%s] %s:%d: ", procname, ev->fileName, ev->lineNum);
75     fprintf(stderr, "[%s/%s] ", 
76             ev->cat->name, xbt_log_priority_names[ev->priority] );
77     vfprintf(stderr, fmt, ev->ap);
78     fprintf(stderr, "\n");
79 }