Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 31bfd73

Browse files
add log output
1 parent bf755d2 commit 31bfd73

6 files changed

Lines changed: 142 additions & 116 deletions

File tree

OFS-lib/OFS_FileLogging.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "OFS_FileLogging.h"
22
#include "OFS_Util.h"
33
#include "OFS_Profiling.h"
4+
#include "OFS_ImGui.h"
45

56
#include "SDL_log.h"
67
#include "SDL_rwops.h"
@@ -10,6 +11,7 @@
1011
#include "stb_sprintf.h"
1112
#include <vector>
1213

14+
static OFS::AppLog OFS_MainLog;
1315

1416
SDL_RWops* OFS_FileLogger::LogFileHandle = nullptr;
1517

@@ -81,14 +83,32 @@ void OFS_FileLogger::Shutdown() noexcept
8183
SDL_RWclose(LogFileHandle);
8284
}
8385

86+
void OFS_FileLogger::DrawLogWindow(bool* open) noexcept
87+
{
88+
if(!*open) return;
89+
OFS_MainLog.Draw("OFS Log Output", open);
90+
}
91+
8492
inline static void LogToConsole(OFS_LogLevel level, const char* msg) noexcept
8593
{
8694
OFS_PROFILE(__FUNCTION__);
8795
switch(level) {
88-
case OFS_LogLevel::OFS_LOG_INFO: SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, msg); break;
89-
case OFS_LogLevel::OFS_LOG_WARN: SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, msg); break;
90-
case OFS_LogLevel::OFS_LOG_DEBUG: SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, msg); break;
91-
case OFS_LogLevel::OFS_LOG_ERROR: SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, msg); break;
96+
case OFS_LogLevel::OFS_LOG_INFO:
97+
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, msg);
98+
OFS_MainLog.AddLog("[INFO]: %s\n", msg);
99+
break;
100+
case OFS_LogLevel::OFS_LOG_WARN:
101+
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, msg);
102+
OFS_MainLog.AddLog("[WARN]: %s\n", msg);
103+
break;
104+
case OFS_LogLevel::OFS_LOG_DEBUG:
105+
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, msg);
106+
OFS_MainLog.AddLog("[DEBUG]: %s\n", msg);
107+
break;
108+
case OFS_LogLevel::OFS_LOG_ERROR:
109+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, msg);
110+
OFS_MainLog.AddLog("[ERROR]: %s\n", msg);
111+
break;
92112
}
93113
}
94114

OFS-lib/OFS_FileLogging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OFS_FileLogger
2020
static void Shutdown() noexcept;
2121

2222
static void Flush() noexcept;
23+
static void DrawLogWindow(bool* open) noexcept;
2324

2425
static void LogToFileR(const char* prefix, const char* msg, bool newLine = true) noexcept;
2526
static void LogToFileR(OFS_LogLevel level, const char* msg, uint32_t size = 0, bool newLine = true) noexcept;

OFS-lib/UI/OFS_ImGui.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,117 @@
33
#include "imgui.h"
44

