comparison UnitTestsSources/LoggingTests.cpp @ 3353:54cdad5a7228 emscripten-logging

Added a public function that will use emscripten-specific logging functions when using its SDK + scaffolding work. Build and UT OK on msvc15 x64. Not actually tested under *nix or emscripten yet
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 23 Apr 2019 21:40:57 +0200
parents
children 815b81142ff7
comparison
equal deleted inserted replaced
3350:ba051f674f4b 3353:54cdad5a7228
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-2019 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 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35 #include <boost/regex.hpp>
36 #include <sstream>
37
38 #include "../Core/Logging.h"
39
40 using namespace Orthanc::Logging;
41
42 static std::stringstream testErrorStream;
43 void TestError(const char* message)
44 {
45 testErrorStream << message;
46 }
47
48 static std::stringstream testWarningStream;
49 void TestWarning(const char* message)
50 {
51 testWarningStream << message;
52 }
53
54 static std::stringstream testInfoStream;
55 void TestInfo(const char* message)
56 {
57 testInfoStream << message;
58 }
59
60 /**
61 Extracts the log line payload
62
63 "E0423 16:55:43.001194 LoggingTests.cpp:102] Foo bar?\r\n"
64 -->
65 "Foo bar"
66
67 If the log line cannot be matched, the function returns false.
68 */
69 static bool GetLogLinePayload(std::string& payload,
70 const std::string& logLine)
71 {
72 const char* regexStr = "[A-Z][0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6} "
73 "[a-zA-Z\\.\\-_]+:[0-9]+\\] (.*)\r\n$";
74 boost::regex regexObj(regexStr);
75
76 //std::stringstream regexSStr;
77 //regexSStr << "E[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6} "
78 // "[a-zA-Z\\.\\-_]+:[0-9]+\\](.*)\r\n$";
79 //std::string regexStr = regexSStr.str();
80 boost::regex pattern(regexStr);
81 boost::cmatch what;
82 if (regex_match(logLine.c_str(), what, regexObj))
83 {
84 payload = what[1];
85 return true;
86 }
87 else
88 {
89 return false;
90 }
91 }
92
93 namespace
94 {
95 class LoggingMementoScope
96 {
97 public:
98 LoggingMementoScope() : memento_(CreateLoggingMemento()) {}
99 ~LoggingMementoScope()
100 {
101 RestoreLoggingMemento(memento_);
102 }
103 private:
104 LoggingMemento memento_;
105 };
106 }
107
108 TEST(FuncStreamBuf, BasicTest)
109 {
110 LoggingMementoScope loggingConfiguration;
111
112 EnableTraceLevel(TRUE);
113
114 typedef void(*LoggingFunctionFunc)(const char*);
115
116 FuncStreamBuf<LoggingFunctionFunc> errorStreamBuf(TestError);
117 std::ostream errorStream(&errorStreamBuf);
118
119 FuncStreamBuf<LoggingFunctionFunc> warningStreamBuf(TestWarning);
120 std::ostream warningStream(&warningStreamBuf);
121
122 FuncStreamBuf<LoggingFunctionFunc> infoStreamBuf(TestInfo);
123 std::ostream infoStream(&infoStreamBuf);
124
125 SetErrorWarnInfoLoggingStreams(&errorStream, &warningStream, &infoStream);
126
127 {
128 const char* text = "E is the set of all sets that do not contain themselves. Does E contain itself?";
129 LOG(ERROR) << text;
130 std::string logLine = testErrorStream.str();
131 testErrorStream.str("");
132 testErrorStream.clear();
133 std::string payload;
134 bool ok = GetLogLinePayload(payload, logLine);
135 ASSERT_STREQ(payload.c_str(), text);
136 }
137
138 // make sure loglines do not accumulate
139 {
140 const char* text = "some more nonsensical babblingiciously stupid gibberish";
141 LOG(ERROR) << text;
142 std::string logLine = testErrorStream.str();
143 testErrorStream.str("");
144 testErrorStream.clear();
145 std::string payload;
146 bool ok = GetLogLinePayload(payload, logLine);
147 ASSERT_STREQ(payload.c_str(), text);
148 }
149
150 {
151 const char* text = "Trougoudou 53535345345353";
152 LOG(WARNING) << text;
153 std::string logLine = testWarningStream.str();
154 testWarningStream.str("");
155 testWarningStream.clear();
156 std::string payload;
157 bool ok = GetLogLinePayload(payload, logLine);
158 ASSERT_STREQ(payload.c_str(), text);
159 }
160
161 {
162 const char* text = "Prout 111929";
163 LOG(INFO) << text;
164 std::string logLine = testInfoStream.str();
165 testInfoStream.str("");
166 testInfoStream.clear();
167 std::string payload;
168 bool ok = GetLogLinePayload(payload, logLine);
169 ASSERT_STREQ(payload.c_str(), text);
170 }
171 }
172
173
174
175
176
177
178
179
180
181