Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a pimple to get the ability to trace the amount of messages per size as histogram...
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* $Id: xbt_log_layout_simple.c 6309 2009-05-26 15:29:22Z mquinson $ */
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[8];
20 extern int xbt_log_no_loc;
21
22 static double simple_begin_of_time = -1;
23
24 static void xbt_log_layout_simple_dynamic(xbt_log_layout_t l,
25                                           xbt_log_event_t ev,
26                                           const char *fmt,
27                                           xbt_log_appender_t app)
28 {
29   xbt_strbuff_t buff = xbt_strbuff_new();
30   char loc_buff[256];
31   char *p;
32
33   /* Put every static information in a static buffer, and copy them in the dyn one */
34   p = loc_buff;
35   p += snprintf(p, 256 - (p - loc_buff), "[");
36
37   if (strlen(xbt_procname()))
38     p += snprintf(p, 256 - (p - loc_buff), "%s:%s:(%d) ",
39                   gras_os_myname(), xbt_procname(), (*xbt_getpid) ());
40   p +=
41     snprintf(p, 256 - (p - loc_buff), "%f] ",
42              gras_os_time() - simple_begin_of_time);
43   if (ev->priority != xbt_log_priority_info && xbt_log_no_loc==0)
44     p +=
45       snprintf(p, 256 - (p - loc_buff), "%s:%d: ", ev->fileName,
46                ev->lineNum);
47   p +=
48     snprintf(p, 256 - (p - loc_buff), "[%s/%s] ", ev->cat->name,
49              xbt_log_priority_names[ev->priority]);
50
51   xbt_strbuff_append(buff, loc_buff);
52
53   vasprintf(&p, fmt, ev->ap_copy);
54   xbt_strbuff_append(buff, p);
55   free(p);
56
57   xbt_strbuff_append(buff, "\n");
58
59   app->do_append(app, buff->data);
60   xbt_strbuff_free(buff);
61 }
62
63 /* only used after the format using: we suppose that the buffer is big enough to display our data */
64 #undef check_overflow
65 #define check_overflow \
66   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
67   xbt_log_layout_simple_dynamic(l,ev,fmt,app); \
68   return; \
69   }
70
71 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
72                                        xbt_log_event_t ev,
73                                        const char *fmt,
74                                        xbt_log_appender_t app)
75 {
76   char *p;
77
78   xbt_assert0(ev->priority >= 0,
79               "Negative logging priority naturally forbidden");
80   xbt_assert1(ev->priority < sizeof(xbt_log_priority_names),
81               "Priority %d is greater than the biggest allowed value",
82               ev->priority);
83
84   if (simple_begin_of_time < 0)
85     simple_begin_of_time = gras_os_time();
86
87   p = ev->buffer;
88   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[");
89   check_overflow;
90
91   /* Display the proc info if available */
92   const char *procname=xbt_procname();
93   if (strlen(procname)) {
94     p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%s:(%d) ",
95                   gras_os_myname(), procname, (*xbt_getpid) ());
96     check_overflow;
97   }
98
99   /* Display the date */
100   p +=
101     snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%f] ",
102              gras_os_time() - simple_begin_of_time);
103   check_overflow;
104
105   /* Display file position if not INFO */
106   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc)
107     p +=
108       snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%d: ",
109                ev->fileName, ev->lineNum);
110
111   /* Display category name */
112   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[%s/%s] ",
113                 ev->cat->name, xbt_log_priority_names[ev->priority]);
114
115   /* Display user-provided message */
116   p += vsnprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), fmt, ev->ap);
117   check_overflow;
118
119   /* End it */
120   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "\n");
121   check_overflow;
122   app->do_append(app, ev->buffer);
123 }
124
125 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
126 {
127   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
128   res->do_layout = xbt_log_layout_simple_doit;
129   return res;
130 }