xref: /freebsd/lib/libdevdctl/exception.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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  * $FreeBSD$
33  */
34 
35 /**
36  * \file zfsd_exception.h
37  *
38  * Definition of the ZfsdException class hierarchy.  All exceptions
39  * explicitly thrown by Zfsd are defined here.
40  */
41 #ifndef	_DEVDCTL_EXCEPTION_H_
42 #define	_DEVDCTL_EXCEPTION_H_
43 
44 /*============================ Namespace Control =============================*/
45 namespace DevdCtl
46 {
47 
48 /*============================= Class Definitions ============================*/
49 
50 /*--------------------------------- Exception --------------------------------*/
51 /**
52  * \brief Class allowing unified reporting/logging of exceptional events.
53  */
54 class Exception
55 {
56 public:
57 	/**
58 	 * \brief Exception constructor allowing arbitrary string
59 	 *        data to be reported.
60 	 *
61 	 * \param fmt  Printf-like string format specifier.
62 	 */
63 	Exception(const char *fmt, ...);
64 
65 	/**
66 	 * \brief Augment/Modify a Exception's string data.
67 	 */
68 	std::string& GetString();
69 
70 	/**
71 	 * \brief Emit exception data to syslog(3).
72 	 */
73 	virtual void Log() const;
74 
75 protected:
76 	Exception();
77 
78 	/**
79 	 * \brief Convert exception string format and arguments provided
80 	 *        in event constructors into a linear string.
81 	 */
82 	void FormatLog(const char *fmt, va_list ap);
83 
84 	std::string   m_log;
85 };
86 
87 inline std::string &
88 Exception::GetString()
89 {
90 	return (m_log);
91 }
92 
93 /*------------------------------ ParseException ------------------------------*/
94 /**
95  * Exception thrown when an event string is not converted to an actionable
96  * Event object.
97  */
98 class ParseException : public Exception
99 {
100 public:
101 	enum Type
102 	{
103 		/** Improperly formatted event string encountered. */
104 		INVALID_FORMAT,
105 
106 		/** No handlers for this event type. */
107 		DISCARDED_EVENT_TYPE,
108 
109 		/** Unhandled event type. */
110 		UNKNOWN_EVENT_TYPE
111 	};
112 
113 	/**
114 	 * Constructor
115 	 *
116 	 * \param type          The type of this exception.
117 	 * \param parsedBuffer  The parsing buffer active at the time of
118 	 *                      the exception.
119 	 * \param offset        The location in the parse buffer where this
120 	 *                      exception occurred.
121 	 */
122 	ParseException(Type type, const std::string &parsedBuffer,
123 		       size_t offset = 0);
124 
125 	/**
126 	 * Accessor
127 	 *
128 	 * \return  The classification for this exception.
129 	 */
130 	Type        GetType()   const;
131 
132 	/**
133 	 * Accessor
134 	 *
135 	 * \return  The offset into the event string where this exception
136 	 *          occurred.
137 	 */
138 	size_t      GetOffset() const;
139 
140 private:
141 	/** The type of this exception. */
142 	Type              m_type;
143 
144 	/** The parsing buffer that was active at the time of the exception. */
145 	const std::string m_parsedBuffer;
146 
147 	/**
148 	 * The offset into the event string buffer from where this
149 	 * exception was triggered.
150 	 */
151 	size_t            m_offset;
152 };
153 
154 //- ParseException Inline Const Public Methods ---------------------------------
155 inline ParseException::Type
156 ParseException::GetType() const
157 {
158 	return (m_type);
159 }
160 
161 inline size_t
162 ParseException::GetOffset() const
163 {
164 	return (m_offset);
165 }
166 
167 } // namespace DevdCtl
168 #endif /* _DEVDCTL_EXCEPTION_H_ */
169