gem5  v22.1.0.0
logging_mock.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "base/gtest/logging.hh"
29 
30 #include <string>
31 
32 #include "base/logging.hh"
33 
34 namespace gem5
35 {
36 
37 namespace {
38 
39 // This custom exception type will help prevent fatal exceptions from being
40 // caught by other code in gem5 and let them escape to the gtest framework.
41 // Unfortunately that results in a somewhat confusing message about an unknown
42 // exception being thrown after the panic/fatal message has been printed, but
43 // there will at least be some indication what went wrong.
44 struct GTestException
45 {};
46 
47 class GTestLogger : public Logger
48 {
49  public:
50  using Logger::Logger;
51 
52  protected:
53  void
54  log(const Loc &loc, std::string s) override
55  {
56  gtestLogOutput << s;
57  SUCCEED() << s;
58  }
59 };
60 
61 class GTestExitLogger : public Logger
62 {
63  public:
64  using Logger::Logger;
65 
66  protected:
67  void
68  log(const Loc &loc, std::string s) override
69  {
70  gtestLogOutput << s;
71  std::cerr << loc.file << ":" << loc.line << ": " << s;
72  }
73  // Throw an exception to escape down to the gtest framework.
74  void exit() override { throw GTestException(); }
75 };
76 
77 } // anonymous namespace
78 
79 // We intentionally put all the loggers on the heap to prevent them from being
80 // destructed at the end of the program. This make them safe to be used inside
81 // destructor of other global objects. Also, we make them function static
82 // veriables to ensure they are initialized ondemand, so it is also safe to use
83 // them inside constructor of other global objects.
84 
85 Logger&
87  static GTestExitLogger* panic_logger = new GTestExitLogger("panic: ");
88  return *panic_logger;
89 }
90 
91 Logger&
93  static GTestExitLogger* fatal_logger = new GTestExitLogger("fatal: ");
94  return *fatal_logger;
95 }
96 
97 Logger&
99  static GTestLogger* warn_logger = new GTestLogger("warn: ");
100  return *warn_logger;
101 }
102 
103 Logger&
105  static GTestLogger* info_logger = new GTestLogger("info: ");
106  return *info_logger;
107 }
108 
109 Logger&
111  static GTestLogger* hack_logger = new GTestLogger("hack: ");
112  return *hack_logger;
113 }
114 
115 } // namespace gem5
static Logger & getWarn()
Definition: logging_mock.cc:98
Logger(const char *prefix)
Definition: logging.hh:91
static Logger & getInfo()
static Logger & getHack()
static Logger & getPanic()
Get a Logger for the specified type of message.
Definition: logging_mock.cc:86
static Logger & getFatal()
Definition: logging_mock.cc:92
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 3 > exit
Definition: misc.hh:855
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
thread_local GTestLogOutput gtestLogOutput
Definition: logging.cc:33

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1