Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions cpp_utils/include/cpp_utils/logging/CustomStdLogConsumer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @file CustomStdLogConsumer.hpp
*/

#pragma once

#include <regex>

#include <cpp_utils/Log.hpp>
#include <cpp_utils/library/library_dll.h>

namespace eprosima {
namespace utils {

/**
* Custom Log Consumer with Standard (logical) behaviour.
*
* Registering this consumer in Fast DDS Log prints every log entry that has a higher kind than the threshold
* given. In case messages are not of Error kind, they are filtered by category to match a regex.
* Info messages are printed in std::cout while others are sent to std::cerr .
*
* @attention This consumer filters the entries that receives, but some other entries could be filtered beforehand
* by Fast DDS Log. To avoid these, set Log verbosity to Info and do not use Category Filter.
*/
class CustomStdLogConsumer : public utils::LogConsumer
{
public:

//! Create new CustomStdLogConsumer with regex filter generated from a string and with maximum verbosity kind.
CPP_UTILS_DllAPI CustomStdLogConsumer(
const std::string& log_filter,
const eprosima::fastdds::dds::Log::Kind& log_verbosity);

//! Default destructor
CPP_UTILS_DllAPI ~CustomStdLogConsumer() noexcept = default;

/**
* @brief Implements \c LogConsumer \c Consume method.
*
* Each entry must be equal or higher the verbosity level \c verbosity_ .
* Each entry category must match with regex stored in \c filter_ , except
* those entries that are Error will be always printed if \c verbosity_ is not Error.
*
* This method will print the \c entry in \c std::cout with info verbosity and in \c std:cerr otherwise.
*
* @param entry entry to consume
*/
CPP_UTILS_DllAPI void Consume(
const Log::Entry& entry) override;

protected:

//! Whether the entry must be accepted depending on kind and category
CPP_UTILS_DllAPI virtual bool accept_entry_(
const Log::Entry& entry);

/**
* @brief Get which stream must be used depending on the entry
*
* @param entry to decide the output stream
*
* @return \c std::out if entry is Info, \c std::cerr otherwise.
*/
CPP_UTILS_DllAPI virtual std::ostream& get_stream_(
const Log::Entry& entry);

//! Regex filter for entry category
std::regex filter_;

//! Maximum Log Kind that will be printed.
eprosima::fastdds::dds::Log::Kind verbosity_;
};

} /* namespace utils */
} /* namespace eprosima */
5 changes: 3 additions & 2 deletions cpp_utils/include/cpp_utils/macros/custom_enumeration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ namespace utils {
/* To string method */ \
inline const std::string& to_string(const enumeration_name& e) \
{ return names_ ## enumeration_name[static_cast<int>(e)]; } \
\
inline std::vector<std::string> string_vector_ ## enumeration_name() \
{ return std::vector<std::string> (names_ ## enumeration_name.begin(), names_ ## enumeration_name.end()); } \
\
/* From string */ \
inline enumeration_name from_string_ ## enumeration_name(const std::string& s) \
Expand All @@ -89,5 +92,3 @@ namespace utils {

} /* namespace utils */
} /* namespace eprosima */


87 changes: 87 additions & 0 deletions cpp_utils/src/cpp/logging/CustomStdLogConsumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @file CustomStdLogConsumer.cpp
*
*/

#include <cpp_utils/logging/CustomStdLogConsumer.hpp>

namespace eprosima {
namespace utils {

CustomStdLogConsumer::CustomStdLogConsumer(
const std::string& log_filter,
const eprosima::fastdds::dds::Log::Kind& log_verbosity)
: filter_(log_filter)
, verbosity_(log_verbosity)
{
// Do nothing
}

void CustomStdLogConsumer::Consume(
const utils::Log::Entry& entry)
{
if (accept_entry_(entry))
{
std::ostream& stream = get_stream_(entry);
print_timestamp(stream, entry, true);
print_header(stream, entry, true);
print_message(stream, entry, true);
print_context(stream, entry, true);
print_new_line(stream, true);
stream.flush();
}
}

bool CustomStdLogConsumer::accept_entry_(
const Log::Entry& entry)
{
// Filter by kind
if (entry.kind > verbosity_)
{
return false;
}
else if (entry.kind == eprosima::fastdds::dds::Log::Kind::Error &&
entry.kind < verbosity_)
{
// In case it is an error message and verbosity is not error, filter does not care
return true;
}

// Filter by regex
if (!std::regex_search(entry.context.category, filter_))
{
return false;
}

return true;
}

std::ostream& CustomStdLogConsumer::get_stream_(
const Log::Entry& entry)
{
if (entry.kind < eprosima::fastdds::dds::Log::Kind::Warning)
{
return std::cout;
}
else
{
return std::cerr;
}
}

} /* namespace utils */
} /* namespace eprosima */