Mercurial > hg > orthanc
comparison OrthancFramework/UnitTestsSources/LoggingTests.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | UnitTestsSources/LoggingTests.cpp@27628b0f6ada |
children | 05b8fd21089c |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1 | |
35 # include <OrthancFramework.h> | |
36 #endif | |
37 | |
38 #include "PrecompiledHeadersUnitTests.h" | |
39 #include "gtest/gtest.h" | |
40 #include <boost/regex.hpp> | |
41 #include <sstream> | |
42 | |
43 #include "../Core/Logging.h" | |
44 | |
45 using namespace Orthanc::Logging; | |
46 | |
47 static std::stringstream testErrorStream; | |
48 void TestError(const char* message) | |
49 { | |
50 testErrorStream << message; | |
51 } | |
52 | |
53 static std::stringstream testWarningStream; | |
54 void TestWarning(const char* message) | |
55 { | |
56 testWarningStream << message; | |
57 } | |
58 | |
59 static std::stringstream testInfoStream; | |
60 void TestInfo(const char* message) | |
61 { | |
62 testInfoStream << message; | |
63 } | |
64 | |
65 /** | |
66 Extracts the log line payload | |
67 | |
68 "E0423 16:55:43.001194 LoggingTests.cpp:102] Foo bar?\n" | |
69 --> | |
70 "Foo bar" | |
71 | |
72 If the log line cannot be matched, the function returns false. | |
73 */ | |
74 | |
75 #define EOLSTRING "\n" | |
76 | |
77 static bool GetLogLinePayload(std::string& payload, | |
78 const std::string& logLine) | |
79 { | |
80 const char* regexStr = "[A-Z][0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6} " | |
81 "[a-zA-Z\\.\\-_]+:[0-9]+\\] (.*)" EOLSTRING "$"; | |
82 | |
83 boost::regex regexObj(regexStr); | |
84 | |
85 //std::stringstream regexSStr; | |
86 //regexSStr << "E[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6} " | |
87 // "[a-zA-Z\\.\\-_]+:[0-9]+\\](.*)\r\n$"; | |
88 //std::string regexStr = regexSStr.str(); | |
89 boost::regex pattern(regexStr); | |
90 boost::cmatch what; | |
91 if (regex_match(logLine.c_str(), what, regexObj)) | |
92 { | |
93 payload = what[1]; | |
94 return true; | |
95 } | |
96 else | |
97 { | |
98 return false; | |
99 } | |
100 } | |
101 | |
102 | |
103 namespace | |
104 { | |
105 class LoggingMementoScope | |
106 { | |
107 public: | |
108 LoggingMementoScope() | |
109 { | |
110 } | |
111 | |
112 ~LoggingMementoScope() | |
113 { | |
114 Orthanc::Logging::Reset(); | |
115 } | |
116 }; | |
117 | |
118 | |
119 /** | |
120 * std::streambuf subclass used in FunctionCallingStream | |
121 **/ | |
122 template<typename T> | |
123 class FuncStreamBuf : public std::stringbuf | |
124 { | |
125 public: | |
126 FuncStreamBuf(T func) : func_(func) {} | |
127 | |
128 virtual int sync() | |
129 { | |
130 std::string text = this->str(); | |
131 const char* buf = text.c_str(); | |
132 func_(buf); | |
133 this->str(""); | |
134 return 0; | |
135 } | |
136 private: | |
137 T func_; | |
138 }; | |
139 } | |
140 | |
141 | |
142 TEST(FuncStreamBuf, BasicTest) | |
143 { | |
144 LoggingMementoScope loggingConfiguration; | |
145 | |
146 EnableTraceLevel(true); | |
147 | |
148 typedef void(*LoggingFunctionFunc)(const char*); | |
149 | |
150 FuncStreamBuf<LoggingFunctionFunc> errorStreamBuf(TestError); | |
151 std::ostream errorStream(&errorStreamBuf); | |
152 | |
153 FuncStreamBuf<LoggingFunctionFunc> warningStreamBuf(TestWarning); | |
154 std::ostream warningStream(&warningStreamBuf); | |
155 | |
156 FuncStreamBuf<LoggingFunctionFunc> infoStreamBuf(TestInfo); | |
157 std::ostream infoStream(&infoStreamBuf); | |
158 | |
159 SetErrorWarnInfoLoggingStreams(errorStream, warningStream, infoStream); | |
160 | |
161 { | |
162 const char* text = "E is the set of all sets that do not contain themselves. Does E contain itself?"; | |
163 LOG(ERROR) << text; | |
164 std::string logLine = testErrorStream.str(); | |
165 testErrorStream.str(""); | |
166 testErrorStream.clear(); | |
167 std::string payload; | |
168 bool ok = GetLogLinePayload(payload, logLine); | |
169 ASSERT_TRUE(ok); | |
170 ASSERT_STREQ(payload.c_str(), text); | |
171 } | |
172 | |
173 // make sure loglines do not accumulate | |
174 { | |
175 const char* text = "some more nonsensical babblingiciously stupid gibberish"; | |
176 LOG(ERROR) << text; | |
177 std::string logLine = testErrorStream.str(); | |
178 testErrorStream.str(""); | |
179 testErrorStream.clear(); | |
180 std::string payload; | |
181 bool ok = GetLogLinePayload(payload, logLine); | |
182 ASSERT_TRUE(ok); | |
183 ASSERT_STREQ(payload.c_str(), text); | |
184 } | |
185 | |
186 { | |
187 const char* text = "Trougoudou 53535345345353"; | |
188 LOG(WARNING) << text; | |
189 std::string logLine = testWarningStream.str(); | |
190 testWarningStream.str(""); | |
191 testWarningStream.clear(); | |
192 std::string payload; | |
193 bool ok = GetLogLinePayload(payload, logLine); | |
194 ASSERT_TRUE(ok); | |
195 ASSERT_STREQ(payload.c_str(), text); | |
196 } | |
197 | |
198 { | |
199 const char* text = "Prout 111929"; | |
200 LOG(INFO) << text; | |
201 std::string logLine = testInfoStream.str(); | |
202 testInfoStream.str(""); | |
203 testInfoStream.clear(); | |
204 std::string payload; | |
205 bool ok = GetLogLinePayload(payload, logLine); | |
206 ASSERT_TRUE(ok); | |
207 ASSERT_STREQ(payload.c_str(), text); | |
208 } | |
209 } |