1 /*
2  * \file       trc_component.cpp
3  * \brief      OpenCSD :
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 /*
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "common/trc_component.h"
36 
37 class errLogAttachMonitor : public IComponentAttachNotifier
38 {
39 public:
40     errLogAttachMonitor()
41     {
42         m_pComp = 0;
43     };
44     virtual ~ errLogAttachMonitor() {};
45     virtual void attachNotify(const int num_attached)
46     {
47         if(m_pComp)
48             m_pComp->do_attach_notify(num_attached);
49     }
50     void Init(TraceComponent *pComp)
51     {
52         m_pComp = pComp;
53         if(m_pComp)
54             m_pComp->getErrorLogAttachPt()->set_notifier(this);
55     }
56 private:
57     TraceComponent *m_pComp;
58 };
59 
60 TraceComponent::TraceComponent(const std::string &name)
61 {
62     Init(name);
63 }
64 
65 TraceComponent::TraceComponent(const std::string &name, int instIDNum)
66 {
67     std::string name_combined = name;
68     char num_buffer[32];
69     sprintf(num_buffer,"_%04d",instIDNum);
70     name_combined += (std::string)num_buffer;
71     Init(name_combined);
72 }
73 
74 TraceComponent::~TraceComponent()
75 {
76 }
77 
78 void TraceComponent::Init(const std::string &name)
79 {
80     m_errLogHandle = OCSD_INVALID_HANDLE;
81     m_errVerbosity = OCSD_ERR_SEV_NONE;
82     m_name = name;
83 
84     m_supported_op_flags = 0;
85     m_op_flags = 0;
86     m_assocComp = 0;
87 
88     m_pErrAttachMon = new (std::nothrow) errLogAttachMonitor();
89     if(m_pErrAttachMon)
90         m_pErrAttachMon->Init(this);
91 }
92 
93 void TraceComponent::LogError(const ocsdError &Error)
94 {
95     if((m_errLogHandle != OCSD_INVALID_HANDLE) &&
96         isLoggingErrorLevel(Error.getErrorSeverity()))
97     {
98         // ensure we have not disabled the attachPt
99         if(m_error_logger.first())
100             m_error_logger.first()->LogError(m_errLogHandle,&Error);
101     }
102 }
103 
104 void TraceComponent::LogMessage(const ocsd_err_severity_t filter_level, const std::string &msg)
105 {
106     if ((m_errLogHandle != OCSD_INVALID_HANDLE) &&
107         isLoggingErrorLevel(filter_level))
108     {
109         // ensure we have not disabled the attachPt
110         if (m_error_logger.first())
111             m_error_logger.first()->LogMessage(this->m_errLogHandle, filter_level, msg);
112     }
113 
114 }
115 
116 void TraceComponent::do_attach_notify(const int num_attached)
117 {
118     if(num_attached)
119     {
120         // ensure we have not disabled the attachPt
121         if(m_error_logger.first())
122         {
123             m_errLogHandle = m_error_logger.first()->RegisterErrorSource(m_name);
124             m_errVerbosity = m_error_logger.first()->GetErrorLogVerbosity();
125         }
126     }
127     else
128     {
129         m_errLogHandle = OCSD_INVALID_HANDLE;
130     }
131 }
132 
133 void TraceComponent::updateErrorLogLevel()
134 {
135     if(m_error_logger.first())
136     {
137         m_errVerbosity = m_error_logger.first()->GetErrorLogVerbosity();
138     }
139 }
140 
141 ocsd_err_t TraceComponent::setComponentOpMode(uint32_t op_flags)
142 {
143     if( (~m_supported_op_flags & op_flags) != 0)
144         return OCSD_ERR_INVALID_PARAM_VAL;
145     m_op_flags = op_flags;
146     return OCSD_OK;
147 }
148 
149 /* End of File trc_component.cpp */
150