1 /* layout_simple - a dumb log layout */
3 /* Copyright (c) 2007-2022. 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 "simgrid/engine.h" /* simgrid_get_clock */
9 #include "simgrid/host.h"
10 #include "src/xbt/log_private.hpp"
11 #include "xbt/sysdep.h"
12 #include "xbt/virtu.h"
16 static constexpr const char* ERRMSG =
17 "Unknown %%%c sequence in layout format (%s).\n"
19 " what: %%m: user message %%c: log category %%p: log priority\n"
21 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n"
22 " runtime: %%h: hostname %%a: actor %%i: PID\n"
23 " when: %%d: date %%r: app. age\n"
24 " other: %%%%: %% %%n: new line %%e: plain space\n";
26 #define check_overflow(len) \
27 if ((rem_size -= (len)) > 0) { \
32 #define set_sz_from_precision() \
35 if (precision != -1) { \
37 sz = precision + 1; /* +1 for the final '\0' */ \
43 #define show_it(data, letter) \
53 if (precision == -1) { \
54 len = snprintf(p, rem_size, "%*" letter, wd, (data)); \
56 len = snprintf(p, rem_size, "%*.*" letter, wd, precision, (data)); \
59 check_overflow(len); \
63 #define show_string(data) \
65 const char* show_string_data = (data); \
66 show_it(show_string_data ? show_string_data : "(null)", "s"); \
69 #define show_int(data) show_it((data), "d")
70 #define show_double(data) show_it((data), "f")
72 static bool xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_event_t ev, const char* msg_fmt)
75 int rem_size = ev->buffer_size;
79 auto* q = static_cast<char*>(l->data);
95 fprintf(stderr, "Layout format (%s) ending with %%\n", (char*)l->data);
101 case 'n': /* platform-dependant line separator; LOG4J compliant */
105 case 'e': /* plain space; SimGrid extension */
109 case '.': /* precision specifier */
110 precision = static_cast<int>(strtol(q + 1, &q, 10));
111 continue; /* conversion specifier still not found, continue reading */
121 case '9': /* length modifier */
122 length = static_cast<int>(strtol(q, &q, 10));
123 continue; /* conversion specifier still not found, continue reading */
124 case 'c': /* category name; LOG4J compliant
125 should accept a precision postfix to show the hierarchy */
126 show_string(ev->cat->name);
128 case 'p': /* priority name; LOG4J compliant */
129 show_string(xbt_log_priority_names[ev->priority]);
131 case 'h': /* host name; SimGrid extension */
132 show_string(sg_host_self_get_name());
134 case 't': /* thread/process name; LOG4J compliant */
135 case 'P': /* Used before SimGrid 3.26 and kept for compatiblity. Should not hurt. */
136 case 'a': /* actor name; SimGrid extension */
137 show_string(xbt_procname());
139 case 'i': /* actor ID; 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 */
146 set_sz_from_precision();
147 len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
148 check_overflow(std::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 'd': /* date; LOG4J compliant */
157 case 'r': /* application age; LOG4J compliant */
158 show_double(simgrid_get_clock());
160 case 'm': /* user-provided message; LOG4J compliant */
161 set_sz_from_precision();
164 len = vsnprintf(p, sz, msg_fmt, ap);
166 check_overflow(std::min(sz, len));
169 fprintf(stderr, ERRMSG, *q, (char*)l->data);
172 break; /* done, continue normally */
181 static void xbt_log_layout_format_free(const s_xbt_log_layout_t* lay)
186 xbt_log_layout_t xbt_log_layout_format_new(const char* arg)
188 auto* res = xbt_new0(s_xbt_log_layout_t, 1);
189 res->do_layout = &xbt_log_layout_format_doit;
190 res->free_ = &xbt_log_layout_format_free;
191 res->data = xbt_strdup(arg);