Logo AND Algorithmique Numérique Distribuée

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