1 /*!
2  * \file       ocsd_error.h
3  * \brief      OpenCSD : Library Error class
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 
9 /*
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of the copyright holder nor the names of its contributors
21  * may be used to endorse or promote products derived from this software without
22  * specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef ARM_OCSD_ERROR_H_INCLUDED
37 #define ARM_OCSD_ERROR_H_INCLUDED
38 
39 #include "opencsd/ocsd_if_types.h"
40 #include <string>
41 /** @ingroup ocsd_infrastructure
42 @{*/
43 
44 /*!
45  * @class ocsdError
46  *
47  *  This class is the error object for the Ocsd.
48  *
49  *  Errors are created with a severity (ocsd_err_severity_t) and a standard ocsd_err_t error code.
50  *  Errors can optionally be created with a trace index (offset from start of capture buffer), and
51  *  trace CoreSight source channel ID.
52  *
53  *  A custom error message can be appended to the error.
54  *
55  *  The ocsdError class contains a static function to output a formatted string representation of an error.
56  *
57  */
58 class ocsdError {
59 public:
60     ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code);    /**< Default error constructor with severity and error code. */
61     ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx); /**< Constructor with optional trace index. */
62     ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx, const uint8_t chan_id);  /**< Constructor with optional trace index and channel ID. */
63     ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const std::string &msg);    /**< Default error constructor with severity and error code - plus message. */
64     ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx, const std::string &msg); /**< Constructor with optional trace index - plus message. */
65     ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx, const uint8_t chan_id, const std::string &msg);  /**< Constructor with optional trace index and channel ID - plus message. */
66 
67     ocsdError(const ocsdError *pError);   /**< Copy constructor */
68     ocsdError(const ocsdError &Error);    /**< Copy constructor */
69     ~ocsdError();  /**< Destructor */
70 
71     ocsdError& operator=(const ocsdError *p_err);
72     ocsdError& operator=(const ocsdError &err);
73 
74     void setMessage(const std::string &msg) { m_err_message = msg; };   /**< Set custom error message */
75     const std::string &getMessage() const { return m_err_message; };    /**< Get custom error message */
76 
77     const ocsd_err_t getErrorCode() const { return m_error_code; };    /**< Get error code. */
78     const ocsd_err_severity_t getErrorSeverity() const  { return m_sev; }; /**< Get error severity. */
79     const ocsd_trc_index_t getErrorIndex() const { return m_idx; };  /**< Get trace index associated with the error. */
80     const uint8_t getErrorChanID() const { return m_chan_ID; }; /**< Get the trace source channel ID associated with the error. */
81 
82     static const std::string getErrorString(const ocsdError &error);   /**< Generate a formatted error string for the supplied error. */
83 
84 private:
85     static void appendErrorDetails(std::string &errStr, const ocsdError &error);   /**< build the error string. */
86     ocsdError();   /**< Make no parameter default constructor inaccessible. */
87 
88     ocsd_err_t m_error_code; /**< Error code for this error */
89     ocsd_err_severity_t m_sev;   /**< severity for this error */
90     ocsd_trc_index_t m_idx;    /**< Trace buffer index associated with this error (optional) */
91     uint8_t m_chan_ID;    /**< trace  source ID associated with this error (optional) */
92 
93     std::string m_err_message;  /**< Additional text associated with this error (optional) */
94 };
95 
96 inline ocsdError& ocsdError::operator=(const ocsdError *p_err)
97 {
98     this->m_error_code = p_err->getErrorCode();
99     this->m_sev = p_err->getErrorSeverity();
100     this->m_idx = p_err->getErrorIndex();
101     this->m_chan_ID = p_err->getErrorChanID();
102     this->m_err_message = p_err->getMessage();
103     return *this;
104 }
105 
106 inline ocsdError& ocsdError::operator=(const ocsdError &err)
107 {
108     return (*this = &err);
109 }
110 
111 
112 /** @}*/
113 
114 #endif // ARM_OCSD_ERROR_H_INCLUDED
115 
116 /* End of File ocsd_error.h */
117