55
namespace OFS {
6+
// ExampleAppLog taken from "imgui_demo.cpp"
7+
struct AppLog
8+
{
9+
ImGuiTextBuffer Buf;
10+
ImGuiTextFilter Filter;
11+
ImVector<int> LineOffsets; // Index to lines offset. We maintain this with AddLog() calls.
12+
bool AutoScroll; // Keep scrolling if already at the bottom.
13+
14+
AppLog() noexcept
15+
{
16+
AutoScroll = true;
17+
Clear();
18+
}
19+
20+
void Clear() noexcept
21+
{
22+
Buf.clear();
23+
LineOffsets.clear();
24+
LineOffsets.push_back(0);
25+
}
26+
27+
inline int LogSizeBytes() const noexcept
28+
{
29+
return Buf.Buf.size_in_bytes() + LineOffsets.size_in_bytes();
30+
}
31+
32+
inline int AllocatedSizeBytes() const noexcept
33+
{
34+
return Buf.Buf.Capacity + LineOffsets.Capacity*sizeof(int);
35+
}
36+
37+
void AddLog(const char* fmt, ...) noexcept IM_FMTARGS(2)
38+
{
39+
int old_size = Buf.size();
40+
va_list args;
41+
va_start(args, fmt);
42+
Buf.appendfv(fmt, args);
43+
va_end(args);
44+
for (int new_size = Buf.size(); old_size < new_size; old_size++)
45+
if (Buf[old_size] == '\n')
46+
LineOffsets.push_back(old_size + 1);
47+
}
48+
49+
void Draw(const char* title, bool* p_open = NULL) noexcept
50+
{
51+
if (!ImGui::Begin(title, p_open)) {
52+
ImGui::End();
53+
return;
54+
}
55+
56+
// Options menu
57+
if (ImGui::BeginPopup("Options")) {
58+
ImGui::Checkbox("Auto-scroll", &AutoScroll);
59+
ImGui::EndPopup();
60+
}
61+
62+
// Main window
63+
if (ImGui::Button("Options"))
64+
ImGui::OpenPopup("Options");
65+
ImGui::SameLine();
66+
bool clear = ImGui::Button("Clear");
67+
ImGui::SameLine();
68+
bool copy = ImGui::Button("Copy");
69+
ImGui::SameLine();
70+
Filter.Draw("Filter", -100.0f);
71+
72+
ImGui::Text("Used: %.2f MB", LogSizeBytes() / (1024.f * 1024.f));
73+
ImGui::SameLine();
74+
ImGui::Text("Allocated: %.2f MB", AllocatedSizeBytes() / (1024.f * 1024.f));
75+
ImGui::Separator();
76+
ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
77+
78+
if (clear)
79+
Clear();
80+
if (copy)
81+
ImGui::LogToClipboard();
82+
83+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
84+
const char* buf = Buf.begin();
85+
const char* buf_end = Buf.end();
86+
if (Filter.IsActive()) {
87+
for (int line_no = 0; line_no < LineOffsets.Size; line_no++) {
88+
const char* line_start = buf + LineOffsets[line_no];
89+
const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end;
90+
if (Filter.PassFilter(line_start, line_end))
91+
ImGui::TextUnformatted(line_start, line_end);
92+
}
93+
}
94+
else {
95+
ImGuiListClipper clipper;
96+
clipper.Begin(LineOffsets.Size);
97+
while (clipper.Step()) {
98+
for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) {
99+
const char* line_start = buf + LineOffsets[line_no];
100+
const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end;
101+
ImGui::TextUnformatted(line_start, line_end);
102+
}
103+
}
104+
clipper.End();
105+
}
106+
ImGui::PopStyleVar();
107+
108+
if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
109+
ImGui::SetScrollHereY(1.0f);
110+
111+
ImGui::EndChild();
112+
ImGui::End();
113+
}
114+
};
115+
116+
6117
// same as ImGui::Image except it has an id
7118
void ImageWithId(ImGuiID id, ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& tint_col = ImVec4(1, 1, 1, 1), const ImVec4& border_col = ImVec4(0, 0, 0, 0)) noexcept;
8119
bool GamepadContextMenu() noexcept;

