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
14 changes: 7 additions & 7 deletions cpp_utils/include/cpp_utils/logging/CustomStdLogConsumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

#include <regex>

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

namespace eprosima {
namespace utils {
Expand All @@ -40,10 +41,9 @@ class CustomStdLogConsumer : public utils::LogConsumer
{
public:

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

//! Default destructor
CPP_UTILS_DllAPI ~CustomStdLogConsumer() noexcept = default;
Expand Down Expand Up @@ -78,11 +78,11 @@ class CustomStdLogConsumer : public utils::LogConsumer
CPP_UTILS_DllAPI virtual std::ostream& get_stream_(
const Log::Entry& entry);

//! Regex filter for entry category
std::regex filter_;
//! Regex filter for entry category or message.
LogFilter filter_;

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

} /* namespace utils */
Expand Down
100 changes: 100 additions & 0 deletions cpp_utils/include/cpp_utils/logging/LogConfiguration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2024 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\.

#pragma once

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

#include <map>
#include <ostream>
#include <string>

namespace eprosima {
namespace utils {

using VerbosityKind = Log::Kind;
using LogFilter = std::map<VerbosityKind, Fuzzy<std::string>>;

/**
* The collection of settings related to Logging.
*
* The Logging settings are:
* - Verbosity
* - Filter
*/
struct LogConfiguration
{
/////////////////////////
// CONSTRUCTORS
/////////////////////////

//! Default LogConfiguration constructor
CPP_UTILS_DllAPI LogConfiguration();

/////////////////////////
// METHODS
/////////////////////////

/**
* @brief \c is_valid method.
*/
CPP_UTILS_DllAPI
virtual bool is_valid(
Formatter& error_msg) const noexcept;

/**
* @brief Replace verbosity with a given log_verbosity if verbosity has a lower fuzzy level.
*/
CPP_UTILS_DllAPI
void set(
const utils::Fuzzy<VerbosityKind>& log_verbosity) noexcept;

/**
* @brief Replace filter with a given log_filter if filter has a lower fuzzy level.
*/
CPP_UTILS_DllAPI
void set(
const LogFilter& log_filter);

/////////////////////////
// VARIABLES
/////////////////////////

//! Verbosity kind
Fuzzy<VerbosityKind> verbosity;
//! Log Filter
LogFilter filter;

};

/**
* @brief \c VerbosityKind to stream Fuzzy level
*/
CPP_UTILS_DllAPI
std::ostream& operator <<(
std::ostream& os,
const Fuzzy<VerbosityKind>& kind);

/**
* @brief \c LogFilter to stream serialization
*/
CPP_UTILS_DllAPI
std::ostream& operator <<(
std::ostream& os,
const LogFilter& filter);

} /* namespace utils */
} /* namespace eprosima */
36 changes: 13 additions & 23 deletions cpp_utils/src/cpp/logging/CustomStdLogConsumer.cpp
Comment thread
tempate marked this conversation as resolved.
Comment thread
tempate marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ 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)
const LogConfiguration* log_configuration)
: filter_(log_configuration->filter)
, verbosity_(log_configuration->verbosity)
{
// Do nothing
}

void CustomStdLogConsumer::Consume(
const utils::Log::Entry& entry)
const Log::Entry& entry)
{
if (accept_entry_(entry))
{
Expand All @@ -49,31 +48,22 @@ void CustomStdLogConsumer::Consume(
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;
}
std::regex filter_regex(filter_[entry.kind].get_value());
const bool is_category_valid = std::regex_search(entry.context.category, filter_regex);
const bool is_message_valid = std::regex_search(entry.message, filter_regex);
const bool is_content_valid = is_category_valid || is_message_valid;

// Filter by kind
const bool is_kind_valid = entry.kind <= verbosity_;

return true;
return is_kind_valid && is_content_valid;
}

std::ostream& CustomStdLogConsumer::get_stream_(
const Log::Entry& entry)
{
if (entry.kind < eprosima::fastdds::dds::Log::Kind::Warning)
if (entry.kind < VerbosityKind::Warning)
{
return std::cout;
}
Expand Down
90 changes: 90 additions & 0 deletions cpp_utils/src/cpp/logging/LogConfiguration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2024 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 LogConfiguration.cpp
*
*/

#include <cpp_utils/logging/LogConfiguration.hpp>
#include <cpp_utils/utils.hpp>

namespace eprosima {
namespace utils {


LogConfiguration::LogConfiguration()
: verbosity{VerbosityKind::Warning, FuzzyLevelValues::fuzzy_level_default}
{
filter[VerbosityKind::Info].set_value("", FuzzyLevelValues::fuzzy_level_default);
filter[VerbosityKind::Warning].set_value("", FuzzyLevelValues::fuzzy_level_default);
filter[VerbosityKind::Error].set_value("", FuzzyLevelValues::fuzzy_level_default);
}

bool LogConfiguration::is_valid(
Formatter& error_msg) const noexcept
{
return true;
}

void LogConfiguration::set(
const utils::Fuzzy<VerbosityKind>& log_verbosity) noexcept
{
if (verbosity.get_level() <= log_verbosity.get_level())
{
verbosity = log_verbosity;
}
}

void LogConfiguration::set(
const LogFilter& log_filter)
{
if (filter[VerbosityKind::Error].get_level() <= log_filter.at(VerbosityKind::Error).get_level())
{
filter[VerbosityKind::Error] = log_filter.at(VerbosityKind::Error);
}

if (filter[VerbosityKind::Warning].get_level() <= log_filter.at(VerbosityKind::Warning).get_level())
{
filter[VerbosityKind::Warning] = log_filter.at(VerbosityKind::Warning);
}

if (filter[VerbosityKind::Info].get_level() <= log_filter.at(VerbosityKind::Info).get_level())
{
filter[VerbosityKind::Info] = log_filter.at(VerbosityKind::Info);
}
}

std::ostream& operator <<(
std::ostream& os,
const Fuzzy<VerbosityKind>& kind)
{
os << "Fuzzy{Level(" << kind.get_level_as_str() << ") " << kind.get_reference() << "}";
return os;
}

std::ostream& operator <<(
std::ostream& os,
const LogFilter& filter)
{
os << "Log Filter: {Kind: Error, Regex: " << filter.at(VerbosityKind::Error).get_value() << "}; "
<< "{Kind: Warning, Regex: " << filter.at(VerbosityKind::Warning).get_value() << "}; "
<< "{Kind: Info, Regex: " << filter.at(VerbosityKind::Info).get_value() << "}";

return os;
}

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