Logo AND Algorithmique Numérique Distribuée

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