Logo AND Algorithmique Numérique Distribuée

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