Logo AND Algorithmique Numérique Distribuée

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