Debug system v2 -- now, it supports debug levels, there's a global level, but overrid...
[dbw.git] / src / console / console.hpp
blob726c22de6cc0ec7478ef9e59f50576d0b81db01f
1 /*
2 Copyright © 2007-2008 Kővágó Zoltán <DirtY.iCE.hu@gmail.com>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /** @file console/console.hpp
22 * Conole headers
23 * Headers for Console class
26 #ifndef CONSOLE_HPP
27 #define CONSOLE_HPP
29 #include <iostream>
31 /// Really this does DebugOut's job
32 class DebugStream : public std::streambuf
34 public:
35 DebugStream(const std::string& message);
36 /**
37 * Set's the actual file, line, and function
38 * @warning Don't use it directly, leave this job for debug, warning and error macros!
39 * @param file The current file (__FILE__)
40 * @param line The current line (__LINE__)
41 * @param func The current function (__PRETTY_FUNCTION__)
43 void SetPos(const std::string& file, unsigned short line, const std::string& func)
44 { sync(); this->file = file; this->line = line; this->func = func; };
46 #ifdef DEBUG
47 /// If true, it will display the current function true (only in DEBUG mode)
48 static bool show_func;
49 #endif
50 protected:
51 virtual int overflow(int c);
52 virtual int sync();
53 private:
54 void PutLineBeg();
55 /// Buffer
56 char buf[1024];
58 std::string message, file, func;
59 unsigned short line;
60 static unsigned short file_maxw, line_maxw, func_maxw;
61 bool putline;
64 /**
65 * DebugOut is a class that displays debug messages on the screen, but
66 * with printing a message; the file and line if compiled in debug mode,
67 * and also the function if it enabled by <tt>--show-func</tt> argument.
69 class DebugOut : public std::ostream
71 public:
72 /**
73 * Creates a new DebugOut.
74 * @param message The message displayed between []
76 DebugOut(const std::string& message) : std::ostream(stream = new DebugStream(message)) {};
78 /// Poniter to the stream object
79 DebugStream * stream;
80 private:
81 /// Don't copy it
82 DebugOut(const DebugOut&) : stream(NULL) {};
83 /// Really! Don't copy it. It's just stupid
84 DebugOut& operator=(const DebugOut&) { return *this; }
87 #ifdef DEBUG
89 #include <map>
91 /// Class to manage debug levels
92 class DebugLevelManager
94 public:
95 static void ParseOpts(const std::string& opts);
96 static bool IsToPrint(const std::string& module, unsigned char level);
98 protected:
99 static unsigned char global_level;
100 static std::map<std::string, unsigned char> level_map;
103 extern DebugOut debug_out, warning_out, error_out;
104 #define debug(module, level) if (DebugLevelManager::IsToPrint((module), (level))) debug_out.stream->SetPos(__FILE__ + 7, __LINE__, __PRETTY_FUNCTION__), debug_out
105 #define warning warning_out.stream->SetPos(__FILE__ + 7, __LINE__, __PRETTY_FUNCTION__), warning_out
106 #define error error_out.stream->SetPos(__FILE__ + 7, __LINE__, __PRETTY_FUNCTION__), error_out
108 #else //ifdef DEBUG
110 extern DebugOut warning_out, error_out;
111 #define debug if (false) std::cout
112 #define warning warning_out
113 #define error error_out
115 #endif //ifndef DEBUG
117 #endif