2 * \file ocsd_error_logger.cpp
5 * \copyright Copyright (c) 2015, ARM Limited. All Rights Reserved.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
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.
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.
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.
36 #include "common/ocsd_error_logger.h"
41 ocsdDefaultErrorLogger::ocsdDefaultErrorLogger() :
42 m_Verbosity(OCSD_ERR_SEV_ERROR),
44 m_created_output_logger(false)
47 for(int i = 0; i < 0x80; i++)
49 m_error_sources.push_back("Gen_Err"); // handle 0
50 m_error_sources.push_back("Gen_Warn"); // handle 1
51 m_error_sources.push_back("Gen_Info"); // handle 2
54 ocsdDefaultErrorLogger::~ocsdDefaultErrorLogger()
56 if(m_created_output_logger)
57 delete m_output_logger;
62 for(int i = 0; i < 0x80; i++)
63 if(m_lastErrID[i] != 0) delete m_lastErrID[i];
66 bool ocsdDefaultErrorLogger::initErrorLogger(const ocsd_err_severity_t verbosity, bool bCreateOutputLogger /*= false*/)
69 m_Verbosity = verbosity;
70 if(bCreateOutputLogger)
72 m_output_logger = new (std::nothrow) ocsdMsgLogger();
75 m_created_output_logger = true;
76 m_output_logger->setLogOpts(ocsdMsgLogger::OUT_STDERR);
84 void ocsdDefaultErrorLogger::setOutputLogger(ocsdMsgLogger *pLogger)
86 // if we created the current logger, delete it.
87 if(m_output_logger && m_created_output_logger)
88 delete m_output_logger;
89 m_created_output_logger = false;
90 m_output_logger = pLogger;
93 const ocsd_hndl_err_log_t ocsdDefaultErrorLogger::RegisterErrorSource(const std::string &component_name)
95 ocsd_hndl_err_log_t handle = m_error_sources.size();
96 m_error_sources.push_back(component_name);
100 void ocsdDefaultErrorLogger::LogError(const ocsd_hndl_err_log_t handle, const ocsdError *Error)
102 // only log errors that match or exceed the current verbosity
103 if(m_Verbosity >= Error->getErrorSeverity())
105 // print out only if required
108 if(m_output_logger->isLogging())
110 std::string errStr = "unknown";
111 if(handle < m_error_sources.size())
112 errStr = m_error_sources[handle];
113 errStr += " : " + ocsdError::getErrorString(Error);
114 m_output_logger->LogMsg(errStr);
120 CreateErrorObj(&m_lastErr,Error);
124 // log last error associated with an ID
125 if(OCSD_IS_VALID_CS_SRC_ID(Error->getErrorChanID()))
127 if(m_lastErrID[Error->getErrorChanID()] == 0)
128 CreateErrorObj(&m_lastErrID[Error->getErrorChanID()], Error);
130 *m_lastErrID[Error->getErrorChanID()] = Error;
135 void ocsdDefaultErrorLogger::LogMessage(const ocsd_hndl_err_log_t handle, const ocsd_err_severity_t filter_level, const std::string &msg )
137 // only log errors that match or exceed the current verbosity
138 if((m_Verbosity >= filter_level))
142 if(m_output_logger->isLogging())
144 std::string errStr = "unknown";
145 if(handle < m_error_sources.size())
146 errStr = m_error_sources[handle];
147 errStr += " : " + msg;
148 m_output_logger->LogMsg(errStr);
154 void ocsdDefaultErrorLogger::CreateErrorObj(ocsdError **ppErr, const ocsdError *p_from)
156 *ppErr = new (std::nothrow) ocsdError(p_from);
159 /* End of File ocsd_error_logger.cpp */