119 lines
2.8 KiB
C++
119 lines
2.8 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: IPCSink.cpp
|
|
Date: 2022-6-16
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "IPCSink.hpp"
|
|
|
|
namespace Aurora::Logging::Sinks
|
|
{
|
|
IPCSink::IPCSink(const AuSPtr<Aurora::IO::IPC::IPCPipe> &pPipe,
|
|
bool bLengthPrefixed) :
|
|
pPipe_(pPipe),
|
|
bLengthPrefixed_(bLengthPrefixed)
|
|
{
|
|
|
|
}
|
|
|
|
bool IPCSink::Init()
|
|
{
|
|
return static_cast<bool>(this->pPipe_);
|
|
}
|
|
|
|
AuSPtr<IO::IPC::IPCPipe> IPCSink::ToPipe()
|
|
{
|
|
return this->pPipe_;
|
|
}
|
|
|
|
void IPCSink::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);
|
|
}
|
|
}
|
|
|
|
msg.Write(this->logBuffer_);
|
|
|
|
if (!this->logBuffer_)
|
|
{
|
|
this->logBuffer_.writePtr = this->logBuffer_.base + startOffset;
|
|
}
|
|
}
|
|
|
|
bool IPCSink::OnMessageNonblocking(AuUInt8 level, const ConsoleMessage &msg)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void IPCSink::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();
|
|
}
|
|
}
|
|
|
|
IIPCLogger *NewIPCSinkNew(const AuString &str, bool lengthPrefixed)
|
|
{
|
|
auto pipe = AuIPC::ImportPipe(str);
|
|
if (!pipe)
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
auto logger = _new IPCSink(pipe, lengthPrefixed);
|
|
if (!logger)
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
if (!logger->Init())
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
return logger;
|
|
}
|
|
|
|
void NewIPCSinkRelease(IIPCLogger *logger)
|
|
{
|
|
AuSafeDelete<IPCSink *>(logger);
|
|
}
|
|
} |