Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change mmalloc.h into a public header
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/strbuff.h"        /* For dynamic version when the static one fails */
11 #include "xbt/log_private.h"
12 #include "xbt/synchro.h"        /* xbt_thread_name */
13
14 #include "gras/virtu.h"
15 #include <stdio.h>
16 #include "portable.h"
17
18 extern const char *xbt_log_priority_names[8];
19 extern int xbt_log_no_loc;
20
21 static double simple_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 {
28   xbt_strbuff_t buff = xbt_strbuff_new();
29   char loc_buff[256];
30   char *p;
31
32   /* Put every static information in a static buffer, and copy them in the dyn one */
33   p = loc_buff;
34   p += snprintf(p, 256 - (p - loc_buff), "[");
35
36   if (strlen(xbt_procname()))
37     p += snprintf(p, 256 - (p - loc_buff), "%s:%s:(%d) ",
38                   gras_os_myname(), xbt_procname(), (*xbt_getpid) ());
39   p +=
40     snprintf(p, 256 - (p - loc_buff), "%f] ",
41              gras_os_time() - simple_begin_of_time);
42   if (ev->priority != xbt_log_priority_info && xbt_log_no_loc==0)
43     p +=
44       snprintf(p, 256 - (p - loc_buff), "%s:%d: ", ev->fileName,
45                ev->lineNum);
46   p +=
47     snprintf(p, 256 - (p - loc_buff), "[%s/%s] ", ev->cat->name,
48              xbt_log_priority_names[ev->priority]);
49
50   xbt_strbuff_append(buff, loc_buff);
51
52   vasprintf(&p, fmt, ev->ap_copy);
53   xbt_strbuff_append(buff, p);
54   free(p);
55
56   xbt_strbuff_append(buff, "\n");
57
58   app->do_append(app, buff->data);
59   xbt_strbuff_free(buff);
60 }
61
62 /* only used after the format using: we suppose that the buffer is big enough to display our data */
63 #undef check_overflow
64 #define check_overflow \
65   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
66   xbt_log_layout_simple_dynamic(l,ev,fmt,app); \
67   return; \
68   }
69
70 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
71                                        xbt_log_event_t ev,
72                                        const char *fmt,
73                                        xbt_log_appender_t app)
74 {
75   char *p;
76
77   xbt_assert0(ev->priority >= 0,
78               "Negative logging priority naturally forbidden");
79   xbt_assert1(ev->priority < sizeof(xbt_log_priority_names),
80               "Priority %d is greater than the biggest allowed value",
81               ev->priority);
82
83   p = ev->buffer;
84   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[");
85   check_overflow;
86
87   /* Display the proc info if available */
88   const char *procname=xbt_procname();
89   if (strlen(procname)) {
90     p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%s:(%d) ",
91                   gras_os_myname(), procname, (*xbt_getpid) ());
92     check_overflow;
93   }
94
95   /* Display the date */
96   p +=
97     snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%f] ",
98              gras_os_time() - simple_begin_of_time);
99   check_overflow;
100
101   /* Display file position if not INFO */
102   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc)
103     p +=
104       snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%d: ",
105                ev->fileName, ev->lineNum);
106
107   /* Display category name */
108   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[%s/%s] ",
109                 ev->cat->name, xbt_log_priority_names[ev->priority]);
110
111   /* Display user-provided message */
112   p += vsnprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), fmt, ev->ap);
113   check_overflow;
114
115   /* End it */
116   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "\n");
117   check_overflow;
118   app->do_append(app, ev->buffer);
119 }
120
121 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
122 {
123   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
124   res->do_layout = xbt_log_layout_simple_doit;
125
126   if (simple_begin_of_time < 0)
127     simple_begin_of_time = gras_os_time();
128
129   return res;
130 }