File: err.h

package info (click to toggle)
mailavenger 0.8.1-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,860 kB
  • ctags: 5,015
  • sloc: cpp: 21,195; ansic: 15,232; sh: 10,544; makefile: 265
file content (141 lines) | stat: -rw-r--r-- 3,765 bytes parent folder | download | duplicates (4)
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
// -*-c++-*-
/* $Id$ */

/*
 *
 * Copyright (C) 1998 David Mazieres (dm@uun.org)
 *
 * This program 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 2, or (at
 * your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#ifndef _ASYNC_ERR_H_
#define _ASYNC_ERR_H_ 1

#include "str.h"

extern bssstr progname;
extern str progdir;
extern void (*fatalhook) ();

extern int errfd;
extern bool fatal_no_destruct;
extern void (*_err_output) (suio *, int);
extern void (*_err_reset_hook) ();
void err_reset ();
void _err_output_sync (suio *, int);

void setprogname (char *argv0);
void setprogpid (int p);

/* Old-style C functions for compatibility */
extern "C" {
  void sfs_warn (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  void sfs_warnx (const char *fmt, ...)
    __attribute__ ((format (printf, 1, 2)));
  void sfs_vwarn (const char *fmt, va_list ap);
  void sfs_vwarnx (const char *fmt, va_list ap);
  void fatal (const char *fmt, ...)
    __attribute__ ((noreturn, format (printf, 1, 2)));
  void panic (const char *fmt, ...)
    __attribute__ ((noreturn, format (printf, 1, 2)));
}

class warnobj : public strbuf {
  const int flags;
public:
  enum { xflag = 1, fatalflag = 2, panicflag = 4, timeflag = 8 };

  explicit warnobj (int);
  ~warnobj ();
  const warnobj &operator() (const char *fmt, ...) const
    __attribute__ ((format (printf, 2, 3)));
};
#define warn warnobj (0)
#define vwarn warn.vfmt
#define warnx warnobj (int (::warnobj::xflag))
#define warnt warnobj (int (::warnobj::timeflag))
#define vwarnx warnx.vfmt

#ifndef __attribute__
/* Fatalobj is just a warnobj with a noreturn destructor. */
class fatalobj : public warnobj {
public:
  explicit fatalobj (int f) : warnobj (f) {}
  ~fatalobj () __attribute__ ((noreturn));
};
#else /* __attribute__ */
# define fatalobj warnobj
#endif /* __attribute__ */
#define fatal fatalobj (int (::warnobj::fatalflag))
#define panic fatalobj (int (::warnobj::panicflag)) ("%s\n", __BACKTRACE__)

struct traceobj : public strbuf {
  int current_level;
  const char *prefix;
  const bool dotime;
  bool doprint;

  traceobj (int current_level, const char *prefix = "", bool dotime = false)
    : current_level (current_level), prefix (prefix), dotime (dotime) {}
  ~traceobj ();
  void init ();

  const traceobj &operator() (int threshold = 0);
  const traceobj &operator() (int threshold, const char *fmt, ...)
    __attribute__ ((format (printf, 3, 4)));
};

template<class T> inline const traceobj &
operator<< (const traceobj &sb, const T &a)
{
  if (sb.doprint)
    strbuf_cat (sb, a);
  return sb;
}
inline const traceobj &
operator<< (const traceobj &sb, const str &s)
{
  if (sb.doprint)
    suio_print (sb.tosuio (), s);
  return sb;
}


#undef assert
#define assert(e)						\
  do {								\
    if (!(e))							\
      panic ("assertion \"%s\" failed at %s\n", #e, __FL__);	\
  } while (0)

#ifdef DMALLOC
inline void myabort () __attribute__ ((noreturn));
inline void
myabort ()
{
  static bool shutdown_called;
  if (!shutdown_called) {
    shutdown_called = true;
    dmalloc_shutdown ();
  }
  abort ();
}
#else /* !DMALLOC */
# define myabort abort
#endif /* !DMALLOC */

#endif /* !_ASYNC_ERR_H_ */