Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
there were no difference between __mmalloc_free and mfree anymore; merge them
[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 "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[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         xbt_die("Layout format (%s) ending with %%\n", (char *)l->data);
92       case '%':
93         *p = '%';
94         check_overflow(1);
95         break;
96       case 'n':         /* platform-dependant line separator; LOG4J compliant */
97         *p = '\n';
98         check_overflow(1);
99         break;
100       case 'e':                 /* plain space; SimGrid extension */
101         *p = ' ';
102         check_overflow(1);
103         break;
104       case '.':                 /* precision specifier */
105         precision = strtol(q + 1, &q, 10);
106         goto handle_modifier;
107       case '0':
108       case '1':
109       case '2':
110       case '3':
111       case '4':
112       case '5':
113       case '6':
114       case '7':
115       case '8':
116       case '9':                 /* length modifier */
117         length = strtol(q, &q, 10);
118         goto handle_modifier;
119       case 'c':                 /* category name; LOG4J compliant
120                                    should accept a precision postfix to show the
121                                    hierarchy */
122         show_string(ev->cat->name);
123         break;
124       case 'p':                 /* priority name; LOG4J compliant */
125         show_string(xbt_log_priority_names[ev->priority]);
126         break;
127       case 'h':                 /* host name; SimGrid extension */
128         show_string(gras_os_myname());
129         break;
130       case 't':                 /* thread name; LOG4J compliant */
131         show_string(xbt_thread_self_name());
132         break;
133       case 'P':                 /* process name; SimGrid extension */
134         show_string(xbt_procname());
135         break;
136       case 'i':                 /* process PID name; SimGrid extension */
137         show_int(xbt_getpid());
138         break;
139       case 'F':                 /* file name; LOG4J compliant */
140         show_string(ev->fileName);
141         break;
142       case 'l': {               /* location; LOG4J compliant */
143         int len, sz;
144         set_sz_from_precision();
145         len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
146         check_overflow(MIN(sz, len));
147         break;
148       }
149       case 'L':                 /* line number; LOG4J compliant */
150         show_int(ev->lineNum);
151         break;
152       case 'M':                /* method (ie, function) name; LOG4J compliant */
153         show_string(ev->functionName);
154         break;
155       case 'b':                 /* backtrace; called %throwable in LOG4J */
156       case 'B':         /* short backtrace; called %throwable{short} in LOG4J */
157 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
158         {
159           xbt_ex_t e;
160
161           e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE);
162           e.bt_strings = NULL;
163           e.msg = NULL;
164           e.remote = 0;
165           xbt_ex_setup_backtrace(&e);
166           if (*q == 'B') {
167             show_string(e.bt_strings[1] + 8);
168           } else {
169             xbt_strbuff_t buff = xbt_strbuff_new();
170             int i;
171             xbt_strbuff_append(buff, e.bt_strings[1] + 8);
172             for (i = 2; i < e.used; i++) {
173               xbt_strbuff_append(buff, "\n");
174               xbt_strbuff_append(buff, e.bt_strings[i] + 8);
175             }
176             show_string(buff->data);
177             xbt_strbuff_free(buff);
178           }
179           xbt_ex_free(e);
180         }
181 #else
182         show_string("(no backtrace on this arch)");
183 #endif
184         break;
185       case 'd':                 /* date; LOG4J compliant */
186         show_double(gras_os_time());
187         break;
188       case 'r':                 /* application age; LOG4J compliant */
189         show_double(gras_os_time() - format_begin_of_time);
190         break;
191       case 'm': {               /* user-provided message; LOG4J compliant */
192         int len, sz;
193         set_sz_from_precision();
194         len = vsnprintf(p, sz, msg_fmt, ev->ap);
195         check_overflow(MIN(sz, len));
196         break;
197       }
198       default:
199         xbt_die(ERRMSG, *q, (char *)l->data);
200       }
201     } else {
202       *p = *q;
203       check_overflow(1);
204     }
205   }
206   *p = '\0';
207
208   return 1;
209 }
210
211 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
212 {
213   free(lay->data);
214 }
215
216 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
217 {
218   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
219   res->do_layout = xbt_log_layout_format_doit;
220   res->free_ = xbt_log_layout_format_free;
221   res->data = xbt_strdup((char *) arg);
222
223   if (format_begin_of_time < 0)
224     format_begin_of_time = gras_os_time();
225
226   return res;
227 }