Flowgrind
Advanced TCP traffic generator
fg_error.c
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2014 Alexander Zimmermann <alexander.zimmermann@netapp.com>
8  *
9  * This file is part of Flowgrind.
10  *
11  * Flowgrind is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * Flowgrind is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Flowgrind. If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29 
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include "fg_progname.h"
36 #include "fg_error.h"
37 
38 void error(enum error_levels level, int errnum, const char *fmt, ...)
39 {
40 
41  const char *err_prefix = NULL;
42  switch (level) {
43  case ERR_WARNING:
44  err_prefix = "warning";
45  break;
46  case ERR_ERROR:
47  case ERR_CRIT:
48  err_prefix = "error";
49  break;
50  default:
51  err_prefix = "unknown error";
52  }
53 
54  fprintf(stderr, "%s: %s: ", progname, err_prefix);
55 
56  va_list ap;
57  va_start(ap, fmt);
58  vfprintf(stderr, fmt, ap);
59  va_end(ap);
60 
61  const char *err_errnum = NULL;
62  if (errnum) {
63  err_errnum = strerror(errnum);
64  if (!err_errnum)
65  err_errnum = "unknown system error";
66  fprintf (stderr, ": %s", err_errnum);
67  }
68 
69  fprintf(stderr, "\n");
70  fflush (stderr);
71 
72  if (level > ERR_ERROR)
73  exit(EXIT_FAILURE);
74 }
fg_progname.h
Program name management.
ERR_CRIT
@ ERR_CRIT
Critical conditions.
Definition: fg_error.h:63
fg_error.h
Error-reporting routines used by Flowgrind.
error
void error(enum error_levels level, int errnum, const char *fmt,...)
Definition: fg_error.c:38
ERR_WARNING
@ ERR_WARNING
Warning conditions.
Definition: fg_error.h:59
ERR_ERROR
@ ERR_ERROR
Error conditions.
Definition: fg_error.h:61
error_levels
error_levels
Error level, in order of increasing importance.
Definition: fg_error.h:57
config.h
progname
const char * progname
String containing name the program is called with.
Definition: fg_progname.c:35