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
23 * Contains methods to write to console
30 #include "console.hpp"
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();
47 for (char * pos
= pbase(); pos
< pptr(); pos
++)
52 putline
= (*pos
== '\n');
57 if (c
!= traits_type::eof())
62 putline
= (char(c
) == '\n');
67 setp(buf
, buf
+ sizeof(buf
));
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
;
83 if (file
.size() > file_maxw
) file_maxw
= file
.size();
85 if (str
.str().size() > line_maxw
) line_maxw
= str
.str().size();
87 if (func
.size() > func_maxw
) func_maxw
= func
.size();
89 str
<< "[" << message
<< "] { ";
92 str
<< std::setw(func_maxw
) << func
<< " (";
94 str
<< std::setw(file_maxw
) << file
<< ":" << std::setw(line_maxw
) << line
;
101 str
<< "[" << message
<< "] ";
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;
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
++)
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
;
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
;
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 ");
165 DebugOut
warning_out("WARNING");
166 DebugOut
error_out ("!ERROR!");