Skip to content

Commit

Permalink
Make NUClear logs always emit (#91)
Browse files Browse the repository at this point in the history
For many log handlers, the display level is important as only the logs
you want to view should be displayed.
This display level has been typically set using the log level in each
reactor and nuclear has filtered out logs below the display level from
reaching the log handlers.
However, some log handlers are able to store and process logs more
intelligently and make them available to access beyond their set display
level.
  • Loading branch information
TrentHouliston authored Sep 25, 2023
1 parent 68e154a commit 2fbf7e3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class PowerPlant;
*/
class Environment {
public:
Environment(PowerPlant& powerplant, std::string reactor_name, const LogLevel& log_level)
: powerplant(powerplant), reactor_name(std::move(reactor_name)), log_level(log_level) {}
Environment(PowerPlant& powerplant, std::string reactor_name)
: powerplant(powerplant), reactor_name(std::move(reactor_name)) {}

private:
friend class PowerPlant;
Expand All @@ -55,8 +55,6 @@ class Environment {
PowerPlant& powerplant;
/// @brief The name of the reactor
std::string reactor_name;
/// @brief The log level for this reactor
LogLevel log_level;
};

} // namespace NUClear
Expand Down
9 changes: 9 additions & 0 deletions src/LogLevel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ namespace NUClear {
* The logging level of a reactor can be changed by setting it in the install function.
*/
enum LogLevel {
/**
* @brief
* Don't use this log level when emitting logs, it is for setting reactor log level from non reactor sources.
*
* Specifically when a NUClear::log is called from code that is not running in a reaction (even transitively) then
* the reactor_level will be set to UNKNOWN.
*/
UNKNOWN,

/**
* @brief
* The Trace level contains messages that are used to trace the exact flow of execution.
Expand Down
2 changes: 1 addition & 1 deletion src/PowerPlant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class PowerPlant {
* @tparam T The type of the reactor to build and install
* @tparam level The initial logging level for this reactor to use
*/
template <typename T, enum LogLevel level = DEBUG>
template <typename T>
void install();

/**
Expand Down
23 changes: 11 additions & 12 deletions src/PowerPlant.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ inline PowerPlant::PowerPlant(Configuration config, int argc, const char* argv[]
emit(std::make_unique<message::CommandLineArguments>(args));
}

template <typename T, enum LogLevel level>
template <typename T>
void PowerPlant::install() {

// Make sure that the class that we received is a reactor
static_assert(std::is_base_of<Reactor, T>::value, "You must install Reactors");

// The reactor constructor should handle subscribing to events
reactors.push_back(
std::make_unique<T>(std::make_unique<Environment>(*this, util::demangle(typeid(T).name()), level)));
reactors.push_back(std::make_unique<T>(std::make_unique<Environment>(*this, util::demangle(typeid(T).name()))));
}

// Default emit with no types
Expand Down Expand Up @@ -172,16 +171,16 @@ void PowerPlant::log(Arguments&&... args) {
// Get the current task
const auto* current_task = threading::ReactionTask::get_current_task();

// Only log if we are not from a reaction, or if our reactor's log level is high enough
if (current_task == nullptr || level >= current_task->parent.reactor.log_level) {
// Build our log message by concatenating everything to a stream
std::stringstream output_stream;
log_impl(output_stream, std::forward<Arguments>(args)...);
// Build our log message by concatenating everything to a stream
std::stringstream output_stream;
log_impl(output_stream, std::forward<Arguments>(args)...);

// Direct emit the log message so that any direct loggers can use it
powerplant->emit<dsl::word::emit::Direct>(std::make_unique<message::LogMessage>(
message::LogMessage{level, output_stream.str(), current_task != nullptr ? current_task->stats : nullptr}));
}
// Direct emit the log message so that any direct loggers can use it
powerplant->emit<dsl::word::emit::Direct>(std::make_unique<message::LogMessage>(
level,
current_task != nullptr ? current_task->parent.reactor.log_level : LogLevel::UNKNOWN,
output_stream.str(),
current_task != nullptr ? current_task->stats : nullptr));
}

} // namespace NUClear
6 changes: 2 additions & 4 deletions src/Reactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ class Reactor {
friend class PowerPlant;

explicit Reactor(std::unique_ptr<Environment> environment)
: powerplant(environment->powerplant)
, reactor_name(environment->reactor_name)
, log_level(environment->log_level) {}
: powerplant(environment->powerplant), reactor_name(environment->reactor_name) {}
Reactor(const Reactor& /*other*/) = default;
Reactor(Reactor&& /*other*/) noexcept = default;
Reactor& operator=(const Reactor& /*rhs*/) = delete;
Expand Down Expand Up @@ -413,7 +411,7 @@ class Reactor {
* @param args The arguments we are logging
*/
template <enum LogLevel level = DEBUG, typename... Arguments>
void log(Arguments&&... args) {
void log(Arguments&&... args) const {

// If the log is above or equal to our log level
PowerPlant::log<level>(std::forward<Arguments>(args)...);
Expand Down
17 changes: 17 additions & 0 deletions src/message/LogMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,26 @@ namespace message {
*/
struct LogMessage {

/**
* @brief Construct a new Log Message object
*
* @param level the logging level of the log
* @param display_level the logging level of the reactor that made this log
* @param message the string contents of the message
* @param task the currently executing task that made this message or nullptr if not in a task
*/
LogMessage(const LogLevel& level,
const LogLevel& display_level,
std::string message,
std::shared_ptr<ReactionStatistics> task)
: level(level), display_level(display_level), message(std::move(message)), task(std::move(task)) {}

/// @brief The logging level of the log.
LogLevel level{};

/// @brief The logging level of the reactor that made the log (the level to display at).
LogLevel display_level{};

/// @brief The string contents of the message.
std::string message{};

Expand Down
4 changes: 3 additions & 1 deletion tests/log/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class TestReactor : public NUClear::Reactor {

// Capture the log messages
on<Trigger<NUClear::message::LogMessage>>().then([](const NUClear::message::LogMessage& log_message) {
messages.push_back(LogTestOutput{log_message.message, log_message.level, log_message.task != nullptr});
if (log_message.level >= log_message.display_level) {
messages.push_back(LogTestOutput{log_message.message, log_message.level, log_message.task != nullptr});
}
});

// Run each test
Expand Down

0 comments on commit 2fbf7e3

Please sign in to comment.