Skip to content

Commit

Permalink
OrcCommand: UtilitiesMain: use UtilitiesLoggerConfiguration for early…
Browse files Browse the repository at this point in the history
… log level
  • Loading branch information
fabienfl-orc committed Feb 11, 2021
1 parent 7759094 commit 11c0bbe
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 111 deletions.
110 changes: 6 additions & 104 deletions src/OrcCommand/Log/UtilitiesLogger.cpp
Expand Up @@ -9,16 +9,14 @@

#include "stdafx.h"

#include <optional>
#include "UtilitiesLogger.h"

#include <spdlog/cfg/env.h>

#ifdef ORC_BUILD_BOOST_STACKTRACE
# include <boost/stacktrace.hpp>
#endif

#include "UtilitiesLogger.h"
#include "ParameterCheck.h"
#include "Utils/Result.h"

using namespace Orc::Command;
Expand Down Expand Up @@ -84,6 +82,9 @@ std::pair<SpdlogLogger::Ptr, SpdlogLogger::Ptr> CreateFacilities(SpdlogSink::Ptr

} // namespace

namespace Orc {
namespace Command {

UtilitiesLogger::UtilitiesLogger()
{
m_fileSink = ::CreateFileSink();
Expand All @@ -104,104 +105,5 @@ UtilitiesLogger::~UtilitiesLogger()
Orc::Log::SetDefaultLogger(nullptr);
}

// Portage of LogFileWriter::ConfigureLoggingOptions
// Parse directly argc/argv to allow initializing logs very early
void Orc::Command::UtilitiesLogger::Configure(int argc, const wchar_t* argv[]) const
{
HRESULT hr = E_FAIL;
bool verbose = false;
std::optional<Log::Level> level;

for (int i = 0; i < argc; i++)
{
switch (argv[i][0])
{
case L'/':
case L'-':
if (!_wcsnicmp(argv[i] + 1, L"Verbose", wcslen(L"Verbose")))
{
verbose = true;
}

if (!_wcsnicmp(argv[i] + 1, L"Critical", wcslen(L"Critical")))
{
level = Log::Level::Critical;
verbose = true;
}
if (!_wcsnicmp(argv[i] + 1, L"Error", wcslen(L"Error")))
{
level = Log::Level::Error;
verbose = true;
}
else if (!_wcsnicmp(argv[i] + 1, L"Warn", wcslen(L"Warn")))
{
level = Log::Level::Warning;
verbose = true;
}
else if (!_wcsnicmp(argv[i] + 1, L"Info", wcslen(L"Info")))
{
level = Log::Level::Info;
verbose = true;
}
else if (!_wcsnicmp(argv[i] + 1, L"Debug", wcslen(L"Debug")))
{
level = Log::Level::Debug;
verbose = true;
}
else if (!_wcsnicmp(argv[i] + 1, L"Trace", wcslen(L"Trace")))
{
level = Log::Level::Trace;
verbose = true;
}
else if (!_wcsnicmp(argv[i] + 1, L"NoConsole", wcslen(L"NoConsole")))
{
verbose = false;
}
else if (!_wcsnicmp(argv[i] + 1, L"LogFile", wcslen(L"LogFile")))
{
LPCWSTR pEquals = wcschr(argv[i], L'=');
WCHAR szLogFile[MAX_PATH] = {};
if (!pEquals)
{
Log::Error(L"Option /LogFile should be like: /LogFile=c:\\temp\\logfile.log\r\n");
continue;
}
else
{
if (FAILED(hr = GetOutputFile(pEquals + 1, szLogFile, MAX_PATH)))
{
Log::Error(L"Invalid logging file specified: {} [{}]", pEquals + 1, SystemError(hr));
continue;
}

std::error_code ec;
m_fileSink->Open(szLogFile, FileDisposition::CreateNew, ec);
if (ec)
{
Log::Error(L"Failed to initialize log file '{}': {}", szLogFile, ec);
continue;
}
}
}
}
}

// Load log levels from environment variable (ex: "SPDLOG_LEVEL=info,mylogger=trace")
spdlog::cfg::load_env_levels();

if (verbose)
{
if (!level)
{
level = Log::Level::Debug;
}

m_consoleSink->SetLevel(*level);
}

if (level)
{
m_logger->Get(Logger::Facility::kDefault)->SetLevel(*level);
m_logger->Get(Logger::Facility::kLogFile)->SetLevel(*level);
}
}
} // namespace Command
} // namespace Orc
4 changes: 2 additions & 2 deletions src/OrcCommand/Log/UtilitiesLogger.h
Expand Up @@ -19,6 +19,8 @@
namespace Orc {
namespace Command {

class UtilitiesLoggerConfiguration;

class UtilitiesLogger
{
public:
Expand Down Expand Up @@ -58,8 +60,6 @@ class UtilitiesLogger
UtilitiesLogger();
~UtilitiesLogger();

void Configure(int argc, const wchar_t* argv[]) const;

Orc::Logger& logger() { return *m_logger; }
const Orc::Logger& logger() const { return *m_logger; }

Expand Down
4 changes: 2 additions & 2 deletions src/OrcCommand/UtilitiesMain.cpp
Expand Up @@ -48,7 +48,7 @@ UtilitiesMain::UtilitiesMain()

void UtilitiesMain::Configure(int argc, const wchar_t* argv[])
{
m_logging.Configure(argc, argv);
UtilitiesLoggerConfiguration::ApplyLogLevel(m_logging, argc, argv);

// FIX: Some arguments must be processed very early as others depends
// on their value. This is not a clean fix but a more global refactor is
Expand Down Expand Up @@ -736,7 +736,7 @@ bool UtilitiesMain::IgnoreLoggingOptions(LPCWSTR szArg)
|| !_wcsnicmp(szArg, L"Debug", wcslen(L"Debug")) || !_wcsnicmp(szArg, L"Info", wcslen(L"Info"))
|| !_wcsnicmp(szArg, L"Warn", wcslen(L"Warn")) || !_wcsnicmp(szArg, L"Error", wcslen(L"Error"))
|| !_wcsnicmp(szArg, L"Critical", wcslen(L"Critical")) || !_wcsnicmp(szArg, L"LogFile", wcslen(L"LogFile"))
|| !_wcsnicmp(szArg, L"NoConsole", wcslen(L"NoConsole")))
|| !_wcsnicmp(szArg, L"NoConsole", wcslen(L"NoConsole")) || !_wcsnicmp(szArg, L"Log", wcslen(L"Log")))
return true;
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/OrcCommand/UtilitiesMain.h
Expand Up @@ -49,6 +49,7 @@
#include "Text/Fmt/TimeUtc.h"
#include "Utils/Guard.h"
#include "Log/UtilitiesLogger.h"
#include "Log/UtilitiesLoggerConfiguration.h"

#pragma managed(push, off)

Expand All @@ -66,11 +67,10 @@ class ORCLIB_API UtilitiesMain
};

// Common configuration
class UtilitiesConfiguration
struct UtilitiesConfiguration
{
public:
std::wstring strComputerName;
std::filesystem::path logFile;
UtilitiesLoggerConfiguration log;
};

template <class T>
Expand Down

0 comments on commit 11c0bbe

Please sign in to comment.