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
9#include <algorithm>
10#include <iterator>
11#include <iostream>
12#include <fstream>
13#include <string>
14#include <vector>
15
16#include <boost/lexical_cast.hpp>
17
18#include "constants.hh"
19#include "platform.hh"
20#include "sanity.hh"
21#include "transforms.hh"
22#include "ui.hh"
23
24using namespace std;
25using boost::format;
26
27// debugging / logging system
28
29sanity global_sanity;
30
31sanity::sanity() :
32 debug(false), quiet(false), relaxed(false), logbuf(0xffff)
33{
34 std::string flavour;
35 get_system_flavour(flavour);
36 L(F("started up on %s\n") % flavour);
37}
38
39sanity::~sanity()
40{}
41
42void
43sanity::dump_buffer()
44{
45 if (filename != "")
46 {
47 ofstream out(filename.c_str());
48 if (out)
49 {
50 copy(logbuf.begin(), logbuf.end(), ostream_iterator<char>(out));
51 ui.inform(string("wrote debugging log to ") + filename + "\n");
52 }
53 else
54 ui.inform("failed to write debugging log to " + filename + "\n");
55 }
56 else
57 ui.inform(string("discarding debug log (maybe you want --debug or --dump?)\n"));
58}
59
60void
61sanity::set_debug()
62{
63 quiet = false;
64 debug = true;
65
66 // it is possible that some pre-setting-of-debug data
67 // accumulated in the log buffer (during earlier option processing)
68 // so we will dump it now
69 ostringstream oss;
70 vector<string> lines;
71 copy(logbuf.begin(), logbuf.end(), ostream_iterator<char>(oss));
72 split_into_lines(oss.str(), lines);
73 for (vector<string>::const_iterator i = lines.begin(); i != lines.end(); ++i)
74 ui.inform((*i) + "\n");
75}
76
77void
78sanity::set_quiet()
79{
80 debug = false;
81 quiet = true;
82}
83
84void
85sanity::set_relaxed(bool rel)
86{
87 relaxed = rel;
88}
89
90void
91sanity::log(format const & fmt,
92 char const * file, int line)
93{
94 string str;
95 try
96 {
97 str = fmt.str();
98 }
99 catch (std::exception & e)
100 {
101 ui.inform("fatal: formatter failed on "
102 + string(file)
103 + ":" + boost::lexical_cast<string>(line)
104 + ": " + e.what());
105 throw e;
106 }
107
108 if (str.size() > constants::log_line_sz)
109 {
110 str.resize(constants::log_line_sz);
111 if (str.at(str.size() - 1) != '\n')
112 str.at(str.size() - 1) = '\n';
113 }
114 copy(str.begin(), str.end(), back_inserter(logbuf));
115 if (debug)
116 ui.inform(str);
117}
118
119void
120sanity::progress(format const & fmt,
121 char const * file, int line)
122{
123 string str;
124 try
125 {
126 str = fmt.str();
127 }
128 catch (std::exception & e)
129 {
130 ui.inform("fatal: formatter failed on "
131 + string(file)
132 + ":" + boost::lexical_cast<string>(line)
133 + ": " + e.what());
134 throw e;
135 }
136 if (str.size() > constants::log_line_sz)
137 {
138 str.resize(constants::log_line_sz);
139 if (str.at(str.size() - 1) != '\n')
140 str.at(str.size() - 1) = '\n';
141 }
142 copy(str.begin(), str.end(), back_inserter(logbuf));
143 if (! quiet)
144 ui.inform(str);
145}
146
147void
148sanity::warning(format const & fmt,
149 char const * file, int line)
150{
151 string str;
152 try
153 {
154 str = fmt.str();
155 }
156 catch (std::exception & e)
157 {
158 ui.inform("fatal: formatter failed on "
159 + string(file)
160 + ":" + boost::lexical_cast<string>(line)
161 + ": " + e.what());
162 throw e;
163 }
164 if (str.size() > constants::log_line_sz)
165 {
166 str.resize(constants::log_line_sz);
167 if (str.at(str.size() - 1) != '\n')
168 str.at(str.size() - 1) = '\n';
169 }
170 string str2 = "warning: " + str;
171 copy(str2.begin(), str2.end(), back_inserter(logbuf));
172 if (! quiet)
173 ui.warn(str);
174}
175
176void
177sanity::naughty_failure(string const & expr, format const & explain,
178 string const & file, int line)
179{
180 string message;
181 log(format("%s:%d: usage constraint '%s' violated\n") % file % line % expr,
182 file.c_str(), line);
183 prefix_lines_with("misuse: ", explain.str(), message);
184 throw informative_failure(message);
185}
186
187void
188sanity::invariant_failure(string const & expr,
189 string const & file, int line)
190{
191 format fmt =
192 format("%s:%d: invariant '%s' violated\n")
193 % file % line % expr;
194 log(fmt, file.c_str(), line);
195 throw logic_error(fmt.str());
196}
197
198void
199sanity::index_failure(string const & vec_expr,
200 string const & idx_expr,
201 unsigned long sz,
202 unsigned long idx,
203 string const & file, int line)
204{
205 format fmt =
206 format("%s:%d: index '%s' = %d overflowed vector '%s' with size %d\n")
207 % file % line % idx_expr % idx % vec_expr % sz;
208 log(fmt, file.c_str(), line);
209 throw logic_error(fmt.str());
210}

Archive Download this file

Branches

Tags

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