[6bc578]: libutil++ / cverb.cpp Maximize Restore History

Download this file

cverb.cpp    124 lines (99 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @file cverb.cpp
* verbose output stream
*
* @remark Copyright 2002, 2004 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include "cverb.h"
using namespace std;
cverb_object cverb;
verbose vlevel1("level1");
verbose vdebug("debug");
verbose vstats("stats");
verbose vsfile("sfile");
verbose vxml("xml");
namespace {
// The right way is to use: ofstream fout; but cverb(fout.rdbuf()) receive
// a null pointer and stl shipped with 2.91 segfault.
ofstream fout("/dev/null");
ostream null_stream(fout.rdbuf());
// Used to setup the bad bit in our null stream so output will fail earlier
// and overhead will be smaller.
struct setup_stream {
setup_stream();
};
setup_stream::setup_stream()
{
null_stream.clear(ios::badbit);
}
setup_stream setup;
// We use a multimap because user can create multiple verbose object with
// the same name, these are synonymous, setting up one to true will setup
// all with the same name to true.
typedef multimap<string, verbose *> recorder_t;
// The recorder is lazilly created by verbose object ctor
static recorder_t * object_map;
} // anonymous namespace
verbose::verbose(char const * name)
:
set(false)
{
// all params is treated a part, there is no need to create a
// verbose all("all"); it's meaningless. "all" verbose named object is
// reserved.
if (strcmp(name, "all") == 0)
return;
if (!object_map)
object_map = new recorder_t;
object_map->insert(recorder_t::value_type(name, this));
}
verbose verbose::operator|(verbose const & rhs)
{
verbose result(*this);
result.set = result.set || rhs.set;
return result;
}
verbose verbose::operator&(verbose const & rhs)
{
verbose result(*this);
result.set = result.set && rhs.set;
return result;
}
bool verbose::setup(string const & name)
{
if (name == "all") {
null_stream.rdbuf(cout.rdbuf());
null_stream.clear();
return true;
}
if (!object_map)
object_map = new recorder_t;
pair<recorder_t::iterator, recorder_t::iterator> p_it =
object_map->equal_range(name);
if (p_it.first == p_it.second)
return false;
for (; p_it.first != p_it.second; ++p_it.first)
p_it.first->second->set = true;
return true;
}
bool verbose::setup(vector<string> const & names)
{
for (size_t i = 0; i < names.size(); ++i)
if (!setup(names[i]))
return false;
return true;
}
ostream& operator<<(cverb_object &, verbose const & v)
{
return v.set ? cout : null_stream;
}