TrueReality  v0.1.1912
Exception.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * @author Matthew W. Campbell
22 * @author Maxim Serebrennik
23 */
24 
25 #include <trUtil/Exception.h>
26 
27 #include <sstream>
28 #include <iostream>
29 #include <osgDB/FileNameUtils>
30 
31 namespace trUtil
32 {
34  Exception::Exception(const std::string& message, const std::string& filename,
35  unsigned int lineNum)
36  : mMessage(message)
37  , mFileName(osgDB::getSimpleFileName(filename))
38  , mLineNum(lineNum)
39  {
41  }
42 
44  void Exception::Print() const
45  {
46  std::cerr << "Exception: " << mMessage << std::endl <<
47  "\tFile: " << mFileName << std::endl <<
48  "\tLine: " << mLineNum << std::endl;
49  }
50 
52  std::string Exception::ToString() const
53  {
54  std::ostringstream ss;
55  ss << "Reason: " << mMessage << " | File: " << mFileName << " | Line: " << mLineNum;
56  return ss.str();
57  }
58 
60  void Exception::LogException(trUtil::Logging::LogLevel level) const
61  {
63  }
64 
66  void Exception::LogException(trUtil::Logging::LogLevel level, const std::string& loggerName) const
67  {
69  }
70 
72  void Exception::LogException(trUtil::Logging::LogLevel level, trUtil::Logging::Log& logger) const
73  {
74  if (logger.IsLevelEnabled(level))
75  {
77  "Exception Thrown: " + mMessage, level);
78  }
79  }
80 
83  {
84 
85  }
86 
88  const std::string& Exception::What() const
89  {
90  return mMessage;
91  }
92 
94  const std::string& Exception::File() const
95  {
96  return mFileName;
97  }
98 
100  unsigned int Exception::Line() const
101  {
102  return mLineNum;
103  }
104 
106  TR_UTIL_EXPORT std::ostream& operator << (std::ostream& o, const Exception& ex)
107  {
108  o << ex.ToString();
109  return o;
110  }
111 }
112 
Log class which the engine uses for all of its logging needs.
Definition: Log.h:197
LOG_DEBUG
Definition: LogLevel.h:64
std::string ToString() const
Converts this exception to a string.
Definition: Exception.cpp:52
void LogMessage(const std::string &cppFile, const std::string &method, int line, const std::string &msg, LogLevel logLevel) const
Logs a time-stamped message.
Definition: Log.cpp:85
virtual ~Exception()
Destructor.
Definition: Exception.cpp:82
This is the exception class used throughout the engine.
Definition: Exception.h:48
unsigned int mLineNum
Definition: Exception.h:145
Exception(const std::string &message, const std::string &filename, unsigned int linenum)
Constructor - Initializes the exception and logs it.
Definition: Exception.cpp:34
bool IsLevelEnabled(LogLevel logLevel) const
Queries if a level is enabled.
Definition: Log.cpp:286
void Print() const
Prints the exception to the console.
Definition: Exception.cpp:44
unsigned int Line() const
The line number associated with this exception.
Definition: Exception.cpp:100
void LogException(trUtil::Logging::LogLevel level=trUtil::Logging::LogLevel::LOG_ERROR) const
Logs the exception to the default logger.
Definition: Exception.cpp:60
TR_UTIL_EXPORT std::ostream & operator<<(std::ostream &os, const EnumerationString &e)
Helper method to print EnumerationNumeric to an output stream.
std::string mFileName
Definition: Exception.h:144
std::string mMessage
Definition: Exception.h:144
const std::string & File() const
The filename associated with this exception.
Definition: Exception.cpp:94
A class that represents date time utility.
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
const std::string & What() const
The message to be displayed when this exception is thrown.
Definition: Exception.cpp:88
static Log & GetInstance(const std::string &name=Log::LOG_DEFAULT_NAME)
Retrieve singleton instance of the log class for a give string name.
Definition: Log.cpp:203