Skip to content

Commit

Permalink
Adding file
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <caguero@openrobotics.org>
  • Loading branch information
caguero committed Aug 20, 2024
1 parent d1402ec commit 85c149e
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions log/src/SplitSink.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2024 Open Source Robotics Foundation
*
* 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.
*
*/
#include <memory>
#include <string>

#include <gz/utils/log/SplitSink.hh>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

namespace gz::utils::log
{
/// \brief Private data for the SplitConsoleSink class.
class SplitConsoleSink::Implementation
{
/// \brief Constructor.
/// \param[in] _loggerName Logger name.
public: Implementation()
{
}

/// \brief Standard output.
public: spdlog::sinks::stdout_color_sink_mt stdout;

/// \brief Standard error.
public: spdlog::sinks::stderr_color_sink_mt stderr;
};

/////////////////////////////////////////////////
SplitConsoleSink::SplitConsoleSink()
: dataPtr(gz::utils::MakeUniqueImpl<Implementation>())
{
}

/////////////////////////////////////////////////
void SplitConsoleSink::log(const spdlog::details::log_msg &_msg)
{
if (_msg.level == spdlog::level::warn ||
_msg.level == spdlog::level::err ||
_msg.level == spdlog::level::critical)
{
this->dataPtr->stderr.log(_msg);
}
else
this->dataPtr->stdout.log(_msg);
}

/////////////////////////////////////////////////
void SplitConsoleSink::flush()
{
this->dataPtr->stdout.flush();
this->dataPtr->stderr.flush();
}

/////////////////////////////////////////////////
void SplitConsoleSink::set_pattern(const std::string &_pattern)
{
this->dataPtr->stdout.set_pattern(_pattern);
this->dataPtr->stderr.set_pattern(_pattern);
}

/////////////////////////////////////////////////
void SplitConsoleSink::set_formatter(
std::unique_ptr<spdlog::formatter> _sinkFormatter)
{
this->dataPtr->stdout.set_formatter(_sinkFormatter->clone());
this->dataPtr->stderr.set_formatter(std::move(_sinkFormatter));
}

/////////////////////////////////////////////////
void SplitConsoleSink::set_color_mode(spdlog::color_mode _mode)
{
this->dataPtr->stdout.set_color_mode(_mode);
this->dataPtr->stderr.set_color_mode(_mode);
}


} // namespace gz::utils::log

0 comments on commit 85c149e

Please sign in to comment.