Files
PowerToys/src/common/logger/logger.cpp

119 lines
3.4 KiB
C++
Raw Normal View History

2020-11-18 12:15:14 +02:00
// logger.cpp : Defines the functions for the static library.
//
#include "pch.h"
#include "framework.h"
#include "logger.h"
#include <unordered_map>
2020-11-18 12:15:14 +02:00
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>
#include <spdlog/sinks/null_sink.h>
#include <spdlog/sinks/stdout_color_sinks-inl.h>
2020-11-18 12:15:14 +02:00
#include <iostream>
using spdlog::sinks_init_list;
using spdlog::level::level_enum;
using spdlog::sinks::daily_file_sink_mt;
using spdlog::sinks::msvc_sink_mt;
using std::make_shared;
2020-11-18 12:15:14 +02:00
namespace
{
const std::unordered_map<std::wstring, level_enum> logLevelMapping = {
{ L"trace", level_enum::trace },
{ L"debug", level_enum::debug },
{ L"info", level_enum::info },
{ L"warn", level_enum::warn },
{ L"err", level_enum::err },
{ L"critical", level_enum::critical },
{ L"off", level_enum::off },
};
}
2020-11-18 12:15:14 +02:00
level_enum getLogLevel(std::wstring_view logSettingsPath)
2020-11-18 12:15:14 +02:00
{
auto logLevel = get_log_settings(logSettingsPath).logLevel;
if (auto it = logLevelMapping.find(logLevel); it != logLevelMapping.end())
2020-11-18 12:15:14 +02:00
{
return it->second;
2020-11-18 12:15:14 +02:00
}
if (auto it = logLevelMapping.find(LogSettings::defaultLogLevel); it != logLevelMapping.end())
{
return it->second;
}
return level_enum::trace;
2020-11-18 12:15:14 +02:00
}
2021-03-10 13:57:06 +02:00
std::shared_ptr<spdlog::logger> Logger::logger = spdlog::null_logger_mt("null");
2020-11-30 16:16:49 +02:00
bool Logger::wasLogFailedShown()
2020-11-18 12:15:14 +02:00
{
2020-11-30 16:16:49 +02:00
wchar_t* pValue;
size_t len;
_wdupenv_s(&pValue, &len, logFailedShown.c_str());
delete[] pValue;
return len;
2020-11-18 12:15:14 +02:00
}
2020-11-30 16:16:49 +02:00
void Logger::init(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath)
2020-11-18 12:15:14 +02:00
{
auto logLevel = getLogLevel(logSettingsPath);
Self-contained .NET (#22217) * dotnet sc * MD preview - C# app - working self-contained * Gcode preview - C# app * DevFiles preview - C# app * Fix passing path with spaces as cmd arg and monacocpp proj file * Pdf preview - C# app * Svg preview - C# app * Fix comment * Gcode thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix GcodeThumbnailProviderCpp.vcxproj * Svg thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix Svg tests * Thumbnail providers - installer * Self-contained Hosts and FileLocksmith * Fix hardcoded <RuntimeIdentifier> * Remove unneeded files * Try to fix Nuget in PR CI * Prefix new dlls with PowerToys. Sign new dlls and exes * Add new .exe files to ProcessList * ci: debug by listing all env vars * ci: try setting variable in the right ci file * Bring back hardcoded RuntimeIdentifier * ci: Add comment and remove debug action * Remove unneeded lib * [WIP] Platform conditional dotnet files & hardlinks * Cleanup * Update expect.txt * Test fix - ARM installer * Fix uninstall bug * Update docs * Fix failing test * Add dll details * Minor cleanup * Improve resizing * Add some logs * Test fix - release build * Remove InvokeOnControlThread * Test fix: logger initialization * Fix arm64 installer Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Dustin L. Howett <dustin@howett.net>
2022-12-14 13:37:23 +01:00
bool newLoggerCreated = false;
2020-11-18 12:15:14 +02:00
try
{
Self-contained .NET (#22217) * dotnet sc * MD preview - C# app - working self-contained * Gcode preview - C# app * DevFiles preview - C# app * Fix passing path with spaces as cmd arg and monacocpp proj file * Pdf preview - C# app * Svg preview - C# app * Fix comment * Gcode thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix GcodeThumbnailProviderCpp.vcxproj * Svg thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix Svg tests * Thumbnail providers - installer * Self-contained Hosts and FileLocksmith * Fix hardcoded <RuntimeIdentifier> * Remove unneeded files * Try to fix Nuget in PR CI * Prefix new dlls with PowerToys. Sign new dlls and exes * Add new .exe files to ProcessList * ci: debug by listing all env vars * ci: try setting variable in the right ci file * Bring back hardcoded RuntimeIdentifier * ci: Add comment and remove debug action * Remove unneeded lib * [WIP] Platform conditional dotnet files & hardlinks * Cleanup * Update expect.txt * Test fix - ARM installer * Fix uninstall bug * Update docs * Fix failing test * Add dll details * Minor cleanup * Improve resizing * Add some logs * Test fix - release build * Remove InvokeOnControlThread * Test fix: logger initialization * Fix arm64 installer Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Dustin L. Howett <dustin@howett.net>
2022-12-14 13:37:23 +01:00
logger = spdlog::get(loggerName);
if (logger == nullptr)
{
Self-contained .NET (#22217) * dotnet sc * MD preview - C# app - working self-contained * Gcode preview - C# app * DevFiles preview - C# app * Fix passing path with spaces as cmd arg and monacocpp proj file * Pdf preview - C# app * Svg preview - C# app * Fix comment * Gcode thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix GcodeThumbnailProviderCpp.vcxproj * Svg thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix Svg tests * Thumbnail providers - installer * Self-contained Hosts and FileLocksmith * Fix hardcoded <RuntimeIdentifier> * Remove unneeded files * Try to fix Nuget in PR CI * Prefix new dlls with PowerToys. Sign new dlls and exes * Add new .exe files to ProcessList * ci: debug by listing all env vars * ci: try setting variable in the right ci file * Bring back hardcoded RuntimeIdentifier * ci: Add comment and remove debug action * Remove unneeded lib * [WIP] Platform conditional dotnet files & hardlinks * Cleanup * Update expect.txt * Test fix - ARM installer * Fix uninstall bug * Update docs * Fix failing test * Add dll details * Minor cleanup * Improve resizing * Add some logs * Test fix - release build * Remove InvokeOnControlThread * Test fix: logger initialization * Fix arm64 installer Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Dustin L. Howett <dustin@howett.net>
2022-12-14 13:37:23 +01:00
auto sink = make_shared<daily_file_sink_mt>(logFilePath, 0, 0, false, LogSettings::retention);
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);
}
newLoggerCreated = true;
}
2020-11-18 12:15:14 +02:00
}
catch (...)
{
2020-11-30 16:16:49 +02:00
logger = spdlog::null_logger_mt(loggerName);
if (!wasLogFailedShown())
{
// todo: that message should be shown from init caller and strings should be localized
2020-11-30 16:16:49 +02:00
MessageBoxW(NULL,
L"Logger cannot be initialized",
2020-11-30 16:16:49 +02:00
L"PowerToys",
MB_OK | MB_ICONERROR);
SetEnvironmentVariable(logFailedShown.c_str(), L"yes");
}
return;
2020-11-18 12:15:14 +02:00
}
Self-contained .NET (#22217) * dotnet sc * MD preview - C# app - working self-contained * Gcode preview - C# app * DevFiles preview - C# app * Fix passing path with spaces as cmd arg and monacocpp proj file * Pdf preview - C# app * Svg preview - C# app * Fix comment * Gcode thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix GcodeThumbnailProviderCpp.vcxproj * Svg thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix Svg tests * Thumbnail providers - installer * Self-contained Hosts and FileLocksmith * Fix hardcoded <RuntimeIdentifier> * Remove unneeded files * Try to fix Nuget in PR CI * Prefix new dlls with PowerToys. Sign new dlls and exes * Add new .exe files to ProcessList * ci: debug by listing all env vars * ci: try setting variable in the right ci file * Bring back hardcoded RuntimeIdentifier * ci: Add comment and remove debug action * Remove unneeded lib * [WIP] Platform conditional dotnet files & hardlinks * Cleanup * Update expect.txt * Test fix - ARM installer * Fix uninstall bug * Update docs * Fix failing test * Add dll details * Minor cleanup * Improve resizing * Add some logs * Test fix - release build * Remove InvokeOnControlThread * Test fix: logger initialization * Fix arm64 installer Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Dustin L. Howett <dustin@howett.net>
2022-12-14 13:37:23 +01:00
if (newLoggerCreated)
{
logger->set_level(logLevel);
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [p-%P] [t-%t] [%l] %v");
logger->flush_on(logLevel); // Auto flush on every log message.
spdlog::register_logger(logger);
}
2020-11-30 16:16:49 +02:00
logger->info("{} logger is initialized", loggerName);
2020-11-18 12:15:14 +02:00
}
void Logger::init(std::vector<spdlog::sink_ptr> sinks)
{
auto init_logger = std::make_shared<spdlog::logger>("", begin(sinks), end(sinks));
if (!init_logger)
{
return;
}
Logger::logger = init_logger;
}