Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_error_handler.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22 /*
23  * This code is based on error.hh from the "Click Modular Router".
24  * Original copyright follows:
25  */
26 /* 
27  * error.{cc,hh} -- flexible classes for error reporting
28  * Eddie Kohler
29  *
30  * Copyright (c) 1999-2000 Massachusetts Institute of Technology
31  *
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
40  * legally binding.
41  */
42
43 #ifndef INCLUDED_GR_ERROR_HANDLER_H
44 #define INCLUDED_GR_ERROR_HANDLER_H
45
46 #include <stdarg.h>
47 #include <string>
48
49 /*!
50  * \brief abstract error handler
51  * \ingroup base
52  */
53 class gr_error_handler {
54 public:
55   enum seriousness {
56     ERR_DEBUG           = 0x00000000,
57     ERR_MESSAGE         = 0x00010000,
58     ERR_WARNING         = 0x00020000,
59     ERR_ERROR           = 0x00030000,
60     ERR_FATAL           = 0x00040000
61   };
62
63   gr_error_handler() {}
64   virtual ~gr_error_handler();
65
66   static gr_error_handler *default_handler();
67   static gr_error_handler *silent_handler();
68
69   static bool has_default_handler();
70   static void set_default_handler(gr_error_handler *errh);
71
72   void debug(const char *format, ...);
73   void message(const char *format, ...);
74   void warning(const char *format, ...);
75   void error(const char *format, ...);
76   void fatal(const char *format, ...);
77
78   virtual int nwarnings() const = 0;
79   virtual int nerrors() const = 0;
80   virtual void reset_counts() = 0;
81
82   void verror(seriousness s, const char *format, va_list);
83   void verror_text(seriousness s, const std::string &text);
84
85 protected:
86   virtual void count_error(seriousness s) = 0;
87   virtual void handle_text(seriousness s, const std::string &str) = 0;
88   std::string make_text(seriousness s, const char *format, va_list);
89 };
90
91
92 class gr_base_error_handler : public gr_error_handler {
93   int   d_nwarnings;
94   int   d_nerrors;
95
96 public:
97   gr_base_error_handler() : d_nwarnings(0), d_nerrors(0) {}
98   int nwarnings() const { return d_nwarnings; }
99   int nerrors() const   { return d_nerrors; }
100   void reset_counts()   { d_nwarnings = d_nerrors = 0; }
101   void count_error(seriousness s);
102 };
103
104 class gr_file_error_handler : public gr_base_error_handler {
105   FILE          *d_file;
106   int            d_fd;
107 public:
108   gr_file_error_handler(FILE *file);
109   gr_file_error_handler(int file_descriptor);
110   ~gr_file_error_handler();
111
112   void handle_text(seriousness s, const std::string &str);
113 };
114
115 #endif /* INCLUDED_GR_ERROR_HANDLER_H */