1 /* layout_simple - a dumb log layout */
3 /* Copyright (c) 2007-2018. 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/host.h"
9 #include "simgrid/msg.h" /* MSG_get_clock */
10 #include "src/xbt/log_private.h"
11 #include "xbt/sysdep.h"
14 #define MIN(a, b) ((a) < (b) ? (a) : (b))
15 #define MAX(a, b) ((a) > (b) ? (a) : (b))
17 extern const char *xbt_log_priority_names[8];
20 "Unknown %%%c sequence in layout format (%s).\n" \
21 "Known sequences:\n" \
22 " what: %%m: user message %%c: log category %%p: log priority\n" \
24 " source: %%F: file %%L: line %%M: function %%l: location (%%F:%%L)\n" \
25 " runtime: %%h: hostname %%t: thread %%P: process %%i: PID\n" \
26 " when: %%d: date %%r: app. age\n" \
27 " other: %%%%: %% %%n: new line %%e: plain space\n"
29 #define check_overflow(len) \
30 if ((rem_size -= (len)) > 0) { \
35 #define set_sz_from_precision() \
38 if (precision != -1) { \
40 sz = precision + 1; /* +1 for the final '\0' */ \
45 #define show_it(data, letter) \
55 if (precision == -1) { \
56 len = snprintf(p, rem_size, "%*" letter, wd, data); \
58 len = snprintf(p, rem_size, "%*.*" letter, wd, precision, data); \
61 check_overflow(len); \
64 #define show_string(data) \
66 const char *show_string_data = (data); \
67 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 int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *msg_fmt)
75 int rem_size = ev->buffer_size;
79 for (char* q = l->data ; *q != '\0' ; q++) {
85 fprintf(stderr, "Layout format (%s) ending with %%\n", (char*)l->data);
92 case 'n': /* platform-dependant line separator; LOG4J compliant */
96 case 'e': /* plain space; SimGrid extension */
100 case '.': /* precision specifier */
101 precision = strtol(q + 1, &q, 10);
102 continue; /* conversion specifier still not found, continue reading */
112 case '9': /* length modifier */
113 length = strtol(q, &q, 10);
114 continue; /* conversion specifier still not found, continue reading */
115 case 'c': /* category name; LOG4J compliant
116 should accept a precision postfix to show the hierarchy */
117 show_string(ev->cat->name);
119 case 'p': /* priority name; LOG4J compliant */
120 show_string(xbt_log_priority_names[ev->priority]);
122 case 'h': /* host name; SimGrid extension */
123 show_string(sg_host_self_get_name());
125 case 't': /* thread/process name; LOG4J compliant */
126 case 'P': /* process name; SimGrid extension */
127 show_string(xbt_procname());
129 case 'i': /* process PID name; SimGrid extension */
130 show_int(xbt_getpid());
132 case 'F': /* file name; LOG4J compliant */
133 show_string(ev->fileName);
135 case 'l': { /* location; LOG4J compliant */
137 set_sz_from_precision();
138 int len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
139 check_overflow(MIN(sz, len));
142 case 'L': /* line number; LOG4J compliant */
143 show_int(ev->lineNum);
145 case 'M': /* method (ie, function) name; LOG4J compliant */
146 show_string(ev->functionName);
148 case 'd': /* date; LOG4J compliant */
149 show_double(MSG_get_clock());
151 case 'r': /* application age; LOG4J compliant */
152 show_double(MSG_get_clock());
154 case 'm': { /* user-provided message; LOG4J compliant */
156 set_sz_from_precision();
159 int len = vsnprintf(p, sz, msg_fmt, ap);
161 check_overflow(MIN(sz, len));
165 fprintf(stderr, ERRMSG, *q, (char*)l->data);
168 break; /* done, continue normally */
180 static void xbt_log_layout_format_free(xbt_log_layout_t lay)
185 xbt_log_layout_t xbt_log_layout_format_new(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((char *) arg);