Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / xbt / xbt_log_layout_simple.cpp
1 /* layout_simple - a dumb log layout                                        */
2
3 /* Copyright (c) 2007-2023. 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 <simgrid/actor.h>
11 #include <simgrid/s4u/Actor.hpp>
12
13 #include "simgrid/engine.h" /* simgrid_get_clock */
14 #include "simgrid/host.h"   /* sg_host_self_get_name */
15 #include <cstdio>
16
17 extern int xbt_log_no_loc;
18
19 #define check_overflow(len)                                                                                            \
20   do {                                                                                                                 \
21     rem_size -= (len);                                                                                                 \
22     if (rem_size <= 0)                                                                                                 \
23       return false;                                                                                                    \
24     p += (len);                                                                                                        \
25   } while (0)
26
27 static bool xbt_log_layout_simple_doit(const s_xbt_log_layout_t*, xbt_log_event_t ev, const char* fmt)
28 {
29   char *p = ev->buffer;
30   int rem_size = ev->buffer_size;
31   const char *procname;
32   int len;
33
34   *p = '[';
35   check_overflow(1);
36
37   /* Display the proc info if available */
38   procname = sg_actor_self_get_name();
39   if (procname && strcmp(procname,"maestro")) {
40     len = snprintf(p, rem_size, "%s:%s:(%ld) ", sg_host_self_get_name(), procname, sg_actor_self_get_pid());
41     check_overflow(len);
42   } else if (not procname) {
43     len = snprintf(p, rem_size, "%s::(%ld) ", sg_host_self_get_name(), sg_actor_self_get_pid());
44     check_overflow(len);
45   }
46
47   /* Display the date */
48   len = snprintf(p, rem_size, "%f] ", simgrid_get_clock());
49   check_overflow(len);
50
51   /* Display file position if not INFO */
52   if (ev->priority != xbt_log_priority_info && not 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 true;
71 }
72
73 xbt_log_layout_t xbt_log_layout_simple_new(const char*)
74 {
75   auto* res      = xbt_new0(s_xbt_log_layout_t, 1);
76   res->do_layout = &xbt_log_layout_simple_doit;
77
78   return res;
79 }