pars 0.2.1
Loading...
Searching...
No Matches
setup.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "pars/log.h"
33
34namespace pars::app
35{
36
38{
39private:
40 const char* default_pattern()
41 {
42 return "%^[%H:%M:%S.%f %z] [%6P %6t] [%L]: %$%v";
43 }
44
45 const char* default_pattern_with_source_loc()
46 {
47 return "%^[%H:%M:%S.%f %z] [%6P %6t] [%L]: %$%v \x1b[90m(%s:%#)\x1b[0m";
48 }
49
50public:
52 {
53 spdlog::set_pattern(default_pattern_with_source_loc());
54 }
55
56 void setup()
57 {
58 if constexpr (pars_log_enabled)
59 {
60 using namespace spdlog;
61
63
64 std::vector<sink_ptr> sinks;
65
66 auto stderr_s = std::make_shared<sinks::stderr_color_sink_mt>();
67
68 if constexpr (pars_log_enable_stderr)
69 {
70 auto stderr_sink = std::make_shared<sinks::stderr_color_sink_mt>();
71
72 sinks.push_back(stderr_s);
73 }
74
75 if constexpr (pars_log_enable_file)
76 {
77 auto file_s = std::make_shared<sinks::basic_file_sink_mt>("pars.log");
78
79 sinks.push_back(file_s);
80 }
81
82#if defined(PARS_LOG_ENABLE_MSVC)
83 auto msvc_s = std::make_shared<sinks::msvc_sink_mt>();
84
85 sinks.push_back(msvc_s);
86#endif
87
88#if defined(PARS_LOG_ENABLE_SYSLOG)
89 auto syslog_s = std::make_shared<sinks::syslog_sink_mt>(
90 "pars", LOG_CONS | LOG_NDELAY, LOG_LOCAL2, true);
91
92 sinks.push_back(syslog_s);
93#endif
94
96
97 {
98 auto default_l = std::make_shared<logger>("pars");
99
100 // default_l->set_pattern(default_pattern);
101
102 default_l->sinks() = sinks;
103
104 register_logger(default_l);
105
106 set_pattern(default_pattern());
107
108 set_default_logger(default_l);
109
110 set_level(static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL));
111 }
112 }
113 }
114
115protected:
117};
118
119} // namespace pars::app
#define SPDLOG_ACTIVE_LEVEL
Definition log.h:41