Skip to content

Commit

Permalink
OrcCommand: WolfLauncher: add support for 'log' option
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Feb 11, 2021
1 parent 4390270 commit e47d8eb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
Expand Up @@ -11,6 +11,7 @@
// WOLFLAUNCHER
#include "ConfigFile_Common.h"
#include "ConfigFile_WOLFLauncher.h"
#include "Log/UtilitiesLoggerConfiguration.h"

// <TempDir>%Temp%\WorkingTemp</TempDir>

Expand Down Expand Up @@ -175,7 +176,7 @@ HRESULT Orc::Config::Wolf::root(ConfigItem& item)
return hr;
if (FAILED(hr = item.AddChild(recipient, WOLFLAUNCHER_RECIPIENT)))
return hr;
if (FAILED(hr = item.AddChild(L"log", Orc::Config::Common::output, WOLFLAUNCHER_LOG)))
if (FAILED(hr = item.AddChild(Orc::Command::UtilitiesLoggerConfiguration::Register, WOLFLAUNCHER_LOG)))
return hr;
if (FAILED(hr = item.AddChild(L"outline", Orc::Config::Common::output, WOLFLAUNCHER_OUTLINE)))
return hr;
Expand Down
2 changes: 0 additions & 2 deletions src/OrcCommand/Command/WolfLauncher/WolfLauncher.h
Expand Up @@ -79,7 +79,6 @@ class ORCUTILS_API Main : public UtilitiesMain
Configuration()
{
Output.supportedTypes = OutputSpec::Kind::Directory;
Log.supportedTypes = OutputSpec::Kind::File;
Outline.supportedTypes = OutputSpec::Kind::StructuredFile;
Outcome.supportedTypes = OutputSpec::Kind::StructuredFile;
TempWorkingDir.supportedTypes = OutputSpec::Kind::Directory;
Expand All @@ -89,7 +88,6 @@ class ORCUTILS_API Main : public UtilitiesMain

OutputSpec Output;

OutputSpec Log;
OutputSpec Outline;
OutputSpec Outcome;

Expand Down
56 changes: 39 additions & 17 deletions src/OrcCommand/Command/WolfLauncher/WolfLauncher_Config.cpp
Expand Up @@ -25,10 +25,13 @@
#include "SystemDetails.h"
#include "ConfigFile_OrcConfig.h"
#include "ConfigFile_WOLFLauncher.h"
#include "Log/UtilitiesLoggerConfiguration.h"

using namespace Orc;
using namespace Orc::Command::Wolf;

namespace fs = std::filesystem;

ConfigItem::InitFunction Main::GetXmlConfigBuilder()
{
return Orc::Config::Wolf::root;
Expand Down Expand Up @@ -150,14 +153,26 @@ HRESULT Main::GetConfigurationFromConfig(const ConfigItem& configitem)
}
}

if (configitem[WOLFLAUNCHER_LOG])
if (!configitem[WOLFLAUNCHER_LOG].empty())
{
hr = config.Log.Configure(configitem[WOLFLAUNCHER_LOG]);
// Deprecated: compatibility with 10.0.x log configuration
OutputSpec output;
auto hr = output.Configure(configitem[WOLFLAUNCHER_LOG]);
if (FAILED(hr))
{
Log::Warn("Failed to configure DFIR-Orc log file [{}]", SystemError(hr));
Log::Warn(L"Failed to configure DFIR-Orc log file [{}]", SystemError(hr));
}
else
{
m_utilitiesConfig.log.file.path = output.Path;
m_utilitiesConfig.log.file.encoding = ToEncoding(output.OutputEncoding);
m_utilitiesConfig.log.file.disposition = ToFileDisposition(output.disposition);
}
}
else if (configitem[WOLFLAUNCHER_LOG])
{
UtilitiesLoggerConfiguration::Parse(configitem[WOLFLAUNCHER_LOG], m_utilitiesConfig.log);
}

