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