Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Get serious about checking buffer overflows during log construction
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* $Id$ */
2
3 /* layout_simple - a dumb log layout                                        */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/synchro.h" /* xbt_thread_name */
13 #include "gras/virtu.h"
14 #include <stdio.h>
15
16 extern const char *xbt_log_priority_names[7];
17
18 /* only used after the format using: we suppose that the buffer is big enough to display our data */
19 #define check_overflow \
20   if (p-ev->buffer > XBT_LOG_BUFF_SIZE) { /* buffer overflow */ \
21      p=ev->buffer + XBT_LOG_BUFF_SIZE - strlen(" >> OUTPUT TRUNCATED <<\n"); \
22      p+=sprintf(p," >> OUTPUT TRUNCATED <<\n"); \
23   } 
24
25 static void xbt_log_layout_simple_doit(xbt_log_layout_t l,
26                                        xbt_log_event_t ev, 
27                                        const char *fmt) {
28   static double begin_of_time = -1;
29   char *p;  
30
31   xbt_assert0(ev->priority>=0,
32               "Negative logging priority naturally forbidden");
33   xbt_assert1(ev->priority<sizeof(xbt_log_priority_names),
34               "Priority %d is greater than the biggest allowed value",
35               ev->priority);
36
37   if (begin_of_time<0) 
38     begin_of_time=gras_os_time();
39
40   p = ev->buffer;
41   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"[");
42
43   /* Display the proc info if available */
44   if(strlen(xbt_procname()))
45     p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%s:%s:(%d) ", 
46                  gras_os_myname(), xbt_procname(),(*xbt_getpid)());
47
48   /* Display the date */
49   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer),"%f] ", gras_os_time()-begin_of_time);
50
51   /* Display file position if not INFO*/
52   if (ev->priority != xbt_log_priority_info)
53     p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "%s:%d: ", ev->fileName, ev->lineNum);
54
55   /* Display category name */
56   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "[%s/%s] ", 
57                ev->cat->name, xbt_log_priority_names[ev->priority] );
58
59   /* Display user-provided message */
60   p += vsnprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), fmt, ev->ap);
61   check_overflow;
62    
63   /* End it */
64   p += snprintf(p,XBT_LOG_BUFF_SIZE-(p-ev->buffer), "\n");
65   check_overflow;
66 }
67
68 xbt_log_layout_t xbt_log_layout_simple_new(char *arg) {
69   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1);
70   res->do_layout = xbt_log_layout_simple_doit;
71   return res;
72 }