Merge pull request #6199 from lioncash/log-ns

common/log: Move Log namespace into the Common namespace
This commit is contained in:
bunnei 2021-04-14 21:29:44 -07:00 committed by GitHub
commit 60511976bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 58 additions and 45 deletions

View file

@ -25,7 +25,7 @@
#include "common/string_util.h" #include "common/string_util.h"
#include "common/threadsafe_queue.h" #include "common/threadsafe_queue.h"
namespace Log { namespace Common::Log {
/** /**
* Static state as a singleton. * Static state as a singleton.
@ -37,8 +37,11 @@ public:
return backend; return backend;
} }
Impl(Impl const&) = delete; Impl(const Impl&) = delete;
const Impl& operator=(Impl const&) = delete; Impl& operator=(const Impl&) = delete;
Impl(Impl&&) = delete;
Impl& operator=(Impl&&) = delete;
void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num, void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num,
const char* function, std::string message) { const char* function, std::string message) {
@ -132,7 +135,7 @@ private:
std::mutex writing_mutex; std::mutex writing_mutex;
std::thread backend_thread; std::thread backend_thread;
std::vector<std::unique_ptr<Backend>> backends; std::vector<std::unique_ptr<Backend>> backends;
Common::MPSCQueue<Log::Entry> message_queue; MPSCQueue<Entry> message_queue;
Filter filter; Filter filter;
std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()}; std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
}; };
@ -146,16 +149,16 @@ void ColorConsoleBackend::Write(const Entry& entry) {
} }
FileBackend::FileBackend(const std::string& filename) : bytes_written(0) { FileBackend::FileBackend(const std::string& filename) : bytes_written(0) {
if (Common::FS::Exists(filename + ".old.txt")) { if (FS::Exists(filename + ".old.txt")) {
Common::FS::Delete(filename + ".old.txt"); FS::Delete(filename + ".old.txt");
} }
if (Common::FS::Exists(filename)) { if (FS::Exists(filename)) {
Common::FS::Rename(filename, filename + ".old.txt"); FS::Rename(filename, filename + ".old.txt");
} }
// _SH_DENYWR allows read only access to the file for other programs. // _SH_DENYWR allows read only access to the file for other programs.
// It is #defined to 0 on other platforms // It is #defined to 0 on other platforms
file = Common::FS::IOFile(filename, "w", _SH_DENYWR); file = FS::IOFile(filename, "w", _SH_DENYWR);
} }
void FileBackend::Write(const Entry& entry) { void FileBackend::Write(const Entry& entry) {
@ -182,7 +185,7 @@ void FileBackend::Write(const Entry& entry) {
void DebuggerBackend::Write(const Entry& entry) { void DebuggerBackend::Write(const Entry& entry) {
#ifdef _WIN32 #ifdef _WIN32
::OutputDebugStringW(Common::UTF8ToUTF16W(FormatLogMessage(entry).append(1, '\n')).c_str()); ::OutputDebugStringW(UTF8ToUTF16W(FormatLogMessage(entry).append(1, '\n')).c_str());
#endif #endif
} }
@ -342,4 +345,4 @@ void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
instance.PushEntry(log_class, log_level, filename, line_num, function, instance.PushEntry(log_class, log_level, filename, line_num, function,
fmt::vformat(format, args)); fmt::vformat(format, args));
} }
} // namespace Log } // namespace Common::Log

View file

@ -11,7 +11,7 @@
#include "common/logging/filter.h" #include "common/logging/filter.h"
#include "common/logging/log.h" #include "common/logging/log.h"
namespace Log { namespace Common::Log {
class Filter; class Filter;
@ -135,4 +135,4 @@ const char* GetLevelName(Level log_level);
* never get the message * never get the message
*/ */
void SetGlobalFilter(const Filter& filter); void SetGlobalFilter(const Filter& filter);
} // namespace Log } // namespace Common::Log

View file

@ -7,7 +7,7 @@
#include "common/logging/filter.h" #include "common/logging/filter.h"
#include "common/string_util.h" #include "common/string_util.h"
namespace Log { namespace Common::Log {
namespace { namespace {
template <typename It> template <typename It>
Level GetLevelByName(const It begin, const It end) { Level GetLevelByName(const It begin, const It end) {
@ -103,4 +103,4 @@ bool Filter::IsDebug() const {
}); });
} }
} // namespace Log } // namespace Common::Log

View file

@ -9,7 +9,7 @@
#include <string_view> #include <string_view>
#include "common/logging/log.h" #include "common/logging/log.h"
namespace Log { namespace Common::Log {
/** /**
* Implements a log message filter which allows different log classes to have different minimum * Implements a log message filter which allows different log classes to have different minimum
@ -51,4 +51,4 @@ public:
private: private:
std::array<Level, static_cast<std::size_t>(Class::Count)> class_levels; std::array<Level, static_cast<std::size_t>(Class::Count)> class_levels;
}; };
} // namespace Log } // namespace Common::Log

View file

@ -7,7 +7,7 @@
#include <fmt/format.h> #include <fmt/format.h>
#include "common/common_types.h" #include "common/common_types.h"
namespace Log { namespace Common::Log {
// trims up to and including the last of ../, ..\, src/, src\ in a string // trims up to and including the last of ../, ..\, src/, src\ in a string
constexpr const char* TrimSourcePath(std::string_view source) { constexpr const char* TrimSourcePath(std::string_view source) {
@ -148,28 +148,34 @@ void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsig
fmt::make_format_args(args...)); fmt::make_format_args(args...));
} }
} // namespace Log } // namespace Common::Log
#ifdef _DEBUG #ifdef _DEBUG
#define LOG_TRACE(log_class, ...) \ #define LOG_TRACE(log_class, ...) \
::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Trace, \ Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Trace, \
::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__) Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
__VA_ARGS__)
#else #else
#define LOG_TRACE(log_class, fmt, ...) (void(0)) #define LOG_TRACE(log_class, fmt, ...) (void(0))
#endif #endif
#define LOG_DEBUG(log_class, ...) \ #define LOG_DEBUG(log_class, ...) \
::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Debug, \ Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Debug, \
::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__) Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
__VA_ARGS__)
#define LOG_INFO(log_class, ...) \ #define LOG_INFO(log_class, ...) \
::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Info, \ Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Info, \
::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__) Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
__VA_ARGS__)
#define LOG_WARNING(log_class, ...) \ #define LOG_WARNING(log_class, ...) \
::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Warning, \ Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Warning, \
::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__) Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
__VA_ARGS__)
#define LOG_ERROR(log_class, ...) \ #define LOG_ERROR(log_class, ...) \
::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Error, \ Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Error, \
::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__) Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
__VA_ARGS__)
#define LOG_CRITICAL(log_class, ...) \ #define LOG_CRITICAL(log_class, ...) \
::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Critical, \ Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Critical, \
::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__) Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
__VA_ARGS__)

View file

@ -16,7 +16,7 @@
#include "common/logging/text_formatter.h" #include "common/logging/text_formatter.h"
#include "common/string_util.h" #include "common/string_util.h"
namespace Log { namespace Common::Log {
std::string FormatLogMessage(const Entry& entry) { std::string FormatLogMessage(const Entry& entry) {
unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000); unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000);
@ -108,4 +108,4 @@ void PrintColoredMessage(const Entry& entry) {
#undef ESC #undef ESC
#endif #endif
} }
} // namespace Log } // namespace Common::Log

View file

@ -7,7 +7,7 @@
#include <cstddef> #include <cstddef>
#include <string> #include <string>
namespace Log { namespace Common::Log {
struct Entry; struct Entry;
@ -17,4 +17,4 @@ std::string FormatLogMessage(const Entry& entry);
void PrintMessage(const Entry& entry); void PrintMessage(const Entry& entry);
/// Prints the same message as `PrintMessage`, but colored according to the severity level. /// Prints the same message as `PrintMessage`, but colored according to the severity level.
void PrintColoredMessage(const Entry& entry); void PrintColoredMessage(const Entry& entry);
} // namespace Log } // namespace Common::Log

View file

@ -55,9 +55,9 @@ void ConfigureDebug::ApplyConfiguration() {
Settings::values.disable_macro_jit = ui->disable_macro_jit->isChecked(); Settings::values.disable_macro_jit = ui->disable_macro_jit->isChecked();
Settings::values.extended_logging = ui->extended_logging->isChecked(); Settings::values.extended_logging = ui->extended_logging->isChecked();
Debugger::ToggleConsole(); Debugger::ToggleConsole();
Log::Filter filter; Common::Log::Filter filter;
filter.ParseFilterString(Settings::values.log_filter); filter.ParseFilterString(Settings::values.log_filter);
Log::SetGlobalFilter(filter); Common::Log::SetGlobalFilter(filter);
} }
void ConfigureDebug::changeEvent(QEvent* event) { void ConfigureDebug::changeEvent(QEvent* event) {

View file

@ -29,13 +29,13 @@ void ToggleConsole() {
freopen_s(&temp, "CONIN$", "r", stdin); freopen_s(&temp, "CONIN$", "r", stdin);
freopen_s(&temp, "CONOUT$", "w", stdout); freopen_s(&temp, "CONOUT$", "w", stdout);
freopen_s(&temp, "CONOUT$", "w", stderr); freopen_s(&temp, "CONOUT$", "w", stderr);
Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>()); Common::Log::AddBackend(std::make_unique<Common::Log::ColorConsoleBackend>());
} }
} else { } else {
if (FreeConsole()) { if (FreeConsole()) {
// In order to close the console, we have to also detach the streams on it. // In order to close the console, we have to also detach the streams on it.
// Just redirect them to NUL if there is no console window // Just redirect them to NUL if there is no console window
Log::RemoveBackend(Log::ColorConsoleBackend::Name()); Common::Log::RemoveBackend(Common::Log::ColorConsoleBackend::Name());
freopen_s(&temp, "NUL", "r", stdin); freopen_s(&temp, "NUL", "r", stdin);
freopen_s(&temp, "NUL", "w", stdout); freopen_s(&temp, "NUL", "w", stdout);
freopen_s(&temp, "NUL", "w", stderr); freopen_s(&temp, "NUL", "w", stderr);
@ -43,9 +43,9 @@ void ToggleConsole() {
} }
#else #else
if (UISettings::values.show_console) { if (UISettings::values.show_console) {
Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>()); Common::Log::AddBackend(std::make_unique<Common::Log::ColorConsoleBackend>());
} else { } else {
Log::RemoveBackend(Log::ColorConsoleBackend::Name()); Common::Log::RemoveBackend(Common::Log::ColorConsoleBackend::Name());
} }
#endif #endif
} }

View file

@ -171,12 +171,14 @@ void GMainWindow::ShowTelemetryCallout() {
const int GMainWindow::max_recent_files_item; const int GMainWindow::max_recent_files_item;
static void InitializeLogging() { static void InitializeLogging() {
using namespace Common;
Log::Filter log_filter; Log::Filter log_filter;
log_filter.ParseFilterString(Settings::values.log_filter); log_filter.ParseFilterString(Settings::values.log_filter);
Log::SetGlobalFilter(log_filter); Log::SetGlobalFilter(log_filter);
const std::string& log_dir = Common::FS::GetUserPath(Common::FS::UserPath::LogDir); const std::string& log_dir = FS::GetUserPath(FS::UserPath::LogDir);
Common::FS::CreateFullPath(log_dir); FS::CreateFullPath(log_dir);
Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE)); Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
#ifdef _WIN32 #ifdef _WIN32
Log::AddBackend(std::make_unique<Log::DebuggerBackend>()); Log::AddBackend(std::make_unique<Log::DebuggerBackend>());

View file

@ -74,14 +74,16 @@ static void PrintVersion() {
} }
static void InitializeLogging() { static void InitializeLogging() {
using namespace Common;
Log::Filter log_filter(Log::Level::Debug); Log::Filter log_filter(Log::Level::Debug);
log_filter.ParseFilterString(Settings::values.log_filter); log_filter.ParseFilterString(Settings::values.log_filter);
Log::SetGlobalFilter(log_filter); Log::SetGlobalFilter(log_filter);
Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>()); Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
const std::string& log_dir = Common::FS::GetUserPath(Common::FS::UserPath::LogDir); const std::string& log_dir = FS::GetUserPath(FS::UserPath::LogDir);
Common::FS::CreateFullPath(log_dir); FS::CreateFullPath(log_dir);
Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE)); Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
#ifdef _WIN32 #ifdef _WIN32
Log::AddBackend(std::make_unique<Log::DebuggerBackend>()); Log::AddBackend(std::make_unique<Log::DebuggerBackend>());