mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
Fix runner warnings (#8211)
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <spdlog/sinks/daily_file_sink.h>
|
||||
#include <spdlog\sinks\stdout_color_sinks-inl.h>
|
||||
#include <iostream>
|
||||
#include <spdlog\sinks\null_sink.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace spdlog;
|
||||
@@ -33,31 +34,45 @@ level::level_enum getLogLevel(std::wstring_view logSettingsPath)
|
||||
return result;
|
||||
}
|
||||
|
||||
Logger::Logger()
|
||||
std::shared_ptr<spdlog::logger> Logger::logger;
|
||||
|
||||
bool Logger::wasLogFailedShown()
|
||||
{
|
||||
wchar_t* pValue;
|
||||
size_t len;
|
||||
_wdupenv_s(&pValue, &len, logFailedShown.c_str());
|
||||
delete[] pValue;
|
||||
return len;
|
||||
}
|
||||
|
||||
Logger::Logger(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath)
|
||||
void Logger::init(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, LogSettings::retention);
|
||||
this->logger = make_shared<spdlog::logger>(loggerName, sink);
|
||||
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");
|
||||
logger = spdlog::null_logger_mt(loggerName);
|
||||
if (!wasLogFailedShown())
|
||||
{
|
||||
// todo: that message should be shown from init caller and strings should be localized
|
||||
MessageBoxW(NULL,
|
||||
L"Logger can not be initialized",
|
||||
L"PowerToys",
|
||||
MB_OK | MB_ICONERROR);
|
||||
|
||||
SetEnvironmentVariable(logFailedShown.c_str(), L"yes");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
logger->set_level(logLevel);
|
||||
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [p-%P] [t-%t] [%l] %v");
|
||||
spdlog::register_logger(logger);
|
||||
spdlog::flush_every(std::chrono::seconds(3));
|
||||
}
|
||||
|
||||
Logger::~Logger()
|
||||
{
|
||||
this->logger.reset();
|
||||
logger->info("{} logger is initialized", loggerName);
|
||||
}
|
||||
|
||||
@@ -5,53 +5,54 @@
|
||||
class Logger
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> logger;
|
||||
inline const static std::wstring logFailedShown = L"logFailedShown";
|
||||
static std::shared_ptr<spdlog::logger> logger;
|
||||
static bool wasLogFailedShown();
|
||||
|
||||
public:
|
||||
Logger();
|
||||
Logger(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath);
|
||||
Logger() = delete;
|
||||
|
||||
static void init(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath);
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
void trace(const FormatString& fmt, const Args&... args)
|
||||
static void trace(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->trace(fmt, args...);
|
||||
logger->trace(fmt, args...);
|
||||
}
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
void debug(const FormatString& fmt, const Args&... args)
|
||||
static void debug(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->debug(fmt, args...);
|
||||
logger->debug(fmt, args...);
|
||||
}
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
void info(const FormatString& fmt, const Args&... args)
|
||||
static void info(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->info(fmt, args...);
|
||||
logger->info(fmt, args...);
|
||||
}
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
void warn(const FormatString& fmt, const Args&... args)
|
||||
static void warn(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->warn(fmt, args...);
|
||||
logger->warn(fmt, args...);
|
||||
}
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
void error(const FormatString& fmt, const Args&... args)
|
||||
static void error(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->error(fmt, args...);
|
||||
logger->error(fmt, args...);
|
||||
}
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
void critical(const FormatString& fmt, const Args&... args)
|
||||
static void critical(const FormatString& fmt, const Args&... args)
|
||||
{
|
||||
this->logger->critical(fmt, args...);
|
||||
logger->critical(fmt, args...);
|
||||
}
|
||||
|
||||
~Logger();
|
||||
};
|
||||
|
||||
@@ -40,7 +40,6 @@ void to_file(std::wstring_view file_name, const JsonObject& obj)
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "Can not create log config file" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +60,6 @@ LogSettings to_settings(JsonObject jobject)
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "Can not read log level from config file" << std::endl;
|
||||
result.logLevel = LogSettings::defaultLogLevel;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace timeutil
|
||||
|
||||
inline int64_t in_days(const std::time_t to, const std::time_t from)
|
||||
{
|
||||
return static_cast<int64_t>(std::difftime(to, from) / (3600 * 24));
|
||||
return static_cast<int64_t>(std::difftime(to, from) / (3600 * (int64_t)24));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user