VR-Engage  2.3
Loading...
Searching...
No Matches
debugLogPrinter.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2026 MAK Technologies, Inc.
3 * All rights reserved.
4 ******************************************************************************/
5
6#pragma once
7
8//! \file debugLogPrinter.h
9//! \ingroup vreDebugManager
10//! \brief ImGui-based log viewer that captures DtOutputStream output.
11//!
12//! Provides DtImGuiLogPrinter, a DtPrinter subclass that stores log lines
13//! in a thread-safe ring buffer and renders them as a filterable, color-coded
14//! ImGui panel. Register the draw callback on a debug-menu tab to display
15//! the log inside the VR-Engage debug overlay.
16
18
19#include <vlutil/vlPrint.h>
20#include <vrvImgui/imgui.h>
21
22#include <mutex>
23#include <string>
24#include <vector>
25
26namespace makVre
27{
28
29//! \brief DtPrinter subclass that captures log output for ImGui display.
30//!
31//! All log text is stored in a single contiguous ImGuiTextBuffer with a
32//! parallel offset table that records where each line starts, its length,
33//! and its severity level. This design uses one heap allocation for all
34//! text (the ImGuiTextBuffer grows geometrically), avoids per-line
35//! std::string overhead, and makes oldest-line eviction O(1) by advancing
36//! a head index rather than shifting memory.
37//!
38//! Attach an instance to one or more DtOutputStream objects (DtFatal, DtWarn,
39//! DtInfo, DtVerbose, DtDebug). Every write() call appends text to the
40//! buffer. Call drawContents() from an ImGui rendering pass to display
41//! the captured log with text search, severity filtering, auto-scroll,
42//! and color-coded lines.
43//!
44//! Thread safety: write() may be called from any thread. draw methods must
45//! be called from the ImGui (main) thread only.
46class DEBUGMANAGER_DLL DtImGuiLogPrinter : public DtPrinter
47{
48public:
49 //! \brief Constructs the log printer.
50 //! \param maxLines Maximum number of lines to retain before discarding the oldest.
51 explicit DtImGuiLogPrinter(int maxLines = 10000);
52
53 //! \brief Virtual destructor.
55
56 // Non-copyable, non-movable.
59
60 //! \brief DtPrinter override — called by every DtOutputStream this printer
61 //! is connected to.
62 //! \param str The text to log.
63 //! \param notify_level Severity (DtNlFatal … DtNlDebug).
64 //! \param newLine True when str begins a new logical line.
65 virtual void write(const std::string& str, int notify_level, bool newLine) override;
66
67 //! \brief Renders the log contents inside an existing ImGui window or tab.
68 //!
69 //! Draws the toolbar (clear, auto-scroll, severity filters, copy-mode,
70 //! detach button), search bar, and the scrollable log region.
72
73 //! \brief Renders the log in its own detached ImGui window.
74 //!
75 //! Call this from a draw path that is independent of the debug menu
76 //! visibility so the detached window stays open even when the main
77 //! debug menu is hidden. If the user closes the window, the log
78 //! automatically re-attaches to the inline view.
80
81 //! \brief Discards all stored log lines.
82 void clear();
83
84 //! \brief Connects this printer to all five global DtOutputStream instances.
86
87 //! \brief Disconnects this printer from all five global DtOutputStream instances.
89
90 //! \brief Returns true when the printer is attached to the output streams.
91 bool isConnected() const;
92
93 //! \brief Returns true when the log is displayed in its own window.
94 bool isDetached() const;
95
96 //! \brief Sets whether the log is displayed in its own window.
97 void setDetached(bool detached);
98
99private:
100 //! Lightweight descriptor for one line inside the contiguous text buffer.
101 //! 12 bytes per entry with no heap allocation.
102 struct LineInfo
103 {
104 int offset; //!< Byte offset into myBuf where this line starts.
105 int length; //!< Length of the line in bytes (excluding null terminator).
106 int level; //!< Notify level (DtNlFatal … DtNlDebug).
107 };
108
109 //! Builds the toolbar row (Clear, Auto-scroll, severity filters, Copy Mode, Detach).
111
112 //! Builds the scrollable log region using the current snapshot.
114
115 //! Takes a thread-safe snapshot of the line metadata and text into
116 //! mySnapshot / mySnapshotText.
118
119 //! Commits the pending partial line into the contiguous buffer.
121
122 //! Compacts the buffer when the oldest lines have been evicted,
123 //! reclaiming dead space at the front.
125
126 //! Guards all mutable state accessed by write() and draw().
127 std::mutex myMutex;
128
129 //! Single contiguous text buffer holding all log line text.
130 //! Lines are null-terminated within the buffer so that c_str()-style
131 //! pointers can be handed directly to ImGui::TextUnformatted().
132 ImGuiTextBuffer myBuf;
133
134 //! Offset table describing each line stored in myBuf.
135 //! Used as a circular array: valid lines are indices [myHead, myHead + lineCount).
136 std::vector<LineInfo> myLines;
137
138 //! Index of the oldest valid line in myLines (circular).
140
141 //! Total number of valid lines currently stored.
143
144 //! Partial line being accumulated before the next newLine==true write.
145 std::string myPendingLine;
146
147 //! Notify level for the pending partial line.
149
150 //! Maximum retained line count.
152
153 //! ImGui text filter for search.
154 ImGuiTextFilter myFilter;
155
156 //! Per-level visibility toggles (indexed by DtNotifyLevelType).
158
159 //! Whether to auto-scroll to the bottom on new output.
161
162 //! Whether the log is shown in its own detached ImGui window.
164
165 //! Whether copy-mode is active (read-only InputTextMultiline for selection).
167
168 //! Whether we are currently attached to the output streams.
170
171 //! Frame-local snapshot of LineInfo, built by takeSnapshot().
172 std::vector<LineInfo> mySnapshot;
173
174 //! Frame-local snapshot of the text buffer, built by takeSnapshot().
175 std::string mySnapshotText;
176
177 //! Cached flattened text for copy-mode InputTextMultiline.
178 std::string myCopyText;
179
180 //! True when myCopyText needs rebuilding (filter/severity/line count changed).
182};
183
184} // namespace makVre
ImGuiTextBuffer myBuf
Single contiguous text buffer holding all log line text. Lines are null-terminated within the buffer ...
Definition debugLogPrinter.h:132
void disconnectFromAllStreams()
Disconnects this printer from all five global DtOutputStream instances.
bool myCopyMode
Whether copy-mode is active (read-only InputTextMultiline for selection).
Definition debugLogPrinter.h:166
DtImGuiLogPrinter(const DtImGuiLogPrinter &)=delete
virtual void write(const std::string &str, int notify_level, bool newLine) override
DtPrinter override — called by every DtOutputStream this printer is connected to.
void drawContents()
Renders the log contents inside an existing ImGui window or tab.
bool myLevelVisible[5]
Per-level visibility toggles (indexed by DtNotifyLevelType).
Definition debugLogPrinter.h:157
bool isDetached() const
Returns true when the log is displayed in its own window.
int myMaxLines
Maximum retained line count.
Definition debugLogPrinter.h:151
std::mutex myMutex
Guards all mutable state accessed by write() and draw().
Definition debugLogPrinter.h:127
int myPendingLevel
Notify level for the pending partial line.
Definition debugLogPrinter.h:148
DtImGuiLogPrinter(int maxLines=10000)
Constructs the log printer.
bool myConnected
Whether we are currently attached to the output streams.
Definition debugLogPrinter.h:169
void connectToAllStreams()
Connects this printer to all five global DtOutputStream instances.
bool myDetached
Whether the log is shown in its own detached ImGui window.
Definition debugLogPrinter.h:163
std::string mySnapshotText
Frame-local snapshot of the text buffer, built by takeSnapshot().
Definition debugLogPrinter.h:175
std::string myCopyText
Cached flattened text for copy-mode InputTextMultiline.
Definition debugLogPrinter.h:178
int myHead
Index of the oldest valid line in myLines (circular).
Definition debugLogPrinter.h:139
int myLineCount
Total number of valid lines currently stored.
Definition debugLogPrinter.h:142
void drawLogRegion()
Builds the scrollable log region using the current snapshot.
bool myAutoScroll
Whether to auto-scroll to the bottom on new output.
Definition debugLogPrinter.h:160
DtImGuiLogPrinter & operator=(const DtImGuiLogPrinter &)=delete
void drawDetachedWindow()
Renders the log in its own detached ImGui window.
void takeSnapshot()
Takes a thread-safe snapshot of the line metadata and text into mySnapshot / mySnapshotText.
virtual ~DtImGuiLogPrinter()
Virtual destructor.
void setDetached(bool detached)
Sets whether the log is displayed in its own window.
std::string myPendingLine
Partial line being accumulated before the next newLine==true write.
Definition debugLogPrinter.h:145
std::vector< LineInfo > myLines
Offset table describing each line stored in myBuf. Used as a circular array: valid lines are indices ...
Definition debugLogPrinter.h:136
void drawToolbar()
Builds the toolbar row (Clear, Auto-scroll, severity filters, Copy Mode, Detach).
void clear()
Discards all stored log lines.
ImGuiTextFilter myFilter
ImGui text filter for search.
Definition debugLogPrinter.h:154
void commitPendingLine()
Commits the pending partial line into the contiguous buffer.
bool myCopyTextDirty
True when myCopyText needs rebuilding (filter/severity/line count changed).
Definition debugLogPrinter.h:181
bool isConnected() const
Returns true when the printer is attached to the output streams.
std::vector< LineInfo > mySnapshot
Frame-local snapshot of LineInfo, built by takeSnapshot().
Definition debugLogPrinter.h:172
void compactIfNeeded()
Compacts the buffer when the oldest lines have been evicted, reclaiming dead space at the front.
Defines export macros for the VREngage Debug Manager library.
#define DEBUGMANAGER_DLL
Export/import macro for non-Windows platforms.
Definition export.h:40
!
Definition glsVreMessageUtil.h:50
Lightweight descriptor for one line inside the contiguous text buffer. 12 bytes per entry with no hea...
Definition debugLogPrinter.h:103
int level
Notify level (DtNlFatal … DtNlDebug).
Definition debugLogPrinter.h:106
int offset
Byte offset into myBuf where this line starts.
Definition debugLogPrinter.h:104
int length
Length of the line in bytes (excluding null terminator).
Definition debugLogPrinter.h:105