Added basic debug log window
parent
84cb646512
commit
b0142f3348
@ -0,0 +1,116 @@
|
||||
/******************************************************************************
|
||||
* File - logGui.cpp
|
||||
* Author - Joey Pollack
|
||||
* Date - 2021/09/09 (y/m/d)
|
||||
* Mod Date - 2021/09/10 (y/m/d)
|
||||
* Description - A debug log window
|
||||
******************************************************************************/
|
||||
|
||||
#include "logGui.h"
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
// GUI LOG LISTENER
|
||||
////////////////////////////////////////////////////////////
|
||||
GuiListener::GuiListener(LogGui* pGui, uint32_t acceptedLogLevels, uint32_t acceptedLogCategories, const char* myName)
|
||||
: LogListener(acceptedLogLevels, acceptedLogCategories, myName), mpGui(pGui)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool GuiListener::Log(LogMessage& message)
|
||||
{
|
||||
if (!LevelIsSet(message.LogLevel) ||
|
||||
!CategoryIsSet(message.LogCategory))
|
||||
return false;
|
||||
|
||||
std::ostringstream oss;
|
||||
|
||||
oss << std::endl << Logger::TimeStamp() << Logger::GetCategoryName(message.LogCategory)
|
||||
<< Logger::GetLevelName(message.LogLevel) << message.Message << std::flush;
|
||||
|
||||
mpGui->mMsgHistory.push_back(oss.str());
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// GUI LOGGER
|
||||
////////////////////////////////////////////////////////////
|
||||
LogGui* LogGui::mpInstance = nullptr;
|
||||
LogGui::LogGui()
|
||||
: mbShow(true), mListener(this)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
LogGui& LogGui::GetInstance()
|
||||
{
|
||||
if (!mpInstance)
|
||||
{
|
||||
mpInstance = new LogGui;
|
||||
}
|
||||
|
||||
return *mpInstance;
|
||||
}
|
||||
|
||||
void LogGui::FreeInstance()
|
||||
{
|
||||
Logger::GetInstance()->RemoveListener(&mpInstance->mListener);
|
||||
|
||||
delete mpInstance;
|
||||
mpInstance = nullptr;
|
||||
}
|
||||
|
||||
OpRes LogGui::Initialize()
|
||||
{
|
||||
Logger::GetInstance()->AddListener(&mListener);
|
||||
return OpRes::OK();
|
||||
}
|
||||
|
||||
void LogGui::Show()
|
||||
{
|
||||
if (!mbShow)
|
||||
return;
|
||||
|
||||
ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
|
||||
if (!ImGui::Begin("Debug Log", &mbShow))
|
||||
{
|
||||
ImGui::End();
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
|
||||
for (int i = 0; i < mMsgHistory.size(); i++)
|
||||
{
|
||||
const char* msg = mMsgHistory[i].c_str();
|
||||
int len = strlen(msg);
|
||||
ImGui::TextUnformatted(msg);
|
||||
}
|
||||
|
||||
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
|
||||
ImGui::SetScrollHereY(1.0f);
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void LogGui::Clear()
|
||||
{
|
||||
mMsgHistory.clear();
|
||||
}
|
||||
|
||||
void LogGui::SetShow(bool show)
|
||||
{
|
||||
mbShow = show;
|
||||
}
|
||||
|
||||
bool LogGui::IsShown() const
|
||||
{
|
||||
return mbShow;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
/******************************************************************************
|
||||
* File - logGui.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2021/09/09 (y/m/d)
|
||||
* Mod Date - 2021/09/10 (y/m/d)
|
||||
* Description - A debug log window
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef LOG_GUI_H_
|
||||
#define LOG_GUI_H_
|
||||
|
||||
#include <utils/opRes.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <utils/logger.h>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
class LogGui;
|
||||
class GuiListener : public LogListener
|
||||
{
|
||||
public:
|
||||
GuiListener(LogGui* pGui, uint32_t acceptedLogLevels = LogLevel::ANY, uint32_t acceptedLogCategories = LogLevel::ANY, const char* myName = "Gui Listener");
|
||||
virtual bool Log(LogMessage& message);
|
||||
|
||||
private:
|
||||
LogGui* mpGui;
|
||||
};
|
||||
|
||||
class LogGui
|
||||
{
|
||||
public:
|
||||
static LogGui& GetInstance();
|
||||
static void FreeInstance();
|
||||
|
||||
OpRes Initialize();
|
||||
void Show();
|
||||
void Clear();
|
||||
|
||||
void SetShow(bool show);
|
||||
bool IsShown() const;
|
||||
|
||||
private:
|
||||
static LogGui* mpInstance;
|
||||
bool mbShow;
|
||||
std::vector<std::string> mMsgHistory;
|
||||
friend GuiListener;
|
||||
GuiListener mListener;
|
||||
|
||||
private:
|
||||
LogGui();
|
||||
LogGui(const LogGui&) = delete;
|
||||
const LogGui& operator=(const LogGui&) = delete;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LOG_GUI_H_
|
||||
Loading…
Reference in New Issue