Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert "Use C11's atomic_flag."
[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/host.h" /* sg_host_self_get_name */
12 #include "simgrid/msg.h"  /* MSG_get_clock */
13 #include <stdio.h>
14
15 extern const char *xbt_log_priority_names[8];
16 extern int xbt_log_no_loc;
17
18 #define check_overflow(len)                                             \
19   if ((rem_size -= (len)) > 0) {                                        \
20     p += (len);                                                         \
21   } else                                                                \
22     return 0
23
24 static int xbt_log_layout_simple_doit(XBT_ATTRIB_UNUSED xbt_log_layout_t l, xbt_log_event_t ev, const char* fmt)
25 {
26   char *p = ev->buffer;
27   int rem_size = ev->buffer_size;
28   const char *procname;
29   int len;
30
31   *p = '[';
32   check_overflow(1);
33
34   /* Display the proc info if available */
35   procname = xbt_procname();
36   if (procname && strcmp(procname,"maestro")) {
37     len = snprintf(p, rem_size, "%s:%s:(%d) ", sg_host_self_get_name(), procname, xbt_getpid());
38     check_overflow(len);
39   }
40   else if (!procname)  {
41     len = snprintf(p, rem_size, "%s::(%d) ", sg_host_self_get_name(), xbt_getpid());
42     check_overflow(len);
43   }
44
45   /* Display the date */
46   len = snprintf(p, rem_size, "%f] ", MSG_get_clock());
47   check_overflow(len);
48
49   /* Display file position if not INFO */
50   if (ev->priority != xbt_log_priority_info && !xbt_log_no_loc) {
51     len = snprintf(p, rem_size, "%s:%d: ", ev->fileName, ev->lineNum);
52     check_overflow(len);
53   }
54
55   /* Display category name */
56   len = snprintf(p, rem_size, "[%s/%s] ", ev->cat->name, xbt_log_priority_names[ev->priority]);
57   check_overflow(len);
58
59   /* Display user-provided message */
60   len = vsnprintf(p, rem_size, fmt, ev->ap);
61   check_overflow(len);
62
63   /* End it */
64   *p = '\n';
65   check_overflow(1);
66   *p = '\0';
67
68   return 1;
69 }
70
71 xbt_log_layout_t xbt_log_layout_simple_new(XBT_ATTRIB_UNUSED char* arg)
72 {
73   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
74   res->do_layout       = &xbt_log_layout_simple_doit;
75
76   return res;
77 }