Imported Upstream version 3.0
[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 2, 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  */
52 class gr_error_handler {
53 public:
54   enum seriousness {
55     ERR_DEBUG           = 0x00000000,
56     ERR_MESSAGE         = 0x00010000,
57     ERR_WARNING         = 0x00020000,
58     ERR_ERROR           = 0x00030000,
59     ERR_FATAL           = 0x00040000
60   };
61
62   gr_error_handler() {}
63   virtual ~gr_error_handler();
64
65   static gr_error_handler *default_handler();
66   static gr_error_handler *silent_handler();
67
68   static bool has_default_handler();
69   static void set_default_handler(gr_error_handler *errh);
70
71   void debug(const char *format, ...);
72   void message(const char *format, ...);
73   void warning(const char *format, ...);
74   void error(const char *format, ...);
75   void fatal(const char *format, ...);
76
77   virtual int nwarnings() const = 0;
78   virtual int nerrors() const = 0;
79   virtual void reset_counts() = 0;
80
81   void verror(seriousness s, const char *format, va_list);
82   void verror_text(seriousness s, const std::string &text);
83
84 protected:
85   virtual void count_error(seriousness s) = 0;
86   virtual void handle_text(seriousness s, const std::string &str) = 0;
87   std::string make_text(seriousness s, const char *format, va_list);
88 };
89
90
91 class gr_base_error_handler : public gr_error_handler {
92   int   d_nwarnings;
93   int   d_nerrors;
94
95 public:
96   gr_base_error_handler() : d_nwarnings(0), d_nerrors(0) {}
97   int nwarnings() const { return d_nwarnings; }
98   int nerrors() const   { return d_nerrors; }
99   void reset_counts()   { d_nwarnings = d_nerrors = 0; }
100   void count_error(seriousness s);
101 };
102
103 class gr_file_error_handler : public gr_base_error_handler {
104   FILE          *d_file;
105   int            d_fd;
106 public:
107   gr_file_error_handler(FILE *file);
108   gr_file_error_handler(int file_descriptor);
109   ~gr_file_error_handler();
110
111   void handle_text(seriousness s, const std::string &str);
112 };
113
114 #endif /* INCLUDED_GR_ERROR_HANDLER_H */