ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
handler.h
1/*
2 * Copyright 2025 Dominik Czekai
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#include <fstream>
19#include <iostream>
20#include <memory>
21
22#include "formatter.h"
23#include "logstyle.h"
24
25namespace cli::logging
26{
27
30{
31public:
32 virtual ~AbstractHandler() = default;
33
36 virtual void emit(const LogRecord &record) const = 0;
37};
38
41{
42public:
49 BaseHandler(std::ostream &outStream, std::ostream &errStream,
50 std::shared_ptr<AbstractFormatter> formatter, LogLevel minLevel = LogLevel::DEBUG,
51 std::shared_ptr<const LogStyleMap> styles = nullptr)
52 : out(outStream), err(errStream), formatterPtr(std::move(formatter)),
53 styleMapPtr(std::move(styles)), minLevel(minLevel)
54 {
55 }
56
57 ~BaseHandler() override;
58
59 void emit(const LogRecord &record) const override;
60
63 void setStylingEnabled(bool enabled) { stylingEnabled = enabled; }
64
67 void setStyleMap(std::shared_ptr<const LogStyleMap> styles);
68
69protected:
70 std::ostream &out; // standard stream
71 std::ostream &err; // error stream
72private:
73 bool stylingEnabled{true};
74 std::shared_ptr<AbstractFormatter> formatterPtr;
75 std::shared_ptr<const LogStyleMap> styleMapPtr;
76 LogLevel minLevel;
77};
78
81{
82public:
88 std::shared_ptr<AbstractFormatter> formatter, LogLevel minLevel = LogLevel::DEBUG,
89 std::shared_ptr<const LogStyleMap> styles = std::make_shared<LogStyleMap>(defaultStyles()))
90 : BaseHandler(std::cout, std::cerr, formatter, minLevel, std::move(styles))
91 {
92 }
93};
94
97{
98public:
104 explicit FileHandler(const std::string &filename, std::shared_ptr<AbstractFormatter> formatter,
105 LogLevel minLevel = LogLevel::DEBUG,
106 std::shared_ptr<const LogStyleMap> styles = nullptr);
107 ~FileHandler() override;
108
109private:
110 std::ofstream file;
111};
112
113} // namespace cli::logging
Interface for log record handlers.
Definition handler.h:30
virtual void emit(const LogRecord &record) const =0
Emit a log record, using the handlers formatter and specified output.
Basic log handler that writes to specified output streams.
Definition handler.h:41
void setStyleMap(std::shared_ptr< const LogStyleMap > styles)
Attach a style map (for ANSI colors).
Definition handler.cpp:53
void emit(const LogRecord &record) const override
Emit a log record, using the handlers formatter and specified output.
Definition handler.cpp:27
void setStylingEnabled(bool enabled)
Enable or disable styling for log messages.
Definition handler.h:63
BaseHandler(std::ostream &outStream, std::ostream &errStream, std::shared_ptr< AbstractFormatter > formatter, LogLevel minLevel=LogLevel::DEBUG, std::shared_ptr< const LogStyleMap > styles=nullptr)
Construct a new Base Handler using the given out and error streams.
Definition handler.h:49
Console log handler.
Definition handler.h:81
ConsoleHandler(std::shared_ptr< AbstractFormatter > formatter, LogLevel minLevel=LogLevel::DEBUG, std::shared_ptr< const LogStyleMap > styles=std::make_shared< LogStyleMap >(defaultStyles()))
Construct a new Console Handler that uses std::cout and std::cerr.
Definition handler.h:87
File log handler.
Definition handler.h:97
Data structure representing a log record.
Definition logrecord.h:28