Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve xbt_log_layout_simple_doit().
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2007-2011. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/strbuff.h"        /* For dynamic version when the static one fails */
11 #include "xbt/log_private.h"
12 #include "xbt/synchro.h"        /* xbt_thread_name */
13
14 #include "gras/virtu.h"
15 #include <stdio.h>
16 #include "portable.h"
17
18 extern const char *xbt_log_priority_names[8];
19 extern int xbt_log_no_loc;
20
21 static double simple_begin_of_time = -1;
22
23 #define check_overflow(len)                                             \
24   if ((rem_size -= (len)) > 0) {                                        \
25     p += (len);                                                         \
26   } else                                                                \
27     return 0
28
29 static int xbt_log_layout_simple_doit(xbt_log_layout_t l,
30                                       xbt_log_event_t ev,
31                                       const char *fmt)
32 {
33   char *p = ev->buffer;
34   int rem_size = ev->buffer_size;
35   const char *procname;
36   int len;
37
38   *p = '[';
39   check_overflow(1);
40
41   /* Display the proc info if available */
42   procname = xbt_procname();
43   if (*procname) {
44     len = snprintf(p, rem_size, "%s:%s:(%d) ",
45                    gras_os_myname(), procname, xbt_getpid());
46     check_overflow(len);
47   }
48
49   /* Display the date */
50   len = snprintf(p, rem_size, "%f] ",
51                  gras_os_time() - simple_begin_of_time);
52   check_overflow(len);
53
54   /* Display file position if not INFO */
55   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc) {
56     len = snprintf(p, rem_size, "%s:%d: ",
57                    ev->fileName, ev->lineNum);
58     check_overflow(len);
59   }
60
61   /* Display category name */
62   len = snprintf(p, rem_size, "[%s/%s] ",
63                  ev->cat->name, xbt_log_priority_names[ev->priority]);
64   check_overflow(len);
65
66   /* Display user-provided message */
67   len = vsnprintf(p, rem_size, fmt, ev->ap);
68   check_overflow(len);
69
70   /* End it */
71   *p = '\n';
72   check_overflow(1);
73   *p = '\0';
74
75   return 1;
76 }
77
78 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
79 {
80   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
81   res->do_layout = xbt_log_layout_simple_doit;
82
83   if (simple_begin_of_time < 0)
84     simple_begin_of_time = gras_os_time();
85
86   return res;
87 }