Line data Source code
1 : /*
2 : * Files:
3 : * tests/log_for_test.cpp
4 : *
5 : * License:
6 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
7 : *
8 : * https://snapwebsites.org/
9 : * contact@m2osw.com
10 : *
11 : * This program is free software; you can redistribute it and/or modify
12 : * it under the terms of the GNU General Public License as published by
13 : * the Free Software Foundation; either version 2 of the License, or
14 : * (at your option) any later version.
15 : *
16 : * This program is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU General Public License along
22 : * with this program; if not, write to the Free Software Foundation, Inc.,
23 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 : *
25 : * Authors:
26 : * Alexis Wilke alexis@m2osw.com
27 : */
28 :
29 : // self
30 : //
31 : #include "main.h"
32 :
33 : // advgetopt lib
34 : //
35 : #include <advgetopt/log.h>
36 :
37 : // libexcept lib
38 : //
39 : #include <libexcept/exception.h>
40 :
41 :
42 :
43 : namespace SNAP_CATCH2_NAMESPACE
44 : {
45 :
46 :
47 :
48 : namespace
49 : {
50 :
51 2 : std::vector<std::string> g_expected_logs = std::vector<std::string>();
52 :
53 : }
54 : // no name namespace
55 :
56 :
57 :
58 220 : void push_expected_log(std::string const & message)
59 : {
60 220 : g_expected_logs.push_back(message);
61 220 : }
62 :
63 :
64 220 : void log_for_test(advgetopt::log_level_t level, std::string const & message)
65 : {
66 440 : if(SNAP_CATCH2_NAMESPACE::g_verbose()
67 220 : || g_expected_logs.empty())
68 : {
69 0 : std::cerr << "logger sent:\n"
70 0 : << advgetopt::to_string(level)
71 0 : << ": "
72 0 : << message
73 0 : << std::endl;
74 : }
75 :
76 : // at this time it's impossible to debug the location of the empty
77 : // problem without a proper stack trace...
78 : //
79 220 : if(g_expected_logs.empty())
80 : {
81 0 : libexcept::stack_trace_t trace(libexcept::collect_stack_trace_with_line_numbers());
82 0 : std::cerr << "*** STACK TRACE ***" << std::endl;
83 0 : for(auto const & l : trace)
84 : {
85 0 : std::cerr << l << std::endl;
86 : }
87 0 : std::cerr << "***" << std::endl;
88 : }
89 :
90 220 : CATCH_REQUIRE_FALSE(g_expected_logs.empty());
91 :
92 440 : std::stringstream ss;
93 220 : ss << advgetopt::to_string(level) << ": " << message;
94 :
95 : // again, the REQUIRE() is not going to be useful in terms of line number
96 : //
97 220 : if(g_expected_logs[0] != ss.str())
98 : {
99 0 : libexcept::stack_trace_t trace(libexcept::collect_stack_trace_with_line_numbers());
100 0 : std::cerr << "*** STACK TRACE ***" << std::endl;
101 0 : for(auto const & l : trace)
102 : {
103 0 : std::cerr << l << std::endl;
104 : }
105 0 : std::cerr << "***" << std::endl;
106 : }
107 :
108 440 : std::string expected_msg(g_expected_logs[0]);
109 220 : g_expected_logs.erase(g_expected_logs.begin());
110 :
111 220 : CATCH_REQUIRE(expected_msg == ss.str());
112 220 : }
113 :
114 :
115 80 : void expected_logs_stack_is_empty()
116 : {
117 80 : if(!g_expected_logs.empty())
118 : {
119 0 : std::cerr << "List of expected error logs which did not occur:" << std::endl;
120 0 : for(auto l : g_expected_logs)
121 : {
122 0 : std::cerr << " " << l << std::endl;
123 : }
124 0 : g_expected_logs.clear();
125 0 : throw std::logic_error("a test left an unexpected error message in the g_expected_logs vector.");
126 : }
127 80 : }
128 :
129 :
130 6 : } // SNAP_CATCH2_NAMESPACE namespace
131 : // vim: ts=4 sw=4 et
|