1 /* layout_simple - a dumb log layout */
3 /* Copyright (c) 2007-2014. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "src/internal_config.h" /* execinfo when available */
10 #include "xbt/synchro_core.h" /* xbt_thread_self_name */
11 #include "src/xbt/ex_interface.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/strbuff.h"
14 #include "src/xbt/log_private.h"
15 #include "simgrid/simix.h" /* SIMIX_host_self_get_name */
16 #include "surf/surf.h"
20 # include <execinfo.h> /* Function backtrace */
23 extern const char *xbt_log_priority_names[8];
25 static double format_begin_of_time = -1;
28 "Unknown %%%c sequence in layout format (%s).\n" \
29 "Known sequences:\n" \
30 " what: %%m: user message %%c: log category %%p: log priority\n" \
32 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n" \
33 " runtime: %%h: hostname %%t: thread %%P: process %%i: PID\n" \
34 " backtrace: %%b: full %%B: short\n" \
35 " when: %%d: date %%r: app. age\n" \
36 " other: %%%%: %% %%n: new line %%e: plain space\n"
38 #define check_overflow(len) \
39 if ((rem_size -= (len)) > 0) { \
44 #define set_sz_from_precision() \
47 if (precision != -1) { \
49 sz = precision + 1; /* +1 for the final '\0' */ \
54 #define show_it(data, letter) \
63 if (precision == -1) { \
64 len = snprintf(p, rem_size, "%*" letter, wd, data); \
66 len = snprintf(p, rem_size, "%*.*" letter, wd, precision, data); \
69 check_overflow(len); \
72 #define show_string(data) \
74 const char *show_string_data = (data); \
75 show_it(show_string_data ? show_string_data : "(null)", "s"); \
77 #define show_int(data) show_it(data, "d")
78 #define show_double(data) show_it(data, "f")
80 static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *msg_fmt)
83 int rem_size = ev->buffer_size;
88 for (q = l->data ; *q != '\0' ; q++) {
94 fprintf(stderr, "Layout format (%s) ending with %%\n", (char *)l->data);
100 case 'n': /* platform-dependant line separator; LOG4J compliant */
104 case 'e': /* plain space; SimGrid extension */
108 case '.': /* precision specifier */
109 precision = strtol(q + 1, &q, 10);
110 goto handle_modifier;
120 case '9': /* length modifier */
121 length = strtol(q, &q, 10);
122 goto handle_modifier;
123 case 'c': /* category name; LOG4J compliant
124 should accept a precision postfix to show the hierarchy */
125 show_string(ev->cat->name);
127 case 'p': /* priority name; LOG4J compliant */
128 show_string(xbt_log_priority_names[ev->priority]);
130 case 'h': /* host name; SimGrid extension */
131 show_string(SIMIX_host_self_get_name());
133 case 't': /* thread name; LOG4J compliant */
134 show_string(SIMIX_process_self_get_name());
136 case 'P': /* process name; SimGrid extension */
137 show_string(xbt_procname());
139 case 'i': /* process PID name; SimGrid extension */
140 show_int(xbt_getpid());
142 case 'F': /* file name; LOG4J compliant */
143 show_string(ev->fileName);
145 case 'l': { /* location; LOG4J compliant */
147 set_sz_from_precision();
148 len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
149 check_overflow(MIN(sz, len));
152 case 'L': /* line number; LOG4J compliant */
153 show_int(ev->lineNum);
155 case 'M': /* method (ie, function) name; LOG4J compliant */
156 show_string(ev->functionName);
158 case 'b': /* backtrace; called %throwable in LOG4J */
159 case 'B': /* short backtrace; called %throwable{short} in LOG4J */
160 #if HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
164 e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE);
167 xbt_ex_setup_backtrace(&e);
169 show_string(e.bt_strings[1] + 8);
171 xbt_strbuff_t buff = xbt_strbuff_new();
173 xbt_strbuff_append(buff, e.bt_strings[1] + 8);
174 for (i = 2; i < e.used; i++) {
175 xbt_strbuff_append(buff, "\n");
176 xbt_strbuff_append(buff, e.bt_strings[i] + 8);
178 show_string(buff->data);
179 xbt_strbuff_free(buff);
184 show_string("(no backtrace on this arch)");
187 case 'd': /* date; LOG4J compliant */
188 show_double(surf_get_clock());
190 case 'r': /* application age; LOG4J compliant */
191 show_double(surf_get_clock() - format_begin_of_time);
193 case 'm': { /* user-provided message; LOG4J compliant */
195 set_sz_from_precision();
196 len = vsnprintf(p, sz, msg_fmt, ev->ap);
197 check_overflow(MIN(sz, len));
201 fprintf(stderr, ERRMSG, *q, (char *)l->data);
214 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
219 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
221 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
222 res->do_layout = xbt_log_layout_format_doit;
223 res->free_ = xbt_log_layout_format_free;
224 res->data = xbt_strdup((char *) arg);
226 if (format_begin_of_time < 0)
227 format_begin_of_time = surf_get_clock();