Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make dictionary internal table dynamic (and automatically resized).
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* $Id$ */
2
3 /* layout_simple - a dumb log layout                                        */
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/strbuff.h" /* For dynamic version when the static one fails */
12 #include "xbt/log_private.h"
13 #include "xbt/synchro.h" /* xbt_thread_name */
14
15 #include "gras/virtu.h"
16 #include <stdio.h>
17 #include "portable.h"
18
19 extern const char *xbt_log_priority_names[7];
20
21 static double begin_of_time = -1;
22
23 static void xbt_log_layout_simple_dynamic(xbt_log_layout_t l,
24                                           xbt_log_event_t ev,
25                                           const char*fmt,
26                                           xbt_log_appender_t app) {
27    xbt_strbuff_t buff = xbt_strbuff_new();
28    char *p;
29    
30    /* Put every static information in the static buffer, and copy them in the dyn one */
31    p = ev->buffer;
32    p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"[");
33
34    if(strlen(xbt_procname()))
35      p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s:%s:(%d) ", 
36                    gras_os_myname(), xbt_procname(),(*xbt_getpid)());
37    p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%f] ", gras_os_time()-begin_of_time);
38    if (ev->priority != xbt_log_priority_info)
39      p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s:%d: ", ev->fileName, ev->lineNum);
40    p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "[%s/%s] ", 
41                  ev->cat->name, xbt_log_priority_names[ev->priority] );
42    
43    xbt_strbuff_append(buff,ev->buffer);
44
45    vasprintf(&p,fmt,ev->ap_copy);
46    xbt_strbuff_append(buff,p);
47    free(p);
48
49    xbt_strbuff_append(buff,"\n");
50
51    app->do_append(app,buff->data);
52    xbt_strbuff_free(buff);
53 }
54
55 /* only used after the format using: we suppose that the buffer is big enough to display our data */
56 #define check_overflow \
57   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
58      xbt_log_layout_simple_dynamic(l,ev,fmt,app); \
59      return; \
60   } 
61
62 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
63                                        xbt_log_event_t ev, 
64                                        const char *fmt,
65                                        xbt_log_appender_t app) {
66   char *p;  
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   if (begin_of_time<0) 
75     begin_of_time=gras_os_time();
76
77   p = ev->buffer;
78   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"[");
79
80   /* Display the proc info if available */
81   if(strlen(xbt_procname()))
82     p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s:%s:(%d) ", 
83                  gras_os_myname(), xbt_procname(),(*xbt_getpid)());
84
85   /* Display the date */
86   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%f] ", gras_os_time()-begin_of_time);
87
88   /* Display file position if not INFO*/
89   if (ev->priority != xbt_log_priority_info)
90     p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s:%d: ", ev->fileName, ev->lineNum);
91
92   /* Display category name */
93   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "[%s/%s] ", 
94                ev->cat->name, xbt_log_priority_names[ev->priority] );
95
96   /* Display user-provided message */
97   p += vsnprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), fmt, ev->ap);
98   check_overflow;
99    
100   /* End it */
101   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "\n");
102   check_overflow;
103   app->do_append(app,ev->buffer);
104 }
105
106 xbt_log_layout_t xbt_log_layout_simple_new(char *arg) {
107   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1);
108   res->do_layout = xbt_log_layout_simple_doit;
109   return res;
110 }