if (configitem[WOLFLAUNCHER_OUTLINE])
{
Expand Down Expand Up @@ -437,6 +452,8 @@ HRESULT Main::GetConfigurationFromArgcArgv(int argc, LPCWSTR argv[])

std::wstring strTags;

UtilitiesLoggerConfiguration::Parse(argc, argv, m_utilitiesConfig.log);

for (int i = 0; i < argc; i++)
{
std::wstring strPriority;
Expand Down Expand Up @@ -571,6 +588,25 @@ HRESULT Main::GetConfigurationFromArgcArgv(int argc, LPCWSTR argv[])
HRESULT Main::CheckConfiguration()
{
HRESULT hr = E_FAIL;

fs::path logPath;
if (m_utilitiesConfig.log.file.path)
{
// Apply the output directory path to the log file
logPath = fs::path(config.Output.Path) / fs::path(*m_utilitiesConfig.log.file.path).filename();
m_utilitiesConfig.log.file.path = logPath;
}

if (m_utilitiesConfig.log.logFile)
{
// Deprecated: 10.0.x compatilbility options
// Apply the output directory path to the log file
logPath = fs::path(config.Output.Path) / fs::path(*m_utilitiesConfig.log.logFile).filename();
m_utilitiesConfig.log.logFile = logPath;
}

UtilitiesLoggerConfiguration::Apply(m_logging, m_utilitiesConfig.log);

if ((config.bRepeatCreateNew ? 1 : 0) + (config.bRepeatOnce ? 1 : 0) + (config.bRepeatOverwrite ? 1 : 0) > 1)
{
Log::Error("options /createnew, /once and /overwrite are mutually exclusive");
Expand Down Expand Up @@ -625,20 +661,6 @@ HRESULT Main::CheckConfiguration()
return E_INVALIDARG;
}

if (config.Log.Type != OutputSpec::Kind::None)
{
if (config.Log.IsFile())
{
// We need to apply the output directory path to the log file
config.Log.Path = std::filesystem::path(config.Output.Path) / config.Log.FileName;
}
else
{
Log::Error("Invalid log file type");
return E_INVALIDARG;
}
}

if (config.Outline.Type != OutputSpec::Kind::None)
{
if (config.Outline.IsStructuredFile())
Expand Down
Expand Up @@ -165,7 +165,6 @@ void Main::PrintParameters()
PrintValue(node, L"Overwrite", config.bRepeatOverwrite);
PrintValue(node, L"Repeat behavior", WolfExecution::ToString(config.RepeatBehavior));
PrintValue(node, L"Offline", config.strOfflineLocation.value_or(L"<empty>"));
PrintValue(node, L"Log file", config.Log.Path);
PrintValue(node, L"Outline file", config.Outline.Path);
PrintValue(node, L"Outcome file", config.Outcome.Path);
PrintValue(node, L"Priority", config.Priority);
Expand Down
19 changes: 6 additions & 13 deletions src/OrcCommand/Command/WolfLauncher/WolfLauncher_Run.cpp
Expand Up @@ -663,17 +663,6 @@ HRESULT Main::Run_Execute()
{
HRESULT hr = E_FAIL;

if (config.Log.Type != OutputSpec::Kind::None)
{
std::error_code ec;
m_logging.fileSink()->Open(config.Log.Path, FileDisposition::CreateNew, ec);
if (ec)
{
Log::Error("Failed to create log stream [{}]", ec);
return ToHRESULT(ec);
}
}

if (config.Output.UploadOutput)
{
if (FAILED(hr = InitializeUpload(*config.Output.UploadOutput)))
Expand Down Expand Up @@ -880,9 +869,13 @@ HRESULT Main::Run_Execute()
{
fileSink->Close();

if (auto hr = UploadSingleFile(config.Log.FileName, config.Log.Path); FAILED(hr))
const auto localPath = fileSink->OutputPath();
if (localPath)
{
Log::Error(L"Failed to upload log file [{}]", SystemError(hr));
if (auto hr = UploadSingleFile(localPath->filename(), *localPath); FAILED(hr))
{
Log::Error(L"Failed to upload log file [{}]", SystemError(hr));
}
}
}

Expand Down

0 comments on commit e47d8eb

Please sign in to comment.