Logger updated with static functions for each log level

Platform terminal class implemented - allows changing terminal colors (test on linux)
CoreLogListener implemented to use the terminal colors
core_refactor
Joey Pollack 4 years ago
parent 46083c836c
commit c7928af683

@ -68,8 +68,9 @@ set(LUNARIUM_SRC
"src/utils/helpers.cpp"
"src/utils/highResTimer.cpp"
"src/utils/logger.cpp"
"src/utils/opRes.cpp"
"src/window/window.cpp"
"src/utils/op_res.cpp"
"src/platform/window.cpp"
"src/platform/terminal.cpp"
"src/graphics/opengl/glGraphics.cpp"
"src/graphics/opengl/glText.cpp"
"src/graphics/opengl/glShader.cpp"

@ -8,14 +8,19 @@ Build System:
Core:
☐ Research EnTT for the ECS (https://github.com/skypjack/entt/)
Add Terminal subsystem to allow for printing colored text in a cross-platform way
Create a LogListener that uses the colored text (replace the current stdout listener)
Add Terminal subsystem to allow for printing colored text in a cross-platform way @done(22-05-16 18:05)
Create a LogListener that uses the colored text (replace the current stdout listener) @done(22-05-16 18:23)
☐ Replace XML with JSON (https://github.com/nlohmann/json) @high
☐ Move internal libs back into the core and refactor @high
✔ Utils @done(22-05-13 17:29)
☐ assets
☐ gui
☐ Replace the File Browser (imgui) class with the NFD library (https://github.com/btzy/nativefiledialog-extended) @high
☐ For scripting switch to Wren instead of LUA (https://github.com/wren-lang/wren) @high
☐ Remove SOL
☐ Add log settings to the state file
✔ Refactor log system to use separate log level methods instead of passing log level @done(22-05-12 16:46)
✔ Add log level options to config script @done(22-05-12 16:46)
✔ Add run modes (Editor, Game, Test) to state file @done (9/15/2021, 7:27:03 PM)
✔ Add run mode interface class @done (9/15/2021, 8:22:35 PM)
✔ Read the window size and position on shutdown and write these to the state file @done (2/8/2022, 4:39:37 PM)
@ -38,6 +43,31 @@ Core:
✔ Add Roboto-Regular.ttf as an internal font @high @done (11/3/2021, 8:35:51 PM)
✔ Allow an image size to be passed in for rendering to an image @high @done (2/3/2022, 4:07:33 PM)
Scripting:
☐ Replace LUA with Wren for scripting
Script Managment class:
✘ Manage LUA states @cancelled(22-05-13 17:31)
☐ Initialize new scripts
☐ Run given script with given state
☐ Add any generated errors to the Script object
Interface Class (Core API):
☐ Provide Methods that give access to the C++ code
ECS:
☐ Research using ECS with a quadtree (regions will essentially be out quadtree)
☐ Use 1 Entt registry with a component that stores the region ID (Index?)
☐ Entity class - Store a reference to the registry?
Components:
☐ Transform (store decomposed matrix)
☐ Sprite Renderer
☐ Scripts
Physics:
☐ Rigid Body (Box2D)
☐ Box Collider (Box2D)
World (Lunariums version of a "Scene"):
GUI:
✔ Dear ImGui class with basic initialization @done (9/10/2021, 1:42:19 PM)
@ -69,15 +99,7 @@ Core:
☐ Add OpenAL to the project
☐ Design Audio API
Scripting:
Script Managment class:
☐ Manage LUA states
☐ Initialize new scripts
☐ Run given script with given state
☐ Add any generated errors to the Script object
Interface Class:
☐ Provide Methods that give access to the C++ code
Utils:
✔ Make Logger fully static (no need to ever GetInstance) @done (10/26/2021, 4:43:55 PM)

@ -39,4 +39,14 @@ static_assert(sizeof(i64) == 8, "Expected i64 to be 8 bytes");
static_assert(sizeof(f32) == 4, "Expected f32 to be 4 bytes");
static_assert(sizeof(f64) == 8, "Expected f64 to be 8 bytes");
// Platform detection
#if defined(_WIN32) || defined(_WIN64)
#define LPLATFORM_WINDOWS
#elif defined(__linux__) || defined(__gnu_linux__)
#define LPLATFORM_LINUX
#else
#error "Unsupported platform. Must build on windows or linux"
#endif
#endif // LUNARIUM_COMMON_DEFS_H_

@ -14,12 +14,14 @@
#include <gui/dearimgui/imgui.h>
#include <LunariumConfig.h>
#include <platform/terminal.h>
// Run modes
#include <run_modes/testbed/testbed.h>
#include <run_modes/editor/editor.h>
// Sub Systems
#include <window/window.h>
#include <platform/window.h>
#include <input/inputManager.h>
#include <graphics/opengl/glGraphics.h>
#include <gui/gui.h>
@ -433,4 +435,36 @@ namespace lunarium
delete mPanels[id];
mPanels[id] = nullptr;
}
CoreLogListener::CoreLogListener(uint32_t acceptedLogLevels, uint32_t acceptedLogCategories, const char* myName)
: LogListener(acceptedLogLevels, acceptedLogCategories, myName)
{
}
bool CoreLogListener::Log(LogMessage& message)
{
if (!LevelIsSet(message.LogLevel) ||
!CategoryIsSet(message.LogCategory))
return false;
std::cout << std::endl;
switch (message.LogLevel)
{
case LogLevel::GRAPHICS_INTERNAL_ERROR:
case LogLevel::FATAL_ERROR:
case LogLevel::ERROR:
std::cout << Terminal::Color(TermColor::TC_RED); break;
case LogLevel::WARNING: std::cout << Terminal::Color(TermColor::TC_YELLOW); break;
case LogLevel::INFO: std::cout << Terminal::Color(TermColor::TC_GREEN); break;
case LogLevel::DEBUG: std::cout << Terminal::Color(TermColor::TC_BLUE); break;
case LogLevel::TRACE: std::cout << Terminal::Color(TermColor::TC_CYAN); break;
case LogLevel::GRAPHICS_INTERNAL_DEBUG: break;
}
std::cout << Logger::TimeStamp() << Logger::GetCategoryName(message.LogCategory)
<< Logger::GetLevelName(message.LogLevel) << message.Message << Terminal::Color(TermColor::TC_DEFAULT) << std::flush;
return true;
}
}

@ -98,6 +98,15 @@ namespace lunarium
TestBed* mpTester;
};
class CoreLogListener : public LogListener
{
public:
CoreLogListener(u32 acceptedLogLevels = LogLevel::ANY,
u32 acceptedLogCategories = LogCategory::ANY, const char* myName = "CoreLogListener");
bool Log(LogMessage& message);
};
}
#endif // CORE_H_

@ -9,7 +9,7 @@
#ifndef RUN_MODE_H_
#define RUN_MODE_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <input/inputManager.h>
#include <input/keyboard.h>

@ -10,7 +10,7 @@
#define STATE_H_
#include <string>
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium
{

@ -14,7 +14,7 @@
#include <string>
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium
{

@ -11,7 +11,7 @@
#define IGRAPHICS_H_
#include <core/types.h>
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium
{

@ -8,7 +8,7 @@
#include "glGraphics.h"
#include "defaultShaders.h"
#include <window/window.h>
#include <platform/window.h>
#include <assets/types/image.h>
#include "../openFontData.h"
#include <utils/logger.h>

@ -15,7 +15,7 @@
#include <string>
#include <glad/gl.h>
#include "glShader.h"
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <core/types.h>
#include <glm/glm.hpp>

@ -8,7 +8,7 @@
*
******************************************************************************/
#include <window/window.h>
#include <platform/window.h>
#include "inputManager.h"
#include <gui/gui.h>
#include <utils/logger.h>

@ -14,7 +14,7 @@
#include <map>
#include <string>
#include <window/keyCodes.h>
#include <platform/keyCodes.h>
namespace lunarium
{

@ -13,7 +13,7 @@
#include <string>
#include <map>
#include <assets/types/asset.h>
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <core/types.h>
namespace lunarium

@ -14,7 +14,7 @@
#ifndef FILE_SELECT_H_
#define FILE_SELECT_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <vector>
#include <filesystem>

@ -9,7 +9,7 @@
#ifndef GUI_H_
#define GUI_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
struct GLFWwindow;
struct ImGuiStyle;

@ -9,7 +9,7 @@
#ifndef LOG_GUI_H_
#define LOG_GUI_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <vector>
#include <string>
#include <utils/logger.h>

@ -9,7 +9,7 @@
#ifndef LUA_CONSOLE_H_
#define LUA_CONSOLE_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <string>
#include <vector>

@ -39,7 +39,7 @@ int main(int argc, char** argv)
#endif
// All log messages will go to stdout
lunarium::Logger::GetInstance()->AddListener(new lunarium::StandardListener(
lunarium::Logger::GetInstance()->AddListener(new lunarium::CoreLogListener(
ll, lunarium::LogCategory::ANY));
lunarium::Core& core = lunarium::Core::GetInstance();

@ -0,0 +1,134 @@
/******************************************************************************
* File - terminal.cpp
* Author - Joey Pollack
* Date - 2022/05/13 (y/m/d)
* Mod Date - 2022/05/13 (y/m/d)
* Description - API for the cross-platform terminal
******************************************************************************/
#include "terminal.h"
#include <iostream>
#if defined(LPLATFORM_WINDOWS)
#define WIN32_LEAN_AN_MEAN
#include <windows.h>
#elif defined(LPLATFORM_LINUX)
#endif
namespace lunarium
{
u32 Terminal::WinPrevOutMode;
u32 Terminal::WinPrevInMode;
OpRes Terminal::Initialize()
{
#if defined(LPLATFORM_WINDOWS)
return InitWindowsTerm();
#elif defined(LPLATFORM_LINUX)
// No init needed for linux
#endif
}
void Terminal::Shutdown()
{
// Reset colors
std::cout << Color(TermColor::TC_DEFAULT);
#if defined(LPLATFORM_WINDOWS)
ShutdownWinTerm();
#elif defined(LPLATFORM_LINUX)
// No shutdown needed for linux
#endif
}
const char* Terminal::Color(TermColor color)
{
switch (color)
{
case TermColor::TC_WHITE: return "\033[37m";
case TermColor::TC_BLACK: return "\033[30m";
case TermColor::TC_BLUE: return "\033[34m";
case TermColor::TC_CYAN: return "\033[36m";
case TermColor::TC_GREEN: return "\033[32m";
case TermColor::TC_MAGENTA: return "\033[35m";
case TermColor::TC_RED: return "\033[31m";
case TermColor::TC_YELLOW: return "\033[33m";
case TermColor::TC_DEFAULT: return "\033[0m";
}
return "";
}
void Terminal::SetDefaultColor()
{
std::cout << "\033[0m";
}
OpRes Terminal::InitWindowsTerm()
{
// This code is from microsoft docs:
// https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
// Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE)
{
return OpRes::Fail("Unable to get std out handle");
}
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
if (hIn == INVALID_HANDLE_VALUE)
{
return OpRes::Fail("Unable to get std in handle");
}
DWORD dwOriginalOutMode = 0;
DWORD dwOriginalInMode = 0;
if (!GetConsoleMode(hOut, &dwOriginalOutMode))
{
return OpRes::Fail("Unable to get current out console mode");
}
if (!GetConsoleMode(hIn, &dwOriginalInMode))
{
return OpRes::Fail("Unable to get current in console mode");
}
DWORD dwRequestedOutModes = ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
DWORD dwRequestedInModes = ENABLE_VIRTUAL_TERMINAL_INPUT;
GetConsoleMode(hOut, (LPDWORD)&WinPrevOutMode);
GetConsoleMode(hIn, (LPDWORD)&WinPrevInMode);
DWORD dwOutMode = dwOriginalOutMode | dwRequestedOutModes;
if (!SetConsoleMode(hOut, dwOutMode))
{
// we failed to set both modes, try to step down mode gracefully.
dwRequestedOutModes = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
dwOutMode = dwOriginalOutMode | dwRequestedOutModes;
if (!SetConsoleMode(hOut, dwOutMode))
{
// Failed to set any VT mode, can't do anything here.
return OpRes::Fail("Unable to set console mode");
}
}
DWORD dwInMode = dwOriginalInMode | ENABLE_VIRTUAL_TERMINAL_INPUT;
if (!SetConsoleMode(hIn, dwInMode))
{
// Failed to set VT input mode, can't do anything here.
return OpRes::Fail("Unable to set console mode");
}
return OpRes::OK();
}
void Terminal::ShutdownWinTerm()
{
// Assume we'll get valid handles since init worked
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
// Reset console mode
SetConsoleMode(hOut, WinPrevOutMode);
SetConsoleMode(hIn, WinPrevInMode);
}
}

@ -0,0 +1,48 @@
/******************************************************************************
* File - terminal.h
* Author - Joey Pollack
* Date - 2022/05/13 (y/m/d)
* Mod Date - 2022/05/13 (y/m/d)
* Description - API for the cross-platform terminal
******************************************************************************/
#ifndef LUNARIUM_TERMINAL_H_
#define LUNARIUM_TERMINAL_H_
#include <core/common_defs.h>
#include <utils/op_res.h>
namespace lunarium
{
enum TermColor
{
TC_WHITE,
TC_BLACK,
TC_RED,
TC_YELLOW,
TC_GREEN,
TC_BLUE,
TC_CYAN,
TC_MAGENTA,
TC_DEFAULT
};
class Terminal
{
public:
static OpRes Initialize();
static void Shutdown();
static const char* Color(TermColor color);
static TermColor GetColor();
static void SetDefaultColor();
private:
static OpRes InitWindowsTerm();
static void ShutdownWinTerm();
static u32 WinPrevOutMode;
static u32 WinPrevInMode;
};
}
#endif // LUNARIUM_TERMINAL_H_

@ -15,7 +15,7 @@
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <core/state.h>
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium
{

@ -14,7 +14,7 @@
#include "definitions.h"
#include <filesystem>
#include <string>
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <map>
#include <vector>

@ -13,7 +13,7 @@
#include "content_manager.h"
#include <filesystem>
#include <string>
#include <utils\opRes.h>
#include <utils\op_res.h>
namespace lunarium { namespace editor
{

@ -11,7 +11,7 @@
#include "definitions.h"
#include <core/types.h>
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <map>
namespace lunarium { class IGraphics; }

@ -10,7 +10,7 @@
#define EDITOR_H_
#include <core/run_mode.h>
#include <utils/opRes.h>
#include <utils/op_res.h>
#include "project.h"
#include "panel_manager.h"

@ -9,7 +9,7 @@
#ifndef PANEL_MANAGER_H_
#define PANEL_MANAGER_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <gui/dearimgui/imgui.h>
#include <gui/panel.h>
#include <map>

@ -9,7 +9,7 @@
#ifndef PROJECT_H_
#define PROJECT_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
#include <string>
#include <vector>

@ -12,7 +12,7 @@
#include <vector>
#include <map>
#include <string>
#include <utils/opRes.h>
#include <utils/op_res.h>
#include "../../panel_manager.h"
#include <gui/file_browser.h>
#include <editor/contents/definitions.h>

@ -15,7 +15,7 @@
#include <vector>
#include <map>
#include <core/types.h>
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium
{

@ -11,7 +11,7 @@
#ifndef CORE_API_H_
#define CORE_API_H_
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium

@ -12,7 +12,7 @@
#define SCRIPT_MANAGER_H_
#include <sol/sol.hpp>
#include <utils/opRes.h>
#include <utils/op_res.h>
namespace lunarium
{

@ -7,7 +7,7 @@
#include <imgui_impl_opengl3.h>
#include <pugixml.hpp>
#include "utils/logger.h"
#include "utils/opRes.h"
#include "utils/op_res.h"
#include "core/state.h"
#include "core/version.h"
#include "utils/args.h"

@ -8,7 +8,7 @@
******************************************************************************/
#include "logger.h"
#include "opRes.h"
#include "op_res.h"
#include <stdarg.h>
Loading…
Cancel
Save