Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Coding style: ! -> 'not'.
[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 "src/xbt/log_private.hpp"
9 #include "xbt/sysdep.h"
10 #include "xbt/virtu.h"
11
12 #include "simgrid/engine.h" /* simgrid_get_clock */
13 #include "simgrid/host.h"   /* sg_host_self_get_name */
14 #include <cstdio>
15
16 extern int xbt_log_no_loc;
17
18 #define check_overflow(len)                                                                                            \
19   do {                                                                                                                 \
20     rem_size -= (len);                                                                                                 \
21     if (rem_size <= 0)                                                                                                 \
22       return false;                                                                                                    \
23     p += (len);                                                                                                        \
24   } while (0)
25
26 static bool xbt_log_layout_simple_doit(const s_xbt_log_layout_t*, 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) ", sg_host_self_get_name(), procname, xbt_getpid());
40     check_overflow(len);
41   } else if (not 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 && not 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 }