Debug system v2 -- now, it supports debug levels, there's a global level, but overrid...
[dbw.git] / src / console / console.cpp
blob056bcf8b93ec3fc58c10888ff00fd6daab41dce1
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.cpp
22 * Console routines
23 * Contains methods to write to console
26 #include <cstdio>
27 #include <iomanip>
28 #include <sstream>
30 #include "console.hpp"
32 /**
33 * Initializes DebugStream and set the message to
34 * @param message Set message to
36 DebugStream::DebugStream(const std::string& message) : message(message), file(""), func(""), line(0), putline(true)
38 setp(buf, buf + sizeof(buf));
41 /// If the buffer is out of space
42 int DebugStream::overflow(int c)
44 size_t size = pptr() - pbase();
45 if (!size) return 0;
47 for (char * pos = pbase(); pos < pptr(); pos++)
49 if (putline)
50 PutLineBeg();
52 putline = (*pos == '\n');
54 putchar(*pos);
57 if (c != traits_type::eof())
59 if (putline)
60 PutLineBeg();
62 putline = (char(c) == '\n');
64 putchar(char(c));
67 setp(buf, buf + sizeof(buf));
68 return 0;
71 /// Flush the cache
72 int DebugStream::sync()
74 return overflow(traits_type::eof());
77 /// Writes out the beginning of the line (message, func, file, line)
78 void DebugStream::PutLineBeg()
80 std::stringstream str;
82 #ifdef DEBUG
83 if (file.size() > file_maxw) file_maxw = file.size();
84 str << line;
85 if (str.str().size() > line_maxw) line_maxw = str.str().size();
86 str.str("");
87 if (func.size() > func_maxw) func_maxw = func.size();
89 str << "[" << message << "] { ";
91 if (show_func)
92 str << std::setw(func_maxw) << func << " (";
94 str << std::setw(file_maxw) << file << ":" << std::setw(line_maxw) << line;
96 if (show_func)
97 str << ")";
98 str << " } : ";
100 #else
101 str << "[" << message << "] ";
102 #endif
104 fputs(str.str().c_str(), stdout);
107 unsigned short DebugStream::file_maxw = 16;
108 unsigned short DebugStream::line_maxw = 3;
109 unsigned short DebugStream::func_maxw = 74;
110 bool DebugStream::show_func = false;
112 #ifdef DEBUG
114 #include <boost/lexical_cast.hpp>
115 #include <boost/tokenizer.hpp>
117 /* static */ void DebugLevelManager::ParseOpts(const std::string& opts)
119 boost::char_separator<char> sep(",");
120 boost::tokenizer<boost::char_separator<char> > tokens(opts, sep);
122 for (boost::tokenizer<boost::char_separator<char> >::iterator iter = tokens.begin(); iter != tokens.end(); iter++)
124 size_t pos;
125 if ((pos = iter->find_first_of("=")) == std::string::npos)
129 global_level = boost::lexical_cast<unsigned short>(*iter);
131 catch (const boost::bad_lexical_cast& e)
133 error << "Failed to set global level to '" << *iter << "'!" << std::endl
134 << "The error was: " << e.what() << " Parameter ignored." << std::endl;
137 else
141 level_map[iter->substr(0, pos)] = boost::lexical_cast<unsigned short>(iter->substr(pos + 1, iter->size() - pos - 1));
143 catch (const boost::bad_lexical_cast& e)
145 error << "Failed to set '" << iter->substr(0, pos) << "' group to '" << iter->substr(pos + 1, iter->size() - pos - 1) << "'!" << std::endl
146 << "The error was: " << e.what() << " Ignored." << std::endl;
152 /* static */ bool DebugLevelManager::IsToPrint(const std::string& module, const unsigned char level)
154 if (level_map.count(module) == 0)
155 return global_level >= level;
156 else
157 return level_map[module] >= level;
160 unsigned char DebugLevelManager::global_level = 0;
161 std::map<std::string, unsigned char> DebugLevelManager::level_map;
163 DebugOut debug_out (" Debug ");
164 #endif //ifdef DEBUG
165 DebugOut warning_out("WARNING");
166 DebugOut error_out ("!ERROR!");