mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-02 13:03:31 +00:00
Add a helper class to redirect the logger output.
This is added for some unit tests that will check the output of the logs for validation. (The unit tests will be committed once their classes are finished.)
This commit is contained in:
parent
097c48f5f2
commit
3bdec13280
30
src/log.cpp
30
src/log.cpp
@ -46,8 +46,29 @@ static std::ostream null_ostream(new null_streambuf);
|
||||
static int indent = 0;
|
||||
static bool timestamp = true;
|
||||
|
||||
static std::ostream *output_stream = NULL;
|
||||
|
||||
static std::ostream& output()
|
||||
{
|
||||
if(output_stream) {
|
||||
return *output_stream;
|
||||
}
|
||||
return std::cerr;
|
||||
}
|
||||
|
||||
namespace lg {
|
||||
|
||||
tredirect_output_setter::tredirect_output_setter(std::ostream& stream)
|
||||
: old_stream_(output_stream)
|
||||
{
|
||||
output_stream = &stream;
|
||||
}
|
||||
|
||||
tredirect_output_setter::~tredirect_output_setter()
|
||||
{
|
||||
output_stream = old_stream_;
|
||||
}
|
||||
|
||||
typedef std::map<std::string, int> domain_map;
|
||||
static domain_map *domains;
|
||||
void timestamps(bool t) { timestamp = t; }
|
||||
@ -108,17 +129,18 @@ std::ostream &logger::operator()(log_domain const &domain, bool show_names, bool
|
||||
if (severity_ > domain.domain_->second)
|
||||
return null_ostream;
|
||||
else {
|
||||
std::ostream& stream = output();
|
||||
if(do_indent) {
|
||||
for(int i = 0; i != indent; ++i)
|
||||
std::cerr << " ";
|
||||
stream << " ";
|
||||
}
|
||||
if (timestamp) {
|
||||
std::cerr << get_timestamp(time(NULL));
|
||||
stream << get_timestamp(time(NULL));
|
||||
}
|
||||
if (show_names) {
|
||||
std::cerr << name_ << ' ' << domain.domain_->first << ": ";
|
||||
stream << name_ << ' ' << domain.domain_->first << ": ";
|
||||
}
|
||||
return std::cerr;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
|
||||
|
29
src/log.hpp
29
src/log.hpp
@ -35,6 +35,35 @@
|
||||
|
||||
namespace lg {
|
||||
|
||||
/**
|
||||
* Helper class to redirect the output of the logger in a certain scope.
|
||||
*
|
||||
* The main usage of the redirection is for the unit tests to validate the
|
||||
* ourput on the logger with the expected output.
|
||||
*/
|
||||
class tredirect_output_setter
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param stream The stream to direct the output to.
|
||||
*/
|
||||
explicit tredirect_output_setter(std::ostream& stream);
|
||||
|
||||
~tredirect_output_setter();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* The previously set redirection.
|
||||
*
|
||||
* This value is stored here to be restored in this destructor.
|
||||
*/
|
||||
std::ostream* old_stream_;
|
||||
};
|
||||
|
||||
class logger;
|
||||
|
||||
typedef std::pair<const std::string, int> logd;
|
||||
|
Loading…
x
Reference in New Issue
Block a user