qxLib
file_logger_stream_mapping.inl
Go to the documentation of this file.
1 /**
2 
3  @file file_logger_stream_mapping.inl
4  @author Khrapov
5  @date 15.01.2026
6  @copyright (c) Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
14  const config& streamConfig,
15  unit<size_t, units::data> initialMapSize) noexcept
16  : base_file_logger_stream(streamConfig)
17 {
18  const std::filesystem::path path = prepare_folder_and_get_log_file_path(streamConfig);
19 
20 #if QX_WIN
21  SYSTEM_INFO si {};
22  GetSystemInfo(&si);
23  m_nGranularity = si.dwAllocationGranularity ? static_cast<size_t>(si.dwAllocationGranularity) : size_t { 65536 };
24 
25  m_hFile = CreateFileW(
26  path.c_str(),
27  GENERIC_READ | GENERIC_WRITE,
28  FILE_SHARE_READ,
29  nullptr,
30  streamConfig.eLogFilePolicy == log_file_policy::clear_then_upend ? CREATE_ALWAYS : OPEN_ALWAYS,
31  FILE_ATTRIBUTE_NORMAL,
32  nullptr);
33 
34  if (m_hFile == INVALID_HANDLE_VALUE)
35  return;
36 
37  LARGE_INTEGER sz {};
38  if (!GetFileSizeEx(m_hFile, &sz))
39  return;
40 
41  m_nSize = static_cast<size_t>(sz.QuadPart);
42 #else
43  const long ps = ::sysconf(_SC_PAGESIZE);
44  m_nGranularity = ps > 0 ? static_cast<size_t>(ps) : 4096ull;
45 
46  int flags = O_RDWR | O_CREAT;
47  if (streamConfig.eLogFilePolicy == log_file_policy::clear_then_upend)
48  flags |= O_TRUNC;
49 
50  m_Fd = ::open(path.c_str(), flags, 0644);
51  if (m_Fd < 0)
52  return;
53 
54  struct stat st {};
55  if (::fstat(m_Fd, &st) != 0)
56  return;
57 
58  m_nSize = static_cast<size_t>(st.st_size);
59 #endif
60 
61  const size_t nDefaultStartSize = initialMapSize ? convert(initialMapSize).to(units::data::bytes) : m_nGranularity;
62  size_t nInitialSize = m_nSize < nDefaultStartSize ? nDefaultStartSize : m_nSize;
63  nInitialSize = align_up_u64(nInitialSize, m_nGranularity);
64 
65  remap_to_capacity(nInitialSize);
66 }
67 
69  : base_file_logger_stream(std::move(other))
70 {
71 #if QX_WIN
72  std::swap(m_hFile, other.m_hFile);
73  std::swap(m_hMap, other.m_hMap);
74 #else
75  std::swap(m_Fd, other.m_Fd);
76 #endif
77  std::swap(m_pData, other.m_pData);
78  std::swap(m_nSize, other.m_nSize);
79  std::swap(m_nCapacity, other.m_nCapacity);
80  std::swap(m_nGranularity, other.m_nGranularity);
81 }
82 
83 inline file_logger_stream_mapping::~file_logger_stream_mapping() noexcept
84 {
85 #if QX_WIN
86  if (m_hFile == INVALID_HANDLE_VALUE)
87  return;
88 #else
89  if (m_Fd < 0)
90  return;
91 #endif
92 
94 
95  if (m_pData)
96  {
97 #if QX_WIN
98  // No extended unmapping flags are needed.
99  UnmapViewOfFile(m_pData); //-V2001
100 #else
101  ::munmap(m_pData, static_cast<size_t>(m_nCapacity));
102 #endif
103  m_pData = nullptr;
104  }
105 
106 #if QX_WIN
107  if (m_hMap)
108  {
109  CloseHandle(m_hMap);
110  m_hMap = nullptr;
111  }
112 #endif
113 
114 #if QX_WIN
115  LARGE_INTEGER li {};
116  li.QuadPart = static_cast<LONGLONG>(m_nSize);
117 
118  if (!SetFilePointerEx(m_hFile, li, nullptr, FILE_BEGIN))
119  return;
120 
121  if (!SetEndOfFile(m_hFile))
122  return;
123 
124  if (!FlushFileBuffers(m_hFile))
125  return;
126 
127  CloseHandle(m_hFile);
128  m_hFile = INVALID_HANDLE_VALUE;
129 #else
130  ::ftruncate(m_Fd, static_cast<off_t>(m_nSize));
131  ::fsync(m_Fd);
132  ::close(m_Fd);
133  m_Fd = -1;
134 #endif
135 
136  m_nCapacity = 0;
137  m_nSize = 0;
138 }
139 
141  const category& category,
142  verbosity eVerbosity,
143  std::thread::id threadId,
144  std::chrono::system_clock::time_point messageTime,
145  string_view svFile,
146  string_view svFunction,
147  int nLine,
148  string_view svMessage)
149 {
150  const cstring_view svUtf8 = to_utf8(svMessage);
151  if (!ensure_capacity(svUtf8.size()))
152  return;
153 
154  std::memcpy(m_pData + m_nSize, svUtf8.data(), svUtf8.size());
155  m_nSize += svUtf8.size();
156 }
157 
159 {
160  if (!m_pData || m_nSize == 0)
161  return;
162 
163  // flush memory mapped data to the file
164 #if QX_WIN
165  if (!FlushViewOfFile(m_pData, m_nSize))
166  return;
167 #else
168  ::msync(m_pData, static_cast<size_t>(m_nSize), MS_SYNC);
169 #endif
170 
171 #if QX_WIN
172  if (m_hFile != INVALID_HANDLE_VALUE)
173 #else
174  if (m_Fd >= 0)
175 #endif
176  {
177  // update the file on disc
178 #if QX_WIN
179  FlushFileBuffers(m_hFile);
180 #else
181  ::fsync(m_Fd);
182 #endif
183 
184  // notify the OS that file changed (makes editors such as VS Code to reload it)
185 #if QX_WIN
186  FILETIME ft;
187  GetSystemTimeAsFileTime(&ft);
188  SetFileTime(m_hFile, nullptr, nullptr, &ft);
189 #else
190  struct timespec ts[2];
191  clock_gettime(CLOCK_REALTIME, &ts[0]);
192  ts[1] = ts[0];
193 
194  futimens(m_Fd, ts);
195 #endif
196  }
197 }
198 
199 inline bool file_logger_stream_mapping::remap_to_capacity(size_t nNewCapacity) noexcept
200 {
201  if (m_pData)
202  {
203 #if QX_WIN
204  // No extended unmapping flags are needed.
205  UnmapViewOfFile(m_pData); //-V2001
206 #else
207  ::munmap(m_pData, static_cast<size_t>(m_nCapacity));
208 #endif
209  m_pData = nullptr;
210  }
211 
212 #if QX_WIN
213  if (m_hMap)
214  {
215  CloseHandle(m_hMap);
216  m_hMap = nullptr;
217  }
218 #endif
219 
220 #if QX_WIN
221  LARGE_INTEGER li {};
222  li.QuadPart = static_cast<LONGLONG>(nNewCapacity);
223  if (!SetFilePointerEx(m_hFile, li, nullptr, FILE_BEGIN))
224  return false;
225  if (!SetEndOfFile(m_hFile))
226  return false;
227 #else
228  if (::ftruncate(m_Fd, static_cast<off_t>(nNewCapacity)) != 0)
229  return false;
230 #endif
231 
232 #if QX_WIN
233  const u64 nMappingSize = static_cast<u64>(nNewCapacity);
234  const DWORD hi = static_cast<DWORD>(nMappingSize >> std::numeric_limits<DWORD>::digits);
235  const DWORD lo = static_cast<DWORD>(nMappingSize);
236 
237  m_hMap = CreateFileMappingW(m_hFile, nullptr, PAGE_READWRITE, hi, lo, nullptr);
238  if (!m_hMap)
239  return false;
240 
241  // Let the OS choose the address; MapViewOfFileEx with nullptr is equivalent.
242  void* p = MapViewOfFile(m_hMap, FILE_MAP_ALL_ACCESS, 0, 0, nNewCapacity); //-V2001
243  if (!p)
244  return false;
245 #else
246  void* p = ::mmap(nullptr, static_cast<size_t>(nNewCapacity), PROT_READ | PROT_WRITE, MAP_SHARED, m_Fd, 0);
247  if (p == MAP_FAILED)
248  return false;
249 #endif
250 
251  m_pData = static_cast<std::byte*>(p);
252  m_nCapacity = nNewCapacity;
253 
254  return true;
255 }
256 
257 inline bool file_logger_stream_mapping::ensure_capacity(size_t nAdditionalBytes) noexcept
258 {
259  if (m_nSize + nAdditionalBytes <= m_nCapacity)
260  return true;
261 
262  // grow: double until enough (with alignment)
263  size_t nRequiredSize = m_nSize + nAdditionalBytes;
264  size_t nNewCapacity = m_nCapacity ? m_nCapacity : m_nGranularity;
265  while (nNewCapacity < nRequiredSize)
266  nNewCapacity *= 2;
267 
268  nNewCapacity = align_up_u64(nNewCapacity, m_nGranularity);
269  return remap_to_capacity(nNewCapacity);
270 }
271 
272 inline size_t file_logger_stream_mapping::align_up_u64(size_t nValue, size_t nAlignment) noexcept
273 {
274  if (nAlignment == 0)
275  return nValue;
276 
277  const size_t nResult = nValue % nAlignment;
278  return nResult ? (nValue + (nAlignment - nResult)) : nValue;
279 }
280 
281 } // namespace qx
Base class for all file logger streams.
static cstring_view to_utf8(string_view svMessage)
Encode a message as UTF-8 for a byte output.
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
A conversion class.
Definition: base.h:77
High-performance file logger based on memory-mapped I/O.
virtual void do_log(const category &category, verbosity eVerbosity, std::thread::id threadId, std::chrono::system_clock::time_point messageTime, string_view svFile, string_view svFunction, int nLine, string_view svMessage) override
Proceed stream logging.
virtual void do_flush() override
Flush the stream.
file_logger_stream_mapping(const config &streamConfig=config(), unit< size_t, units::data > initialMapSize={ 1, units::data::mebibytes }) noexcept
file_logger_stream_mapping object constructor
Wrapper for enumerations to be used as a list of flags.
Definition: flags.h:64
Definition: base.h:52
std::uint64_t u64
Definition: typedefs.h:26