Internal: Log/Capture: Rework to add an internal LogToBuffer() function which is useful for writing automated tests. Clarified logging state by adding an enum.

This commit is contained in:
omar 2019-02-23 15:24:01 +01:00
parent 3eba840053
commit 2cd7de5666
2 changed files with 63 additions and 23 deletions

View File

@ -3615,7 +3615,7 @@ void ImGui::Shutdown(ImGuiContext* context)
fclose(g.LogFile); fclose(g.LogFile);
g.LogFile = NULL; g.LogFile = NULL;
} }
g.LogClipboard.clear(); g.LogBuffer.clear();
g.Initialized = false; g.Initialized = false;
} }
@ -8786,7 +8786,7 @@ void ImGui::LogText(const char* fmt, ...)
if (g.LogFile) if (g.LogFile)
vfprintf(g.LogFile, fmt, args); vfprintf(g.LogFile, fmt, args);
else else
g.LogClipboard.appendfv(fmt, args); g.LogBuffer.appendfv(fmt, args);
va_end(args); va_end(args);
} }
@ -8830,7 +8830,7 @@ void ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char*
} }
} }
// Start logging ImGui output to TTY // Start logging/capturing text output to TTY
void ImGui::LogToTTY(int max_depth) void ImGui::LogToTTY(int max_depth)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -8841,12 +8841,13 @@ void ImGui::LogToTTY(int max_depth)
IM_ASSERT(g.LogFile == NULL); IM_ASSERT(g.LogFile == NULL);
g.LogFile = stdout; g.LogFile = stdout;
g.LogEnabled = true; g.LogEnabled = true;
g.LogType = ImGuiLogType_TTY;
g.LogStartDepth = window->DC.TreeDepth; g.LogStartDepth = window->DC.TreeDepth;
if (max_depth >= 0) if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth; g.LogAutoExpandMaxDepth = max_depth;
} }
// Start logging ImGui output to given file // Start logging/capturing text output to given file
void ImGui::LogToFile(int max_depth, const char* filename) void ImGui::LogToFile(int max_depth, const char* filename)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -8855,26 +8856,25 @@ void ImGui::LogToFile(int max_depth, const char* filename)
ImGuiWindow* window = g.CurrentWindow; ImGuiWindow* window = g.CurrentWindow;
if (!filename) if (!filename)
{
filename = g.IO.LogFilename; filename = g.IO.LogFilename;
if (!filename) if (!filename || !filename[0])
return; return;
}
IM_ASSERT(g.LogFile == NULL); IM_ASSERT(g.LogFile == NULL);
g.LogFile = ImFileOpen(filename, "ab"); g.LogFile = ImFileOpen(filename, "ab"); // FIXME: Why not logging in text mode? Then we don't need to bother the user with IM_NEWLINE..
if (!g.LogFile) if (!g.LogFile)
{ {
IM_ASSERT(0); IM_ASSERT(0);
return; return;
} }
g.LogEnabled = true; g.LogEnabled = true;
g.LogType = ImGuiLogType_File;
g.LogStartDepth = window->DC.TreeDepth; g.LogStartDepth = window->DC.TreeDepth;
if (max_depth >= 0) if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth; g.LogAutoExpandMaxDepth = max_depth;
} }
// Start logging ImGui output to clipboard // Start logging/capturing text output to clipboard
void ImGui::LogToClipboard(int max_depth) void ImGui::LogToClipboard(int max_depth)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -8883,8 +8883,27 @@ void ImGui::LogToClipboard(int max_depth)
ImGuiWindow* window = g.CurrentWindow; ImGuiWindow* window = g.CurrentWindow;
IM_ASSERT(g.LogFile == NULL); IM_ASSERT(g.LogFile == NULL);
g.LogFile = NULL; IM_ASSERT(g.LogBuffer.empty());
g.LogEnabled = true; g.LogEnabled = true;
g.LogType = ImGuiLogType_Clipboard;
g.LogFile = NULL;
g.LogStartDepth = window->DC.TreeDepth;
if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth;
}
void ImGui::LogToBuffer(int max_depth)
{
ImGuiContext& g = *GImGui;
if (g.LogEnabled)
return;
ImGuiWindow* window = g.CurrentWindow;
IM_ASSERT(g.LogFile == NULL);
IM_ASSERT(g.LogBuffer.empty());
g.LogEnabled = true;
g.LogType = ImGuiLogType_Clipboard;
g.LogFile = NULL;
g.LogStartDepth = window->DC.TreeDepth; g.LogStartDepth = window->DC.TreeDepth;
if (max_depth >= 0) if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth; g.LogAutoExpandMaxDepth = max_depth;
@ -8897,23 +8916,30 @@ void ImGui::LogFinish()
return; return;
LogText(IM_NEWLINE); LogText(IM_NEWLINE);
if (g.LogFile != NULL) switch (g.LogType)
{ {
if (g.LogFile == stdout) case ImGuiLogType_TTY:
fflush(g.LogFile); fflush(g.LogFile);
else break;
fclose(g.LogFile); case ImGuiLogType_File:
g.LogFile = NULL; fclose(g.LogFile);
} break;
if (g.LogClipboard.size() > 1) case ImGuiLogType_Buffer:
{ break;
SetClipboardText(g.LogClipboard.begin()); case ImGuiLogType_Clipboard:
g.LogClipboard.clear(); if (!g.LogBuffer.empty())
SetClipboardText(g.LogBuffer.begin());
break;
} }
g.LogEnabled = false; g.LogEnabled = false;
g.LogType = ImGuiLogType_None;
g.LogFile = NULL;
g.LogBuffer.clear();
} }
// Helper to display logging buttons // Helper to display logging buttons
// FIXME-OBSOLETE: We should probably obsolete this and let the user have their own helper (this is one of the oldest function alive!)
void ImGui::LogButtons() void ImGui::LogButtons()
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;

