1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | |
---|
21 | #include "sbuild-ctty.h" |
---|
22 | #include "sbuild-error.h" |
---|
23 | #include "sbuild-i18n.h" |
---|
24 | #include "sbuild-log.h" |
---|
25 | #include "sbuild-nostream.h" |
---|
26 | #include "sbuild-util.h" |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | |
---|
30 | #include <boost/format.hpp> |
---|
31 | |
---|
32 | using boost::format; |
---|
33 | |
---|
34 | namespace |
---|
35 | { |
---|
36 | |
---|
37 | /** |
---|
38 | * Log an exception reason. Log the reason an exception was thrown, |
---|
39 | * if the exception contains reason information. |
---|
40 | * |
---|
41 | * @param e the exception to log. |
---|
42 | * @param ctty true to log to the CTTY or false to log to cerr. |
---|
43 | */ |
---|
44 | void |
---|
45 | log_reason (std::exception const& e, |
---|
46 | bool ctty) |
---|
47 | { |
---|
48 | try |
---|
49 | { |
---|
50 | sbuild::error_base const& eb(dynamic_cast<sbuild::error_base const&>(e)); |
---|
51 | sbuild::string_list lines = sbuild::split_string(eb.why(), "\n"); |
---|
52 | for (sbuild::string_list::const_iterator line = lines.begin(); |
---|
53 | line != lines.end(); |
---|
54 | ++line) |
---|
55 | ctty ? sbuild::log_ctty_info() : sbuild::log_info() |
---|
56 | << *line << std::endl; |
---|
57 | } |
---|
58 | catch (std::bad_cast const& discard) |
---|
59 | { |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Log an exception reason as an informational message. |
---|
65 | * |
---|
66 | * @param e the exception to log. |
---|
67 | */ |
---|
68 | void |
---|
69 | log_exception_reason (std::exception const& e) |
---|
70 | { |
---|
71 | log_reason(e, false); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Log an exception reason as as an informational message to the |
---|
76 | * Controlling TTY. |
---|
77 | * |
---|
78 | * @param e the exception to log. |
---|
79 | */ |
---|
80 | void |
---|
81 | log_ctty_exception_reason (std::exception const& e) |
---|
82 | { |
---|
83 | log_reason(e, true); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | std::ostream& |
---|
88 | sbuild::log_info () |
---|
89 | { |
---|
90 | // TRANSLATORS: "I" is an abbreviation of "Information" |
---|
91 | return std::cerr << _("I: "); |
---|
92 | } |
---|
93 | |
---|
94 | std::ostream& |
---|
95 | sbuild::log_warning () |
---|
96 | { |
---|
97 | // TRANSLATORS: "W" is an abbreviation of "Warning" |
---|
98 | return std::cerr << _("W: "); |
---|
99 | } |
---|
100 | |
---|
101 | std::ostream& |
---|
102 | sbuild::log_error () |
---|
103 | { |
---|
104 | // TRANSLATORS: "E" is an abbreviation of "Error" |
---|
105 | return std::cerr << _("E: "); |
---|
106 | } |
---|
107 | |
---|
108 | std::ostream& |
---|
109 | sbuild::log_debug (sbuild::debug_level level) |
---|
110 | { |
---|
111 | if (debug_log_level > 0 && |
---|
112 | level >= debug_log_level) |
---|
113 | // TRANSLATORS: %1% = integer debug level |
---|
114 | // TRANSLATORS: "D" is an abbreviation of "Debug" |
---|
115 | return std::cerr << format(_("D(%1%): ")) % level; |
---|
116 | else |
---|
117 | return sbuild::cnull; |
---|
118 | } |
---|
119 | |
---|
120 | std::ostream& |
---|
121 | sbuild::log_ctty_info () |
---|
122 | { |
---|
123 | // TRANSLATORS: "I" is an abbreviation of "Information" |
---|
124 | return cctty << _("I: "); |
---|
125 | } |
---|
126 | |
---|
127 | std::ostream& |
---|
128 | sbuild::log_ctty_warning () |
---|
129 | { |
---|
130 | // TRANSLATORS: "W" is an abbreviation of "Warning" |
---|
131 | return cctty << _("W: "); |
---|
132 | } |
---|
133 | |
---|
134 | std::ostream& |
---|
135 | sbuild::log_ctty_error () |
---|
136 | { |
---|
137 | // TRANSLATORS: "E" is an abbreviation of "Error" |
---|
138 | return cctty << _("E: "); |
---|
139 | } |
---|
140 | |
---|
141 | void |
---|
142 | sbuild::log_exception_warning (std::exception const& e) |
---|
143 | { |
---|
144 | log_warning() << e.what() << std::endl; |
---|
145 | log_exception_reason(e); |
---|
146 | } |
---|
147 | |
---|
148 | void |
---|
149 | sbuild::log_exception_error (std::exception const& e) |
---|
150 | { |
---|
151 | log_error() << e.what() << std::endl; |
---|
152 | log_exception_reason(e); |
---|
153 | } |
---|
154 | |
---|
155 | void |
---|
156 | sbuild::log_ctty_exception_warning (std::exception const& e) |
---|
157 | { |
---|
158 | log_ctty_warning() << e.what() << std::endl; |
---|
159 | log_ctty_exception_reason(e); |
---|
160 | } |
---|
161 | |
---|
162 | void |
---|
163 | sbuild::log_ctty_exception_error (std::exception const& e) |
---|
164 | { |
---|
165 | log_ctty_error() << e.what() << std::endl; |
---|
166 | log_ctty_exception_reason(e); |
---|
167 | } |
---|
168 | |
---|
169 | void |
---|
170 | sbuild::log_unknown_exception_error () |
---|
171 | { |
---|
172 | log_error() << _("An unknown exception occurred") << std::endl; |
---|
173 | } |
---|
174 | |
---|
175 | sbuild::debug_level sbuild::debug_log_level = sbuild::DEBUG_NONE; |
---|