Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : init equal_to field to -1 for each allocated block/fragment at the...
[simgrid.git] / src / xbt / xbt_log_layout_format.c
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2007-2011. 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 "portable.h"           /* execinfo when available */
10 #include "xbt/ex_interface.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/strbuff.h"
13 #include "xbt/log_private.h"
14 #include "simgrid/simix.h"      /* SIMIX_host_self_get_name */
15 #include "surf/surf.h"
16 #include <stdio.h>
17
18 extern const char *xbt_log_priority_names[8];
19
20 static double format_begin_of_time = -1;
21
22 #define ERRMSG                                                          \
23   "Unknown %%%c sequence in layout format (%s).\n"                      \
24   "Known sequences:\n"                                                  \
25   "  what:        %%m: user message  %%c: log category  %%p: log priority\n" \
26   "  where:\n"                                                          \
27   "    source:    %%F: file          %%L: line          %%M: function  %%l: location (%%F:%%L)\n" \
28   "    runtime:   %%h: hostname      %%t: thread        %%P: process   %%i: PID\n" \
29   "    backtrace: %%b: full          %%B: short\n"                      \
30   "  when:        %%d: date          %%r: app. age\n"                   \
31   "  other:       %%%%: %%             %%n: new line      %%e: plain space\n"
32
33 #define check_overflow(len)                                             \
34   if ((rem_size -= (len)) > 0) {                                        \
35     p += (len);                                                         \
36   } else                                                                \
37     return 0
38
39 #define set_sz_from_precision()                                         \
40   if (1) {                                                              \
41     sz = rem_size;                                                      \
42     if (precision != -1) {                                              \
43       if (precision < sz)                                               \
44         sz = precision + 1;     /* +1 for the final '\0' */             \
45       precision = -1;                                                   \
46     }                                                                   \
47   } else (void)0
48
49 #define show_it(data, letter)                                           \
50   if (1) {                                                              \
51     int len, wd;                                                        \
52     if (length == -1) {                                                 \
53       wd = 0;                                                           \
54     } else {                                                            \
55       wd = length;                                                      \
56       length = -1;                                                      \
57     }                                                                   \
58     if (precision == -1) {                                              \
59       len = snprintf(p, rem_size, "%*" letter, wd, data);               \
60     } else {                                                            \
61       len = snprintf(p, rem_size, "%*.*" letter, wd, precision, data);  \
62       precision = -1;                                                   \
63     }                                                                   \
64     check_overflow(len);                                                \
65   } else (void)0
66
67 #define show_string(data)                                               \
68   if (1) {                                                              \
69     const char *show_string_data = (data);                              \
70     show_it(show_string_data ? show_string_data : "(null)", "s");       \
71   } else (void)0
72 #define show_int(data)    show_it(data, "d")
73 #define show_double(data) show_it(data, "f")
74
75 static int xbt_log_layout_format_doit(xbt_log_layout_t l,
76                                       xbt_log_event_t ev,
77                                       const char *msg_fmt)
78 {
79   char *p = ev->buffer;
80   int rem_size = ev->buffer_size;
81   int precision = -1;
82   int length = -1;
83   char *q;
84
85   for (q = l->data ; *q != '\0' ; q++) {
86     if (*q == '%') {
87       q++;
88     handle_modifier:
89       switch (*q) {
90       case '\0':
91         fprintf(stderr, "Layout format (%s) ending with %%\n", (char *)l->data);
92         xbt_abort();
93       case '%':
94         *p = '%';
95         check_overflow(1);
96         break;
97       case 'n':         /* platform-dependant line separator; LOG4J compliant */
98         *p = '\n';
99         check_overflow(1);
100         break;
101       case 'e':                 /* plain space; SimGrid extension */
102         *p = ' ';
103         check_overflow(1);
104         break;
105       case '.':                 /* precision specifier */
106         precision = strtol(q + 1, &q, 10);
107         goto handle_modifier;
108       case '0':
109       case '1':
110       case '2':
111       case '3':
112       case '4':
113       case '5':
114       case '6':
115       case '7':
116       case '8':
117       case '9':                 /* length modifier */
118         length = strtol(q, &q, 10);
119         goto handle_modifier;
120       case 'c':                 /* category name; LOG4J compliant
121                                    should accept a precision postfix to show the
122                                    hierarchy */
123         show_string(ev->cat->name);
124         break;
125       case 'p':                 /* priority name; LOG4J compliant */
126         show_string(xbt_log_priority_names[ev->priority]);
127         break;
128       case 'h':                 /* host name; SimGrid extension */
129         show_string(SIMIX_host_self_get_name());
130         break;
131       case 't':                 /* thread name; LOG4J compliant */
132         show_string(xbt_thread_self_name());
133         break;
134       case 'P':                 /* process name; SimGrid extension */
135         show_string(xbt_procname());
136         break;
137       case 'i':                 /* process PID name; SimGrid extension */
138         show_int(xbt_getpid());
139         break;
140       case 'F':                 /* file name; LOG4J compliant */
141         show_string(ev->fileName);
142         break;
143       case 'l': {               /* location; LOG4J compliant */
144         int len, sz;
145         set_sz_from_precision();
146         len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
147         check_overflow(MIN(sz, len));
148         break;
149       }
150       case 'L':                 /* line number; LOG4J compliant */
151         show_int(ev->lineNum);
152         break;
153       case 'M':                /* method (ie, function) name; LOG4J compliant */
154         show_string(ev->functionName);
155         break;
156       case 'b':                 /* backtrace; called %throwable in LOG4J */
157       case 'B':         /* short backtrace; called %throwable{short} in LOG4J */
158 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
159         {
160           xbt_ex_t e;
161
162           e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE);
163           e.bt_strings = NULL;
164           e.msg = NULL;
165           e.remote = 0;
166           xbt_ex_setup_backtrace(&e);
167           if (*q == 'B') {
168             show_string(e.bt_strings[1] + 8);
169           } else {
170             xbt_strbuff_t buff = xbt_strbuff_new();
171             int i;
172             xbt_strbuff_append(buff, e.bt_strings[1] + 8);
173             for (i = 2; i < e.used; i++) {
174               xbt_strbuff_append(buff, "\n");
175               xbt_strbuff_append(buff, e.bt_strings[i] + 8);
176             }
177             show_string(buff->data);
178             xbt_strbuff_free(buff);
179           }
180           xbt_ex_free(e);
181         }
182 #else
183         show_string("(no backtrace on this arch)");
184 #endif
185         break;
186       case 'd':                 /* date; LOG4J compliant */
187         show_double(surf_get_clock());
188         break;
189       case 'r':                 /* application age; LOG4J compliant */
190         show_double(surf_get_clock() - format_begin_of_time);
191         break;
192       case 'm': {               /* user-provided message; LOG4J compliant */
193         int len, sz;
194         set_sz_from_precision();
195         len = vsnprintf(p, sz, msg_fmt, ev->ap);
196         check_overflow(MIN(sz, len));
197         break;
198       }
199       default:
200         fprintf(stderr, ERRMSG, *q, (char *)l->data);
201         xbt_abort();
202       }
203     } else {
204       *p = *q;
205       check_overflow(1);
206     }
207   }
208   *p = '\0';
209
210   return 1;
211 }
212
213 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
214 {
215   free(lay->data);
216 }
217
218 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
219 {
220   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
221   res->do_layout = xbt_log_layout_format_doit;
222   res->free_ = xbt_log_layout_format_free;
223   res->data = xbt_strdup((char *) arg);
224
225   if (format_begin_of_time < 0)
226     format_begin_of_time = surf_get_clock();
227
228   return res;
229 }