1/*
2 * \file       ocsd_error_logger.cpp
3 * \brief      OpenCSD :
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#include "common/ocsd_error_logger.h"
37
38//#include <iostream>
39#include <sstream>
40
41ocsdDefaultErrorLogger::ocsdDefaultErrorLogger() :
42    m_Verbosity(OCSD_ERR_SEV_ERROR),
43    m_output_logger(0),
44    m_created_output_logger(false)
45{
46    m_lastErr = 0;
47    for(int i = 0; i < 0x80; i++)
48        m_lastErrID[i] = 0;
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
52}
53
54ocsdDefaultErrorLogger::~ocsdDefaultErrorLogger()
55{
56    if(m_created_output_logger)
57        delete m_output_logger;
58
59    if(m_lastErr)
60        delete m_lastErr;
61
62    for(int i = 0; i < 0x80; i++)
63        if(m_lastErrID[i] != 0) delete m_lastErrID[i];
64}
65
66bool ocsdDefaultErrorLogger::initErrorLogger(const ocsd_err_severity_t verbosity, bool bCreateOutputLogger /*= false*/)
67{
68    bool bInit = true;
69    m_Verbosity = verbosity;
70    if(bCreateOutputLogger)
71    {
72        m_output_logger = new (std::nothrow) ocsdMsgLogger();
73        if(m_output_logger)
74        {
75            m_created_output_logger = true;
76            m_output_logger->setLogOpts(ocsdMsgLogger::OUT_STDERR);
77        }
78        else
79            bInit = false;
80    }
81    return bInit;
82}
83
84void ocsdDefaultErrorLogger::setOutputLogger(ocsdMsgLogger *pLogger)
85{
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;
91}
92
93const ocsd_hndl_err_log_t ocsdDefaultErrorLogger::RegisterErrorSource(const std::string &component_name)
94{
95    ocsd_hndl_err_log_t handle = m_error_sources.size();
96    m_error_sources.push_back(component_name);
97    return handle;
98}
99
100void ocsdDefaultErrorLogger::LogError(const ocsd_hndl_err_log_t handle, const ocsdError *Error)
101{
102    // only log errors that match or exceed the current verbosity
103    if(m_Verbosity >= Error->getErrorSeverity())
104    {
105        // print out only if required
106        if(m_output_logger)
107        {
108            if(m_output_logger->isLogging())
109            {
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);
115            }
116        }
117
118        // log last error
119        if(m_lastErr == 0)
120            CreateErrorObj(&m_lastErr,Error);
121        else
122            *m_lastErr = Error;
123
124        // log last error associated with an ID
125        if(OCSD_IS_VALID_CS_SRC_ID(Error->getErrorChanID()))
126        {
127            if(m_lastErrID[Error->getErrorChanID()] == 0)
128                CreateErrorObj(&m_lastErrID[Error->getErrorChanID()], Error);
129            else
130                *m_lastErrID[Error->getErrorChanID()] = Error;
131        }
132    }
133}
134
135void ocsdDefaultErrorLogger::LogMessage(const ocsd_hndl_err_log_t handle, const ocsd_err_severity_t filter_level, const std::string &msg )
136{
137    // only log errors that match or exceed the current verbosity
138    if((m_Verbosity >= filter_level))
139    {
140        if(m_output_logger)
141        {
142            if(m_output_logger->isLogging())
143            {
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);
149            }
150        }
151    }
152}
153
154void ocsdDefaultErrorLogger::CreateErrorObj(ocsdError **ppErr, const ocsdError *p_from)
155{
156    *ppErr = new (std::nothrow) ocsdError(p_from);
157}
158
159/* End of File ocsd_error_logger.cpp */
160