Rise
The Vieneo Province
Loading...
Searching...
No Matches
CrashBreadcrumbs.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdio>
5
6// Fixed storage: recording render progress must not allocate or write every frame.
8{
9public:
10 static constexpr std::size_t Capacity = 64;
11 static constexpr std::size_t EntrySize = 80;
12
13 void Add(const char* message)
14 {
15 std::snprintf(entries[next], EntrySize, "%s", message ? message : "");
16 next = (next + 1) % Capacity;
17 if (count < Capacity)
18 ++count;
19 }
20
21 std::size_t Count() const { return count; }
22
23 const char* Recent(std::size_t index) const
24 {
25 return index < count ? entries[(next + Capacity - 1 - index) % Capacity] : "";
26 }
27
28 // The existing network packet has only 256 bytes. Keep its newest five entries,
29 // bounded individually, while the local fatal log retains the complete history.
30 template<std::size_t Size>
31 void FormatPacket(char(&destination)[Size]) const
32 {
33 static_assert(Size > 0, "A breadcrumb packet needs a terminator");
34 destination[0] = '\0';
35 std::size_t used = 0;
36 for (std::size_t i = 0; i < count && i < 5 && used + 1 < Size; ++i)
37 {
38 const int written = std::snprintf(destination + used, Size - used, "%.49s\n", Recent(i));
39 if (written < 0 || static_cast<std::size_t>(written) >= Size - used)
40 break;
41 used += static_cast<std::size_t>(written);
42 }
43 }
44
45private:
47 std::size_t next = 0;
48 std::size_t count = 0;
49};
std::size_t Count() const
void Add(const char *message)
void FormatPacket(char(&destination)[Size]) const
static constexpr std::size_t EntrySize
static constexpr std::size_t Capacity
char entries[Capacity][EntrySize]
const char * Recent(std::size_t index) const