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 "xbt/sysdep.h"
12 #include "xbt/strbuff.h"
13 #include "src/xbt/log_private.h"
14 #include "simgrid/simix.h" /* SIMIX_host_self_get_name */
15 #include "surf/surf.h"
19 # include <execinfo.h> /* Function backtrace */
22 extern const char *xbt_log_priority_names[8];
24 static double format_begin_of_time = -1;
27 "Unknown %%%c sequence in layout format (%s).\n" \
28 "Known sequences:\n" \
29 " what: %%m: user message %%c: log category %%p: log priority\n" \
31 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n" \
32 " runtime: %%h: hostname %%t: thread %%P: process %%i: PID\n" \
33 " backtrace: %%b: full %%B: short\n" \
34 " when: %%d: date %%r: app. age\n" \
35 " other: %%%%: %% %%n: new line %%e: plain space\n"
37 #define check_overflow(len) \
38 if ((rem_size -= (len)) > 0) { \
43 #define set_sz_from_precision() \
46 if (precision != -1) { \
48 sz = precision + 1; /* +1 for the final '\0' */ \
53 #define show_it(data, letter) \
62 if (precision == -1) { \
63 len = snprintf(p, rem_size, "%*" letter, wd, data); \
65 len = snprintf(p, rem_size, "%*.*" letter, wd, precision, data); \
68 check_overflow(len); \
71 #define show_string(data) \
73 const char *show_string_data = (data); \
74 show_it(show_string_data ? show_string_data : "(null)", "s"); \
76 #define show_int(data) show_it(data, "d")
77 #define show_double(data) show_it(data, "f")
79 static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *msg_fmt)
82 int rem_size = ev->buffer_size;
87 for (q = l->data ; *q != '\0' ; q++) {
93 fprintf(stderr, "Layout format (%s) ending with %%\n", (char *)l->data);
99 case 'n': /* platform-dependant line separator; LOG4J compliant */
103 case 'e': /* plain space; SimGrid extension */
107 case '.': /* precision specifier */
108 precision = strtol(q + 1, &q, 10);
109 goto handle_modifier;
119 case '9': /* length modifier */
120 length = strtol(q, &q, 10);
121 goto handle_modifier;
122 case 'c': /* category name; LOG4J compliant
123 should accept a precision postfix to show the hierarchy */
124 show_string(ev->cat->name);
126 case 'p': /* priority name; LOG4J compliant */
127 show_string(xbt_log_priority_names[ev->priority]);
129 case 'h': /* host name; SimGrid extension */
130 show_string(SIMIX_host_self_get_name());
132 case 't': /* thread name; LOG4J compliant */
133 show_string(SIMIX_process_self_get_name());
135 case 'P': /* process name; SimGrid extension */
136 show_string(xbt_procname());
138 case 'i': /* process PID name; SimGrid extension */
139 show_int(xbt_getpid());
141 case 'F': /* file name; LOG4J compliant */
142 show_string(ev->fileName);
144 case 'l': { /* location; LOG4J compliant */
146 set_sz_from_precision();
147 len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
148 check_overflow(MIN(sz, len));
151 case 'L': /* line number; LOG4J compliant */
152 show_int(ev->lineNum);
154 case 'M': /* method (ie, function) name; LOG4J compliant */
155 show_string(ev->functionName);
157 case 'b': /* backtrace; called %throwable in LOG4J */
158 case 'B': /* short backtrace; called %throwable{short} in LOG4J */
160 #if 0 && HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
164 e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE);
166 xbt_ex_setup_backtrace(&e);
168 show_string(e.bt_strings[1] + 8);
170 xbt_strbuff_t buff = xbt_strbuff_new();
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);
177 show_string(buff->data);
178 xbt_strbuff_free(buff);
182 show_string("(no backtrace on this arch)");
185 case 'd': /* date; LOG4J compliant */
186 show_double(surf_get_clock());
188 case 'r': /* application age; LOG4J compliant */
189 show_double(surf_get_clock() - format_begin_of_time);
191 case 'm': { /* user-provided message; LOG4J compliant */
193 set_sz_from_precision();
194 len = vsnprintf(p, sz, msg_fmt, ev->ap);
195 check_overflow(MIN(sz, len));
199 fprintf(stderr, ERRMSG, *q, (char *)l->data);
212 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
217 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
219 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
220 res->do_layout = xbt_log_layout_format_doit;
221 res->free_ = xbt_log_layout_format_free;
222 res->data = xbt_strdup((char *) arg);
224 if (format_begin_of_time < 0)
225 format_begin_of_time = surf_get_clock();