Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
84571d6a8637caeb018627ffdd8f13bfa65c5da4
[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 "simgrid/simix.h"      /* SIMIX_host_self_get_name */
15 #include "surf/surf.h"
16 #include <stdio.h>
17 #include "portable.h"
18
19 extern const char *xbt_log_priority_names[8];
20 extern int xbt_log_no_loc;
21
22 static double simple_begin_of_time = -1;
23
24 #define check_overflow(len)                                             \
25   if ((rem_size -= (len)) > 0) {                                        \
26     p += (len);                                                         \
27   } else                                                                \
28     return 0
29
30 static int xbt_log_layout_simple_doit(xbt_log_layout_t l,
31                                       xbt_log_event_t ev,
32                                       const char *fmt)
33 {
34   char *p = ev->buffer;
35   int rem_size = ev->buffer_size;
36   const char *procname;
37   int len;
38
39   *p = '[';
40   check_overflow(1);
41
42   /* Display the proc info if available */
43   procname = xbt_procname();
44   if (procname && *procname) {
45     len = snprintf(p, rem_size, "%s:%s:(%d) ",
46                    SIMIX_host_self_get_name(), procname, xbt_getpid());
47     check_overflow(len);
48   }
49   else if (!procname)  {
50   len = snprintf(p, rem_size, "%s::(%d) ",
51                  SIMIX_host_self_get_name(), xbt_getpid());
52   check_overflow(len);
53   }
54
55   /* Display the date */
56   len = snprintf(p, rem_size, "%f] ",
57                  surf_get_clock() - simple_begin_of_time);
58   check_overflow(len);
59
60   /* Display file position if not INFO */
61   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc) {
62     len = snprintf(p, rem_size, "%s:%d: ",
63                    ev->fileName, ev->lineNum);
64     check_overflow(len);
65   }
66
67   /* Display category name */
68   len = snprintf(p, rem_size, "[%s/%s] ",
69                  ev->cat->name, xbt_log_priority_names[ev->priority]);
70   check_overflow(len);
71
72   /* Display user-provided message */
73   len = vsnprintf(p, rem_size, fmt, ev->ap);
74   check_overflow(len);
75
76   /* End it */
77   *p = '\n';
78   check_overflow(1);
79   *p = '\0';
80
81   return 1;
82 }
83
84 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
85 {
86   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
87   res->do_layout = xbt_log_layout_simple_doit;
88
89   if (simple_begin_of_time < 0)
90     simple_begin_of_time = surf_get_clock();
91
92   return res;
93 }