Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TestLogger.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* TestLogger.cc (C) 2000-2022 */
9/* */
10/* Utility class for logging test information. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/TestLogger.h"
15
16#include "arcane/utils/String.h"
17#include "arcane/utils/PlatformUtils.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/FatalErrorException.h"
20
21#include <sstream>
22#include <iostream>
23#include <string>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
35{
36 public:
37
38 std::ostringstream m_stream;
39};
40
41namespace
42{
43 TestLoggerImpl& _impl()
44 {
45 static thread_local TestLoggerImpl logger;
46 return logger;
47 }
48} // namespace
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
53std::ostream&
54TestLogger::stream()
55{
56 return _impl().m_stream;
57}
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62void TestLogger::
63dump(std::ostream& o)
64{
65 std::string str = _impl().m_stream.str();
66 o << str;
67}
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
71
72int TestLogger::
73compare()
74{
75 String result_file = platform::getEnvironmentVariable("ARCANE_TEST_RESULT_FILE");
76 if (result_file.null())
77 return 0;
78
79 UniqueArray<std::byte> bytes;
80 if (platform::readAllFile(result_file, false, bytes))
81 ARCANE_FATAL("Can not read test result file '{0}'", result_file);
82
83 std::string expected_str(reinterpret_cast<const char*>(bytes.data()), bytes.length());
84 std::string str = _impl().m_stream.str();
85
86 if (expected_str != str) {
87 std::cout << "TestLogger: ERROR during test comparison:\n";
88 std::cout << "Current:\n";
89 std::cout << "\n'''" << str << "'''\n";
90 std::cout << "\nExpected:\n";
91 std::cout << "\n'''" << expected_str << "'''\n";
92 return 1;
93 }
94
95 return 0;
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101} // namespace Arcane
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
bool readAllFile(StringView filename, bool is_binary, ByteArray &out_bytes)
Reads the content of a file and stores it in out_bytes.
String getEnvironmentVariable(const String &name)
Environment variable named name.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --