1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file      cm_log.h
24 //! \brief     Contains Class CmLogger declarations.
25 //!
26 
27 #ifndef MEDIADRIVER_AGNOSTIC_COMMON_CM_CMLOG_H_
28 #define MEDIADRIVER_AGNOSTIC_COMMON_CM_CMLOG_H_
29 
30 #include <fstream>
31 #include <ostream>
32 #include <string>
33 #include <sstream>
34 
35 #include "cm_hal.h"
36 
37 #if (_DEBUG || _RELEASE_INTERNAL)
38 #define CM_LOG_ON                   1
39 #endif
40 
41 #if !(CM_LOG_ON)
42 #define INSERT_API_CALL_LOG(halState)
43 #define TASK_LOG(_pTask)
44 #define DEVICE_LOG(_pDev)
45 #define EVENT_LOG(_pEvt)
46 
47 #else  // #if !(CM_LOG_ON)
48 
49 #include "cm_perf.h"  // definition of CmTimer
50 
51 typedef enum _CM_LOG_LEVEL{
52     CM_LOG_LEVEL_NONE     = 0, // This emu must be zero.
53     CM_LOG_LEVEL_ERROR   = 1,
54     CM_LOG_LEVEL_WARN    = 2,
55     CM_LOG_LEVEL_DEBUG   = 3,
56     CM_LOG_LEVEL_INFO    = 4
57 }CM_LOG_LEVEL;
58 
59 #define _CM_LOG(priority, msg, halState) {        \
60     std::ostringstream __debug_stream__; \
61     __debug_stream__ << msg; \
62     CmLogger::GetInstance(halState).Print(priority, __FILE__, __LINE__, \
63                                           __debug_stream__.str()); \
64     }
65 
66 #define CM_DEBUG(msg, halState)  _CM_LOG(CM_LOG_LEVEL_DEBUG, msg, halState)
67 #define INSERT_API_CALL_LOG(halState) CmLogTimer _LogTimer(__FUNCTION__, halState)
68 #define TASK_LOG(_pTask)    CM_DEBUG(_pTask->Log(), _pTask->GetHalState());
69 #define DEVICE_LOG(_pDev)   CM_DEBUG(_pDev->Log(), _pDev->GetHalState());
70 #define EVENT_LOG(_pEvt)    CM_DEBUG(_pEvt->Log(__FUNCTION__), _pEvt->GetHalState());
71 
72 class CmLogger
73 {
74 public:
GetInstance(CM_HAL_STATE * halState)75     static CmLogger& GetInstance(CM_HAL_STATE *halState)
76     {
77         static CmLogger m_globalCmLogger(halState);
78         return m_globalCmLogger;
79     }
80 
81     static void LogDataArrayHex(std::ostringstream &oss,
82                                 unsigned char *data,
83                                 unsigned int size);
84 
85     void Print(const unsigned int verbosityLevel,
86                const std::string &sourceFile,
87                const int codeLine,
88                const std::string &message);
89 
90 private:
91     /**
92      * \brief Initial part of the name of the file used for Logging.
93      * Date and time are automatically appended.
94      */
95     std::string m_logFile;
96 
97     /**
98      * \brief Stream used when logging on a file or screen
99      */
100     std::ofstream m_streamOut;
101 
102     /**
103      * \brief Verbosity threshold
104      */
105     unsigned int  m_verbosityLevel;
106 
107     CmLogger(CM_HAL_STATE *halState);
108 
109     ~CmLogger();
110 
111     /**
112      * \brief Method to lock in case of multithreading
113      */
114     inline static void Lock();
115 
116     /**
117      * \brief Method to unlock in case of multithreading
118      */
119     inline static void Unlock();
120 
121     /**
122      * \brief Method to get verbosity level from register key
123      */
124     void GetVerbosityLevel(CM_HAL_STATE *halState);
125 };
126 
127 class CmLogTimer
128 {
129 
130 public:
CmLogTimer(const std::string & str,CM_HAL_STATE * halState)131     CmLogTimer(const std::string &str, CM_HAL_STATE *halState)
132             : m_string(str),
133               m_timer(str),
134               m_halState(halState) {}
135 
136     ~CmLogTimer();
137 
138     void Stop();
139 
140 private:
141     std::string m_string;
142 
143     CmTimer m_timer;
144 
145     CM_HAL_STATE *m_halState;
146 };
147 
148 #endif  // #if !(CM_LOG_ON)
149 
150 #endif  // #ifndef MEDIADRIVER_AGNOSTIC_COMMON_CM_CMLOG_H_
151