1 /* layout_simple - a dumb log layout */
3 /* Copyright (c) 2007-2020. 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"
15 extern const char *xbt_log_priority_names[8];
17 static constexpr const char* ERRMSG =
18 "Unknown %%%c sequence in layout format (%s).\n"
20 " what: %%m: user message %%c: log category %%p: log priority\n"
22 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n"
23 " runtime: %%h: hostname %%t: thread %%P: process %%i: PID\n"
24 " when: %%d: date %%r: app. age\n"
25 " other: %%%%: %% %%n: new line %%e: plain space\n";
27 #define check_overflow(len) \
28 if ((rem_size -= (len)) > 0) { \
33 #define set_sz_from_precision() \
36 if (precision != -1) { \
38 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"); \
68 #define show_int(data) show_it((data), "d")
69 #define show_double(data) show_it((data), "f")
71 static int xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_event_t ev, const char* msg_fmt)
74 int rem_size = ev->buffer_size;
78 char* q = static_cast<char*>(l->data);
85 fprintf(stderr, "Layout format (%s) ending with %%\n", (char*)l->data);
91 case 'n': /* platform-dependant line separator; LOG4J compliant */
95 case 'e': /* plain space; SimGrid extension */
99 case '.': /* precision specifier */
100 precision = strtol(q + 1, &q, 10);
101 continue; /* conversion specifier still not found, continue reading */
111 case '9': /* length modifier */
112 length = strtol(q, &q, 10);
113 continue; /* conversion specifier still not found, continue reading */
114 case 'c': /* category name; LOG4J compliant
115 should accept a precision postfix to show the hierarchy */
116 show_string(ev->cat->name);
118 case 'p': /* priority name; LOG4J compliant */
119 show_string(xbt_log_priority_names[ev->priority]);
121 case 'h': /* host name; SimGrid extension */
122 show_string(sg_host_self_get_name());
124 case 't': /* thread/process name; LOG4J compliant */
125 case 'P': /* process name; SimGrid extension */
126 show_string(xbt_procname());
128 case 'i': /* process PID name; SimGrid extension */
129 show_int(xbt_getpid());
131 case 'F': /* file name; LOG4J compliant */
132 show_string(ev->fileName);
134 case 'l': { /* location; LOG4J compliant */
136 set_sz_from_precision();
137 int len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
138 check_overflow(std::min(sz, len));
141 case 'L': /* line number; LOG4J compliant */
142 show_int(ev->lineNum);
144 case 'M': /* method (ie, function) name; LOG4J compliant */
145 show_string(ev->functionName);
147 case 'd': /* date; LOG4J compliant */
148 show_double(simgrid_get_clock());
150 case 'r': /* application age; LOG4J compliant */
151 show_double(simgrid_get_clock());
153 case 'm': { /* user-provided message; LOG4J compliant */
155 set_sz_from_precision();
158 int len = vsnprintf(p, sz, msg_fmt, ap);
160 check_overflow(std::min(sz, len));
164 fprintf(stderr, ERRMSG, *q, (char*)l->data);
167 break; /* done, continue normally */
180 static void xbt_log_layout_format_free(const s_xbt_log_layout_t* lay)
185 xbt_log_layout_t xbt_log_layout_format_new(const char* arg)
187 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
188 res->do_layout = &xbt_log_layout_format_doit;
189 res->free_ = &xbt_log_layout_format_free;
190 res->data = xbt_strdup(arg);