Logo AND Algorithmique Numérique Distribuée

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