src/OpenFunscripter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,8 @@ void OpenFunscripter::step() noexcept {
16851685
extensions->ShowExtensions();
16861686
tcode->DrawWindow(&settings->data().show_tcode, player->getCurrentPositionSecondsInterp());
16871687

1688+
OFS_FileLogger::DrawLogWindow(&settings->data().show_debug_log);
1689+
16881690
if (keybinds.ShowBindingWindow()) {
16891691
keybinds.save();
16901692
}
@@ -2771,6 +2773,7 @@ void OpenFunscripter::ShowMainMenuBar() noexcept
27712773
ImGui::Separator();
27722774
if (ImGui::BeginMenu("Debug")) {
27732775
if (ImGui::MenuItem("Metrics", NULL, &DebugMetrics)) {}
2776+
if (ImGui::MenuItem("Log output", NULL, &settings->data().show_debug_log)) {}
27742777
#ifndef NDEBUG
27752778
if (ImGui::MenuItem("ImGui Demo", NULL, &DebugDemo)) {}
27762779
#endif

src/UI/OpenFunscripterSettings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class OpenFunscripterSettings
5353
bool force_hw_decoding = false;
5454
bool mirror_mode = false;
5555
bool show_tcode = false;
56+
bool show_debug_log = false;
5657

5758
int32_t vsync = 0;
5859
int32_t framerateLimit = 150;
@@ -112,6 +113,7 @@ class OpenFunscripterSettings
112113
OFS_REFLECT(font_override, ar);
113114
OFS_REFLECT(show_tcode, ar);
114115
OFS_REFLECT(defaultMetadata, ar);
116+
OFS_REFLECT(show_debug_log, ar);
115117
OFS_REFLECT_NAMED("SplineMode", BaseOverlay::SplineMode, ar);
116118
OFS_REFLECT_NAMED("SyncLineEnable", BaseOverlay::SyncLineEnable, ar);
117119
}

src/lua/OFS_LuaExtensions.cpp

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -36,123 +36,13 @@
3636
// return timestamp;
3737
//}
3838

39-
// ExampleAppLog taken from "imgui_demo.cpp"
40-
struct ExtensionLog
41-
{
42-
ImGuiTextBuffer Buf;
43-
ImGuiTextFilter Filter;
44-
ImVector<int> LineOffsets; // Index to lines offset. We maintain this with AddLog() calls.
45-
bool AutoScroll; // Keep scrolling if already at the bottom.
46-
47-
ExtensionLog() noexcept
48-
{
49-
AutoScroll = true;
50-
Clear();
51-
}
52-
53-
void Clear() noexcept
54-
{
55-
Buf.clear();
56-
LineOffsets.clear();
57-
LineOffsets.push_back(0);
58-
}
59-
60-
inline int LogSizeBytes() const noexcept
61-
{
62-
return Buf.Buf.size_in_bytes() + LineOffsets.size_in_bytes();
63-
}
64-
65-
inline int AllocatedSizeBytes() const noexcept
66-
{
67-
return Buf.Buf.Capacity + LineOffsets.Capacity*sizeof(int);
68-
}
69-
70-
void AddLog(const char* fmt, ...) noexcept IM_FMTARGS(2)
71-
{
72-
int old_size = Buf.size();
73-
va_list args;
74-
va_start(args, fmt);
75-
Buf.appendfv(fmt, args);
76-
va_end(args);
77-
for (int new_size = Buf.size(); old_size < new_size; old_size++)
78-
if (Buf[old_size] == '\n')
79-
LineOffsets.push_back(old_size + 1);
80-
}
81-
82-
void Draw(const char* title, bool* p_open = NULL) noexcept
83-
{
84-
if (!ImGui::Begin(title, p_open)) {
85-
ImGui::End();
86-
return;
87-
}
88-
89-
// Options menu
90-
if (ImGui::BeginPopup("Options")) {
91-
ImGui::Checkbox("Auto-scroll", &AutoScroll);
92-
ImGui::EndPopup();
93-
}
94-
95-
// Main window
96-
if (ImGui::Button("Options"))
97-
ImGui::OpenPopup("Options");
98-
ImGui::SameLine();
99-
bool clear = ImGui::Button("Clear");
100-
ImGui::SameLine();
101-
bool copy = ImGui::Button("Copy");
102-
ImGui::SameLine();
103-
Filter.Draw("Filter", -100.0f);
104-
105-
ImGui::Text("Used: %.2f MB", LogSizeBytes() / (1024.f * 1024.f));
106-
ImGui::SameLine();
107-
ImGui::Text("Allocated: %.2f MB", AllocatedSizeBytes() / (1024.f * 1024.f));
108-
ImGui::Separator();
109-
ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
110-
111-
if (clear)
112-
Clear();
113-
if (copy)
114-
ImGui::LogToClipboard();
115-
116-
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
117-
const char* buf = Buf.begin();
118-
const char* buf_end = Buf.end();
119-
if (Filter.IsActive()) {
120-
for (int line_no = 0; line_no < LineOffsets.Size; line_no++) {
121-
const char* line_start = buf + LineOffsets[line_no];
122-
const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end;
123-
if (Filter.PassFilter(line_start, line_end))
124-
ImGui::TextUnformatted(line_start, line_end);
125-
}
126-
}
127-
else {
128-
ImGuiListClipper clipper;
129-
clipper.Begin(LineOffsets.Size);
130-
while (clipper.Step()) {
131-
for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) {
132-
const char* line_start = buf + LineOffsets[line_no];
133-
const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end;
134-
ImGui::TextUnformatted(line_start, line_end);
135-
}
136-
}
137-
clipper.End();
138-
}
139-
ImGui::PopStyleVar();
140-
141-
if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
142-
ImGui::SetScrollHereY(1.0f);
143-
144-
ImGui::EndChild();
145-
ImGui::End();
146-
}
147-
};
148-
14939
constexpr const char* LuaDefaultFunctions = R"(
15040
function clamp(val, min, max)
15141
return math.min(max, math.max(val, min))
15242
end
15343
)";
15444

155-
static ExtensionLog ExtensionLogBuffer;
45+
static OFS::AppLog ExtensionLogBuffer;
15646

15747
SDL_threadID OFS_LuaExtensions::MainThread = SDL_ThreadID();
15848
bool OFS_LuaExtensions::DevMode = false;
@@ -197,7 +87,6 @@ static int LuaPrint(lua_State* L) noexcept
19787
}
19888
}
19989

200-
LOG_INFO(logMsg.c_str());
20190
logMsg.append(1, '\n');
20291
ExtensionLogBuffer.AddLog(logMsg.c_str());
20392
return 0;

0 commit comments

Comments
 (0)