CppLogging  1.0.4.0
C++ Logging Library
hash_layout.cpp
Go to the documentation of this file.
1 
10 
11 #include <cstring>
12 #include <vector>
13 
14 namespace CppLogging {
15 
16 uint32_t HashLayout::Hash(std::string_view message)
17 {
18  const uint32_t FNV_PRIME = 16777619u;
19  const uint32_t OFFSET_BASIS = 2166136261u;
20 
21  uint32_t hash = OFFSET_BASIS;
22  for (size_t i = 0; i < message.size(); ++i)
23  {
24  hash ^= message[i];
25  hash *= FNV_PRIME;
26  }
27  return hash;
28 }
29 
31 {
32  // Calculate logging record size
33  uint32_t size = (uint32_t)(sizeof(uint64_t) + sizeof(uint64_t) + sizeof(Level) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t) + record.buffer.size());
34 
35  // Resize the raw buffer to the required size
36  record.raw.resize(sizeof(uint32_t) + size + 1);
37 
38  // Get the raw buffer start position
39  uint8_t* buffer = record.raw.data();
40 
41  // Serialize the logging record
42  std::memcpy(buffer, &size, sizeof(uint32_t));
43  buffer += sizeof(uint32_t);
44  std::memcpy(buffer, &record.timestamp, sizeof(uint64_t));
45  buffer += sizeof(uint64_t);
46  std::memcpy(buffer, &record.thread, sizeof(uint64_t));
47  buffer += sizeof(uint64_t);
48  std::memcpy(buffer, &record.level, sizeof(Level));
49  buffer += sizeof(Level);
50 
51  // Serialize the logger name hash
52  uint32_t logger_hash = Hash(record.logger);
53  std::memcpy(buffer, &logger_hash, sizeof(uint32_t));
54  buffer += sizeof(uint32_t);
55 
56  // Serialize the logging message hash
57  uint32_t message_hash = Hash(record.message);
58  std::memcpy(buffer, &message_hash, sizeof(uint32_t));
59  buffer += sizeof(uint32_t);
60 
61  // Serialize the logging buffer
62  uint32_t buffer_size = (uint32_t)record.buffer.size();
63  std::memcpy(buffer, &buffer_size, sizeof(uint32_t));
64  buffer += sizeof(uint32_t);
65  std::memcpy(buffer, record.buffer.data(), record.buffer.size());
66  buffer += record.buffer.size();
67 
68  // Write the last zero byte
69  *buffer = 0;
70 }
71 
72 } // namespace CppLogging
static uint32_t Hash(std::string_view message)
Hash the given string message using FNV-1a hashing algorithm.
Definition: hash_layout.cpp:16
void LayoutRecord(Record &record) override
Layout the given logging record into a raw buffer.
Definition: hash_layout.cpp:30
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
Hash layout definition.
C++ Logging project definitions.
Definition: appender.h:15
Level
Logging level.
Definition: level.h:18