2018-12-10 05:12:10 +00:00
|
|
|
#include <hecl/Console.hpp>
|
|
|
|
|
2019-09-04 21:22:05 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-01-11 09:35:27 +00:00
|
|
|
#include "hecl/CVar.hpp"
|
2019-09-04 21:22:05 +00:00
|
|
|
#include "hecl/CVarManager.hpp"
|
2018-06-13 02:03:29 +00:00
|
|
|
#include "hecl/hecl.hpp"
|
2019-09-04 21:22:05 +00:00
|
|
|
|
|
|
|
#include <athena/Utility.hpp>
|
|
|
|
#include <boo/IWindow.hpp>
|
|
|
|
#include <boo/graphicsdev/IGraphicsCommandQueue.hpp>
|
|
|
|
#include <logvisor/logvisor.hpp>
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
namespace hecl {
|
2018-01-11 09:35:27 +00:00
|
|
|
Console* Console::m_instance = nullptr;
|
2018-12-08 05:18:42 +00:00
|
|
|
Console::Console(CVarManager* cvarMgr) : m_cvarMgr(cvarMgr), m_overwrite(false), m_cursorAtEnd(false) {
|
|
|
|
m_instance = this;
|
|
|
|
registerCommand("help", "Prints information about a given function", "<command>",
|
2019-10-21 04:53:12 +00:00
|
|
|
[this](Console* console, const std::vector<std::string>& args) { help(console, args); });
|
2018-12-08 05:18:42 +00:00
|
|
|
registerCommand("listCommands", "Prints a list of all available Commands", "",
|
2019-10-21 04:53:12 +00:00
|
|
|
[this](Console* console, const std::vector<std::string>& args) { listCommands(console, args); });
|
2018-12-08 05:18:42 +00:00
|
|
|
registerCommand("listCVars", "Lists all available CVars", "",
|
2019-10-21 04:53:12 +00:00
|
|
|
[this](Console* console, const std::vector<std::string>& args) { m_cvarMgr->list(console, args); });
|
|
|
|
registerCommand(
|
|
|
|
"setCVar", "Sets a given Console Variable to the specified value", "<cvar> <value>",
|
|
|
|
[this](Console* console, const std::vector<std::string>& args) { m_cvarMgr->setCVar(console, args); });
|
|
|
|
registerCommand(
|
|
|
|
"getCVar", "Prints the value stored in the specified Console Variable", "<cvar>",
|
|
|
|
[this](Console* console, const std::vector<std::string>& args) { m_cvarMgr->getCVar(console, args); });
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::registerCommand(std::string_view name, std::string_view helpText, std::string_view usage,
|
2019-10-21 05:03:27 +00:00
|
|
|
std::function<void(Console*, const std::vector<std::string>&)>&& func,
|
2018-12-08 05:18:42 +00:00
|
|
|
SConsoleCommand::ECommandFlags cmdFlags) {
|
2019-10-21 05:03:27 +00:00
|
|
|
std::string lowName{name};
|
2018-12-08 05:18:42 +00:00
|
|
|
athena::utility::tolower(lowName);
|
2019-10-21 05:03:27 +00:00
|
|
|
|
|
|
|
if (m_commands.find(lowName) != m_commands.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_commands.emplace(std::move(lowName), SConsoleCommand{std::string{name}, std::string{helpText}, std::string{usage},
|
|
|
|
std::move(func), cmdFlags});
|
2018-04-26 21:10:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::unregisterCommand(std::string_view name) {
|
2019-10-21 05:07:38 +00:00
|
|
|
std::string lowName{name};
|
2018-12-08 05:18:42 +00:00
|
|
|
athena::utility::tolower(lowName);
|
2019-10-21 05:07:38 +00:00
|
|
|
|
|
|
|
const auto iter = m_commands.find(lowName);
|
|
|
|
if (iter == m_commands.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_commands.erase(iter);
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::executeString(const std::string& str) {
|
|
|
|
if (str.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* First let's split semi-colon delimited commands */
|
|
|
|
std::vector<std::string> commands = athena::utility::split(str, ';');
|
|
|
|
|
|
|
|
if (commands.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (std::string command : commands) {
|
|
|
|
command = athena::utility::trim(command);
|
|
|
|
std::vector<std::string> tmpArgs = athena::utility::split(command, ' ');
|
|
|
|
if (tmpArgs.empty())
|
|
|
|
continue;
|
|
|
|
std::vector<std::string> args;
|
|
|
|
args.reserve(tmpArgs.size());
|
|
|
|
/* detect string literals */
|
|
|
|
bool isInLiteral = false;
|
|
|
|
std::string curLiteral;
|
|
|
|
int depth = 0;
|
|
|
|
for (std::string arg : tmpArgs) {
|
|
|
|
if ((arg.front() == '\'' || arg.front() == '"')) {
|
|
|
|
++depth;
|
|
|
|
isInLiteral = true;
|
|
|
|
curLiteral += arg;
|
|
|
|
} else if ((arg.back() == '\'' || arg.back() == '"') && isInLiteral) {
|
|
|
|
--depth;
|
|
|
|
curLiteral += arg;
|
|
|
|
args.push_back(curLiteral);
|
|
|
|
if (depth <= 0) {
|
|
|
|
depth = 0;
|
|
|
|
isInLiteral = false;
|
|
|
|
curLiteral.clear();
|
2018-04-30 03:11:37 +00:00
|
|
|
}
|
2019-10-21 05:26:19 +00:00
|
|
|
} else if (isInLiteral) {
|
2018-12-08 05:18:42 +00:00
|
|
|
curLiteral += arg;
|
2019-10-21 05:26:19 +00:00
|
|
|
} else {
|
|
|
|
args.push_back(std::move(arg));
|
|
|
|
}
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
if (isInLiteral) {
|
|
|
|
if ((curLiteral.back() != '\'' && curLiteral.back() != '"') || depth > 1) {
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Warning, FMT_STRING("Unterminated string literal"));
|
2018-01-11 09:35:27 +00:00
|
|
|
return;
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2019-10-21 05:26:19 +00:00
|
|
|
args.push_back(std::move(curLiteral));
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
std::string commandName = args[0];
|
|
|
|
args.erase(args.begin());
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
std::string lowComName = commandName;
|
|
|
|
athena::utility::tolower(lowComName);
|
2019-10-21 05:26:19 +00:00
|
|
|
if (const auto iter = m_commands.find(lowComName); iter != m_commands.end()) {
|
|
|
|
const SConsoleCommand& cmd = iter->second;
|
2018-12-08 05:18:42 +00:00
|
|
|
if (bool(cmd.m_flags & SConsoleCommand::ECommandFlags::Developer) && !com_developer->toBoolean()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Error, FMT_STRING("This command can only be executed in developer mode"), commandName);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
if (bool(cmd.m_flags & SConsoleCommand::ECommandFlags::Cheat) && !com_enableCheats->toBoolean()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Error, FMT_STRING("This command can only be executed with cheats enabled"), commandName);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-21 05:26:19 +00:00
|
|
|
cmd.m_func(this, args);
|
2018-12-08 05:18:42 +00:00
|
|
|
} else if (const CVar* cv = m_cvarMgr->findCVar(commandName)) {
|
2019-10-21 05:26:19 +00:00
|
|
|
args.insert(args.begin(), std::move(commandName));
|
2018-12-08 05:18:42 +00:00
|
|
|
if (args.size() > 1)
|
|
|
|
m_cvarMgr->setCVar(this, args);
|
|
|
|
else
|
|
|
|
m_cvarMgr->getCVar(this, args);
|
2019-10-21 05:26:19 +00:00
|
|
|
} else {
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Error, FMT_STRING("'{}' is not a valid command or variable!"), commandName);
|
2019-10-21 05:26:19 +00:00
|
|
|
}
|
2019-10-25 05:49:41 +00:00
|
|
|
}
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::help(Console* /*con*/, const std::vector<std::string>& args) {
|
|
|
|
if (args.empty()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Info, FMT_STRING("Expected usage: help <command>"));
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string cmd = args.front();
|
|
|
|
athena::utility::tolower(cmd);
|
|
|
|
auto it = m_commands.find(cmd);
|
|
|
|
if (it == m_commands.end()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Error, FMT_STRING("No such command '{}'"), args.front());
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Info, FMT_STRING("{}: {}"), it->second.m_displayName, it->second.m_helpString);
|
2018-12-08 05:18:42 +00:00
|
|
|
if (!it->second.m_usage.empty())
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Info, FMT_STRING("Usage: {} {}"), it->second.m_displayName, it->second.m_usage);
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::listCommands(Console* /*con*/, const std::vector<std::string>& /*args*/) {
|
|
|
|
for (const auto& comPair : m_commands)
|
2020-04-11 22:48:11 +00:00
|
|
|
report(Level::Info, FMT_STRING("'{}': {}"), comPair.second.m_displayName, comPair.second.m_helpString);
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 05:30:16 +00:00
|
|
|
bool Console::commandExists(std::string_view cmd) const {
|
2019-10-21 05:28:20 +00:00
|
|
|
std::string cmdName{cmd};
|
2018-12-08 05:18:42 +00:00
|
|
|
athena::utility::tolower(cmdName);
|
2018-01-11 16:26:33 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return m_commands.find(cmdName) != m_commands.end();
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 04:22:58 +00:00
|
|
|
void Console::vreport(Level level, fmt::string_view fmt, fmt::format_args args) {
|
|
|
|
std::string tmp = fmt::vformat(fmt, args);
|
2018-12-08 05:18:42 +00:00
|
|
|
std::vector<std::string> lines = athena::utility::split(tmp, '\n');
|
2019-10-21 05:37:04 +00:00
|
|
|
for (std::string& line : lines) {
|
|
|
|
m_log.emplace_back(std::move(line), level);
|
|
|
|
}
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("{}\n"), tmp);
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:12:10 +00:00
|
|
|
void Console::init(boo::IWindow* window) {
|
|
|
|
m_window = window;
|
|
|
|
}
|
2018-01-14 02:39:32 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::proc() {
|
|
|
|
if (m_state == State::Opened) {
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("\r{} "), m_commandString);
|
2018-12-08 05:18:42 +00:00
|
|
|
fflush(stdout);
|
|
|
|
} else if (m_state == State::Opening)
|
|
|
|
m_state = State::Opened;
|
2018-12-10 05:12:10 +00:00
|
|
|
else if (m_state == State::Closing) {
|
2018-12-08 05:18:42 +00:00
|
|
|
m_state = State::Closed;
|
2018-12-10 05:12:10 +00:00
|
|
|
m_commandString.clear();
|
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
if (m_cursorPosition > int(m_commandString.size() - 1))
|
|
|
|
m_cursorPosition = int(m_commandString.size() - 1);
|
|
|
|
if (m_cursorPosition < -1)
|
|
|
|
m_cursorPosition = -1;
|
|
|
|
|
|
|
|
if (m_logOffset > int(m_log.size() - 1))
|
|
|
|
m_logOffset = int(m_log.size() - 1);
|
|
|
|
if (m_logOffset < 0)
|
|
|
|
m_logOffset = 0;
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:12:10 +00:00
|
|
|
void Console::draw(boo::IGraphicsCommandQueue* /* gfxQ */) {
|
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
void Console::handleCharCode(unsigned long chr, boo::EModifierKey /*mod*/, bool /*repeat*/) {
|
|
|
|
if (chr == U'`' || chr == U'~') {
|
|
|
|
if (m_state == State::Closed || m_state == State::Closing)
|
|
|
|
m_state = State::Opening;
|
|
|
|
else
|
|
|
|
m_state = State::Closing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == State::Opened) {
|
|
|
|
if (!m_commandString.empty() && m_cursorPosition + 1 < int(m_commandString.size())) {
|
|
|
|
if (m_overwrite)
|
|
|
|
m_commandString[unsigned(m_cursorPosition + 1)] = char(chr);
|
|
|
|
else
|
|
|
|
m_commandString.insert(m_commandString.begin() + m_cursorPosition + 1, char(chr));
|
|
|
|
} else
|
|
|
|
m_commandString += char(chr);
|
|
|
|
|
|
|
|
++m_cursorPosition;
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 02:39:32 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::handleSpecialKeyDown(boo::ESpecialKey sp, boo::EModifierKey mod, bool /*repeat*/) {
|
2019-10-21 05:53:03 +00:00
|
|
|
if (m_state != State::Opened) {
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
2019-10-21 05:53:03 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
switch (sp) {
|
|
|
|
case boo::ESpecialKey::Insert:
|
|
|
|
m_overwrite ^= 1;
|
|
|
|
break;
|
|
|
|
case boo::ESpecialKey::Backspace: {
|
|
|
|
if (!m_commandString.empty()) {
|
2019-04-03 04:29:04 +00:00
|
|
|
if (True(mod & boo::EModifierKey::Ctrl)) {
|
2018-12-08 05:18:42 +00:00
|
|
|
size_t index = m_commandString.rfind(' ', size_t(m_cursorPosition - 1));
|
|
|
|
|
|
|
|
if (index == std::string::npos) {
|
|
|
|
m_commandString.clear();
|
|
|
|
m_cursorPosition = -1;
|
|
|
|
} else {
|
|
|
|
m_commandString.erase(index, (index - m_commandString.size()));
|
|
|
|
m_cursorPosition = int(index);
|
2018-01-14 02:39:32 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
|
|
|
if (m_cursorPosition < 0)
|
2018-01-14 02:39:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
m_commandString.erase(size_t(m_cursorPosition), 1);
|
|
|
|
--m_cursorPosition;
|
2018-01-14 02:39:32 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::Delete: {
|
|
|
|
if (!m_commandString.empty()) {
|
|
|
|
// Don't try to delete if the cursor is at the end of the line
|
|
|
|
if ((m_cursorPosition + 1) >= int(m_commandString.size()))
|
2018-01-14 02:39:32 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-03 04:29:04 +00:00
|
|
|
if (True(mod & boo::EModifierKey::Ctrl)) {
|
2018-12-08 05:18:42 +00:00
|
|
|
size_t index = m_commandString.find_first_of(' ', size_t(m_cursorPosition + 1));
|
|
|
|
if (index != std::string::npos)
|
|
|
|
m_commandString.erase(size_t(m_cursorPosition + 1), index + 1);
|
|
|
|
else
|
|
|
|
m_commandString.erase(size_t(m_cursorPosition + 1), size_t(m_cursorPosition + 1) - m_commandString.size());
|
2018-01-14 02:39:32 +00:00
|
|
|
break;
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
|
|
|
m_commandString.erase(size_t(m_cursorPosition + 1), 1);
|
2018-01-14 02:39:32 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::PgUp: {
|
|
|
|
if (m_logOffset < int(m_log.size() - m_maxLines) - 1)
|
|
|
|
m_logOffset++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::PgDown: {
|
|
|
|
if (m_logOffset > 0)
|
|
|
|
m_logOffset--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::Enter: {
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("\n"));
|
2018-12-08 05:18:42 +00:00
|
|
|
executeString(m_commandString);
|
|
|
|
m_cursorPosition = -1;
|
|
|
|
m_commandHistory.insert(m_commandHistory.begin(), m_commandString);
|
|
|
|
m_commandString.clear();
|
|
|
|
m_showCursor = true;
|
|
|
|
m_cursorTime = 0.f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::Left: {
|
|
|
|
if (m_cursorPosition < 0)
|
|
|
|
break;
|
|
|
|
|
2019-04-03 04:29:04 +00:00
|
|
|
if (True(mod & boo::EModifierKey::Ctrl))
|
2018-12-08 05:18:42 +00:00
|
|
|
m_cursorPosition = int(m_commandString.rfind(' ', size_t(m_cursorPosition) - 1));
|
|
|
|
else
|
|
|
|
m_cursorPosition--;
|
|
|
|
|
|
|
|
m_showCursor = true;
|
|
|
|
m_cursorTime = 0.f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::Right: {
|
|
|
|
if (m_cursorPosition >= int(m_commandString.size() - 1))
|
|
|
|
break;
|
|
|
|
|
2019-04-03 04:29:04 +00:00
|
|
|
if (True(mod & boo::EModifierKey::Ctrl)) {
|
2018-12-08 05:18:42 +00:00
|
|
|
if (m_commandString[size_t(m_cursorPosition)] == ' ')
|
|
|
|
m_cursorPosition++;
|
|
|
|
|
|
|
|
size_t tmpPos = m_commandString.find(' ', size_t(m_cursorPosition));
|
|
|
|
if (tmpPos == std::string::npos)
|
2018-01-14 07:37:00 +00:00
|
|
|
m_cursorPosition = int(m_commandString.size() - 1);
|
2018-12-08 05:18:42 +00:00
|
|
|
else
|
|
|
|
m_cursorPosition = int(tmpPos);
|
|
|
|
} else
|
|
|
|
m_cursorPosition++;
|
|
|
|
|
|
|
|
m_showCursor = true;
|
|
|
|
m_cursorTime = 0.f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case boo::ESpecialKey::Up: {
|
|
|
|
if (m_commandHistory.size() == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
m_currentCommand++;
|
|
|
|
|
|
|
|
if (m_currentCommand > int(m_commandHistory.size() - 1))
|
|
|
|
m_currentCommand = int(m_commandHistory.size() - 1);
|
|
|
|
|
|
|
|
m_commandString = m_commandHistory[size_t(m_currentCommand)];
|
|
|
|
m_cursorPosition = int(m_commandString.size() - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::Down: {
|
|
|
|
if (m_commandHistory.empty())
|
|
|
|
break;
|
|
|
|
m_currentCommand--;
|
|
|
|
if (m_currentCommand >= 0) {
|
|
|
|
m_commandString = m_commandHistory[size_t(m_currentCommand)];
|
|
|
|
} else if (m_currentCommand <= -1) {
|
|
|
|
m_currentCommand = -1;
|
|
|
|
m_commandString.clear();
|
2018-01-14 02:39:32 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
m_cursorPosition = int(m_commandString.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case boo::ESpecialKey::Home:
|
|
|
|
m_cursorPosition = -1;
|
|
|
|
break;
|
|
|
|
case boo::ESpecialKey::End:
|
|
|
|
m_cursorPosition = int(m_commandString.size() - 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::handleSpecialKeyUp(boo::ESpecialKey /*sp*/, boo::EModifierKey /*mod*/) {}
|
2018-01-11 16:26:33 +00:00
|
|
|
|
2019-07-20 04:22:58 +00:00
|
|
|
void Console::LogVisorAdapter::report(const char* modName, logvisor::Level severity,
|
|
|
|
fmt::string_view format, fmt::format_args args) {
|
2021-04-06 16:55:23 +00:00
|
|
|
auto tmp = fmt::vformat(format, args);
|
2018-12-08 05:18:42 +00:00
|
|
|
std::vector<std::string> lines = athena::utility::split(tmp, '\n');
|
|
|
|
for (const std::string& line : lines) {
|
2020-04-11 22:48:11 +00:00
|
|
|
auto v = fmt::format(FMT_STRING("[{}] {}"), modName, line);
|
2019-08-24 07:17:58 +00:00
|
|
|
m_con->m_log.emplace_back(std::move(v), Console::Level(severity));
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 04:22:58 +00:00
|
|
|
void Console::LogVisorAdapter::report(const char* modName, logvisor::Level severity,
|
|
|
|
fmt::wstring_view format, fmt::wformat_args args) {
|
2021-04-06 16:55:23 +00:00
|
|
|
auto tmp = fmt::vformat(format, args);
|
2018-12-08 05:18:42 +00:00
|
|
|
std::vector<std::string> lines = athena::utility::split(athena::utility::wideToUtf8(tmp), '\n');
|
|
|
|
for (const std::string& line : lines) {
|
2020-04-11 22:48:11 +00:00
|
|
|
auto v = fmt::format(FMT_STRING("[{}] {}"), modName, line);
|
2019-08-24 07:17:58 +00:00
|
|
|
m_con->m_log.emplace_back(std::move(v), Console::Level(severity));
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::LogVisorAdapter::reportSource(const char* modName, logvisor::Level severity, const char* file,
|
2019-07-20 04:22:58 +00:00
|
|
|
unsigned linenum, fmt::string_view format, fmt::format_args args) {
|
2021-04-06 16:55:23 +00:00
|
|
|
auto tmp = fmt::vformat(format, args);
|
2020-04-11 22:48:11 +00:00
|
|
|
auto v = fmt::format(FMT_STRING("[{}] {} {}:{}"), modName, tmp, file, linenum);
|
2019-08-24 07:17:58 +00:00
|
|
|
m_con->m_log.emplace_back(std::move(v), Console::Level(severity));
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::LogVisorAdapter::reportSource(const char* modName, logvisor::Level severity, const char* file,
|
2019-07-20 04:22:58 +00:00
|
|
|
unsigned linenum, fmt::wstring_view format, fmt::wformat_args args) {
|
2021-04-06 16:55:23 +00:00
|
|
|
auto tmp = fmt::vformat(format, args);
|
2018-12-08 05:18:42 +00:00
|
|
|
std::vector<std::string> lines = athena::utility::split(athena::utility::wideToUtf8(tmp), '\n');
|
|
|
|
for (const std::string& line : lines) {
|
2020-04-11 22:48:11 +00:00
|
|
|
auto v = fmt::format(FMT_STRING("[{}] {} {}:{}"), modName, line, file, linenum);
|
2019-08-24 07:17:58 +00:00
|
|
|
m_con->m_log.emplace_back(std::move(v), Console::Level(severity));
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-01-11 16:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::dumpLog() {
|
|
|
|
for (const auto& l : m_log) {
|
|
|
|
switch (l.second) {
|
|
|
|
case Level::Info:
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("{}\n"), l.first);
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
|
|
|
case Level::Warning:
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("[Warning] {}\n"), l.first);
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
|
|
|
case Level::Error:
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("[ Error ] {}\n"), l.first);
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
|
|
|
case Level::Fatal:
|
2020-04-11 22:48:11 +00:00
|
|
|
fmt::print(FMT_STRING("[ Fatal ] {}\n"), l.first);
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void Console::RegisterLogger(Console* con) { logvisor::MainLoggers.emplace_back(new LogVisorAdapter(con)); }
|
2018-01-11 16:26:33 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
Console* Console::instance() { return m_instance; }
|
|
|
|
} // namespace hecl
|