Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:Adrien.Gougeon/simgrid into master
[simgrid.git] / src / xbt / xbt_log_layout_format.cpp
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2007-2020. The SimGrid Team.                               */
4
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. */
7
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 <algorithm>
13 #include <cstdio>
14
15 extern const char *xbt_log_priority_names[8];
16
17 static constexpr const char* ERRMSG =
18     "Unknown %%%c sequence in layout format (%s).\n"
19     "Known sequences:\n"
20     "  what:        %%m: user message  %%c: log category  %%p: log priority\n"
21     "  where:\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";
26
27 #define check_overflow(len)                                             \
28   if ((rem_size -= (len)) > 0) {                                        \
29     p += (len);                                                         \
30   } else                                                                \
31     return 0
32
33 #define set_sz_from_precision()                                                                                        \
34   if (true) {                                                                                                          \
35     sz = rem_size;                                                                                                     \
36     if (precision != -1) {                                                                                             \
37       if (precision < sz)                                                                                              \
38         sz = precision + 1; /* +1 for the final '\0' */                                                                \
39       precision = -1;                                                                                                  \
40     }                                                                                                                  \
41   } else                                                                                                               \
42     (void)0
43
44 #define show_it(data, letter)                                                                                          \
45   if (true) {                                                                                                          \
46     int len;                                                                                                           \
47     int wd;                                                                                                            \
48     if (length == -1) {                                                                                                \
49       wd = 0;                                                                                                          \
50     } else {                                                                                                           \
51       wd     = length;                                                                                                 \
52       length = -1;                                                                                                     \
53     }                                                                                                                  \
54     if (precision == -1) {                                                                                             \
55       len = snprintf(p, rem_size, "%*" letter, wd, (data));                                                            \
56     } else {                                                                                                           \
57       len       = snprintf(p, rem_size, "%*.*" letter, wd, precision, (data));                                         \
58       precision = -1;                                                                                                  \
59     }                                                                                                                  \
60     check_overflow(len);                                                                                               \
61   } else                                                                                                               \
62     (void)0
63
64 #define show_string(data)                                                                                              \
65   if (true) {                                                                                                          \
66     const char* show_string_data = (data);                                                                             \
67     show_it(show_string_data ? show_string_data : "(null)", "s");                                                      \
68   } else                                                                                                               \
69     (void)0
70 #define show_int(data) show_it((data), "d")
71 #define show_double(data) show_it((data), "f")
72
73 static int xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_event_t ev, const char* msg_fmt)
74 {
75   char *p = ev->buffer;
76   int rem_size = ev->buffer_size;
77   int precision = -1;
78   int length = -1;
79
80   char* q = static_cast<char*>(l->data);
81   while (*q != '\0') {
82     if (*q == '%') {
83       q++;
84       do {
85         switch (*q) {
86           case '\0':
87             fprintf(stderr, "Layout format (%s) ending with %%\n", (char*)l->data);
88             xbt_abort();
89           case '%':
90             *p = '%';
91             check_overflow(1);
92             break;
93           case 'n': /* platform-dependant line separator; LOG4J compliant */
94             *p = '\n';
95             check_overflow(1);
96             break;
97           case 'e': /* plain space; SimGrid extension */
98             *p = ' ';
99             check_overflow(1);
100             break;
101           case '.': /* precision specifier */
102             precision = static_cast<int>(strtol(q + 1, &q, 10));
103             continue; /* conversion specifier still not found, continue reading */
104           case '0':
105           case '1':
106           case '2':
107           case '3':
108           case '4':
109           case '5':
110           case '6':
111           case '7':
112           case '8':
113           case '9': /* length modifier */
114             length = static_cast<int>(strtol(q, &q, 10));
115             continue; /* conversion specifier still not found, continue reading */
116           case 'c':   /* category name; LOG4J compliant
117                          should accept a precision postfix to show the hierarchy */
118             show_string(ev->cat->name);
119             break;
120           case 'p': /* priority name; LOG4J compliant */
121             show_string(xbt_log_priority_names[ev->priority]);
122             break;
123           case 'h': /* host name; SimGrid extension */
124             show_string(sg_host_self_get_name());
125             break;
126           case 't': /* thread/process name; LOG4J compliant */
127           case 'P': /* process name; SimGrid extension */
128             show_string(xbt_procname());
129             break;
130           case 'i': /* process PID name; SimGrid extension */
131             show_int(xbt_getpid());
132             break;
133           case 'F': /* file name; LOG4J compliant */
134             show_string(ev->fileName);
135             break;
136           case 'l': { /* location; LOG4J compliant */
137             int sz;
138             set_sz_from_precision();
139             int len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
140             check_overflow(std::min(sz, len));
141             break;
142           }
143           case 'L': /* line number; LOG4J compliant */
144             show_int(ev->lineNum);
145             break;
146           case 'M': /* method (ie, function) name; LOG4J compliant */
147             show_string(ev->functionName);
148             break;
149           case 'd': /* date; LOG4J compliant */
150             show_double(simgrid_get_clock());
151             break;
152           case 'r': /* application age; LOG4J compliant */
153             show_double(simgrid_get_clock());
154             break;
155           case 'm': { /* user-provided message; LOG4J compliant */
156             int sz;
157             set_sz_from_precision();
158             va_list ap;
159             va_copy(ap, ev->ap);
160             int len = vsnprintf(p, sz, msg_fmt, ap);
161             va_end(ap);
162             check_overflow(std::min(sz, len));
163             break;
164           }
165           default:
166             fprintf(stderr, ERRMSG, *q, (char*)l->data);
167             xbt_abort();
168         }
169         break; /* done, continue normally */
170       } while (true);
171     } else {
172       *p = *q;
173       check_overflow(1);
174     }
175     q++;
176   }
177   *p = '\0';
178
179   return 1;
180 }
181
182 static void xbt_log_layout_format_free(const s_xbt_log_layout_t* lay)
183 {
184   xbt_free(lay->data);
185 }
186
187 xbt_log_layout_t xbt_log_layout_format_new(const char* arg)
188 {
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(arg);
193
194   return res;
195 }