diff --git a/src/PowerPlant.cpp b/src/PowerPlant.cpp index 89f30ea6..deabde45 100644 --- a/src/PowerPlant.cpp +++ b/src/PowerPlant.cpp @@ -49,7 +49,7 @@ PowerPlant* PowerPlant::powerplant = nullptr; // This is taking argc and argv as given by main so this should not take an array // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) PowerPlant::PowerPlant(Configuration config, int argc, const char* argv[]) - : scheduler(config.default_pool_concurrency) { + : scheduler(config.default_pool_concurrency), logger(*this) { // Stop people from making more then one powerplant if (powerplant != nullptr) { @@ -103,21 +103,6 @@ void PowerPlant::submit(std::unique_ptr&& task) noexcep scheduler.submit(std::move(task)); } -void PowerPlant::log(const LogLevel& level, std::string message) { - // Get the current task - const auto* current_task = threading::ReactionTask::get_current_task(); - - // Inline emit the log message to default handlers to pause the current task until the log message is processed - emit(std::make_unique( - level, - current_task != nullptr ? current_task->parent->reactor.log_level : LogLevel::UNKNOWN, - std::move(message), - current_task != nullptr ? current_task->statistics : nullptr)); -} -void PowerPlant::log(const LogLevel& level, std::stringstream& message) { - log(level, message.str()); -} - void PowerPlant::shutdown(bool force) { // Emit our shutdown event diff --git a/src/PowerPlant.hpp b/src/PowerPlant.hpp index c7dc6969..2b783c01 100644 --- a/src/PowerPlant.hpp +++ b/src/PowerPlant.hpp @@ -38,6 +38,7 @@ #include "threading/ReactionTask.hpp" #include "threading/scheduler/Scheduler.hpp" #include "util/FunctionFusion.hpp" +#include "util/Logger.hpp" #include "util/demangle.hpp" namespace NUClear { @@ -188,32 +189,41 @@ class PowerPlant { * Logs a message through the system so the various log handlers can access it. * The arguments being logged should be able to be added into a stringstream. * - * @tparam level The level to log at (defaults to DEBUG) + * @tparam level The level to log at * @tparam Arguments The types of the arguments we are logging * * @param args The arguments we are logging */ template void log(Arguments&&... args) { - log(level, std::forward(args)...); + logger.log(nullptr, level, std::forward(args)...); } template void log(const LogLevel& level, Arguments&&... args) { - std::stringstream ss; - log(level, ss, std::forward(args)...); + logger.log(nullptr, level, std::forward(args)...); } - template - void log(const LogLevel& level, std::stringstream& ss, First&& first, Arguments&&... args) { - ss << std::forward(first) << " "; - log(level, ss, std::forward(args)...); + + /** + * Log a message through NUClear's system. + * + * This version of log takes a pointer to a reactor as an argument. + * When logging from a reactor's log function, even if it's not logging within a reactor needs the context to add + * the extra information about the reactors name. + * + * @tparam level The level to log at (defaults to DEBUG) + * @tparam Arguments The types of the arguments we are logging + * + * @param reactor The reactor that is logging + * @param args The arguments we are logging + */ + template + void log(const Reactor* reactor, Arguments&&... args) { + logger.log(reactor, level, std::forward(args)...); } - template - void log(const LogLevel& level, std::stringstream& ss, Last&& last) { - ss << std::forward(last); - log(level, ss); + template + void log(const Reactor* reactor, const LogLevel& level, Arguments&&... args) { + logger.log(reactor, level, std::forward(args)...); } - void log(const LogLevel& level, std::stringstream& message); - void log(const LogLevel& level, std::string message); /** * Emits data to the system and routes it to the other systems that use it. @@ -235,8 +245,7 @@ class PowerPlant { emit(std::move(data)); } template