3 * Copyright 2005 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
23 * This code is based on error.cc from the "Click Modular Router".
24 * Original copyright follows:
27 * error.{cc,hh} -- flexible classes for error reporting
30 * Copyright (c) 1999-2000 Massachusetts Institute of Technology
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, subject to the conditions
35 * listed in the Click LICENSE file. These conditions include: you must
36 * preserve this copyright notice, and you cannot mention the copyright
37 * holders in advertising related to the Software without their permission.
38 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
39 * notice is a summary of the Click LICENSE file; the license in that file is
47 #include <gr_error_handler.h>
57 static gr_error_handler *s_default_handler = 0;
58 static gr_error_handler *s_silent_handler = 0;
61 gr_error_handler::has_default_handler()
63 return s_default_handler != 0;
67 gr_error_handler::set_default_handler(gr_error_handler *errh)
69 s_default_handler = errh;
73 gr_error_handler::default_handler()
75 assert (s_default_handler != 0);
76 return s_default_handler;
80 gr_error_handler::silent_handler()
82 assert (s_silent_handler != 0);
83 return s_silent_handler;
86 // ----------------------------------------------------------------
88 gr_error_handler::~gr_error_handler()
94 gr_error_handler::debug(const char *format, ...)
97 va_start(val, format);
98 verror(ERR_DEBUG, format, val);
103 gr_error_handler::message(const char *format, ...)
106 va_start(val, format);
107 verror(ERR_MESSAGE, format, val);
112 gr_error_handler::warning(const char *format, ...)
115 va_start(val, format);
116 verror(ERR_WARNING, format, val);
121 gr_error_handler::error(const char *format, ...)
124 va_start(val, format);
125 verror(ERR_ERROR, format, val);
130 gr_error_handler::fatal(const char *format, ...)
133 va_start(val, format);
134 verror(ERR_FATAL, format, val);
139 gr_error_handler::verror(seriousness s, const char *format, va_list val)
141 std::string text = make_text(s, format, val);
142 handle_text(s, text);
147 gr_error_handler::verror_text(seriousness s, const std::string &text)
149 // text is already made
150 handle_text(s, text);
155 gr_error_handler::make_text(seriousness s, const char *format, va_list val)
158 vsnprintf(text_buf, sizeof(text_buf), format, val);
159 text_buf[sizeof(text_buf)-1] = 0;
163 // ----------------------------------------------------------------
166 gr_base_error_handler::count_error(seriousness s)
170 else if (s < ERR_ERROR)
176 // ----------------------------------------------------------------
178 gr_file_error_handler::gr_file_error_handler(FILE *file)
179 : d_file(file), d_fd(-1)
183 gr_file_error_handler::gr_file_error_handler(int file_descriptor)
185 d_fd = dup(file_descriptor); // so we can fclose it
187 perror("gr_file_error_handler:dup");
188 throw std::invalid_argument("gr_file_error_handler:dup");
190 d_file = fdopen(d_fd, "w");
192 perror("gr_file_error_handler:fdopen");
193 throw std::invalid_argument("gr_file_error_handler:fdopen");
197 gr_file_error_handler::~gr_file_error_handler()
205 gr_file_error_handler::handle_text(seriousness s, const std::string &text)
207 if (text.length() <= 0)
210 fwrite(text.data(), 1, text.length(), d_file);
211 if (text[text.length()-1] != '\n')
212 fwrite("\n", 1, 1, d_file);
215 fflush(d_file); // keep synced with any other users of fd
219 // ----------------------------------------------------------------
220 // static error handlers
223 class gr_silent_error_handler : public gr_base_error_handler
226 gr_silent_error_handler() {}
227 void handle_text(seriousness s, const std::string &str);
231 gr_silent_error_handler::handle_text(seriousness s, const std::string &str)
240 s_default_handler = new gr_file_error_handler(stdout);
241 s_silent_handler = new gr_silent_error_handler();
245 static force_init kludge;