[+] AuLogging::NewStreamSink

[*] Logging QOL fixes
This commit is contained in:
Reece Wilson 2024-04-10 11:05:00 +01:00
parent 1bb3197941
commit 9742db62d3
8 changed files with 216 additions and 19 deletions

View File

@ -12,7 +12,6 @@ namespace Aurora::IO::IPC
struct IPCPipe;
}
namespace Aurora::Logging
{
struct IIPCLogger : IBasicSink

View File

@ -12,6 +12,11 @@ namespace Aurora
struct SocketConsole;
}
namespace Aurora::IO
{
struct IStreamWriter;
}
namespace Aurora::Logging
{
struct DirectoryLogger
@ -65,6 +70,12 @@ namespace Aurora::Logging
*/
AUKN_SHARED_API(NewRingLogger, IBasicSinkRB, AuUInt32 maxLogEntries);
AUKN_SHARED_API(NewStreamSink, IFormattedSink,
const AuSPtr<Aurora::IO::IStreamWriter> &pOutputStream,
bool bText,
bool bLengthPrefixed);
/**
* @brief Constructs a logger object from an array of sinks
*/

View File

@ -13,6 +13,7 @@
#include "Sinks/RingBuffer.hpp"
#include "Sinks/DirLogArchive.hpp"
#include "Sinks/IPCSink.hpp"
#include "Sinks/StreamSink.hpp"
#if defined(AURORA_PLATFORM_WIN32)
#include "Sinks/EventLog.Win32.hpp"
@ -113,6 +114,21 @@ namespace Aurora::Logging
return Sinks::NewRingBufferNew(maxLogEntries);
}
AUKN_SYM void NewStreamSinkRelease(IFormattedSink *pSink)
{
Sinks::NewStreamSinkRelease(pSink);
}
AUKN_SYM IFormattedSink *NewStreamSinkNew(const AuSPtr<Aurora::IO::IStreamWriter> &pOutputStream,
bool bText,
bool bLengthPrefixed)
{
return Sinks::NewStreamSinkNew(pOutputStream,
bText,
bLengthPrefixed);
}
AUKN_SYM void NewRingLoggerRelease(IBasicSinkRB *pSink)
{
Sinks::NewRingBufferRelease(pSink);

View File

@ -19,7 +19,7 @@ namespace Aurora::Logging::Sinks
bool FIOSink::Init()
{
this->file_ = AuIOFS::OpenWriteUnique(this->path_);
this->file_ = AuFS::OpenWriteUnique(this->path_, AuFS::EFileAdvisoryLockLevel::eBlockWrite);
return static_cast<bool>(this->file_);
}

View File

@ -10,29 +10,38 @@
namespace Aurora::Logging::Sinks
{
IPCSink::IPCSink(const AuSPtr<Aurora::IO::IPC::IPCPipe> &pipe, bool lengthPrefixed) : pipe_(pipe), lengthPrefixed_(lengthPrefixed)
IPCSink::IPCSink(const AuSPtr<Aurora::IO::IPC::IPCPipe> &pPipe,
bool bLengthPrefixed) :
pPipe_(pPipe),
bLengthPrefixed_(bLengthPrefixed)
{
}
bool IPCSink::Init()
{
return static_cast<bool>(this->pipe_);
return static_cast<bool>(this->pPipe_);
}
AuSPtr<IO::IPC::IPCPipe> IPCSink::ToPipe()
{
return this->pipe_;
return this->pPipe_;
}
void IPCSink::OnMessageBlocking(AuUInt8 level, const ConsoleMessage &msg)
void IPCSink::OnMessageBlocking(AuUInt8 uLevel, const ConsoleMessage &msg)
{
AU_LOCK_GUARD(this->logMutex_);
auto startOffset = this->logBuffer_.GetWriteOffset();
if (this->lengthPrefixed_)
if (this->bLengthPrefixed_)
{
if (this->logBuffer_.readPtr != this->logBuffer_.base)
{
SysPushErrorConcurrentAborted();
return;
}
if (this->logBuffer_.base == this->logBuffer_.writePtr)
{
this->logBuffer_.Write<AuUInt32>(0);
@ -54,11 +63,6 @@ namespace Aurora::Logging::Sinks
void IPCSink::OnFlush()
{
if (!this->logMutex_)
{
return;
}
AU_LOCK_GUARD(this->logMutex_);
if (!this->logBuffer_.RemainingBytes())
@ -66,16 +70,24 @@ namespace Aurora::Logging::Sinks
return;
}
auto len = this->logBuffer_.writePtr - this->logBuffer_.base;
if (this->lengthPrefixed_)
if (this->bLengthPrefixed_ &&
this->logBuffer_.readPtr == this->logBuffer_.base)
{
auto len = this->logBuffer_.writePtr - this->logBuffer_.base;
this->logBuffer_.writePtr = this->logBuffer_.base;
this->logBuffer_.Write<AuUInt32>(len);
this->logBuffer_.writePtr = this->logBuffer_.base + len;
}
this->pipe_->Write(AuMemoryViewStreamRead(this->logBuffer_.base, this->logBuffer_.base + len));
this->logBuffer_.ResetPositions();
AuUInt uWritten {};
this->pPipe_->Write(AuMemoryViewStreamRead(this->logBuffer_, uWritten));
this->logBuffer_.readPtr += uWritten;
if (this->logBuffer_.readPtr == this->logBuffer_.writePtr)
{
this->logBuffer_.ResetPositions();
}
}
IIPCLogger *NewIPCSinkNew(const AuString &str, bool lengthPrefixed)

View File

@ -22,10 +22,10 @@ namespace Aurora::Logging::Sinks
void OnFlush() override;
private:
AuSPtr<Aurora::IO::IPC::IPCPipe> pipe_;
AuSPtr<Aurora::IO::IPC::IPCPipe> pPipe_;
AuByteBuffer logBuffer_;
AuThreadPrimitives::Mutex logMutex_;
bool lengthPrefixed_;
bool bLengthPrefixed_;
};
void NewIPCSinkRelease(IIPCLogger *logger);

View File

@ -0,0 +1,120 @@
/***
Copyright (C) 2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: StreamSink.cpp
Date: 2024-4-10
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "StreamSink.hpp"
namespace Aurora::Logging::Sinks
{
StreamSink::StreamSink(const AuSPtr<Aurora::IO::IStreamWriter> &pPipe,
bool bText,
bool bLengthPrefixed) :
pPipe_(pPipe),
bText_(bText),
bLengthPrefixed_(bLengthPrefixed)
{
}
void StreamSink::OnMessageBlocking(AuUInt8 uLevel, const ConsoleMessage &msg)
{
AU_LOCK_GUARD(this->logMutex_);
auto startOffset = this->logBuffer_.GetWriteOffset();
if (this->bLengthPrefixed_)
{
if (this->logBuffer_.readPtr != this->logBuffer_.base)
{
SysPushErrorConcurrentAborted();
return;
}
if (this->logBuffer_.base == this->logBuffer_.writePtr)
{
this->logBuffer_.Write<AuUInt32>(0);
}
}
if (this->bText_)
{
auto str = this->FormatMessageHelper(uLevel, msg, false, true, false);
this->logBuffer_.Write(str.data(), str.size());
#if defined(AURORA_IS_MODERNNT_DERIVED)
this->logBuffer_.Write("\r\n", 2);
#else
this->logBuffer_.Write("\n", 1);
#endif
}
else
{
msg.Write(this->logBuffer_);
}
if (!this->logBuffer_)
{
this->logBuffer_.writePtr = this->logBuffer_.base + startOffset;
}
}
bool StreamSink::OnMessageNonblocking(AuUInt8 level, const ConsoleMessage &msg)
{
return true;
}
void StreamSink::OnFlush()
{
AU_LOCK_GUARD(this->logMutex_);
if (!this->logBuffer_.RemainingBytes())
{
return;
}
if (this->bLengthPrefixed_ &&
this->logBuffer_.readPtr == this->logBuffer_.base)
{
auto len = this->logBuffer_.writePtr - this->logBuffer_.base;
this->logBuffer_.writePtr = this->logBuffer_.base;
this->logBuffer_.Write<AuUInt32>(len);
this->logBuffer_.writePtr = this->logBuffer_.base + len;
}
AuUInt uWritten {};
this->pPipe_->Write(AuMemoryViewStreamRead(this->logBuffer_, uWritten));
this->logBuffer_.readPtr += uWritten;
if (this->logBuffer_.readPtr == this->logBuffer_.writePtr)
{
this->logBuffer_.ResetPositions();
}
this->pPipe_->Flush();
}
void NewStreamSinkRelease(IFormattedSink *pLogger)
{
AuSafeDelete<StreamSink *>(pLogger);
}
IFormattedSink *NewStreamSinkNew(const AuSPtr<Aurora::IO::IStreamWriter> &pPipe,
bool bText,
bool bLengthPrefixed)
{
if (!pPipe)
{
SysPushErrorArg();
return {};
}
return _new StreamSink(pPipe,
bText,
bLengthPrefixed);
}
}

View File

@ -0,0 +1,39 @@
/***
Copyright (C) 2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: StreamSink.hpp
Date: 2024-4-10
Author: Reece
***/
#pragma once
#include "../AuFormatterContainer.hpp"
namespace Aurora::Logging::Sinks
{
struct StreamSink : IFormattedSink
{
StreamSink(const AuSPtr<Aurora::IO::IStreamWriter> &pPipe,
bool bText,
bool bLengthPrefixed);
void OnMessageBlocking(AuUInt8 level, const ConsoleMessage &msg) override;
bool OnMessageNonblocking(AuUInt8 level, const ConsoleMessage &msg) override;
void OnFlush() override;
private:
AuSPtr<Aurora::IO::IStreamWriter> pPipe_;
AuByteBuffer logBuffer_;
AuMutex logMutex_;
bool bLengthPrefixed_;
bool bText_;
ADD_FORMATTER_CONTAINER;
};
void NewStreamSinkRelease(IFormattedSink *pLogger);
IFormattedSink *NewStreamSinkNew(const AuSPtr<Aurora::IO::IStreamWriter> &pPipe,
bool bText,
bool bLengthPrefixed);
}