1 /* layout_simple - a dumb log layout */
3 /* Copyright (c) 2007-2016. The SimGrid Team. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "src/internal_config.h" /* execinfo when available */
9 #include "xbt/sysdep.h"
10 #include "xbt/strbuff.h"
11 #include "src/xbt/log_private.h"
12 #include "simgrid/simix.h" /* SIMIX_host_self_get_name */
13 #include "surf/surf.h"
17 # include <execinfo.h> /* Function backtrace */
20 extern const char *xbt_log_priority_names[8];
22 static double format_begin_of_time = -1;
25 "Unknown %%%c sequence in layout format (%s).\n" \
26 "Known sequences:\n" \
27 " what: %%m: user message %%c: log category %%p: log priority\n" \
29 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n" \
30 " runtime: %%h: hostname %%t: thread %%P: process %%i: PID\n" \
31 " backtrace: %%b: full %%B: short\n" \
32 " when: %%d: date %%r: app. age\n" \
33 " other: %%%%: %% %%n: new line %%e: plain space\n"
35 #define check_overflow(len) \
36 if ((rem_size -= (len)) > 0) { \
41 #define set_sz_from_precision() \
44 if (precision != -1) { \
46 sz = precision + 1; /* +1 for the final '\0' */ \
51 #define show_it(data, letter) \
61 if (precision == -1) { \
62 len = snprintf(p, rem_size, "%*" letter, wd, data); \
64 len = snprintf(p, rem_size, "%*.*" letter, wd, precision, data); \
67 check_overflow(len); \
70 #define show_string(data) \
72 const char *show_string_data = (data); \
73 show_it(show_string_data ? show_string_data : "(null)", "s"); \
75 #define show_int(data) show_it(data, "d")
76 #define show_double(data) show_it(data, "f")
78 static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *msg_fmt)
81 int rem_size = ev->buffer_size;
85 for (char* q = l->data ; *q != '\0' ; q++) {
91 fprintf(stderr, "Layout format (%s) ending with %%\n", (char *)l->data);
98 case 'n': /* platform-dependant line separator; LOG4J compliant */
102 case 'e': /* plain space; SimGrid extension */
106 case '.': /* precision specifier */
107 precision = strtol(q + 1, &q, 10);
108 goto handle_modifier;
118 case '9': /* length modifier */
119 length = strtol(q, &q, 10);
120 goto handle_modifier;
121 case 'c': /* category name; LOG4J compliant
122 should accept a precision postfix to show the hierarchy */
123 show_string(ev->cat->name);
125 case 'p': /* priority name; LOG4J compliant */
126 show_string(xbt_log_priority_names[ev->priority]);
128 case 'h': /* host name; SimGrid extension */
129 show_string(SIMIX_host_self_get_name());
131 case 't': /* thread name; LOG4J compliant */
132 show_string(SIMIX_process_self_get_name());
134 case 'P': /* process name; SimGrid extension */
135 show_string(xbt_procname());
137 case 'i': /* process PID name; SimGrid extension */
138 show_int(xbt_getpid());
140 case 'F': /* file name; LOG4J compliant */
141 show_string(ev->fileName);
143 case 'l': { /* location; LOG4J compliant */
145 set_sz_from_precision();
146 int len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
147 check_overflow(MIN(sz, len));
150 case 'L': /* line number; LOG4J compliant */
151 show_int(ev->lineNum);
153 case 'M': /* method (ie, function) name; LOG4J compliant */
154 show_string(ev->functionName);
156 case 'b': /* backtrace; called %throwable in LOG4J */
157 case 'B': /* short backtrace; called %throwable{short} in LOG4J */
159 #if 0 && HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
163 e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE);
165 xbt_ex_setup_backtrace(&e);
167 show_string(e.bt_strings[1] + 8);
169 xbt_strbuff_t buff = xbt_strbuff_new();
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);
176 show_string(buff->data);
177 xbt_strbuff_free(buff);
181 show_string("(no backtrace on this arch)");
184 case 'd': /* date; LOG4J compliant */
185 show_double(surf_get_clock());
187 case 'r': /* application age; LOG4J compliant */
188 show_double(surf_get_clock() - format_begin_of_time);
190 case 'm': { /* user-provided message; LOG4J compliant */
192 set_sz_from_precision();
193 int len = vsnprintf(p, sz, msg_fmt, ev->ap);
194 check_overflow(MIN(sz, len));
198 fprintf(stderr, ERRMSG, *q, (char *)l->data);
211 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
216 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
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);
223 if (format_begin_of_time < 0)
224 format_begin_of_time = surf_get_clock();