10
10
//
11
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
12
12
13
- #include < mutex>
14
- #include < string>
15
- #include < vector>
16
-
17
-
13
+ #include " iox/string.hpp"
14
+ #include " iox/vector.hpp"
18
15
#include " iox2/log.hpp"
19
16
#include " iox2/log_level.hpp"
20
17
21
18
#include " test.hpp"
22
19
23
20
namespace {
24
21
using namespace iox2 ;
22
+ constexpr uint64_t TEST_LOGGER_CAPACITY = 10 ;
23
+ constexpr uint64_t STRING_CAPACITY = 64 ;
25
24
class Entry {
26
25
private:
27
26
LogLevel m_log_level;
28
- std ::string m_origin;
29
- std ::string m_message;
27
+ iox ::string<STRING_CAPACITY> m_origin;
28
+ iox ::string<STRING_CAPACITY> m_message;
30
29
31
30
public:
32
31
Entry (LogLevel log_level, const char * origin, const char * message)
33
32
: m_log_level { log_level }
34
- , m_origin { origin }
35
- , m_message { message } {
33
+ , m_origin { iox::TruncateToCapacity, origin }
34
+ , m_message { iox::TruncateToCapacity, message } {
36
35
}
37
36
38
37
auto is_equal (LogLevel log_level, const char * origin, const char * message) -> bool {
39
- return m_log_level == log_level && m_origin == origin && m_message == message;
38
+ return m_log_level == log_level && m_origin == iox::string<STRING_CAPACITY>(iox::TruncateToCapacity, origin)
39
+ && m_message == iox::string<STRING_CAPACITY>(iox::TruncateToCapacity, message);
40
40
}
41
41
};
42
42
43
+
43
44
class TestLogger : public Log {
44
45
public:
45
46
static auto set_global_logger () {
@@ -53,22 +54,19 @@ class TestLogger : public Log {
53
54
}
54
55
55
56
void log (LogLevel log_level, const char * origin, const char * message) override {
56
- m_lock. lock ();
57
- m_log_buffer.emplace_back (log_level, origin, message);
58
- m_lock. unlock ();
57
+ if (m_log_buffer. size () < TEST_LOGGER_CAPACITY) {
58
+ m_log_buffer.emplace_back (log_level, origin, message);
59
+ }
59
60
}
60
61
61
- auto get_log_buffer () -> std::vector<Entry> {
62
- m_lock.lock ();
62
+ auto get_log_buffer () -> iox::vector<Entry, TEST_LOGGER_CAPACITY> {
63
63
auto buffer = m_log_buffer;
64
64
m_log_buffer.clear ();
65
- m_lock.unlock ();
66
65
return buffer;
67
66
}
68
67
69
68
private:
70
- std::mutex m_lock;
71
- std::vector<Entry> m_log_buffer;
69
+ iox::vector<Entry, TEST_LOGGER_CAPACITY> m_log_buffer;
72
70
};
73
71
74
72
TEST (Log, custom_logger_works) {
0 commit comments