Skip to content

Commit

Permalink
OrcLib: Log: FileSink: Open: add parameter FileDisposition
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Feb 10, 2021
1 parent 8e9bf4a commit 5270dfe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/OrcCommand/Command/WolfLauncher/WolfLauncher_Run.cpp
Expand Up @@ -666,7 +666,7 @@ HRESULT Main::Run_Execute()
if (config.Log.Type != OutputSpec::Kind::None)
{
std::error_code ec;
m_logging.fileSink()->Open(config.Log.Path, ec);
m_logging.fileSink()->Open(config.Log.Path, FileDisposition::CreateNew, ec);
if (ec)
{
Log::Error("Failed to create log stream [{}]", ec);
Expand Down
2 changes: 1 addition & 1 deletion src/OrcCommand/Log/UtilitiesLogger.cpp
Expand Up @@ -175,7 +175,7 @@ void Orc::Command::UtilitiesLogger::Configure(int argc, const wchar_t* argv[]) c
}

std::error_code ec;
m_fileSink->Open(szLogFile, ec);
m_fileSink->Open(szLogFile, FileDisposition::CreateNew, ec);
if (ec)
{
Log::Error(L"Failed to initialize log file '{}': {}", szLogFile, ec);
Expand Down
6 changes: 5 additions & 1 deletion src/OrcCommand/Log/UtilitiesLogger.h
Expand Up @@ -42,7 +42,11 @@ class UtilitiesLogger
{
}

void Open(const std::filesystem::path& path, std::error_code& ec) { m_fileSink->Open(path, ec); }
void Open(const std::filesystem::path& path, FileDisposition disposition, std::error_code& ec)
{
m_fileSink->Open(path, disposition, ec);
}

bool IsOpen() const { return m_fileSink->IsOpen(); }
void Close() { return m_fileSink->Close(); }
std::optional<std::filesystem::path> OutputPath() const { return m_fileSink->OutputPath(); }
Expand Down
20 changes: 13 additions & 7 deletions src/OrcLib/Log/Sink/FileSink.h
Expand Up @@ -17,6 +17,7 @@
#include <spdlog/sinks/basic_file_sink.h>

#include "Log/Sink/MemorySink.h"
#include "FileDisposition.h"

//
// FileSink will cache some logs into a MemorySink until the log file is opened
Expand All @@ -43,7 +44,7 @@ class FileSink final : public spdlog::sinks::base_sink<Mutex>

~FileSink() override { Close(); }

void Open(const std::filesystem::path& path, std::error_code& ec)
void Open(const std::filesystem::path& path, FileDisposition disposition, std::error_code& ec)
{
std::lock_guard<Mutex> lock(mutex_);

Expand All @@ -53,11 +54,16 @@ class FileSink final : public spdlog::sinks::base_sink<Mutex>
return;
}

std::filesystem::remove(path, ec);
if (ec)
if (disposition == FileDisposition::CreateNew)
{
Log::Debug(L"Failed to remove '{}' [{}]", path, ec);
ec.clear();
std::filesystem::remove(path, ec);
if (ec)
{
Log::Warn(L"Failed to remove '{}' [{}]", path, ec);
ec.clear();
}

disposition = FileDisposition::Append;
}

// Dump memorySink if any
Expand All @@ -66,7 +72,7 @@ class FileSink final : public spdlog::sinks::base_sink<Mutex>
try
{
std::fstream log;
log.open(path, std::ios::out | std::ios::binary);
log.open(path, std::ios::out | std::ios::binary | ToOpenMode(disposition));

const auto& in = m_memorySink->buffer();
std::ostream_iterator<char> out(log);
Expand Down Expand Up @@ -95,7 +101,7 @@ class FileSink final : public spdlog::sinks::base_sink<Mutex>
ec.clear();
}

m_fileSink = std::make_unique<SpdlogFileSink>(absolutePath.string());
m_fileSink = std::make_unique<SpdlogFileSink>(absolutePath.string(), disposition == FileDisposition::Truncate);

// Current sink_it_ will handle filtering
m_fileSink->set_level(spdlog::level::trace);
Expand Down

0 comments on commit 5270dfe

Please sign in to comment.