Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tidy the scope of some more locals
[simgrid.git] / src / xbt / xbt_log_layout_simple.c
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2007-2017. The SimGrid Team. 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 "src/xbt/log_private.h"
10
11 #include "simgrid/simix.h"      /* SIMIX_host_self_get_name */
12 #include "surf/surf.h"
13 #include <stdio.h>
14
15 extern const char *xbt_log_priority_names[8];
16 extern int xbt_log_no_loc;
17
18 static double simple_begin_of_time = -1;
19
20 #define check_overflow(len)                                             \
21   if ((rem_size -= (len)) > 0) {                                        \
22     p += (len);                                                         \
23   } else                                                                \
24     return 0
25
26 static int xbt_log_layout_simple_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *fmt)
27 {
28   char *p = ev->buffer;
29   int rem_size = ev->buffer_size;
30   const char *procname;
31   int len;
32
33   *p = '[';
34   check_overflow(1);
35
36   /* Display the proc info if available */
37   procname = xbt_procname();
38   if (procname && strcmp(procname,"maestro")) {
39     len = snprintf(p, rem_size, "%s:%s:(%d) ", SIMIX_host_self_get_name(), procname, xbt_getpid());
40     check_overflow(len);
41   }
42   else if (!procname)  {
43   len = snprintf(p, rem_size, "%s::(%d) ", SIMIX_host_self_get_name(), xbt_getpid());
44   check_overflow(len);
45   }
46
47   /* Display the date */
48   len = snprintf(p, rem_size, "%f] ", surf_get_clock() - simple_begin_of_time);
49   check_overflow(len);
50
51   /* Display file position if not INFO */
52   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc) {
53     len = snprintf(p, rem_size, "%s:%d: ", ev->fileName, ev->lineNum);
54     check_overflow(len);
55   }
56
57   /* Display category name */
58   len = snprintf(p, rem_size, "[%s/%s] ", ev->cat->name, xbt_log_priority_names[ev->priority]);
59   check_overflow(len);
60
61   /* Display user-provided message */
62   len = vsnprintf(p, rem_size, fmt, ev->ap);
63   check_overflow(len);
64
65   /* End it */
66   *p = '\n';
67   check_overflow(1);
68   *p = '\0';
69
70   return 1;
71 }
72
73 xbt_log_layout_t xbt_log_layout_simple_new(char *arg)
74 {
75   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
76   res->do_layout       = &xbt_log_layout_simple_doit;
77
78   if (simple_begin_of_time < 0)
79     simple_begin_of_time = surf_get_clock();
80
81   return res;
82 }