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 "src/xbt/log_private.h"
11 #include "simgrid/simix.h" /* SIMIX_host_self_get_name */
12 #include "surf/surf.h"
16 # include <execinfo.h> /* Function backtrace */
19 extern const char *xbt_log_priority_names[8];
21 static double format_begin_of_time = -1;
24 "Unknown %%%c sequence in layout format (%s).\n" \
25 "Known sequences:\n" \
26 " what: %%m: user message %%c: log category %%p: log priority\n" \
28 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n" \
29 " runtime: %%h: hostname %%t: thread %%P: process %%i: PID\n" \
30 " backtrace: %%b: full %%B: short\n" \
31 " when: %%d: date %%r: app. age\n" \
32 " other: %%%%: %% %%n: new line %%e: plain space\n"
34 #define check_overflow(len) \
35 if ((rem_size -= (len)) > 0) { \
40 #define set_sz_from_precision() \
43 if (precision != -1) { \
45 sz = precision + 1; /* +1 for the final '\0' */ \
50 #define show_it(data, letter) \
60 if (precision == -1) { \
61 len = snprintf(p, rem_size, "%*" letter, wd, data); \
63 len = snprintf(p, rem_size, "%*.*" letter, wd, precision, data); \
66 check_overflow(len); \
69 #define show_string(data) \
71 const char *show_string_data = (data); \
72 show_it(show_string_data ? show_string_data : "(null)", "s"); \
74 #define show_int(data) show_it(data, "d")
75 #define show_double(data) show_it(data, "f")
77 static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *msg_fmt)
80 int rem_size = ev->buffer_size;
84 for (char* q = l->data ; *q != '\0' ; q++) {
90 fprintf(stderr, "Layout format (%s) ending with %%\n", (char *)l->data);
97 case 'n': /* platform-dependant line separator; LOG4J compliant */
101 case 'e': /* plain space; SimGrid extension */
105 case '.': /* precision specifier */
106 precision = strtol(q + 1, &q, 10);
107 goto handle_modifier;
117 case '9': /* length modifier */
118 length = strtol(q, &q, 10);
119 goto handle_modifier;
120 case 'c': /* category name; LOG4J compliant
121 should accept a precision postfix to show the hierarchy */
122 show_string(ev->cat->name);
124 case 'p': /* priority name; LOG4J compliant */
125 show_string(xbt_log_priority_names[ev->priority]);
127 case 'h': /* host name; SimGrid extension */
128 show_string(SIMIX_host_self_get_name());
130 case 't': /* thread name; LOG4J compliant */
131 show_string(SIMIX_process_self_get_name());
133 case 'P': /* process name; SimGrid extension */
134 show_string(xbt_procname());
136 case 'i': /* process PID name; SimGrid extension */
137 show_int(xbt_getpid());
139 case 'F': /* file name; LOG4J compliant */
140 show_string(ev->fileName);
142 case 'l': { /* location; LOG4J compliant */
144 set_sz_from_precision();
145 int len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
146 check_overflow(MIN(sz, len));
149 case 'L': /* line number; LOG4J compliant */
150 show_int(ev->lineNum);
152 case 'M': /* method (ie, function) name; LOG4J compliant */
153 show_string(ev->functionName);
155 case 'd': /* date; LOG4J compliant */
156 show_double(surf_get_clock());
158 case 'r': /* application age; LOG4J compliant */
159 show_double(surf_get_clock() - format_begin_of_time);
161 case 'm': { /* user-provided message; LOG4J compliant */
163 set_sz_from_precision();
164 int len = vsnprintf(p, sz, msg_fmt, ev->ap);
165 check_overflow(MIN(sz, len));
169 fprintf(stderr, ERRMSG, *q, (char *)l->data);
182 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
187 xbt_log_layout_t xbt_log_layout_format_new(char *arg)
189 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
190 res->do_layout = &xbt_log_layout_format_doit;
191 res->free_ = &xbt_log_layout_format_free;
192 res->data = xbt_strdup((char *) arg);
194 if (format_begin_of_time < 0)
195 format_begin_of_time = surf_get_clock();