ge211  2021.5.1
A student game engine
ge211_error.hxx
1 #pragma once
2 
3 #include "ge211_forward.hxx"
4 #include "ge211_noexcept.hxx"
5 
6 #include <exception>
7 #include <memory>
8 #include <sstream>
9 #include <string>
10 
11 namespace ge211 {
12 
14 namespace exceptions {
15 
24 {
25 public:
30  const char* what() const NOEXCEPT override;
31 
32 private:
33  explicit Exception_base(const std::string& message);
34 
36  friend Client_logic_error;
37  friend Environment_error;
38 
40 };
41 
48 {
49 public:
51  explicit Client_logic_error(const std::string& message);
52 };
53 
60 {
61 public:
64  const std::string& attempted_action() const { return action_; }
65 
66 private:
67  friend class detail::Session;
68 
69  explicit Session_needed_error(const std::string& action);
70 
71  std::string action_;
72 };
73 
78 {
79  explicit Late_paint_error(char const* who);
80 
81  // Throwers
82  friend ::ge211::internal::Render_sprite;
83 };
84 
91 {
92  explicit Environment_error(const std::string& message);
93 
95  friend Window;
96 
98  friend Ge211_logic_error;
99  friend Host_error;
100 };
101 
106 {
107  explicit Ge211_logic_error(const std::string& message);
108 
110  friend Mixer;
111  friend Text_sprite;
112 };
113 
119 {
120  explicit Host_error(const std::string& extra_message = "");
121 
123  friend File_error;
124  friend Font_error;
125  friend Image_error;
126  friend Mixer_error;
127 
129  friend Text_sprite;
130  friend Window;
131  friend ::ge211::internal::Render_sprite;
132  friend class detail::Renderer;
133  friend class detail::Texture;
134 };
135 
137 class File_error final : public Host_error
138 {
139  explicit File_error(const std::string& message);
140  static File_error could_not_open(const std::string& filename);
141 
143  friend class detail::File_resource;
144 
145  template <bool>
146  friend struct detail::ifstream_opener;
147 };
148 
150 class Font_error final : public Host_error
151 {
152  explicit Font_error(const std::string& message);
153  static Font_error could_not_load(const std::string& filename);
154 
156  friend Font;
157 };
158 
160 class Image_error final : public Host_error
161 {
162  explicit Image_error(const std::string& message);
163  static Image_error could_not_load(const std::string& filename);
164 
166  friend Image_sprite;
167 };
168 
171 class Mixer_error : public Host_error
172 {
173  Mixer_error(const std::string& message);
174  static Mixer_error could_not_load(const std::string& filename);
175  static Mixer_error out_of_channels();
176  static Mixer_error not_enabled();
177 
179  friend Mixer;
180  friend Audio_clip;
181  friend Music_track;
182  friend Sound_effect;
183 };
184 
185 } // end namespace exception
186 
187 namespace internal {
188 
189 namespace logging {
190 
192 enum class Log_level
193 {
195  debug,
197  info,
199  warn,
201  fatal,
202 };
203 
206 class Logger
207 {
208 public:
210  Log_level level() const NOEXCEPT { return level_; }
211 
213  void level(Log_level level) NOEXCEPT { level_ = level; }
214 
216  static Logger& instance() NOEXCEPT;
217 
218 private:
219  Logger() NOEXCEPT = default;
220 
221  Log_level level_ = Log_level::warn;
222 };
223 
227 {
228 public:
231  explicit Log_message(Log_level level = Log_level::debug);
232 
235  explicit Log_message(std::string reason,
236  Log_level level = Log_level::debug) NOEXCEPT;
237 
239  template <typename STREAM_INSERTABLE>
240  Log_message& operator<<(STREAM_INSERTABLE const& value)
241  {
242  if (active_) message_ << value;
243  return *this;
244  }
245 
247  virtual ~Log_message();
248 
251  Log_message(const Log_message&) = delete;
252 
255  Log_message& operator=(const Log_message&) = delete;
256 
259  Log_message(Log_message&&) = default;
260 
265 
266 private:
267  std::string reason_;
268  std::ostringstream message_;
269  bool active_;
270 };
271 
273 Log_message debug(std::string reason = "");
274 
276 Log_message info(std::string reason = "");
277 
279 Log_message warn(std::string reason = "");
280 
282 Log_message fatal(std::string reason = "");
283 
284 } // end namespace logging
285 
286 } // end namespace internal
287 
288 namespace detail {
289 
291 
292 // These functions generate log messages with the reason set to the
293 // cause of SDL2's most recent message.
294 
295 Log_message info_sdl();
296 Log_message warn_sdl();
297 Log_message fatal_sdl();
298 
299 } // end namespace detail
300 
301 }
ge211::internal::logging::debug
Log_message debug(std::string reason="")
Returns a debug-level log message.
Definition: ge211_error.cxx:166
ge211::audio::Music_track
A music track, which can be attached to the Mixer and played.
Definition: ge211_audio.hxx:87
std::string
std::shared_ptr< const std::string >
ge211::internal::logging::Log_message::operator=
Log_message & operator=(const Log_message &)=delete
A Log_message cannot be copied, since that would cause it to print twice.
std::exception
ge211::internal::logging::warn
Log_message warn(std::string reason="")
Returns a warn-level log message.
Definition: ge211_error.cxx:176
ge211::internal::logging::Log_message::Log_message
Log_message(Log_message &&)=default
A log message can be moved.
ge211::internal::logging::info
Log_message info(std::string reason="")
Returns a info-level log message.
Definition: ge211_error.cxx:171
ge211::exceptions::Font_error
Indicates an error loading a font front an already-open file.
Definition: ge211_error.hxx:151
ge211::internal::logging::Logger::level
Log_level level() const
Returns the log level of this logger.
Definition: ge211_error.hxx:210
ge211::exceptions::Late_paint_error
Thrown by member functions of internal::Render_sprite when the sprite has already been rendered to th...
Definition: ge211_error.hxx:78
ge211::exceptions::Image_error
Indicates an error loading an image from an already-open file.
Definition: ge211_error.hxx:161
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::internal::logging::Log_message::Log_message
Log_message(const Log_message &)=delete
A Log_message cannot be copied, since that would cause it to print twice.
ge211::exceptions::Session_needed_error
An exception thrown when the client attempts to perform an action that requires a GE211 session befor...
Definition: ge211_error.hxx:60
ge211::audio::Audio_clip
Common interface to classes that load audio data, Music_track and Sound_effect.
Definition: ge211_audio.hxx:29
ge211::Window
Provides access to the game window and its properties.
Definition: ge211_window.hxx:17
ge211::exceptions::Session_needed_error::attempted_action
const std::string & attempted_action() const
The action that the client attempted that couldn't be completed without a GE211 session.
Definition: ge211_error.hxx:64
ge211::internal::logging::Logger::level
void level(Log_level level)
Changes the log level of this logger.
Definition: ge211_error.hxx:213
ge211::exceptions::File_error
Indicates an error opening a file.
Definition: ge211_error.hxx:138
ge211::exceptions::Client_logic_error
An exception that indicates that a logic error was performed by the client.
Definition: ge211_error.hxx:48
ge211::internal::logging::Logger
Right now a Logger just keeps track of the current log level.
Definition: ge211_error.hxx:207
ge211::internal::logging::Log_level
Log_level
How serious is this log message?
Definition: ge211_error.hxx:193
ge211::internal::logging::Log_message::operator=
Log_message & operator=(Log_message &&)=default
A log message can be move-assigned.
ge211::sprites::Text_sprite
A Sprite that displays text.
Definition: ge211_sprites.hxx:232
ge211::exceptions::Ge211_logic_error
Indicates a condition unexpected by ge211, that appears to break its invariants.
Definition: ge211_error.hxx:106
std::ostringstream
ge211::internal::logging::fatal
Log_message fatal(std::string reason="")
Returns a fatal-level log message.
Definition: ge211_error.cxx:181
ge211::exceptions::Environment_error
Indicates that an error was encountered by the game engine or in the client's environment.
Definition: ge211_error.hxx:91
ge211::internal::logging::Log_message::operator<<
Log_message & operator<<(STREAM_INSERTABLE const &value)
Appends more text to this Log_message.
Definition: ge211_error.hxx:240
ge211::audio::Mixer
The entity that coordinates playing all audio tracks.
Definition: ge211_audio.hxx:191
ge211::audio::Sound_effect
A sound effect track, which can be attached to a Mixer channel and played.
Definition: ge211_audio.hxx:125
ge211::exceptions::Mixer_error
Indicates an error in the mixer, which could include the inability to understand an audio file format...
Definition: ge211_error.hxx:172
ge211::Font
Represents a font that can be used to render a sprites::Text_sprite.
Definition: ge211_resource.hxx:70
ge211::internal::logging::Log_message
A Log_message accumulates information and then prints it all at once when it's about to be destroyed.
Definition: ge211_error.hxx:227
ge211::sprites::Image_sprite
A Sprite that displays a bitmap image.
Definition: ge211_sprites.hxx:214
ge211::internal::logging::Log_level::debug
@ debug
extra debugging information
ge211::exceptions::Host_error
Indicates an exception from the host environment being passed along by ge211.
Definition: ge211_error.hxx:119
ge211::exceptions::Exception_base::what
const char * what() const override
The error message associated with the exception.
Definition: ge211_error.cxx:20
ge211::internal::logging::Logger::instance
static Logger & instance()
Returns the one and only logger instance.
Definition: ge211_error.cxx:186
ge211::exceptions::Exception_base
The root of the ge211 exception hierarchy.
Definition: ge211_error.hxx:24