Debian Codegraph @

src/LoggerConfigurator.cpp

raw
1
2 // LoggerConfigurator - reading strings for configuring the Logger
3 //
4 // Author : Klaus-Dieter Gundermann
5 // Created : 30.06.2020
6 //
7 // For all support, instructions and copyright go to:
8 // http://e2guardian.org/
9 // Released under the GPL v2, with the OpenSSL exception described in the README file.
10
11 #ifdef HAVE_CONFIG_H
12 #include "e2config.h"
13 #endif
14
15 #include <algorithm>
16 // #include <string>
17 #include "LoggerConfigurator.hpp"
18 #include "String.hpp"
19
20 // -------------------------------------------------------------
21 // --- Constructor
22 // -------------------------------------------------------------
23 LoggerConfigurator::LoggerConfigurator(Logger *logger) {
24 _logger = logger;
25 };
26
27 LoggerConfigurator::~LoggerConfigurator() {
28 _logger = NULL;
29 };
30
31 const String LoggerConfigurator::Prefix("log_");
32
33 // -------------------------------------------------------------
34 // --- Public Functions
35 // -------------------------------------------------------------
36
37 bool LoggerConfigurator::configure(LoggerSource source, const std::string option) {
38 String line(option);
39 line.removeChar(' ');
40 std::string destination;
41 String filename;
42 if(line.contains(":")) {
43 destination = line.before(":");
44 filename = line.after(":");
45 filename.removeChar('\'');
46 } else {
47 destination = line;
48 }
49
50 LoggerDestination dst = _logger->string2dest(destination);
51
52 DEBUG_config("LoggerConfig:", " source:", _logger->source2string(source), " destination:", destination, " filename:", filename);
53
54 return _logger->setLogOutput(source, dst, filename,true);
55 }
56
57 bool LoggerConfigurator::configure(const std::string option) {
58 String line(option);
59 line.removeChar(' ');
60 if (!line.startsWith(Prefix)) return false;
61
62 size_t pos1 = line.find("=", 0);
63 if (pos1 == std::string::npos) return false;
64
65 std::string source = line.substr(Prefix.size(), pos1 - Prefix.size());
66
67 std::string reduced_option;
68
69 reduced_option = line.substr(pos1 + 1);
70 LoggerSource src = _logger->string2source(source);
71
72 return configure(src, reduced_option);
73 };
74
75 bool LoggerConfigurator::debuglevel(const std::string option) {
76 String line(option);
77 line.removeChar(' ');
78
79 String types, dest, file;
80 LoggerDestination dst = LoggerDestination::none;
81
82 if (line.contains(":")) {
83 types = line.before(":");
84 dest = line.after(":");
85 if (dest.contains(":")) {
86 file = dest.after(":");
87 dest = dest.before(":");
88 }
89 dst = _logger->string2dest(dest);
90 } else {
91 types = line;
92 }
93
94 //std::cerr << "types:" << types << " dest:" << dest << " LoggerDestination " << _logger->dest2string(dst) << " file:" << file << std::endl;
95
96 if (types.contains("ALL")) {
97 String temp = types.before("ALL");
98 temp += "HIGH,LOW";
99 temp += types.after("ALL");
100 types = temp;
101 }
102
103 if (types.contains("HIGH")) {
104 String temp = types.before("HIGH");
105 temp += "icap,avscan,auth,dwload,proxy,thttps";
106 temp += types.after("HIGH");
107 types = temp;
108 }
109
110 if (types.contains("LOW")) {
111 String temp = types.before("LOW");
112 temp += "debug,trace,network,regexp,story,config,content";
113 temp += types.after("LOW");
114 types = temp;
115 }
116 types.toLower();
117
118 //std::cerr << types << std::endl;
119
120 while (!types.empty()) {
121 String temp;
122 if (types.contains(",")) {
123 temp = types.before(",");
124 types = types.after(",");
125 } else {
126 temp = types;
127 types = "";
128 }
129
130 // std::cerr << "dealing with " << temp << " types left are " << types << std::endl;
131
132 bool neg = temp.startsWith("-");
133 if (neg)
134 temp = temp.after("-");
135
136 LoggerSource src = _logger->string2source(temp.c_str());
137 if (src == LoggerSource::none) {
138 std::cerr << "Error: unknown debuglevel " << temp << std::endl;
139 return false;
140 };
141
142 if (neg) {
143 _logger->disable(src);
144 } else {
145 if (dst != LoggerDestination::none) {
146 // E2LOGGER_info("LoggerConfig:", " source:", src, " destination:", dest, " filename:", file );
147 _logger->setLogOutput(src, dst, file);
148 // std::cerr << _logger->source2string(src) << " " << _logger->dest2string(dst) << " " << file << std::endl;
149 } else { // assume useing defaults so just enable
150 _logger->enable(src);
151 }
152 }
153 }
154 return true;
155 }
156
157 void LoggerConfigurator::debugformat(int fmt) {
158 switch (fmt) {
159 case 1:
160 _logger->setMultiFormat(&_logger->debug_messages, false, false, true, true, true);
161 break;
162 case 2:
163 _logger->setMultiFormat(&_logger->debug_messages, false, false, true, false, true);
164 break;
165 case 3:
166 _logger->setMultiFormat(&_logger->debug_messages, false, false, false, false, true);
167 break;
168 case 4:
169 _logger->setMultiFormat(&_logger->debug_messages, false, true, true, true, true);
170 break;
171 case 5:
172 _logger->setMultiFormat(&_logger->debug_messages, false, true, true, false, true);
173 break;
174 case 6:
175 _logger->setMultiFormat(&_logger->debug_messages, false, true, false, false, true);
176 break;
177 }
178 }
179
180