Skip to content

Commit

Permalink
Provide a more general log push function (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael X. Grey <grey@openrobotics.org>
  • Loading branch information
mxgrey committed Mar 9, 2022
1 parent 4fdead0 commit d3e4a11
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
37 changes: 20 additions & 17 deletions rmf_task/include/rmf_task/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ class Log
class View;
class Reader;

/// A computer-friendly ranking of how serious the log entry is.
enum class Tier : uint32_t
{
/// This is a sentinel value that should not generally be used.
Uninitialized = 0,

/// An expected occurrence took place.
Info,

/// An unexpected, problematic occurrence took place, but it can be
/// recovered from. Human attention is recommended but not necessary.
Warning,

/// A problem happened, and humans should be alerted.
Error
};

/// Construct a log.
///
/// \param[in] clock
Expand All @@ -58,6 +75,9 @@ class Log
/// Add an error to the log.
void error(std::string text);

/// Push an entry of the specified severity.
void push(Tier tier, std::string text);

/// Insert an arbitrary entry into the log.
void insert(Log::Entry entry);

Expand All @@ -77,23 +97,6 @@ class Log::Entry
{
public:

/// A computer-friendly ranking of how serious the log entry is.
enum class Tier : uint32_t
{
/// This is a sentinel value that should not generally be used.
Uninitialized = 0,

/// An expected occurrence took place.
Info,

/// An unexpected, problematic occurrence took place, but it can be
/// recovered from. Human attention is recommended but not necessary.
Warning,

/// A problem happened, and humans should be alerted.
Error
};

/// What was the tier of this entry.
Tier tier() const;

Expand Down
25 changes: 18 additions & 7 deletions rmf_task/src/rmf_task/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <rmf_task/Log.hpp>

#include <stdexcept>
#include <list>

namespace rmf_task {
Expand Down Expand Up @@ -245,25 +246,35 @@ Log::Log(std::function<rmf_traffic::Time()> clock)
//==============================================================================
void Log::info(std::string text)
{
_pimpl->entries->emplace_back(
Entry::Implementation::make(
Entry::Tier::Info, _pimpl->seq++, _pimpl->clock(), std::move(text)));
push(Tier::Info, std::move(text));
}

//==============================================================================
void Log::warn(std::string text)
{
_pimpl->entries->emplace_back(
Entry::Implementation::make(
Entry::Tier::Warning, _pimpl->seq++, _pimpl->clock(), std::move(text)));
push(Tier::Warning, std::move(text));
}

//==============================================================================
void Log::error(std::string text)
{
push(Tier::Error, std::move(text));
}

//==============================================================================
void Log::push(Tier tier, std::string text)
{
if (Tier::Uninitialized == tier)
{
// *INDENT-OFF*
throw std::runtime_error(
"[Log::push] Tier was set to Uninitialized, which is illegal.");
// *INDENT-ON*
}

_pimpl->entries->emplace_back(
Entry::Implementation::make(
Entry::Tier::Error, _pimpl->seq++, _pimpl->clock(), std::move(text)));
tier, _pimpl->seq++, _pimpl->clock(), std::move(text)));
}

//==============================================================================
Expand Down

0 comments on commit d3e4a11

Please sign in to comment.