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