1 #ifndef _TCUTESTLOG_HPP
2 #define _TCUTESTLOG_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Test Log C++ Wrapper.
24 *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27 #include "qpTestLog.h"
28 #include "tcuTexture.hpp"
29
30 #include <sstream>
31
32 namespace tcu
33 {
34
35 class Surface;
36 class MessageBuilder;
37 class LogImageSet;
38 class LogImage;
39 class LogSection;
40 class LogShaderProgram;
41 class LogShader;
42 class LogSpirVAssemblySource;
43 class LogKernelSource;
44 class LogSampleList;
45 class LogValueInfo;
46 class SampleBuilder;
47 template<typename T> class LogNumber;
48
49 /*--------------------------------------------------------------------*//*!
50 * \brief Test log
51 *
52 * TestLog provides convinient C++ API for logging. The API has been designed
53 * around stream operators much like STL iostream library. The following
54 * examples demonstrate how to use TestLog.
55 *
56 * \code
57 * TestLog& log = m_testCtx.getLog();
58 *
59 * // Write message to log.
60 * log << TestLog::Message << "Hello, World!" << TestLog::EndMessage;
61 * int myNumber = 3;
62 * log << TestLog::Message << "Diff is " << myNumber << TestLog::EndMessage;
63 *
64 * // Write image
65 * Surface myImage(256, 256);
66 * log << TestLog::Image("TestImage", "My test image", myImage);
67 *
68 * // Multiple commands can be combined:
69 * log << TestLog::Section("Details", "Test case details")
70 * << TestLog::Message << "Here be dragons" << TestLog::EndMessage
71 * << TestLog::ImageSet("Result", "Result images")
72 * << TestLog::Image("ImageA", "Image A", imageA)
73 * << TestLog::Image("ImageB", "Image B", imageB)
74 * << TestLog::EndImageSet << TestLog::EndSection;
75 * \endcode
76 *//*--------------------------------------------------------------------*/
77 class TestLog
78 {
79 public:
80 // Tokens
81 static const class BeginMessageToken {} Message;
82 static const class EndMessageToken {} EndMessage;
83 static const class EndImageSetToken {} EndImageSet;
84 static const class EndSectionToken {} EndSection;
85 static const class EndShaderProgramToken {} EndShaderProgram;
86 static const class SampleInfoToken {} SampleInfo;
87 static const class EndSampleInfoToken {} EndSampleInfo;
88 static const class BeginSampleToken {} Sample;
89 static const class EndSampleToken {} EndSample;
90 static const class EndSampleListToken {} EndSampleList;
91
92 // Typedefs.
93 typedef LogImageSet ImageSet;
94 typedef LogImage Image;
95 typedef LogSection Section;
96 typedef LogShaderProgram ShaderProgram;
97 typedef LogShader Shader;
98 typedef LogSpirVAssemblySource SpirVAssemblySource;
99 typedef LogKernelSource KernelSource;
100 typedef LogSampleList SampleList;
101 typedef LogValueInfo ValueInfo;
102 typedef LogNumber<float> Float;
103 typedef LogNumber<deInt64> Integer;
104
105 explicit TestLog (const char* fileName, deUint32 flags = 0);
106 ~TestLog (void);
107
108 void writeSessionInfo (std::string additionalInfo = "");
109
110 MessageBuilder operator<< (const BeginMessageToken&);
111 MessageBuilder message (void);
112
113 TestLog& operator<< (const ImageSet& imageSet);
114 TestLog& operator<< (const Image& image);
115 TestLog& operator<< (const EndImageSetToken&);
116
117 TestLog& operator<< (const Section& section);
118 TestLog& operator<< (const EndSectionToken&);
119
120 TestLog& operator<< (const ShaderProgram& shaderProgram);
121 TestLog& operator<< (const EndShaderProgramToken&);
122 TestLog& operator<< (const Shader& shader);
123 TestLog& operator<< (const SpirVAssemblySource& module);
124
125 TestLog& operator<< (const KernelSource& kernelSrc);
126
127 template<typename T>
128 TestLog& operator<< (const LogNumber<T>& number);
129
130 TestLog& operator<< (const SampleList& sampleList);
131 TestLog& operator<< (const SampleInfoToken&);
132 TestLog& operator<< (const ValueInfo& valueInfo);
133 TestLog& operator<< (const EndSampleInfoToken&);
134 SampleBuilder operator<< (const BeginSampleToken&);
135 TestLog& operator<< (const EndSampleListToken&);
136
137 // Raw api
138 void writeMessage (const char* message);
139
140 void startImageSet (const char* name, const char* description);
141 void endImageSet (void);
142 void writeImage (const char* name, const char* description, const ConstPixelBufferAccess& surface, const Vec4& scale, const Vec4& bias, qpImageCompressionMode compressionMode = QP_IMAGE_COMPRESSION_MODE_BEST);
143 void writeImage (const char* name, const char* description, qpImageCompressionMode compressionMode, qpImageFormat format, int width, int height, int stride, const void* data);
144
145 void startSection (const char* name, const char* description);
146 void endSection (void);
147
148 void startShaderProgram (bool linkOk, const char* linkInfoLog);
149 void endShaderProgram (void);
150 void writeShader (qpShaderType type, const char* source, bool compileOk, const char* infoLog);
151 void writeSpirVAssemblySource(const char* source);
152 void writeKernelSource (const char* source);
153 void writeCompileInfo (const char* name, const char* description, bool compileOk, const char* infoLog);
154
155 void writeFloat (const char* name, const char* description, const char* unit, qpKeyValueTag tag, float value);
156 void writeInteger (const char* name, const char* description, const char* unit, qpKeyValueTag tag, deInt64 value);
157
158 void startEglConfigSet (const char* name, const char* description);
159 void writeEglConfig (const qpEglConfigInfo* config);
160 void endEglConfigSet (void);
161
162 void startCase (const char* testCasePath, qpTestCaseType testCaseType);
163 void endCase (qpTestResult result, const char* description);
164 void terminateCase (qpTestResult result);
165
166 void startTestsCasesTime (void);
167 void endTestsCasesTime (void);
168
169 void startSampleList (const std::string& name, const std::string& description);
170 void startSampleInfo (void);
171 void writeValueInfo (const std::string& name, const std::string& description, const std::string& unit, qpSampleValueTag tag);
172 void endSampleInfo (void);
173 void startSample (void);
174 void writeSampleValue (double value);
175 void writeSampleValue (deInt64 value);
176 void endSample (void);
177 void endSampleList (void);
178
179 bool isShaderLoggingEnabled (void);
180 private:
181 TestLog (const TestLog& other); // Not allowed!
182 TestLog& operator= (const TestLog& other); // Not allowed!
183
184 qpTestLog* m_log;
185 };
186
187 class MessageBuilder
188 {
189 public:
MessageBuilder(TestLog * log)190 explicit MessageBuilder (TestLog* log) : m_log(log) {}
~MessageBuilder(void)191 ~MessageBuilder (void) {}
192
toString(void) const193 std::string toString (void) const { return m_str.str(); }
194
195 TestLog& operator<< (const TestLog::EndMessageToken&);
196
197 template <typename T>
198 MessageBuilder& operator<< (const T& value);
199
200 MessageBuilder (const MessageBuilder& other);
201 MessageBuilder& operator= (const MessageBuilder& other);
202
203 private:
204 TestLog* m_log;
205 std::ostringstream m_str;
206 };
207
208 class SampleBuilder
209 {
210 public:
SampleBuilder(TestLog * log)211 SampleBuilder (TestLog* log) : m_log(log) {}
212
operator <<(int v)213 SampleBuilder& operator<< (int v) { m_values.push_back(Value((deInt64)v)); return *this; }
operator <<(deInt64 v)214 SampleBuilder& operator<< (deInt64 v) { m_values.push_back(Value(v)); return *this; }
operator <<(float v)215 SampleBuilder& operator<< (float v) { m_values.push_back(Value((double)v)); return *this; }
operator <<(double v)216 SampleBuilder& operator<< (double v) { m_values.push_back(Value(v)); return *this; }
217
218 TestLog& operator<< (const TestLog::EndSampleToken&);
219
220 private:
221 struct Value
222 {
223 enum Type { TYPE_INT64 = 0, TYPE_FLOAT64, TYPE_LAST };
224
225 Type type;
226 union
227 {
228 deInt64 int64;
229 double float64;
230 } value;
231
Valuetcu::SampleBuilder::Value232 Value (void) : type(TYPE_LAST) { value.int64 = 0; }
Valuetcu::SampleBuilder::Value233 Value (double v) : type(TYPE_FLOAT64) { value.float64 = v; }
Valuetcu::SampleBuilder::Value234 Value (deInt64 v) : type(TYPE_INT64) { value.int64 = v; }
235 };
236
237 TestLog* m_log;
238 std::vector<Value> m_values;
239 };
240
241 class LogImageSet
242 {
243 public:
LogImageSet(const std::string & name,const std::string & description)244 LogImageSet (const std::string& name, const std::string& description)
245 : m_name (name)
246 , m_description (description)
247 {
248 }
249
250 void write (TestLog& log) const;
251
252 private:
253 std::string m_name;
254 std::string m_description;
255 };
256
257 // \note Doesn't take copy of surface contents
258 class LogImage
259 {
260 public:
261 LogImage (const std::string& name, const std::string& description, const Surface& surface, qpImageCompressionMode compression = QP_IMAGE_COMPRESSION_MODE_BEST);
262
263 LogImage (const std::string& name, const std::string& description, const ConstPixelBufferAccess& access, qpImageCompressionMode compression = QP_IMAGE_COMPRESSION_MODE_BEST);
264
265 LogImage (const std::string& name, const std::string& description, const ConstPixelBufferAccess& access, const Vec4& scale, const Vec4& bias, qpImageCompressionMode compression = QP_IMAGE_COMPRESSION_MODE_BEST);
266
267 void write (TestLog& log) const;
268
269 private:
270 std::string m_name;
271 std::string m_description;
272 ConstPixelBufferAccess m_access;
273 Vec4 m_scale;
274 Vec4 m_bias;
275 qpImageCompressionMode m_compression;
276 };
277
278 class LogSection
279 {
280 public:
LogSection(const std::string & name,const std::string & description)281 LogSection (const std::string& name, const std::string& description)
282 : m_name (name)
283 , m_description (description)
284 {
285 }
286
287 void write (TestLog& log) const;
288
289 private:
290 std::string m_name;
291 std::string m_description;
292 };
293
294 class LogShaderProgram
295 {
296 public:
LogShaderProgram(bool linkOk,const std::string & linkInfoLog)297 LogShaderProgram (bool linkOk, const std::string& linkInfoLog)
298 : m_linkOk (linkOk)
299 , m_linkInfoLog (linkInfoLog)
300 {
301 }
302
303 void write (TestLog& log) const;
304
305 private:
306 bool m_linkOk;
307 std::string m_linkInfoLog;
308 };
309
310 class LogShader
311 {
312 public:
LogShader(qpShaderType type,const std::string & source,bool compileOk,const std::string & infoLog)313 LogShader (qpShaderType type, const std::string& source, bool compileOk, const std::string& infoLog)
314 : m_type (type)
315 , m_source (source)
316 , m_compileOk (compileOk)
317 , m_infoLog (infoLog)
318 {
319 }
320
321 void write (TestLog& log) const;
322
323 private:
324 qpShaderType m_type;
325 std::string m_source;
326 bool m_compileOk;
327 std::string m_infoLog;
328 };
329
330 class LogSpirVAssemblySource
331 {
332 public:
LogSpirVAssemblySource(const std::string & source)333 LogSpirVAssemblySource (const std::string& source)
334 : m_source (source)
335 {
336 }
337
338 void write (TestLog& log) const;
339
340 private:
341 std::string m_source;
342 };
343
344 class LogKernelSource
345 {
346 public:
LogKernelSource(const std::string & source)347 explicit LogKernelSource (const std::string& source)
348 : m_source(source)
349 {
350 }
351
352 void write (TestLog& log) const;
353
354 private:
355 std::string m_source;
356 };
357
358 class LogSampleList
359 {
360 public:
LogSampleList(const std::string & name,const std::string & description)361 LogSampleList (const std::string& name, const std::string& description)
362 : m_name (name)
363 , m_description (description)
364 {
365 }
366
367 void write (TestLog& log) const;
368
369 private:
370 std::string m_name;
371 std::string m_description;
372 };
373
374 class LogValueInfo
375 {
376 public:
LogValueInfo(const std::string & name,const std::string & description,const std::string & unit,qpSampleValueTag tag)377 LogValueInfo (const std::string& name, const std::string& description, const std::string& unit, qpSampleValueTag tag)
378 : m_name (name)
379 , m_description (description)
380 , m_unit (unit)
381 , m_tag (tag)
382 {
383 }
384
385 void write (TestLog& log) const;
386
387 private:
388 std::string m_name;
389 std::string m_description;
390 std::string m_unit;
391 qpSampleValueTag m_tag;
392 };
393
394 template<typename T>
395 class LogNumber
396 {
397 public:
LogNumber(const std::string & name,const std::string & desc,const std::string & unit,qpKeyValueTag tag,T value)398 LogNumber (const std::string& name, const std::string& desc, const std::string& unit, qpKeyValueTag tag, T value)
399 : m_name (name)
400 , m_desc (desc)
401 , m_unit (unit)
402 , m_tag (tag)
403 , m_value (value)
404 {
405 }
406
407 void write (TestLog& log) const;
408
409 private:
410 std::string m_name;
411 std::string m_desc;
412 std::string m_unit;
413 qpKeyValueTag m_tag;
414 T m_value;
415 };
416
417 // Section helper that closes section when leaving scope.
418 class ScopedLogSection
419 {
420 public:
ScopedLogSection(TestLog & log,const std::string & name,const std::string & description)421 ScopedLogSection (TestLog& log, const std::string& name, const std::string& description)
422 : m_log(log)
423 {
424 m_log << TestLog::Section(name, description);
425 }
426
~ScopedLogSection(void)427 ~ScopedLogSection (void)
428 {
429 m_log << TestLog::EndSection;
430 }
431
432 private:
433 TestLog& m_log;
434 };
435
436 // TestLog stream operators.
437
operator <<(const ImageSet & imageSet)438 inline TestLog& TestLog::operator<< (const ImageSet& imageSet) { imageSet.write(*this); return *this; }
operator <<(const Image & image)439 inline TestLog& TestLog::operator<< (const Image& image) { image.write(*this); return *this; }
operator <<(const EndImageSetToken &)440 inline TestLog& TestLog::operator<< (const EndImageSetToken&) { endImageSet(); return *this; }
operator <<(const Section & section)441 inline TestLog& TestLog::operator<< (const Section& section) { section.write(*this); return *this; }
operator <<(const EndSectionToken &)442 inline TestLog& TestLog::operator<< (const EndSectionToken&) { endSection(); return *this; }
operator <<(const ShaderProgram & shaderProg)443 inline TestLog& TestLog::operator<< (const ShaderProgram& shaderProg) { shaderProg.write(*this); return *this; }
operator <<(const EndShaderProgramToken &)444 inline TestLog& TestLog::operator<< (const EndShaderProgramToken&) { endShaderProgram(); return *this; }
operator <<(const Shader & shader)445 inline TestLog& TestLog::operator<< (const Shader& shader) { shader.write(*this); return *this; }
operator <<(const SpirVAssemblySource & module)446 inline TestLog& TestLog::operator<< (const SpirVAssemblySource& module) { module.write(*this); return *this; }
operator <<(const KernelSource & kernelSrc)447 inline TestLog& TestLog::operator<< (const KernelSource& kernelSrc) { kernelSrc.write(*this); return *this; }
operator <<(const SampleList & sampleList)448 inline TestLog& TestLog::operator<< (const SampleList& sampleList) { sampleList.write(*this); return *this; }
operator <<(const SampleInfoToken &)449 inline TestLog& TestLog::operator<< (const SampleInfoToken&) { startSampleInfo(); return *this; }
operator <<(const ValueInfo & valueInfo)450 inline TestLog& TestLog::operator<< (const ValueInfo& valueInfo) { valueInfo.write(*this); return *this; }
operator <<(const EndSampleInfoToken &)451 inline TestLog& TestLog::operator<< (const EndSampleInfoToken&) { endSampleInfo(); return *this; }
operator <<(const EndSampleListToken &)452 inline TestLog& TestLog::operator<< (const EndSampleListToken&) { endSampleList(); return *this; }
453
454 template<typename T>
operator <<(const LogNumber<T> & number)455 inline TestLog& TestLog::operator<< (const LogNumber<T>& number)
456 {
457 number.write(*this);
458 return *this;
459 }
460
operator <<(TestLog & log,const std::exception & e)461 inline TestLog& operator<< (TestLog& log, const std::exception& e)
462 {
463 // \todo [2012-10-18 pyry] Print type info?
464 return log << TestLog::Message << e.what() << TestLog::EndMessage;
465 }
466
467 // Utility class inline implementations.
468
469 template <typename T>
operator <<(const T & value)470 inline MessageBuilder& MessageBuilder::operator<< (const T& value)
471 {
472 // Overload stream operator to implement custom format
473 m_str << value;
474 return *this;
475 }
476
operator <<(const BeginMessageToken &)477 inline MessageBuilder TestLog::operator<< (const BeginMessageToken&)
478 {
479 return MessageBuilder(this);
480 }
481
message(void)482 inline MessageBuilder TestLog::message (void)
483 {
484 return MessageBuilder(this);
485 }
486
operator <<(const BeginSampleToken &)487 inline SampleBuilder TestLog::operator<< (const BeginSampleToken&)
488 {
489 return SampleBuilder(this);
490 }
491
write(TestLog & log) const492 inline void LogImageSet::write (TestLog& log) const
493 {
494 log.startImageSet(m_name.c_str(), m_description.c_str());
495 }
496
write(TestLog & log) const497 inline void LogSection::write (TestLog& log) const
498 {
499 log.startSection(m_name.c_str(), m_description.c_str());
500 }
501
write(TestLog & log) const502 inline void LogShaderProgram::write (TestLog& log) const
503 {
504 log.startShaderProgram(m_linkOk, m_linkInfoLog.c_str());
505 }
506
write(TestLog & log) const507 inline void LogShader::write (TestLog& log) const
508 {
509 log.writeShader(m_type, m_source.c_str(), m_compileOk, m_infoLog.c_str());
510 }
511
write(TestLog & log) const512 inline void LogSpirVAssemblySource::write (TestLog& log) const
513 {
514 log.writeSpirVAssemblySource(m_source.c_str());
515 }
516
write(TestLog & log) const517 inline void LogKernelSource::write (TestLog& log) const
518 {
519 log.writeKernelSource(m_source.c_str());
520 }
521
write(TestLog & log) const522 inline void LogSampleList::write (TestLog& log) const
523 {
524 log.startSampleList(m_name, m_description);
525 }
526
write(TestLog & log) const527 inline void LogValueInfo::write (TestLog& log) const
528 {
529 log.writeValueInfo(m_name, m_description, m_unit, m_tag);
530 }
531
532 template<>
write(TestLog & log) const533 inline void LogNumber<float>::write (TestLog& log) const
534 {
535 log.writeFloat(m_name.c_str(), m_desc.c_str(), m_unit.c_str(), m_tag, m_value);
536 }
537
538 template<>
write(TestLog & log) const539 inline void LogNumber<deInt64>::write (TestLog& log) const
540 {
541 log.writeInteger(m_name.c_str(), m_desc.c_str(), m_unit.c_str(), m_tag, m_value);
542 }
543
544 } // tcu
545
546 #endif // _TCUTESTLOG_HPP
547