xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_log.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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     CmLogPrint(priority, msg, halState)
61 
62 #define CM_DEBUG(msg, halState)  _CM_LOG(CM_LOG_LEVEL_DEBUG, msg, halState)
63 #define INSERT_API_CALL_LOG(halState) CmLogTimer _LogTimer(__FUNCTION__, halState)
64 #define TASK_LOG(_pTask)    CM_DEBUG(_pTask->Log(), _pTask->GetHalState());
65 #define DEVICE_LOG(_pDev)   CM_DEBUG(_pDev->Log(), _pDev->GetHalState());
66 #define EVENT_LOG(_pEvt)    CM_DEBUG(_pEvt->Log(__FUNCTION__), _pEvt->GetHalState());
67 
68 class CmLogger
69 {
70 public:
71     static CmLogger* GetInstance(CM_HAL_STATE *halState);
72 
73     static void LogDataArrayHex(std::ostringstream &oss,
74                                 unsigned char *data,
75                                 unsigned int size);
76 
77     virtual void Print(const unsigned int verbosityLevel,
78                const std::string &sourceFile,
79                const int codeLine,
80                const std::string &message);
81 
82 private:
83     /**
84      * \brief Initial part of the name of the file used for Logging.
85      * Date and time are automatically appended.
86      */
87     std::string m_logFile;
88 
89     /**
90      * \brief Stream used when logging on a file or screen
91      */
92     std::ofstream m_streamOut;
93 
94     /**
95      * \brief Verbosity threshold
96      */
97     unsigned int  m_verbosityLevel;
98 
99     CmLogger(CM_HAL_STATE *halState);
100 
101     ~CmLogger();
102 
103     /**
104      * \brief Method to lock in case of multithreading
105      */
106     inline static void Lock();
107 
108     /**
109      * \brief Method to unlock in case of multithreading
110      */
111     inline static void Unlock();
112 
113     /**
114      * \brief Method to get verbosity level from register key
115      */
116     void GetVerbosityLevel(CM_HAL_STATE *halState);
117 };
118 
CmLogPrint(CM_LOG_LEVEL priority,std::string msg,CM_HAL_STATE * halState)119 inline void CmLogPrint(CM_LOG_LEVEL priority, std::string msg, CM_HAL_STATE *halState)
120 {
121     std::ostringstream __debug_stream__;
122     __debug_stream__ << msg;
123     CmLogger* logTmp = CmLogger::GetInstance(halState);
124     if (logTmp != nullptr)
125     {
126         logTmp->Print(priority, __FILE__, __LINE__,
127                                         __debug_stream__.str());
128     }
129 }
130 
131 class CmLogTimer
132 {
133 
134 public:
CmLogTimer(const std::string & str,CM_HAL_STATE * halState)135     CmLogTimer(const std::string &str, CM_HAL_STATE *halState)
136             : m_string(str),
137               m_timer(str),
138               m_halState(halState) {}
139 
~CmLogTimer()140     ~CmLogTimer()
141     {
142         m_timer.Stop();
143         m_string = m_timer.ToString();
144         _CM_LOG(CM_LOG_LEVEL_INFO, m_string, m_halState);
145         return;
146     }
147 
Stop()148     void Stop()
149     {
150         m_timer.Stop();
151     }
152 
153 private:
154     std::string m_string;
155 
156     CmTimer m_timer;
157 
158     CM_HAL_STATE *m_halState;
159 };
160 
161 #endif  // #if !(CM_LOG_ON)
162 
163 #endif  // #ifndef MEDIADRIVER_AGNOSTIC_COMMON_CM_CMLOG_H_
164