3 /* layout_simple - a dumb log layout */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
11 #include "xbt/strbuff.h" /* For dynamic version when the static one fails */
12 #include "xbt/log_private.h"
13 #include "xbt/synchro.h" /* xbt_thread_name */
15 #include "gras/virtu.h"
19 extern const char *xbt_log_priority_names[7];
21 static double begin_of_time = -1;
23 static void xbt_log_layout_simple_dynamic(xbt_log_layout_t l,
26 xbt_log_appender_t app)
28 xbt_strbuff_t buff = xbt_strbuff_new();
32 /* Put every static information in a static buffer, and copy them in the dyn one */
34 p += snprintf(p, 256 - (p - ev->buffer), "[");
36 if (strlen(xbt_procname()))
37 p += snprintf(p, 256 - (p - ev->buffer), "%s:%s:(%d) ",
38 gras_os_myname(), xbt_procname(), (*xbt_getpid) ());
40 snprintf(p, 256 - (p - ev->buffer), "%f] ",
41 gras_os_time() - begin_of_time);
42 if (ev->priority != xbt_log_priority_info)
44 snprintf(p, 256 - (p - ev->buffer), "%s:%d: ", ev->fileName,
47 snprintf(p, 256 - (p - ev->buffer), "[%s/%s] ", ev->cat->name,
48 xbt_log_priority_names[ev->priority]);
50 xbt_strbuff_append(buff, loc_buff);
52 vasprintf(&p, fmt, ev->ap_copy);
53 xbt_strbuff_append(buff, p);
56 xbt_strbuff_append(buff, "\n");
58 app->do_append(app, buff->data);
59 xbt_strbuff_free(buff);
62 /* only used after the format using: we suppose that the buffer is big enough to display our data */
63 #define check_overflow \
64 if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
65 xbt_log_layout_simple_dynamic(l,ev,fmt,app); \
69 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
72 xbt_log_appender_t app)
76 xbt_assert0(ev->priority >= 0,
77 "Negative logging priority naturally forbidden");
78 xbt_assert1(ev->priority < sizeof(xbt_log_priority_names),
79 "Priority %d is greater than the biggest allowed value",
82 if (begin_of_time < 0)
83 begin_of_time = gras_os_time();
86 p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[");
89 /* Display the proc info if available */
90 if (strlen(xbt_procname())) {
91 p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%s:(%d) ",
92 gras_os_myname(), xbt_procname(), (*xbt_getpid) ());
96 /* Display the date */
98 snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%f] ",
99 gras_os_time() - begin_of_time);
102 /* Display file position if not INFO */
103 if (ev->priority != xbt_log_priority_info)
105 snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%d: ",
106 ev->fileName, ev->lineNum);
108 /* Display category name */
109 p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[%s/%s] ",
110 ev->cat->name, xbt_log_priority_names[ev->priority]);
112 /* Display user-provided message */
113 p += vsnprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), fmt, ev->ap);
117 p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "\n");
119 app->do_append(app, ev->buffer);
122 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
124 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
125 res->do_layout = xbt_log_layout_simple_doit;