-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos Agüero <caguero@openrobotics.org>
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |