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/portable.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"
19 #ifdef HAVE_EXECINFO_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,
85 int rem_size = ev->buffer_size;
90 for (q = l->data ; *q != '\0' ; q++) {
96 fprintf(stderr, "Layout format (%s) ending with %%\n", (char *)l->data);
102 case 'n': /* platform-dependant line separator; LOG4J compliant */
106 case 'e': /* plain space; SimGrid extension */
110 case '.': /* precision specifier */
111 precision = strtol(q + 1, &q, 10);
112 goto handle_modifier;
122 case '9': /* length modifier */
123 length = strtol(q, &q, 10);
124 goto handle_modifier;
125 case 'c': /* category name; LOG4J compliant
126 should accept a precision postfix to show the
128 show_string(ev->cat->name);
130 case 'p': /* priority name; LOG4J compliant */
131 show_string(xbt_log_priority_names[ev->priority]);
133 case 'h': /* host name; SimGrid extension */
134 show_string(SIMIX_host_self_get_name());
136 case 't': /* thread name; LOG4J compliant */
137 show_string(SIMIX_process_self_get_name());
139 case 'P': /* process name; SimGrid extension */
140 show_string(xbt_procname());
142 case 'i': /* process PID name; SimGrid extension */
143 show_int(xbt_getpid());
145 case 'F': /* file name; LOG4J compliant */
146 show_string(ev->fileName);
148 case 'l': { /* location; LOG4J compliant */
150 set_sz_from_precision();
151 len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
152 check_overflow(MIN(sz, len));
155 case 'L': /* line number; LOG4J compliant */
156 show_int(ev->lineNum);
158 case 'M': /* method (ie, function) name; LOG4J compliant */
159 show_string(ev->functionName);
161 case 'b': /* backtrace; called %throwable in LOG4J */
162 case 'B': /* short backtrace; called %throwable{short} in LOG4J */
163 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
167 e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE);
170 xbt_ex_setup_backtrace(&e);
172 show_string(e.bt_strings[1] + 8);
174 xbt_strbuff_t buff = xbt_strbuff_new();
176 xbt_strbuff_append(buff, e.bt_strings[1] + 8);
177 for (i = 2; i < e.used; i++) {
178 xbt_strbuff_append(buff, "\n");
179 xbt_strbuff_append(buff, e.bt_strings[i] + 8);
181 show_string(buff->data);
182 xbt_strbuff_free(buff);
187 show_string("(no backtrace on this arch)");
190 case 'd': /* date; LOG4J compliant */
191 show_double(surf_get_clock());
193 case 'r': /* application age; LOG4J compliant */
194 show_double(surf_get_clock() - format_begin_of_time);
196 case 'm': { /* user-provided message; LOG4J compliant */
198 set_sz_from_precision();
199 len = vsnprintf(p, sz, msg_fmt, ev->ap);
200 check_overflow(MIN(sz, len));
204 fprintf(stderr, ERRMSG, *q, (char *)l->data);
217 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
222 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
224 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
225 res->do_layout = xbt_log_layout_format_doit;
226 res->free_ = xbt_log_layout_format_free;
227 res->data = xbt_strdup((char *) arg);
229 if (format_begin_of_time < 0)
230 format_begin_of_time = surf_get_clock();