summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/gr_error_handler.cc
blob: 448682966e8ee5d45364a44b7ca898ade19ff729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- c++ -*- */
/*
 * Copyright 2005 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
/*
 * This code is based on error.cc from the "Click Modular Router".
 * Original copyright follows:
 */
/*
 * error.{cc,hh} -- flexible classes for error reporting
 * Eddie Kohler
 *
 * Copyright (c) 1999-2000 Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, subject to the conditions
 * listed in the Click LICENSE file. These conditions include: you must
 * preserve this copyright notice, and you cannot mention the copyright
 * holders in advertising related to the Software without their permission.
 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
 * notice is a summary of the Click LICENSE file; the license in that file is
 * legally binding.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gr_error_handler.h>
#include <assert.h>
#include <stdexcept>
#include <unistd.h>

#ifdef HAVE_IO_H
#include <io.h>
#endif

static gr_error_handler	*s_default_handler = 0;
static gr_error_handler *s_silent_handler = 0;

bool
gr_error_handler::has_default_handler()
{
  return s_default_handler != 0;
}

void
gr_error_handler::set_default_handler(gr_error_handler *errh)
{
  s_default_handler = errh;
}

gr_error_handler *
gr_error_handler::default_handler()
{
  assert (s_default_handler != 0);
  return s_default_handler;
}

gr_error_handler *
gr_error_handler::silent_handler()
{
  assert (s_silent_handler != 0);
  return s_silent_handler;
}

// ----------------------------------------------------------------

gr_error_handler::~gr_error_handler()
{
  // nop
}

void
gr_error_handler::debug(const char *format, ...)
{
  va_list val;
  va_start(val, format);
  verror(ERR_DEBUG, format, val);
  va_end(val);
}

void
gr_error_handler::message(const char *format, ...)
{
  va_list val;
  va_start(val, format);
  verror(ERR_MESSAGE, format, val);
  va_end(val);
}

void
gr_error_handler::warning(const char *format, ...)
{
  va_list val;
  va_start(val, format);
  verror(ERR_WARNING, format, val);
  va_end(val);
}

void
gr_error_handler::error(const char *format, ...)
{
  va_list val;
  va_start(val, format);
  verror(ERR_ERROR, format, val);
  va_end(val);
}

void
gr_error_handler::fatal(const char *format, ...)
{
  va_list val;
  va_start(val, format);
  verror(ERR_FATAL, format, val);
  va_end(val);
}

void
gr_error_handler::verror(seriousness s, const char *format, va_list val)
{
  std::string text = make_text(s, format, val);
  handle_text(s, text);
  count_error(s);
}

void
gr_error_handler::verror_text(seriousness s, const std::string &text)
{
  // text is already made
  handle_text(s, text);
  count_error(s);
}

std::string
gr_error_handler::make_text(seriousness s, const char *format, va_list val)
{
  char text_buf[4096];
  vsnprintf(text_buf, sizeof(text_buf), format, val);
  text_buf[sizeof(text_buf)-1] = 0;
  return text_buf;
}

// ----------------------------------------------------------------

void
gr_base_error_handler::count_error(seriousness s)
{
  if (s < ERR_WARNING)
    /* do nothing */;
  else if (s < ERR_ERROR)
    d_nwarnings++;
  else
    d_nerrors++;
}

// ----------------------------------------------------------------

gr_file_error_handler::gr_file_error_handler(FILE *file)
  : d_file(file), d_fd(-1)
{
}

gr_file_error_handler::gr_file_error_handler(int file_descriptor)
{
  d_fd = dup(file_descriptor);	// so we can fclose it
  if (d_fd == -1){
    perror("gr_file_error_handler:dup");
    throw std::invalid_argument("gr_file_error_handler:dup");
  }
  d_file = fdopen(d_fd, "w");
  if (d_file == 0){
    perror("gr_file_error_handler:fdopen");
    throw std::invalid_argument("gr_file_error_handler:fdopen");
  }
}

gr_file_error_handler::~gr_file_error_handler()
{
  if (d_fd != -1){
    fclose(d_file);
  }
}

void
gr_file_error_handler::handle_text(seriousness s, const std::string &text)
{
  if (text.length() <= 0)
    return;

  fwrite(text.data(), 1, text.length(), d_file);
  if (text[text.length()-1] != '\n')
    fwrite("\n", 1, 1, d_file);

  if (d_fd != -1)
    fflush(d_file);	// keep synced with any other users of fd
}


// ----------------------------------------------------------------
// static error handlers
//

class gr_silent_error_handler : public gr_base_error_handler
{
public:
  gr_silent_error_handler() {}
  void handle_text(seriousness s, const std::string &str);
};

void
gr_silent_error_handler::handle_text(seriousness s, const std::string &str)
{
  // nop
}

class force_init {
public:
  force_init()
  {
    s_default_handler = new gr_file_error_handler(stdout);
    s_silent_handler = new gr_silent_error_handler();
  }
};

static force_init	kludge;