View File

@ -384,6 +384,15 @@ enum ImGuiLayoutType_
ImGuiLayoutType_Vertical = 1 ImGuiLayoutType_Vertical = 1
}; };
enum ImGuiLogType
{
ImGuiLogType_None = 0,
ImGuiLogType_TTY,
ImGuiLogType_File,
ImGuiLogType_Buffer,
ImGuiLogType_Clipboard
};
// X/Y enums are fixed to 0/1 so they may be used to index ImVec2 // X/Y enums are fixed to 0/1 so they may be used to index ImVec2
enum ImGuiAxis enum ImGuiAxis
{ {
@ -928,8 +937,9 @@ struct ImGuiContext
// Logging // Logging
bool LogEnabled; bool LogEnabled;
ImGuiLogType LogType;
FILE* LogFile; // If != NULL log to stdout/ file FILE* LogFile; // If != NULL log to stdout/ file
ImGuiTextBuffer LogClipboard; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators. ImGuiTextBuffer LogBuffer; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators.
int LogStartDepth; int LogStartDepth;
int LogAutoExpandMaxDepth; int LogAutoExpandMaxDepth;
@ -1041,6 +1051,7 @@ struct ImGuiContext
SettingsDirtyTimer = 0.0f; SettingsDirtyTimer = 0.0f;
LogEnabled = false; LogEnabled = false;
LogType = ImGuiLogType_None;
LogFile = NULL; LogFile = NULL;
LogStartDepth = 0; LogStartDepth = 0;
LogAutoExpandMaxDepth = 2; LogAutoExpandMaxDepth = 2;
@ -1396,6 +1407,9 @@ namespace ImGui
IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled);
IMGUI_API void PopItemFlag(); IMGUI_API void PopItemFlag();
// Logging/Capture
IMGUI_API void LogToBuffer(int max_depth = -1); // Start logging to internal buffer
// Popups, Modals, Tooltips // Popups, Modals, Tooltips
IMGUI_API void OpenPopupEx(ImGuiID id); IMGUI_API void OpenPopupEx(ImGuiID id);
IMGUI_API void ClosePopupToLevel(int remaining, bool apply_focus_to_window_under); IMGUI_API void ClosePopupToLevel(int remaining, bool apply_focus_to_window_under);