1 // -*-c++-*-
2 /* $Id$ */
3 
4 /*
5  *
6  * Copyright (C) 1998 David Mazieres (dm@uun.org)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  *
23  */
24 
25 #ifndef _ASYNC_ERR_H_
26 #define _ASYNC_ERR_H_ 1
27 
28 #include "str.h"
29 
30 extern bssstr progname;
31 extern str progdir;
32 extern void (*fatalhook) ();
33 
34 extern int errfd;
35 extern bool fatal_no_destruct;
36 extern void (*_err_output) (suio *, int);
37 extern void (*_err_reset_hook) ();
38 void err_reset ();
39 void _err_output_sync (suio *, int);
40 
41 void setprogname (char *argv0);
42 void setprogpid (int p);
43 
44 /* Old-style C functions for compatibility */
45 extern "C" {
46   void sfs_warn (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
47   void sfs_warnx (const char *fmt, ...)
48     __attribute__ ((format (printf, 1, 2)));
49   void sfs_vwarn (const char *fmt, va_list ap);
50   void sfs_vwarnx (const char *fmt, va_list ap);
51   void fatal (const char *fmt, ...)
52     __attribute__ ((noreturn, format (printf, 1, 2)));
53   void panic (const char *fmt, ...)
54     __attribute__ ((noreturn, format (printf, 1, 2)));
55 }
56 
57 class warnobj : public strbuf {
58   const int flags;
59 public:
60   enum { xflag = 1, fatalflag = 2, panicflag = 4, timeflag = 8 };
61 
62   explicit warnobj (int);
63   ~warnobj ();
64   const warnobj &operator() (const char *fmt, ...) const
65     __attribute__ ((format (printf, 2, 3)));
66 };
67 #define warn warnobj (0)
68 #define vwarn warn.vfmt
69 #define warnx warnobj (int (::warnobj::xflag))
70 #define warnt warnobj (int (::warnobj::timeflag))
71 #define vwarnx warnx.vfmt
72 
73 #ifndef __attribute__
74 /* Fatalobj is just a warnobj with a noreturn destructor. */
75 class fatalobj : public warnobj {
76 public:
fatalobj(int f)77   explicit fatalobj (int f) : warnobj (f) {}
78   ~fatalobj () __attribute__ ((noreturn));
79 };
80 #else /* __attribute__ */
81 # define fatalobj warnobj
82 #endif /* __attribute__ */
83 #define fatal fatalobj (int (::warnobj::fatalflag))
84 #define panic fatalobj (int (::warnobj::panicflag)) ("%s\n", __BACKTRACE__)
85 
86 struct traceobj : public strbuf {
87   int current_level;
88   const char *prefix;
89   const bool dotime;
90   bool doprint;
91 
92   traceobj (int current_level, const char *prefix = "", bool dotime = false)
current_leveltraceobj93     : current_level (current_level), prefix (prefix), dotime (dotime) {}
94   ~traceobj ();
95   void init ();
96 
97   const traceobj &operator() (int threshold = 0);
98   const traceobj &operator() (int threshold, const char *fmt, ...)
99     __attribute__ ((format (printf, 3, 4)));
100 };
101 
102 template<class T> inline const traceobj &
103 operator<< (const traceobj &sb, const T &a)
104 {
105   if (sb.doprint)
106     strbuf_cat (sb, a);
107   return sb;
108 }
109 inline const traceobj &
110 operator<< (const traceobj &sb, const str &s)
111 {
112   if (sb.doprint)
113     suio_print (sb.tosuio (), s);
114   return sb;
115 }
116 
117 
118 #undef assert
119 #define assert(e)						\
120   do {								\
121     if (!(e))							\
122       panic ("assertion \"%s\" failed at %s\n", #e, __FL__);	\
123   } while (0)
124 
125 #ifdef DMALLOC
126 inline void myabort () __attribute__ ((noreturn));
127 inline void
myabort()128 myabort ()
129 {
130   static bool shutdown_called;
131   if (!shutdown_called) {
132     shutdown_called = true;
133     dmalloc_shutdown ();
134   }
135   abort ();
136 }
137 #else /* !DMALLOC */
138 # define myabort abort
139 #endif /* !DMALLOC */
140 
141 #endif /* !_ASYNC_ERR_H_ */
142