Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
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 "xbt/sysdep.h"
9 #include "xbt/strbuff.h"        /* For dynamic version when the static one fails */
10 #include "xbt/log_private.h"
11 #include "xbt/synchro.h"        /* xbt_thread_name */
12
13 #include "gras/virtu.h"
14 #include <stdio.h>
15 #include "portable.h"
16
17 extern const char *xbt_log_priority_names[8];
18 extern int xbt_log_no_loc;
19
20 static double simple_begin_of_time = -1;
21
22 static void xbt_log_layout_simple_dynamic(xbt_log_layout_t l,
23                                           xbt_log_event_t ev,
24                                           const char *fmt,
25                                           xbt_log_appender_t app)
26 {
27   xbt_strbuff_t buff = xbt_strbuff_new();
28   char loc_buff[256];
29   char *p;
30
31   /* Put every static information in a static buffer, and copy them in the dyn one */
32   p = loc_buff;
33   p += snprintf(p, 256 - (p - loc_buff), "[");
34
35   if (strlen(xbt_procname()))
36     p += snprintf(p, 256 - (p - loc_buff), "%s:%s:(%d) ",
37                   gras_os_myname(), xbt_procname(), (*xbt_getpid) ());
38   p +=
39     snprintf(p, 256 - (p - loc_buff), "%f] ",
40              gras_os_time() - simple_begin_of_time);
41   if (ev->priority != xbt_log_priority_info && xbt_log_no_loc==0)
42     p +=
43       snprintf(p, 256 - (p - loc_buff), "%s:%d: ", ev->fileName,
44                ev->lineNum);
45   p +=
46     snprintf(p, 256 - (p - loc_buff), "[%s/%s] ", ev->cat->name,
47              xbt_log_priority_names[ev->priority]);
48
49   xbt_strbuff_append(buff, loc_buff);
50
51   vasprintf(&p, fmt, ev->ap_copy);
52   xbt_strbuff_append(buff, p);
53   free(p);
54
55   xbt_strbuff_append(buff, "\n");
56
57   app->do_append(app, buff->data);
58   xbt_strbuff_free(buff);
59 }
60
61 /* only used after the format using: we suppose that the buffer is big enough to display our data */
62 #undef check_overflow
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); \
66   return; \
67   }
68
69 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
70                                        xbt_log_event_t ev,
71                                        const char *fmt,
72                                        xbt_log_appender_t app)
73 {
74   char *p;
75
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",
80               ev->priority);
81
82   p = ev->buffer;
83   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[");
84   check_overflow;
85
86   /* Display the proc info if available */
87   const char *procname=xbt_procname();
88   if (strlen(procname)) {
89     p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%s:(%d) ",
90                   gras_os_myname(), procname, (*xbt_getpid) ());
91     check_overflow;
92   }
93
94   /* Display the date */
95   p +=
96     snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%f] ",
97              gras_os_time() - simple_begin_of_time);
98   check_overflow;
99
100   /* Display file position if not INFO */
101   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc)
102     p +=
103       snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "%s:%d: ",
104                ev->fileName, ev->lineNum);
105
106   /* Display category name */
107   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "[%s/%s] ",
108                 ev->cat->name, xbt_log_priority_names[ev->priority]);
109
110   /* Display user-provided message */
111   p += vsnprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), fmt, ev->ap);
112   check_overflow;
113
114   /* End it */
115   p += snprintf(p, XBT_LOG_BUFF_SIZE - (p - ev->buffer), "\n");
116   check_overflow;
117   app->do_append(app, ev->buffer);
118 }
119
120 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
121 {
122   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
123   res->do_layout = xbt_log_layout_simple_doit;
124
125   if (simple_begin_of_time < 0)
126     simple_begin_of_time = gras_os_time();
127
128   return res;
129 }