You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lunarium_OLD/src/graphics/gui/logGui.cpp

116 lines
2.9 KiB
C++

/******************************************************************************
* 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;
}
}