mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
Runner logging (#8068)
This commit is contained in:
3
src/common/logger/framework.h
Normal file
3
src/common/logger/framework.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
64
src/common/logger/logger.cpp
Normal file
64
src/common/logger/logger.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// logger.cpp : Defines the functions for the static library.
|
||||
//
|
||||
#include "pch.h"
|
||||
#include "framework.h"
|
||||
#include "logger.h"
|
||||
#include "logger_settings.h"
|
||||
#include <map>
|
||||
#include <spdlog/sinks/daily_file_sink.h>
|
||||
#include <spdlog\sinks\stdout_color_sinks-inl.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace spdlog;
|
||||
|
||||
map<wstring, spdlog::level::level_enum> logLevelMapping = {
|
||||
{ L"trace", level::trace },
|
||||
{ L"debug", level::debug },
|
||||
{ L"info", level::info },
|
||||
{ L"warn", level::warn },
|
||||
{ L"err", level::err },
|
||||
{ L"critical", level::critical },
|
||||
{ L"off", level::off },
|
||||
};
|
||||
|
||||
level::level_enum getLogLevel(std::wstring_view logSettingsPath)
|
||||
{
|
||||
auto logLevel = get_log_settings(logSettingsPath).logLevel;
|
||||
level::level_enum result = logLevelMapping[LogSettings::defaultLogLevel];
|
||||
if (logLevelMapping.find(logLevel) != logLevelMapping.end())
|
||||
{
|
||||
result = logLevelMapping[logLevel];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Logger::Logger()
|
||||
{
|
||||
}
|
||||
|
||||
Logger::Logger(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath)
|
||||
{
|
||||
auto logLevel = getLogLevel(logSettingsPath);
|
||||
try
|
||||
{
|
||||
auto sink = make_shared<sinks::daily_file_sink_mt>(logFilePath, 0, 0, false, 5);
|
||||
this->logger = make_shared<spdlog::logger>(loggerName, sink);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
cerr << "Can not create file logger. Create stdout logger instead" << endl;
|
||||
this->logger = spdlog::stdout_color_mt("some_unique_name");
|
||||
}
|
||||
|
||||
this->logger->set_level(logLevel);
|
||||
this->logger->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [p-%P] [t-%t] [%l] %v");
|
||||
spdlog::register_logger(this->logger);
|
||||
spdlog::flush_every(std::chrono::seconds(3));
|
||||
}
|
||||
|
||||
Logger::~Logger()
|
||||
{
|
||||
this->logger.reset();
|
||||
}
|
||||
50
src/common/logger/logger.h
Normal file
50
src/common/logger/logger.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
class Logger
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> logger;
|
||||
|
||||
public:
|
||||
Logger();
|
||||
Logger(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath);
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void trace(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->trace(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void debug(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->debug(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void info(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->info(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void warn(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->warn(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void error(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->error(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void critical(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->critical(fmt, args...);
|
||||
}
|
||||
|
||||
~Logger();
|
||||
};
|
||||
123
src/common/logger/logger.vcxproj
Normal file
123
src/common/logger/logger.vcxproj
Normal file
@@ -0,0 +1,123 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" />
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</ProjectGuid>
|
||||
<RootNamespace>logger</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<Import Project="..\..\..\deps\spdlog.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>
|
||||
</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>
|
||||
</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="framework.h" />
|
||||
<ClInclude Include="logger.h" />
|
||||
<ClInclude Include="logger_settings.h" />
|
||||
<ClInclude Include="pch.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="logger.cpp" />
|
||||
<ClCompile Include="logger_settings.cpp" />
|
||||
<ClCompile Include="pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\logging\logging.vcxproj">
|
||||
<Project>{7e1e3f13-2bd6-3f75-a6a7-873a2b55c60f}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" />
|
||||
</ImportGroup>
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props'))" />
|
||||
<Error Condition="!Exists('..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets'))" />
|
||||
</Target>
|
||||
</Project>
|
||||
45
src/common/logger/logger.vcxproj.filters
Normal file
45
src/common/logger/logger.vcxproj.filters
Normal file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="framework.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="pch.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="logger.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="logger_settings.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="logger.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="pch.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="logger_settings.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
83
src/common/logger/logger_settings.cpp
Normal file
83
src/common/logger/logger_settings.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "pch.h"
|
||||
#include "logger_settings.h"
|
||||
#include <fstream>
|
||||
#include <Windows.h>
|
||||
#include <winrt/Windows.Data.Json.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace winrt::Windows::Data::Json;
|
||||
|
||||
LogSettings::LogSettings()
|
||||
{
|
||||
this->logLevel = LogSettings::defaultLogLevel;
|
||||
}
|
||||
|
||||
std::optional<JsonObject> from_file(std::wstring_view file_name)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::ifstream file(file_name.data(), std::ios::binary);
|
||||
if (file.is_open())
|
||||
{
|
||||
using isbi = std::istreambuf_iterator<char>;
|
||||
std::string obj_str{ isbi{ file }, isbi{} };
|
||||
return JsonValue::Parse(winrt::to_hstring(obj_str)).GetObjectW();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
void to_file(std::wstring_view file_name, const JsonObject& obj)
|
||||
{
|
||||
std::wstring obj_str{ obj.Stringify().c_str() };
|
||||
try
|
||||
{
|
||||
std::ofstream{ file_name.data(), std::ios::binary } << winrt::to_string(obj_str);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "Can not create log config file" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
JsonObject to_json(LogSettings settings)
|
||||
{
|
||||
JsonObject result;
|
||||
result.SetNamedValue(LogSettings::logLevelOption, JsonValue::CreateStringValue(settings.logLevel));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
LogSettings to_settings(JsonObject jobject)
|
||||
{
|
||||
LogSettings result;
|
||||
try
|
||||
{
|
||||
result.logLevel = jobject.GetNamedString(LogSettings::logLevelOption);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "Can not read log level from config file" << std::endl;
|
||||
result.logLevel = LogSettings::defaultLogLevel;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get log settings from file. File with default options is created if it does not exist
|
||||
LogSettings get_log_settings(std::wstring_view file_name)
|
||||
{
|
||||
auto jobject = from_file(file_name);
|
||||
if (!jobject.has_value())
|
||||
{
|
||||
auto json = to_json(LogSettings());
|
||||
to_file(file_name, json);
|
||||
return to_settings(json);
|
||||
}
|
||||
|
||||
return to_settings(jobject.value());
|
||||
}
|
||||
15
src/common/logger/logger_settings.h
Normal file
15
src/common/logger/logger_settings.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
struct LogSettings
|
||||
{
|
||||
// The following strings are not localizable
|
||||
inline const static std::wstring defaultLogLevel = L"warn";
|
||||
inline const static std::wstring logLevelOption = L"logLevel";
|
||||
|
||||
std::wstring logLevel;
|
||||
LogSettings();
|
||||
};
|
||||
|
||||
// Get log settings from file. File with default options is created if it does not exist
|
||||
LogSettings get_log_settings(std::wstring_view file_name);
|
||||
4
src/common/logger/packages.config
Normal file
4
src/common/logger/packages.config
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Microsoft.Windows.CppWinRT" version="2.0.200729.8" targetFramework="native" />
|
||||
</packages>
|
||||
5
src/common/logger/pch.cpp
Normal file
5
src/common/logger/pch.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
// pch.cpp: source file corresponding to the pre-compiled header
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
|
||||
13
src/common/logger/pch.h
Normal file
13
src/common/logger/pch.h
Normal file
@@ -0,0 +1,13 @@
|
||||
// pch.h: This is a precompiled header file.
|
||||
// Files listed below are compiled only once, improving build performance for future builds.
|
||||
// This also affects IntelliSense performance, including code completion and many code browsing features.
|
||||
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
|
||||
// Do not add files here that you will be updating frequently as this negates the performance advantage.
|
||||
|
||||
#ifndef PCH_H
|
||||
#define PCH_H
|
||||
|
||||
// add headers that you want to pre-compile here
|
||||
#include "framework.h"
|
||||
|
||||
#endif //PCH_H
|
||||
@@ -6,6 +6,7 @@
|
||||
namespace PTSettingsHelper
|
||||
{
|
||||
constexpr inline const wchar_t* settings_filename = L"\\settings.json";
|
||||
constexpr inline const wchar_t* log_settings_filename = L"log_settings.json";
|
||||
|
||||
std::wstring get_root_save_folder_location()
|
||||
{
|
||||
@@ -71,4 +72,11 @@ namespace PTSettingsHelper
|
||||
auto saved_settings = json::from_file(save_file_location);
|
||||
return saved_settings.has_value() ? std::move(*saved_settings) : json::JsonObject{};
|
||||
}
|
||||
|
||||
std::wstring get_log_settings_file_location()
|
||||
{
|
||||
std::filesystem::path result(PTSettingsHelper::get_root_save_folder_location());
|
||||
result = result.append(log_settings_filename);
|
||||
return result.wstring();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,5 +13,5 @@ namespace PTSettingsHelper
|
||||
json::JsonObject load_module_settings(std::wstring_view powertoy_name);
|
||||
void save_general_settings(const json::JsonObject& settings);
|
||||
json::JsonObject load_general_settings();
|
||||
|
||||
std::wstring get_log_settings_file_location();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user