monotone

monotone Mtn Source Tree

Root/sanity.cc

1// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
2// all rights reserved.
3// licensed to the public under the terms of the GNU GPL (>= 2)
4// see the file COPYING for details
5
6#include <stdio.h>
7#include <stdarg.h>
8#include <algorithm>
9#include <iterator>
10#include <iostream>
11#include <string>
12
13#include <boost/lexical_cast.hpp>
14
15#include "constants.hh"
16#include "sanity.hh"
17#include "ui.hh"
18
19using namespace std;
20using boost::format;
21
22// debugging / logging system
23
24sanity global_sanity;
25
26sanity::sanity() :
27 verbose(false), quiet(false), logbuf(0xffff)
28{}
29
30sanity::~sanity()
31{}
32
33void sanity::dump_buffer()
34{
35 copy(logbuf.begin(), logbuf.end(), ostream_iterator<char>(cout));
36}
37
38void sanity::set_verbose()
39{
40 quiet = false;
41 verbose = true;
42}
43
44void sanity::set_quiet()
45{
46 verbose = false;
47 quiet = true;
48}
49
50void sanity::log(format const & fmt)
51{
52 string str = fmt.str();
53 if (str.size() > constants::log_line_sz)
54 {
55 str.resize(constants::log_line_sz);
56 if (str.at(str.size() - 1) != '\n')
57str.at(str.size() - 1) = '\n';
58 }
59 copy(str.begin(), str.end(), back_inserter(logbuf));
60 if (verbose)
61 ui.inform(str);
62}
63
64void sanity::progress(format const & fmt)
65{
66 string str = fmt.str();
67 if (str.size() > constants::log_line_sz)
68 {
69 str.resize(constants::log_line_sz);
70 if (str.at(str.size() - 1) != '\n')
71str.at(str.size() - 1) = '\n';
72 }
73 copy(str.begin(), str.end(), back_inserter(logbuf));
74 if (! quiet)
75 ui.inform(str);
76}
77
78void sanity::warning(format const & fmt)
79{
80 string str = fmt.str();
81 if (str.size() > constants::log_line_sz)
82 {
83 str.resize(constants::log_line_sz);
84 if (str.at(str.size() - 1) != '\n')
85str.at(str.size() - 1) = '\n';
86 }
87 string str2 = "warning: " + str;
88 copy(str2.begin(), str2.end(), back_inserter(logbuf));
89 if (! quiet)
90 ui.warn(str);
91}
92
93void sanity::naughty_failure(string const & expr, format const & explain,
94 string const & file, int line)
95{
96 log(format("%s:%d: usage constraint '%s' violated\n") % file % line % expr);
97 throw informative_failure(string("misuse: ") + explain.str());
98}
99
100void sanity::invariant_failure(string const & expr,
101 string const & file, int line)
102{
103 format fmt =
104 format("%s:%d: invariant '%s' violated\n")
105 % file % line % expr;
106 log(fmt);
107 throw logic_error(fmt.str());
108}
109
110void sanity::index_failure(string const & vec_expr,
111 string const & idx_expr,
112 unsigned long sz,
113 unsigned long idx,
114 string const & file, int line)
115{
116 format fmt =
117 format("%s:%d: index '%s' = %d overflowed vector '%s' with size %d\n")
118 % file % line % idx_expr % idx % vec_expr % sz;
119 log(fmt);
120 throw logic_error(fmt.str());
121}
122

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status