File: logger.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (218 lines) | stat: -rw-r--r-- 6,837 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#include "caf/config.hpp"

#define CAF_SUITE logger
#include "caf/test/unit_test.hpp"

#include <ctime>
#include <string>

#include "caf/all.hpp"

using namespace caf;
using namespace std::chrono;

using std::string;

#define CHECK_FUN_PREFIX(PrefixName)                                           \
  do {                                                                         \
    auto e = CAF_LOG_MAKE_EVENT(0, "caf", CAF_LOG_LEVEL_DEBUG, "");            \
    std::ostringstream oss;                                                    \
    ::caf::logger::render_fun_prefix(oss, e);                                  \
    auto prefix = oss.str();                                                   \
    if (prefix != PrefixName)                                                  \
      CAF_ERROR("rendering the prefix of " << e.pretty_fun << " produced "     \
                                           << prefix << " instead of "         \
                                           << PrefixName);                     \
    else                                                                       \
      CAF_CHECK_EQUAL(prefix, PrefixName);                                     \
  } while (false)

void global_fun() {
  CHECK_FUN_PREFIX("GLOBAL");
}

// Little helper that allows us to write a single check for all compilers.
// Clang expands template parameters in __PRETTY_FUNCTION__, while GCC does
// not. For example, Clang would produce "void foo<int>::bar()", while GCC
// would produce "void foo<T>::bar() [with T = int]". A type called T gives
// us always the same ouptut for the prefix.
struct T {};

namespace {

struct fixture {
  fixture() {
    cfg.set("scheduler.policy", atom("testing"));
  }

  void add(logger::field_type kind) {
    lf.emplace_back(logger::field{kind, std::string{}});
  }

  template <size_t N>
  void add(logger::field_type kind, const char (&str)[N]) {
    lf.emplace_back(logger::field{kind, std::string{str, str + (N - 1)}});
  }

  template <class F, class... Ts>
  string render(F f, Ts&&... xs) {
    std::ostringstream oss;
    f(oss, std::forward<Ts>(xs)...);
    return oss.str();
  }

  actor_system_config cfg;
  logger::line_format lf;
};

void fun() {
  CHECK_FUN_PREFIX("$");
}

const char* ptr_fun(const char* x) {
  CHECK_FUN_PREFIX("$");
  return x;
}

const int& ref_fun(const int& x) {
  CHECK_FUN_PREFIX("$");
  return x;
}

template <class T>
struct tpl {
  static void run() {
    CHECK_FUN_PREFIX("$.tpl<T>");
  }
};

namespace foo {

void fun() {
  CHECK_FUN_PREFIX("$.foo");
}

const char* ptr_fun(const char* x) {
  CHECK_FUN_PREFIX("$.foo");
  return x;
}

const int& ref_fun(const int& x) {
  CHECK_FUN_PREFIX("$.foo");
  return x;
}

template <class T>
struct tpl {
  static void run() {
    CHECK_FUN_PREFIX("$.foo.tpl<T>");
  }
};

} // namespace foo

constexpr const char* file_format = "%r %c %p %a %t %C %M %F:%L %m%n";

} // namespace

CAF_TEST_FIXTURE_SCOPE(logger_tests, fixture)

// copy construction, copy assign, move construction, move assign
// and finally serialization round-trip
CAF_TEST(parse_default_format_strings) {
  actor_system sys{cfg};
  add(logger::runtime_field);
  add(logger::plain_text_field, " ");
  add(logger::category_field);
  add(logger::plain_text_field, " ");
  add(logger::priority_field);
  add(logger::plain_text_field, " ");
  add(logger::actor_field);
  add(logger::plain_text_field, " ");
  add(logger::thread_field);
  add(logger::plain_text_field, " ");
  add(logger::class_name_field);
  add(logger::plain_text_field, " ");
  add(logger::method_field);
  add(logger::plain_text_field, " ");
  add(logger::file_field);
  add(logger::plain_text_field, ":");
  add(logger::line_field);
  add(logger::plain_text_field, " ");
  add(logger::message_field);
  add(logger::newline_field);
  CAF_CHECK_EQUAL(logger::parse_format(file_format), lf);
#if CAF_LOG_LEVEL >= 0
  // Not parsed when compiling without logging enabled.
  CAF_CHECK_EQUAL(sys.logger().file_format(), lf);
#endif
}

CAF_TEST(rendering) {
  // Rendering of time points.
  timestamp t0;
  timestamp t1{timestamp::duration{5000000}}; // epoch + 5000000ns (5ms)
  CAF_CHECK_EQUAL(render(logger::render_time_diff, t0, t1), "5");
  time_t t0_t = 0;
  char t0_buf[50];
  CAF_REQUIRE(strftime(t0_buf, sizeof(t0_buf),
                       "%Y-%m-%dT%H:%M:%S.000", localtime(&t0_t)));
  CAF_CHECK_EQUAL(render(logger::render_date, t0), t0_buf);
  // Rendering of events.
  logger::event e{
    CAF_LOG_LEVEL_WARNING,
    42,
    atom("unit_test"),
    "void ns::foo::bar()",
    "bar",
    "foo.cpp",
    "hello world",
    std::this_thread::get_id(),
    0,
    t0
  };
  CAF_CHECK_EQUAL(render(logger::render_fun_name, e), string_view{"bar"});
  CAF_CHECK_EQUAL(render(logger::render_fun_prefix, e),
                  string_view{"ns.foo"});
  // Exclude %r and %t from rendering test because they are nondeterministic.
  actor_system sys{cfg};
  auto lf = logger::parse_format("%c %p %a %C %M %F:%L %m");
  auto& lg = sys.logger();
  using namespace std::placeholders;
  auto render_event = bind(&logger::render, &lg, _1, _2, _3);
  CAF_CHECK_EQUAL(render(render_event, lf, e),
                  "unit_test WARN actor0 ns.foo bar foo.cpp:42 hello world");
}

CAF_TEST(render_fun_prefix) {
  int i = 42;
  global_fun();
  fun();
  ptr_fun(nullptr);
  ref_fun(i);
  tpl<T>::run();
  foo::fun();
  foo::ptr_fun(nullptr);
  foo::ref_fun(i);
  foo::tpl<T>::run();
}

CAF_TEST_FIXTURE_SCOPE_END()