mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
logging: support msvc debug window output (#8478)
This commit is contained in:
@@ -5,27 +5,31 @@
|
|||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <spdlog/sinks/daily_file_sink.h>
|
#include <spdlog/sinks/daily_file_sink.h>
|
||||||
#include <spdlog\sinks\stdout_color_sinks-inl.h>
|
#include <spdlog/sinks/msvc_sink.h>
|
||||||
|
#include <spdlog/sinks/null_sink.h>
|
||||||
|
#include <spdlog/sinks/stdout_color_sinks-inl.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <spdlog\sinks\null_sink.h>
|
|
||||||
|
|
||||||
using namespace std;
|
using spdlog::sinks_init_list;
|
||||||
using namespace spdlog;
|
using spdlog::level::level_enum;
|
||||||
|
using spdlog::sinks::daily_file_sink_mt;
|
||||||
|
using spdlog::sinks::msvc_sink_mt;
|
||||||
|
using std::make_shared;
|
||||||
|
|
||||||
map<wstring, spdlog::level::level_enum> logLevelMapping = {
|
std::map<std::wstring, level_enum> logLevelMapping = {
|
||||||
{ L"trace", level::trace },
|
{ L"trace", level_enum::trace },
|
||||||
{ L"debug", level::debug },
|
{ L"debug", level_enum::debug },
|
||||||
{ L"info", level::info },
|
{ L"info", level_enum::info },
|
||||||
{ L"warn", level::warn },
|
{ L"warn", level_enum::warn },
|
||||||
{ L"err", level::err },
|
{ L"err", level_enum::err },
|
||||||
{ L"critical", level::critical },
|
{ L"critical", level_enum::critical },
|
||||||
{ L"off", level::off },
|
{ L"off", level_enum::off },
|
||||||
};
|
};
|
||||||
|
|
||||||
level::level_enum getLogLevel(std::wstring_view logSettingsPath)
|
level_enum getLogLevel(std::wstring_view logSettingsPath)
|
||||||
{
|
{
|
||||||
auto logLevel = get_log_settings(logSettingsPath).logLevel;
|
auto logLevel = get_log_settings(logSettingsPath).logLevel;
|
||||||
level::level_enum result = logLevelMapping[LogSettings::defaultLogLevel];
|
level_enum result = logLevelMapping[LogSettings::defaultLogLevel];
|
||||||
if (logLevelMapping.find(logLevel) != logLevelMapping.end())
|
if (logLevelMapping.find(logLevel) != logLevelMapping.end())
|
||||||
{
|
{
|
||||||
result = logLevelMapping[logLevel];
|
result = logLevelMapping[logLevel];
|
||||||
@@ -50,15 +54,24 @@ void Logger::init(std::string loggerName, std::wstring logFilePath, std::wstring
|
|||||||
auto logLevel = getLogLevel(logSettingsPath);
|
auto logLevel = getLogLevel(logSettingsPath);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto sink = make_shared<sinks::daily_file_sink_mt>(logFilePath, 0, 0, false, LogSettings::retention);
|
auto sink = make_shared<daily_file_sink_mt>(logFilePath, 0, 0, false, LogSettings::retention);
|
||||||
logger = make_shared<spdlog::logger>(loggerName, sink);
|
if (IsDebuggerPresent())
|
||||||
|
{
|
||||||
|
auto msvc_sink = make_shared<msvc_sink_mt>();
|
||||||
|
msvc_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%n] [t-%t] [%l] %v");
|
||||||
|
logger = make_shared<spdlog::logger>(loggerName, sinks_init_list{ sink, msvc_sink });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger = make_shared<spdlog::logger>(loggerName, sink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
logger = spdlog::null_logger_mt(loggerName);
|
logger = spdlog::null_logger_mt(loggerName);
|
||||||
if (!wasLogFailedShown())
|
if (!wasLogFailedShown())
|
||||||
{
|
{
|
||||||
// todo: that message should be shown from init caller and strings should be localized
|
// todo: that message should be shown from init caller and strings should be localized
|
||||||
MessageBoxW(NULL,
|
MessageBoxW(NULL,
|
||||||
L"Logger can not be initialized",
|
L"Logger can not be initialized",
|
||||||
L"PowerToys",
|
L"PowerToys",
|
||||||
|
|||||||
Reference in New Issue
Block a user