ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
log_streambuffer.h
1// Copyright 2025 Dominik Czekai
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16#include <memory>
17#include <string>
18#include <sstream>
19#include <functional>
20#include "logging/loglevel.h"
21
22namespace cli::logging
23{
27class LogStreamBuf : public std::stringbuf
28{
29public:
34 explicit LogStreamBuf(std::shared_ptr<std::function<void(LogLevel, const std::string &)>> logFuncPtr,
35 LogLevel lvl, LogLevel lvlMin)
36 : logFuncPtr(logFuncPtr), lvl(lvl), minLevel(lvlMin)
37 {
38 }
39
40 int sync() override;
41
44 void setMinLevel(LogLevel lvlMin) { minLevel = lvlMin; }
45
46private:
47 std::shared_ptr<std::function<void(LogLevel, const std::string &)>> logFuncPtr;
48 LogLevel lvl;
49 LogLevel minLevel;
50};
51} // namespace cli::logging
Log stream buffer with a minimum LogLevel, that redirects the buffered output to a logging function....
Definition log_streambuffer.h:28
LogStreamBuf(std::shared_ptr< std::function< void(LogLevel, const std::string &)> > logFuncPtr, LogLevel lvl, LogLevel lvlMin)
Construct a new LogStreamBuf.
Definition log_streambuffer.h:34
void setMinLevel(LogLevel lvlMin)
Set the minimum log level for this buffer.
Definition log_streambuffer.h:44