AuroraRuntime/Source/Console/Hooks/Hooks.cpp

49 lines
1.2 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Hooks.cpp
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "Hooks.hpp"
namespace Aurora::Console::Hooks
{
static auto gMutex = Threading::Primitives::MutexUnique();
static AuList<Hooks::LineHook_cb> gLineCallbacks;
void AddHook(LineHook_cb hook)
{
Aurora::Threading::WaitableLockGuard guard(gMutex.get());
gLineCallbacks.push_back(hook);
}
void WriteLine(const ConsoleMessage &msg)
{
gMutex->Lock();
auto callbacks = gLineCallbacks;
gMutex->Unlock();
if (msg.line.find('\n') == std::string::npos) [[likely]]
{
for (const auto &callback : callbacks)
{
callback(msg);
}
}
2021-06-30 12:00:32 +00:00
else [[unlikely]]
2021-06-27 21:25:29 +00:00
{
Aurora::Parse::SplitNewlines(msg.line,
[&](const AuString &line)
{
ConsoleMessage dup = msg;
dup.line = line;
for (const auto &callback : callbacks)
{
callback(dup);
}
});
}
}
}