Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more informative error messages on errors within mmalloc
[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 += snprintf(p, 256 - (p - loc_buff), "%f] ",
40                 gras_os_time() - simple_begin_of_time);
41   if (ev->priority != xbt_log_priority_info && xbt_log_no_loc == 0)
42     p += snprintf(p, 256 - (p - loc_buff), "%s:%d: ", ev->fileName,
43                   ev->lineNum);
44   p += snprintf(p, 256 - (p - loc_buff), "[%s/%s] ", ev->cat->name,
45                 xbt_log_priority_names[ev->priority]);
46
47   xbt_strbuff_append(buff, loc_buff);
48
49   p = bvprintf(fmt, ev->ap_copy);
50   xbt_strbuff_append(buff, p);
51   free(p);
52
53   xbt_strbuff_append(buff, "\n");
54
55   app->do_append(app, buff->data);
56   xbt_strbuff_free(buff);
57 }
58
59 /* only used after the format using: we suppose that the buffer is big enough to display our data */
60 #undef check_overflow
61 #define check_overflow \
62   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
63   xbt_log_layout_simple_dynamic(l,ev,fmt,app); \
64   return; \
65   }
66
67 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
68                                        xbt_log_event_t ev,
69                                        const char *fmt,
70                                        xbt_log_appender_t app)
71 {
72   char *p;
73   const char *procname = NULL;
74   xbt_assert(ev->priority >= 0,
75               "Negative logging priority naturally forbidden");
76   xbt_assert(ev->priority < sizeof(xbt_log_priority_names),
77               "Priority %d is greater than the biggest allowed value",
78               ev->priority);
79
80   p = ev->buffer;
81   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[");
82   check_overflow;
83
84   /* Display the proc info if available */
85   procname = xbt_procname();
86   if (strlen(procname)) {
87     p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%s:(%d) ",
88                   gras_os_myname(), procname, (*xbt_getpid) ());
89     check_overflow;
90   }
91
92   /* Display the date */
93   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%f] ",
94                 gras_os_time() - simple_begin_of_time);
95   check_overflow;
96
97   /* Display file position if not INFO */
98   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc)
99     p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%d: ",
100                   ev->fileName, ev->lineNum);
101
102   /* Display category name */
103   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[%s/%s] ",
104                 ev->cat->name, xbt_log_priority_names[ev->priority]);
105
106   /* Display user-provided message */
107   p += vsnprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), fmt, ev->ap);
108   check_overflow;
109
110   /* End it */
111   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "\n");
112   check_overflow;
113   app->do_append(app, ev->buffer);
114 }
115
116 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
117 {
118   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
119   res->do_layout = xbt_log_layout_simple_doit;
120
121   if (simple_begin_of_time < 0)
122     simple_begin_of_time = gras_os_time();
123
124   return res;
125 }