xref: /freebsd-13.1/lib/libdevdctl/exception.h (revision 7a0c41d5)
1*7a0c41d5SAlan Somers /*-
2*7a0c41d5SAlan Somers  * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3*7a0c41d5SAlan Somers  * All rights reserved.
4*7a0c41d5SAlan Somers  *
5*7a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
6*7a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
7*7a0c41d5SAlan Somers  * are met:
8*7a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
9*7a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
10*7a0c41d5SAlan Somers  *    without modification.
11*7a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*7a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
13*7a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
14*7a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
15*7a0c41d5SAlan Somers  *    binary redistribution.
16*7a0c41d5SAlan Somers  *
17*7a0c41d5SAlan Somers  * NO WARRANTY
18*7a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*7a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*7a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*7a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*7a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
29*7a0c41d5SAlan Somers  *
30*7a0c41d5SAlan Somers  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31*7a0c41d5SAlan Somers  *
32*7a0c41d5SAlan Somers  * $FreeBSD$
33*7a0c41d5SAlan Somers  */
34*7a0c41d5SAlan Somers 
35*7a0c41d5SAlan Somers /**
36*7a0c41d5SAlan Somers  * \file zfsd_exception.h
37*7a0c41d5SAlan Somers  *
38*7a0c41d5SAlan Somers  * Definition of the ZfsdException class hierarchy.  All exceptions
39*7a0c41d5SAlan Somers  * explicitly thrown by Zfsd are defined here.
40*7a0c41d5SAlan Somers  */
41*7a0c41d5SAlan Somers #ifndef	_DEVDCTL_EXCEPTION_H_
42*7a0c41d5SAlan Somers #define	_DEVDCTL_EXCEPTION_H_
43*7a0c41d5SAlan Somers 
44*7a0c41d5SAlan Somers /*============================ Namespace Control =============================*/
45*7a0c41d5SAlan Somers namespace DevdCtl
46*7a0c41d5SAlan Somers {
47*7a0c41d5SAlan Somers 
48*7a0c41d5SAlan Somers /*============================= Class Definitions ============================*/
49*7a0c41d5SAlan Somers 
50*7a0c41d5SAlan Somers /*--------------------------------- Exception --------------------------------*/
51*7a0c41d5SAlan Somers /**
52*7a0c41d5SAlan Somers  * \brief Class allowing unified reporting/logging of exceptional events.
53*7a0c41d5SAlan Somers  */
54*7a0c41d5SAlan Somers class Exception
55*7a0c41d5SAlan Somers {
56*7a0c41d5SAlan Somers public:
57*7a0c41d5SAlan Somers 	/**
58*7a0c41d5SAlan Somers 	 * \brief Exception constructor allowing arbitrary string
59*7a0c41d5SAlan Somers 	 *        data to be reported.
60*7a0c41d5SAlan Somers 	 *
61*7a0c41d5SAlan Somers 	 * \param fmt  Printf-like string format specifier.
62*7a0c41d5SAlan Somers 	 */
63*7a0c41d5SAlan Somers 	Exception(const char *fmt, ...);
64*7a0c41d5SAlan Somers 
65*7a0c41d5SAlan Somers 	/**
66*7a0c41d5SAlan Somers 	 * \brief Augment/Modify a Exception's string data.
67*7a0c41d5SAlan Somers 	 */
68*7a0c41d5SAlan Somers 	std::string& GetString();
69*7a0c41d5SAlan Somers 
70*7a0c41d5SAlan Somers 	/**
71*7a0c41d5SAlan Somers 	 * \brief Emit exception data to syslog(3).
72*7a0c41d5SAlan Somers 	 */
73*7a0c41d5SAlan Somers 	virtual void Log() const;
74*7a0c41d5SAlan Somers 
75*7a0c41d5SAlan Somers protected:
76*7a0c41d5SAlan Somers 	Exception();
77*7a0c41d5SAlan Somers 
78*7a0c41d5SAlan Somers 	/**
79*7a0c41d5SAlan Somers 	 * \brief Convert exception string format and arguments provided
80*7a0c41d5SAlan Somers 	 *        in event constructors into a linear string.
81*7a0c41d5SAlan Somers 	 */
82*7a0c41d5SAlan Somers 	void FormatLog(const char *fmt, va_list ap);
83*7a0c41d5SAlan Somers 
84*7a0c41d5SAlan Somers 	std::string   m_log;
85*7a0c41d5SAlan Somers };
86*7a0c41d5SAlan Somers 
87*7a0c41d5SAlan Somers inline std::string &
GetString()88*7a0c41d5SAlan Somers Exception::GetString()
89*7a0c41d5SAlan Somers {
90*7a0c41d5SAlan Somers 	return (m_log);
91*7a0c41d5SAlan Somers }
92*7a0c41d5SAlan Somers 
93*7a0c41d5SAlan Somers /*------------------------------ ParseException ------------------------------*/
94*7a0c41d5SAlan Somers /**
95*7a0c41d5SAlan Somers  * Exception thrown when an event string is not converted to an actionable
96*7a0c41d5SAlan Somers  * Event object.
97*7a0c41d5SAlan Somers  */
98*7a0c41d5SAlan Somers class ParseException : public Exception
99*7a0c41d5SAlan Somers {
100*7a0c41d5SAlan Somers public:
101*7a0c41d5SAlan Somers 	enum Type
102*7a0c41d5SAlan Somers 	{
103*7a0c41d5SAlan Somers 		/** Improperly formatted event string encountered. */
104*7a0c41d5SAlan Somers 		INVALID_FORMAT,
105*7a0c41d5SAlan Somers 
106*7a0c41d5SAlan Somers 		/** No handlers for this event type. */
107*7a0c41d5SAlan Somers 		DISCARDED_EVENT_TYPE,
108*7a0c41d5SAlan Somers 
109*7a0c41d5SAlan Somers 		/** Unhandled event type. */
110*7a0c41d5SAlan Somers 		UNKNOWN_EVENT_TYPE
111*7a0c41d5SAlan Somers 	};
112*7a0c41d5SAlan Somers 
113*7a0c41d5SAlan Somers 	/**
114*7a0c41d5SAlan Somers 	 * Constructor
115*7a0c41d5SAlan Somers 	 *
116*7a0c41d5SAlan Somers 	 * \param type          The type of this exception.
117*7a0c41d5SAlan Somers 	 * \param parsedBuffer  The parsing buffer active at the time of
118*7a0c41d5SAlan Somers 	 *                      the exception.
119*7a0c41d5SAlan Somers 	 * \param offset        The location in the parse buffer where this
120*7a0c41d5SAlan Somers 	 *                      exception occurred.
121*7a0c41d5SAlan Somers 	 */
122*7a0c41d5SAlan Somers 	ParseException(Type type, const std::string &parsedBuffer,
123*7a0c41d5SAlan Somers 		       size_t offset = 0);
124*7a0c41d5SAlan Somers 
125*7a0c41d5SAlan Somers 	/**
126*7a0c41d5SAlan Somers 	 * Accessor
127*7a0c41d5SAlan Somers 	 *
128*7a0c41d5SAlan Somers 	 * \return  The classification for this exception.
129*7a0c41d5SAlan Somers 	 */
130*7a0c41d5SAlan Somers 	Type        GetType()   const;
131*7a0c41d5SAlan Somers 
132*7a0c41d5SAlan Somers 	/**
133*7a0c41d5SAlan Somers 	 * Accessor
134*7a0c41d5SAlan Somers 	 *
135*7a0c41d5SAlan Somers 	 * \return  The offset into the event string where this exception
136*7a0c41d5SAlan Somers 	 *          occurred.
137*7a0c41d5SAlan Somers 	 */
138*7a0c41d5SAlan Somers 	size_t      GetOffset() const;
139*7a0c41d5SAlan Somers 
140*7a0c41d5SAlan Somers private:
141*7a0c41d5SAlan Somers 	/** The type of this exception. */
142*7a0c41d5SAlan Somers 	Type              m_type;
143*7a0c41d5SAlan Somers 
144*7a0c41d5SAlan Somers 	/** The parsing buffer that was active at the time of the exception. */
145*7a0c41d5SAlan Somers 	const std::string m_parsedBuffer;
146*7a0c41d5SAlan Somers 
147*7a0c41d5SAlan Somers 	/**
148*7a0c41d5SAlan Somers 	 * The offset into the event string buffer from where this
149*7a0c41d5SAlan Somers 	 * exception was triggered.
150*7a0c41d5SAlan Somers 	 */
151*7a0c41d5SAlan Somers 	size_t            m_offset;
152*7a0c41d5SAlan Somers };
153*7a0c41d5SAlan Somers 
154*7a0c41d5SAlan Somers //- ParseException Inline Const Public Methods ---------------------------------
155*7a0c41d5SAlan Somers inline ParseException::Type
GetType()156*7a0c41d5SAlan Somers ParseException::GetType() const
157*7a0c41d5SAlan Somers {
158*7a0c41d5SAlan Somers 	return (m_type);
159*7a0c41d5SAlan Somers }
160*7a0c41d5SAlan Somers 
161*7a0c41d5SAlan Somers inline size_t
GetOffset()162*7a0c41d5SAlan Somers ParseException::GetOffset() const
163*7a0c41d5SAlan Somers {
164*7a0c41d5SAlan Somers 	return (m_offset);
165*7a0c41d5SAlan Somers }
166*7a0c41d5SAlan Somers 
167*7a0c41d5SAlan Somers } // namespace DevdCtl
168*7a0c41d5SAlan Somers #endif /* _DEVDCTL_EXCEPTION_H_ */
169