CppLogging 1.0.5.0
C++ Logging Library
Loading...
Searching...
No Matches
binary_layout.cpp
Go to the documentation of this file.
1
10
11#include <cstring>
12#include <vector>
13
14namespace CppLogging {
15
17{
18 // Calculate logging record size
19 uint32_t size = (uint32_t)(sizeof(uint64_t) + sizeof(uint64_t) + sizeof(Level) + sizeof(uint8_t) + record.logger.size() + sizeof(uint16_t) + record.message.size() + sizeof(uint32_t) + record.buffer.size());
20
21 // Resize the raw buffer to the required size
22 record.raw.resize(sizeof(uint32_t) + size + 1);
23
24 // Get the raw buffer start position
25 uint8_t* buffer = record.raw.data();
26
27 // Serialize the logging record
28 std::memcpy(buffer, &size, sizeof(uint32_t));
29 buffer += sizeof(uint32_t);
30 std::memcpy(buffer, &record.timestamp, sizeof(uint64_t));
31 buffer += sizeof(uint64_t);
32 std::memcpy(buffer, &record.thread, sizeof(uint64_t));
33 buffer += sizeof(uint64_t);
34 std::memcpy(buffer, &record.level, sizeof(Level));
35 buffer += sizeof(Level);
36
37 // Serialize the logger name
38 uint8_t logger_size = (uint8_t)record.logger.size();
39 std::memcpy(buffer, &logger_size, sizeof(uint8_t));
40 buffer += sizeof(uint8_t);
41 std::memcpy(buffer, record.logger.data(), record.logger.size());
42 buffer += record.logger.size();
43
44 // Serialize the logging message
45 uint16_t message_size = (uint16_t)record.message.size();
46 std::memcpy(buffer, &message_size, sizeof(uint16_t));
47 buffer += sizeof(uint16_t);
48 std::memcpy(buffer, record.message.data(), record.message.size());
49 buffer += record.message.size();
50
51 // Serialize the logging buffer
52 uint32_t buffer_size = (uint32_t)record.buffer.size();
53 std::memcpy(buffer, &buffer_size, sizeof(uint32_t));
54 buffer += sizeof(uint32_t);
55 std::memcpy(buffer, record.buffer.data(), record.buffer.size());
56 buffer += record.buffer.size();
57
58 // Write the last zero byte
59 *buffer = 0;
60}
61
62} // namespace CppLogging
Binary layout definition.
void LayoutRecord(Record &record) override
Layout the given logging record into a raw buffer.
Logging record.
Definition record.h:37
Level level
Level of the logging record.
Definition record.h:44
std::vector< uint8_t > raw
Record content after layout.
Definition record.h:53
std::string message
Message of the logging record.
Definition record.h:48
uint64_t thread
Thread Id of the logging record.
Definition record.h:42
std::string logger
Logger name of the logging record.
Definition record.h:46
std::vector< uint8_t > buffer
Buffer of the logging record.
Definition record.h:50
uint64_t timestamp
Timestamp of the logging record.
Definition record.h:40
C++ Logging project definitions.
Definition appender.h:15
Level
Logging level.
Definition level.h:18