ge211
ge211_error.cpp
1 #include "ge211_error.h"
2 
3 #include <SDL.h>
4 #include <SDL_image.h>
5 #include <SDL_ttf.h>
6 
7 #include <iostream>
8 
9 namespace ge211 {
10 
11 namespace exceptions {
12 
13 const char* Exception_base::what() const noexcept
14 {
15  return message_->c_str();
16 }
17 
18 Exception_base::Exception_base(const std::string& message)
19  : message_{std::make_shared<std::string>(message)}
20 { }
21 
22 Environment_error::Environment_error(const std::string& message)
23  : Exception_base(message)
24 { }
25 
26 Client_logic_error::Client_logic_error(const std::string& message)
27  : Exception_base(message)
28 { }
29 
30 static std::string build_sdl_error_message(const std::string& message) {
31  const char* reason = SDL_GetError();
32 
33  std::ostringstream oss;
34  if (message.empty())
35  oss << "SDL Error: " << reason;
36  else
37  oss << message << "\n (reason from SDL: " << reason << ")";
38 
39  return oss.str();
40 }
41 
42 Host_error::Host_error(const std::string& message)
43  : Environment_error{build_sdl_error_message(message)}
44 { }
45 
46 File_error::File_error(const std::string& message)
47  : Host_error{message}
48 { }
49 
50 File_error File_error::could_not_open(const std::string& filename)
51 {
52  return File_error("Could not open: " + filename);
53 }
54 
55 Font_error::Font_error(const std::string& message)
56  : Host_error{message}
57 { }
58 
59 Font_error Font_error::could_not_load(const std::string& filename)
60 {
61  return Font_error("Could not load font: " + filename);
62 }
63 
64 Ge211_logic_error::Ge211_logic_error(const std::string& message)
65  : Environment_error("Apparent ge211 bug! " + message)
66 { }
67 
68 Image_error::Image_error(const std::string& message)
69  : Host_error{message}
70 { }
71 
72 Image_error Image_error::could_not_load(const std::string& filename)
73 {
74  return Image_error("Could not load image: " + filename);
75 }
76 
77 Mixer_error::Mixer_error(const std::string& message)
78  : Host_error{message}
79 { }
80 
81 Mixer_error Mixer_error::could_not_load(const std::string& filename)
82 {
83  return Mixer_error("Could not load music: " + filename);
84 }
85 
86 Mixer_error Mixer_error::out_of_channels()
87 {
88  return Mixer_error("Could not play effect: out of channels");
89 }
90 
91 }
92 
93 namespace detail {
94 
95 static const char* log_level_string(Log_level level)
96 {
97  switch (level) {
98  case Log_level::debug:
99  return "debug";
100  case Log_level::info:
101  return "info";
102  case Log_level::warn:
103  return "warn";
104  case Log_level::fatal:
105  return "fatal";
106  }
107 
108  // Shouldn't happen, because switch above is exhaustive. But this
109  // makes gcc warn less.
110  return "<unknown>";
111 }
112 
113 Log_message debug(std::string reason)
114 {
115  return Log_message{std::move(reason), Log_level::debug};
116 }
117 
118 Log_message info(std::string reason)
119 {
120  return Log_message{std::move(reason), Log_level::info};
121 }
122 
123 Log_message warn(std::string reason)
124 {
125  return Log_message{std::move(reason), Log_level::warn};
126 }
127 
128 Log_message fatal(std::string reason)
129 {
130  return Log_message{std::move(reason), Log_level::fatal};
131 }
132 
133 Log_message info_sdl()
134 {
135  return info(SDL_GetError());
136 }
137 
138 Log_message warn_sdl()
139 {
140  return warn(SDL_GetError());
141 }
142 
143 Log_message fatal_sdl()
144 {
145  return fatal(SDL_GetError());
146 }
147 
148 Logger& Logger::instance() noexcept
149 {
150  static Logger instance;
151  return instance;
152 }
153 
154 Log_message::Log_message(std::string reason, Log_message::Level level) noexcept
155  : reason_{std::move(reason)}
156  , message_{}
157  , active_{level >= Logger::instance().level()}
158 {
159  if (active_)
160  message_ << "ge211[" << log_level_string(level) << "]: ";
161 }
162 
163 Log_message::Log_message(Log_message::Level level)
164  : Log_message{"", level}
165 { }
166 
167 Log_message::~Log_message()
168 {
169  if (active_) {
170  std::cerr << message_.str();
171  if (!reason_.empty()) std::cerr << "\n (Reason: " << reason_ << ")";
172  std::cerr << std::endl;
173  }
174 }
175 
176 } // end namespace detail
177 
178 }
179 
The game engine namespace.
Definition: ge211.h:17
Client_logic_error(const std::string &message)
Constructs the error, given the provided error message.
Definition: ge211_error.cpp:26
The root of the ge211 exception hierarchy.
Definition: ge211_error.h:22
const char * what() const noexcept override
The error message associated with the exception.
Definition: ge211_error.cpp:13
Indicates that an error was encountered by the game engine or in the client&#39;s environment.
Definition: ge211_error.h:58