diff options
author | Emma Miler <emma.pi@protonmail.com> | 2022-11-12 15:39:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-12 14:39:39 +0000 |
commit | bf09852285941e20a04731443d03c693e3f8ba2d (patch) | |
tree | a254973af94d819ab18b969e23045a309cbd15f7 /NorthstarDLL/logging.h | |
parent | ab5db7fde23ef6cd87fa8a7e07973179a80e02d4 (diff) | |
download | NorthstarLauncher-bf09852285941e20a04731443d03c693e3f8ba2d.tar.gz NorthstarLauncher-bf09852285941e20a04731443d03c693e3f8ba2d.zip |
Logging improvements + Colours (new) (#320)
* log colours :)
* commend the freopen stuff (it causes problems)
* formatting
* add explanatory comment
* gaming maybe
* move set pattern
* ok this works now i think
* trailing space :)
* Revert "Merge branch 'main' into log-colours"
This reverts commit d40b6496b7bd3b3f20f51cc2311b0369aeabe735, reversing
changes made to a98bcc22e8961b325806f6a9ca85e64353fd574c.
* Revert "Revert "Merge branch 'main' into log-colours""
This reverts commit da792245b1cd7d0ac9e22d5633770142a43ce10c.
* fix shit maybe
* fix shit but better
* initial, still WIP
* Add GetContextName_Short
* colours for in game console
* working, time to improve code
* rename var + add a colour or two
* whoops
* change rpak filesystem to RP NATIVE
* add RP NATIVE and NORTHSTAR tags
* clang-format off because formatting
* formatting 2
* formatting 3: the formattening
* bruh
* revert unnecessary change
* improve comment
* Set console title to show profile
* pain
* Made code not shit
* Formatting and remove warning box
* Update main.cpp
* Formatting
* More format changes
* Again
* Fix small things from code review
* Remove empty line
* renamed NATIVE_EN -> NATIVE_ENGINE
* Update logging.cpp
* Update sourceconsole.cpp
Co-authored-by: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com>
Diffstat (limited to 'NorthstarDLL/logging.h')
-rw-r--r-- | NorthstarDLL/logging.h | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/NorthstarDLL/logging.h b/NorthstarDLL/logging.h index 83fd0c12..a75dce91 100644 --- a/NorthstarDLL/logging.h +++ b/NorthstarDLL/logging.h @@ -1,4 +1,131 @@ #pragma once +#include "pch.h" +#include "spdlog/sinks/base_sink.h" +#include "spdlog/logger.h" +#include "squirrel.h" +#include "color.h" void CreateLogFiles(); void InitialiseLogging(); +void InitialiseConsole(); + +class ColoredLogger; + +struct custom_log_msg : spdlog::details::log_msg +{ + public: + custom_log_msg(ColoredLogger* origin, spdlog::details::log_msg msg) : origin(origin), spdlog::details::log_msg(msg) {} + + ColoredLogger* origin; +}; + +class CustomSink : public spdlog::sinks::base_sink<std::mutex> +{ + public: + void custom_log(const custom_log_msg& msg); + virtual void custom_sink_it_(const custom_log_msg& msg) + { + throw std::runtime_error("Pure virtual call to CustomSink::custom_sink_it_"); + } +}; + +class ColoredLogger : public spdlog::logger +{ + public: + std::string ANSIColor; + SourceColor SRCColor; + + std::vector<std::shared_ptr<CustomSink>> custom_sinks_; + + ColoredLogger(std::string name, Color color, bool first = false) : spdlog::logger(*spdlog::default_logger()) + { + name_ = std::move(name); + if (!first) + { + custom_sinks_ = dynamic_pointer_cast<ColoredLogger>(spdlog::default_logger())->custom_sinks_; + } + + ANSIColor = color.ToANSIColor(); + SRCColor = color.ToSourceColor(); + } + + void sink_it_(const spdlog::details::log_msg& msg) + { + custom_log_msg custom_msg {this, msg}; + + // Ugh + for (auto& sink : sinks_) + { + SPDLOG_TRY + { + sink->log(custom_msg); + } + SPDLOG_LOGGER_CATCH() + } + + for (auto& sink : custom_sinks_) + { + SPDLOG_TRY + { + sink->custom_log(custom_msg); + } + SPDLOG_LOGGER_CATCH() + } + + if (should_flush_(custom_msg)) + { + flush_(); + } + } +}; + +namespace NS::log +{ + // Squirrel + extern std::shared_ptr<ColoredLogger> SCRIPT_UI; + extern std::shared_ptr<ColoredLogger> SCRIPT_CL; + extern std::shared_ptr<ColoredLogger> SCRIPT_SV; + + // Native code + extern std::shared_ptr<ColoredLogger> NATIVE_UI; + extern std::shared_ptr<ColoredLogger> NATIVE_CL; + extern std::shared_ptr<ColoredLogger> NATIVE_SV; + extern std::shared_ptr<ColoredLogger> NATIVE_EN; + + // File system + extern std::shared_ptr<ColoredLogger> fs; + // RPak + extern std::shared_ptr<ColoredLogger> rpak; + // Echo + extern std::shared_ptr<ColoredLogger> echo; + + extern std::shared_ptr<ColoredLogger> NORTHSTAR; +}; // namespace NS::log + +void RegisterCustomSink(std::shared_ptr<CustomSink> sink); + +inline bool g_bSpdLog_UseAnsiColor = true; + +// Could maybe use some different names here, idk +static const char* level_names[] {"trac", "dbug", "info", "warn", "errr", "crit", "off"}; + +// spdlog logger, for cool colour things +class ExternalConsoleSink : public CustomSink +{ + private: + std::map<spdlog::level::level_enum, std::string> m_LogColours = { + {spdlog::level::trace, NS::Colors::TRACE.ToANSIColor()}, + {spdlog::level::debug, NS::Colors::DEBUG.ToANSIColor()}, + {spdlog::level::info, NS::Colors::INFO.ToANSIColor()}, + {spdlog::level::warn, NS::Colors::WARN.ToANSIColor()}, + {spdlog::level::err, NS::Colors::ERR.ToANSIColor()}, + {spdlog::level::critical, NS::Colors::CRIT.ToANSIColor()}, + {spdlog::level::off, NS::Colors::OFF.ToANSIColor()}}; + + std::string default_color = "\033[39;49m"; + + protected: + void sink_it_(const spdlog::details::log_msg& msg) override; + void custom_sink_it_(const custom_log_msg& msg); + void flush_() override; +}; |