Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not require doxygen in maintainer mode
[simgrid.git] / src / xbt / xbt_log_layout_format.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 "portable.h" /* execinfo when available */
11 #include "xbt/sysdep.h"
12 #include "xbt/strbuff.h"
13 #include "xbt/log_private.h"
14 #include "gras/virtu.h" /* gras_os_myname (KILLME) */
15 #include "xbt/synchro.h" /* xbt_thread_self_name */
16 #include <stdio.h>
17
18 extern const char *xbt_log_priority_names[7];
19
20 static double begin_of_time = -1;
21
22 #define append1(fmt,fmt2,elm)                                         \
23   do {                                                               \
24     if (precision == -1) {                                        \
25       xbt_strbuff_append(buff, tmp=bprintf(fmt,elm));            \
26       free(tmp);                                                 \
27     } else {                                                      \
28       xbt_strbuff_append(buff, tmp=bprintf(fmt2,precision,elm)); \
29       free(tmp);                                                 \
30       precision = -1;                                            \
31     }                                                         \
32   } while (0)
33 #define append2(fmt,elm,elm2)                                         \
34   do {                                                               \
35     xbt_strbuff_append(buff, tmp=bprintf(fmt,elm,elm2));          \
36     free(tmp);                                                    \
37     precision = -1;                                               \
38   } while (0)
39
40 #define ERRMSG "Unknown %%%c sequence in layout format (%s).\nKnown sequences:\n"                       \
41   "  what:        %%m: user message  %%c: log category  %%p: log priority\n"                      \
42   "  where:\n"                                                                                    \
43   "    source:    %%F: file          %%L: line          %%M: function  %%l: location (%%F:%%L)\n" \
44   "    runtime:   %%h: hostname      %%t: thread        %%P: process   %%i: PID\n"                \
45   "    backtrace: %%b: full          %%B: short\n"                                                \
46   "  when:        %%d: date          %%r: app. age\n"                                             \
47   "  other:       %%%%: %%             %%n: new line      %%e: plain space\n"
48
49
50 static void xbt_log_layout_format_dynamic(xbt_log_layout_t l,
51                                           xbt_log_event_t ev,
52                                           const char*fmt,
53                                           xbt_log_appender_t app) {
54   xbt_strbuff_t buff = xbt_strbuff_new();
55   int precision=-1;
56   char *q = l->data;
57   char *tmp;
58   char *tmp2;
59   int vres; /* shut gcc up, but ignored */
60
61   while (*q != '\0') {
62     if (*q == '%') {
63       q++;
64       handle_modifier:
65       switch (*q) {
66         case '\0':
67           fprintf(stderr,"Layout format (%s) ending with %%\n",(char*)l->data);
68           abort();
69         case '%':
70           xbt_strbuff_append(buff,"%");
71           break;
72         case 'n': /* platform-dependant line separator (LOG4J compliant) */
73           xbt_strbuff_append(buff,"\n");
74           break;
75         case 'e': /* plain space (SimGrid extension) */
76           xbt_strbuff_append(buff," ");
77           break;
78
79         case '.': /* precision specifyier */
80           q++;
81           q += sscanf(q,"%d",&precision);
82           goto handle_modifier;
83
84         case 'c': /* category name; LOG4J compliant
85                    should accept a precision postfix to show the hierarchy */
86           append1("%s","%.*s",ev->cat->name);
87           break;
88         case 'p': /* priority name; LOG4J compliant */
89           append1("%s","%.*s",xbt_log_priority_names[ev->priority]);
90           break;
91
92         case 'h': /* host name; SimGrid extension */
93           append1("%s","%.*s",gras_os_myname());
94           break;
95         case 't': /* thread name; LOG4J compliant */
96           append1("%s","%.*s",xbt_thread_self_name());
97           break;
98         case 'P': /* process name; SimGrid extension */
99           append1("%s","%.*s",xbt_procname());
100           break;
101         case 'i': /* process PID name; SimGrid extension */
102           append1("%d","%.*d",(*xbt_getpid)());
103           break;
104
105         case 'F': /* file name; LOG4J compliant */
106           append1("%s","%.*s",ev->fileName);
107           break;
108         case 'l': /* location; LOG4J compliant */
109           append2("%s:%d",ev->fileName,ev->lineNum);
110           precision = -1; /* Ignored */
111           break;
112         case 'L': /* line number; LOG4J compliant */
113           append1("%d","%.*d",ev->lineNum);
114           break;
115         case 'M': /* method (ie, function) name; LOG4J compliant */
116           append1("%s","%.*s",ev->functionName);
117           break;
118         case 'b': /* backtrace; called %throwable in LOG4J */
119         case 'B': /* short backtrace; called %throwable{short} in LOG4J */
120 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
121         {
122           xbt_ex_t e;
123           int i;
124
125           e.used     = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
126           e.bt_strings = NULL;
127           e.msg=NULL;
128           e.remote=0;
129           xbt_backtrace_current(&e);
130           if (*q=='B') {
131             append1("%s","%.*s",e.bt_strings[2]+8);
132           } else {
133             for (i=2; i<e.used; i++)
134               append1("%s\n","%.*s\n",e.bt_strings[i]+8);
135           }
136
137           xbt_ex_free(e);
138         }
139 #else
140         append1("%s","%.*s","(no backtrace on this arch)");
141 #endif
142         break;
143
144         case 'd': /* date; LOG4J compliant */
145           append1("%f","%.*f", gras_os_time());
146           break;
147         case 'r': /* application age; LOG4J compliant */
148           append1("%f","%.*f", gras_os_time()-begin_of_time);
149           break;
150
151         case 'm': /* user-provided message; LOG4J compliant */
152           vres = vasprintf(&tmp2, fmt, ev->ap_copy);
153           append1("%s","%.*s",tmp2);
154           free(tmp2);
155           break;
156
157         default:
158           fprintf(stderr,ERRMSG, *q,(char*)l->data);
159           abort();
160       }
161       q++;
162     } else {
163       char tmp2[2];
164       tmp2[0] = *(q++);
165       tmp2[1] = '\0';
166       xbt_strbuff_append(buff,tmp2);
167     }
168   }
169   app->do_append(app,buff->data);
170   xbt_strbuff_free(buff);
171 }
172
173 #define check_overflow \
174   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
175   xbt_log_layout_format_dynamic(l,ev,msg_fmt,app); \
176   return;\
177   }
178 static void xbt_log_layout_format_doit(xbt_log_layout_t l,
179                                        xbt_log_event_t ev,
180                                        const char *msg_fmt,
181                                        xbt_log_appender_t app) {
182   char *p,*q;
183   int precision=-1;
184
185   if (begin_of_time<0)
186     begin_of_time=gras_os_time();
187
188   p = ev->buffer;
189   q = l->data;
190
191   while (*q != '\0') {
192     if (*q == '%') {
193       q++;
194       handle_modifier:
195       switch (*q) {
196         case '\0':
197           fprintf(stderr,"Layout format (%s) ending with %%\n",(char*)l->data);
198           abort();
199         case '%':
200           *p++ = '%';
201           break;
202         case 'n': /* platform-dependant line separator (LOG4J compliant) */
203           p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"\n");
204           check_overflow;
205           break;
206         case 'e': /* plain space (SimGrid extension) */
207           p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer)," ");
208           check_overflow;
209           break;
210
211         case '.': /* precision specifyier */
212           q++;
213           q += sscanf(q,"%d",&precision);
214           goto handle_modifier;
215
216         case 'c': /* category name; LOG4J compliant
217                    should accept a precision postfix to show the hierarchy */
218           if (precision == -1) {
219             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s",ev->cat->name);
220             check_overflow;
221           } else {
222             p += sprintf(p,"%.*s",(int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision),ev->cat->name);
223             check_overflow;
224             precision = -1;
225           }
226           break;
227         case 'p': /* priority name; LOG4J compliant */
228           if (precision == -1) {
229             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s", xbt_log_priority_names[ev->priority] );
230             check_overflow;
231           } else {
232             p += sprintf(p, "%.*s", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), xbt_log_priority_names[ev->priority] );
233             check_overflow;
234             precision = -1;
235           }
236           break;
237
238         case 'h': /* host name; SimGrid extension */
239           if (precision == -1) {
240             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s", gras_os_myname());
241             check_overflow;
242           } else {
243             p += sprintf(p, "%.*s", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), gras_os_myname());
244             check_overflow;
245             precision = -1;
246           }
247           break;
248         case 't': /* thread name; LOG4J compliant */
249           if (precision == -1) {
250             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s", xbt_thread_self_name());
251             check_overflow;
252           } else {
253             p += sprintf(p, "%.*s", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), xbt_thread_self_name());
254             check_overflow;
255             precision = -1;
256           }
257           break;
258         case 'P': /* process name; SimGrid extension */
259           if (precision == -1) {
260             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s", xbt_procname());
261             check_overflow;
262           } else {
263             p += sprintf(p, "%.*s", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision),xbt_procname());
264             check_overflow;
265             precision = -1;
266           }
267           break;
268         case 'i': /* process PID name; SimGrid extension */
269           if (precision == -1) {
270             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%d", (*xbt_getpid)());
271             check_overflow;
272           } else {
273             p += sprintf(p, "%.*d", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), (*xbt_getpid)());
274             check_overflow;
275             precision = -1;
276           }
277           break;
278
279         case 'F': /* file name; LOG4J compliant */
280           if (precision == -1) {
281             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s",ev->fileName);
282             check_overflow;
283           } else {
284             p += sprintf(p,"%.*s",(int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), ev->fileName);
285             check_overflow;
286             precision = -1;
287           }
288           break;
289         case 'l': /* location; LOG4J compliant */
290           if (precision == -1) {
291             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s:%d", ev->fileName, ev->lineNum);
292             check_overflow;
293           } else {
294             p += snprintf(p, (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), "%s:%d", ev->fileName, ev->lineNum);
295             check_overflow;
296             precision = -1;
297           }
298           break;
299         case 'L': /* line number; LOG4J compliant */
300           if (precision == -1) {
301             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%d", ev->lineNum);
302             check_overflow;
303           } else {
304             p += sprintf(p, "%.*d", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), ev->lineNum);
305             check_overflow;
306             precision = -1;
307           }
308           break;
309         case 'M': /* method (ie, function) name; LOG4J compliant */
310           if (precision == -1) {
311             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s", ev->functionName);
312             check_overflow;
313           } else {
314             p += sprintf(p, "%.*s", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), ev->functionName);
315             check_overflow;
316             precision = -1;
317           }
318           break;
319         case 'b': /* backtrace; called %throwable in LOG4J */
320         case 'B': /* short backtrace; called %throwable{short} in LOG4J */
321 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
322         {
323           xbt_ex_t e;
324           int i;
325
326           e.used     = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
327           e.bt_strings = NULL;
328           e.msg=NULL;
329           e.remote=0;
330           xbt_backtrace_current(&e);
331           if (*q=='B') {
332             if (precision == -1) {
333               p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s",e.bt_strings[2]+8);
334               check_overflow;
335             } else {
336               p += sprintf(p,"%.*s",(int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), e.bt_strings[2]+8);
337               check_overflow;
338               precision = -1;
339             }
340           } else {
341             for (i=2; i<e.used; i++)
342               if (precision == -1) {
343                 p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s\n",e.bt_strings[i]+8);
344                 check_overflow;
345               } else {
346                 p += sprintf(p,"%.*s\n",(int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision),e.bt_strings[i]+8);
347                 check_overflow;
348                 precision = -1;
349               }
350           }
351
352           xbt_ex_free(e);
353         }
354 #else
355         p+=snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"(no backtrace on this arch)");
356         check_overflow;
357 #endif
358         break;
359
360         case 'd': /* date; LOG4J compliant */
361           if (precision == -1) {
362             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%f", gras_os_time());
363             check_overflow;
364           } else {
365             p += sprintf(p,"%.*f", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), gras_os_time());
366             check_overflow;
367             precision = -1;
368           }
369           break;
370         case 'r': /* application age; LOG4J compliant */
371           if (precision == -1) {
372             p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%f", gras_os_time()-begin_of_time);
373             check_overflow;
374           } else {
375             p += sprintf(p,"%.*f", (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), gras_os_time()-begin_of_time);
376             check_overflow;
377             precision = -1;
378           }
379           break;
380
381         case 'm': /* user-provided message; LOG4J compliant */
382           if (precision == -1) {
383             p += vsnprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), msg_fmt, ev->ap);
384             check_overflow;
385           } else {
386             p += vsnprintf(p, (int)MIN(XBT_LOG_BUFF_SIZE-(p-ev->buffer),precision), msg_fmt, ev->ap);
387             check_overflow;
388             precision = -1;
389           }
390           break;
391
392         default:
393           fprintf(stderr,ERRMSG,*q,(char*)l->data);
394           abort();
395       }
396       q++;
397     } else {
398       *(p++) = *(q++);
399       check_overflow;
400     }
401   }
402   *p = '\0';
403   app->do_append(app,ev->buffer);
404 }
405
406 static void xbt_log_layout_format_free(xbt_log_layout_t lay) {
407   free(lay->data);
408 }
409 xbt_log_layout_t xbt_log_layout_format_new(char *arg) {
410   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1);
411   res->do_layout = xbt_log_layout_format_doit;
412   res->free_     = xbt_log_layout_format_free;
413   res->data = xbt_strdup((char*)arg);
414   return res;
415 }