Bug 574: Check whether the Console instance still exists before accessing it.
[supertux.git] / src / util / log.cpp
1 //  SuperTux Debug Helper Functions
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "util/log.hpp"
19
20 #include <iostream>
21
22 #include "math/rectf.hpp"
23 #include "supertux/console.hpp"
24
25 static std::ostream& get_logging_instance (void)
26 {
27   if (Console::instance != NULL)
28     return (Console::output);
29   else
30     return (std::cerr);
31 }
32
33 static std::ostream& log_generic_f (const char *prefix, const char* file, int line)
34 {
35   get_logging_instance () << prefix << " " << file << ":" << line << " ";
36   return (get_logging_instance ());
37 }
38
39 std::ostream& log_debug_f(const char* file, int line) 
40 {
41   return (log_generic_f ("[DEBUG]", file, line));
42 }
43
44 std::ostream& log_info_f(const char* file, int line) 
45 {
46   return (log_generic_f ("[INFO]", file, line));
47 }
48
49 std::ostream& log_warning_f(const char* file, int line) 
50 {
51   return (log_generic_f ("[WARNING]", file, line));
52 }
53
54 std::ostream& log_fatal_f(const char* file, int line) 
55 {
56   return (log_generic_f ("[FATAL]", file, line));
57 }
58
59 std::ostream& operator<<(std::ostream& out, const Vector& vector)
60 {
61   out << '[' << vector.x << ',' << vector.y << ']';
62   return out;
63 }
64
65 std::ostream& operator<<(std::ostream& out, const Rectf& rect)
66 {
67   out << "[" << rect.get_left() << "," << rect.get_top() << "   "
68       << rect.get_right() << "," << rect.get_bottom() << "]";
69   return out;
70 }
71
72 /* EOF */