xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_log.cpp (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 Cm Logger definitions
25 //!
26 
27 #include <iostream>
28 #include <iomanip>
29 #include "cm_log.h"
30 #include "cm_csync.h"
31 #include "mos_utilities.h"
32 
33 #if CM_LOG_ON
34 
35 // Definition (and initialization) of static attributes
36 CMRT_UMD::CSync globalCmLogLock;
37 
38 /**
39  * Logger Constructor.
40  * It is a private constructor, called only by getInstance() and only the
41  * first time. It is called inside a lock, so lock inside this method
42  * is not required.
43  * It only initializes the initial time. All configuration is done inside the
44  * configure() method.
45  */
CmLogger(CM_HAL_STATE * halState)46 CmLogger::CmLogger(CM_HAL_STATE *halState)
47 {
48     //Get Verbosity Level
49     GetVerbosityLevel(halState);
50 
51     if (m_verbosityLevel == CM_LOG_LEVEL_NONE)
52     {  // if it is not set, no file will be generated.
53         return;
54     }
55 
56     //Get Log File name
57     std::ostringstream OutPutFile;
58     char               fileNamePrefix[MAX_PATH];
59 
60     SYSTEMTIME systime;
61     GetLocalTime(&systime);
62 
63     OutPutFile << "CM_LOG_" << CmGetCurProcessId() << "_" << systime.wMonth
64                << "_" << systime.wDay << "_" << systime.wHour
65                << "_" << systime.wMinute << "_" << systime.wSecond << ".log";
66     m_logFile = OutPutFile.str();
67     GetLogFileLocation(OutPutFile.str().c_str(), fileNamePrefix, halState->osInterface->pOsContext);
68 
69     // Open file
70     m_streamOut.open(fileNamePrefix, std::ios::app);
71     if (!m_streamOut)
72     {
73         CM_ASSERTMESSAGE("Open file failed!");
74     }
75 }
76 
~CmLogger()77 CmLogger::~CmLogger()
78 {
79     if (m_logFile.empty())
80     {  // empty, dump to screen
81         m_streamOut.close();
82     }
83 }
84 
GetInstance(CM_HAL_STATE * halState)85 CmLogger* CmLogger::GetInstance(CM_HAL_STATE *halState)
86 {
87     static CmLogger m_globalCmLogger(halState);
88     return &m_globalCmLogger;
89 }
90 
GetVerbosityLevel(CM_HAL_STATE * halState)91 void CmLogger::GetVerbosityLevel(CM_HAL_STATE *halState)
92 {
93     // Read VerbosityLevel from RegisterKey
94     MOS_USER_FEATURE_VALUE_DATA userFeatureValueData;
95     // User feature key reads
96     MOS_ZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
97 
98     userFeatureValueData.u32Data = CM_LOG_LEVEL_NONE;  // default value
99     userFeatureValueData.i32DataFlag = MOS_USER_FEATURE_VALUE_DATA_FLAG_CUSTOM_DEFAULT_VALUE_TYPE;
100     MOS_UserFeature_ReadValue_ID(
101         nullptr,
102         __MEDIA_USER_FEATURE_VALUE_MDF_LOG_LEVEL_ID,
103         &userFeatureValueData,
104         halState->osInterface->pOsContext);
105 
106     m_verbosityLevel = userFeatureValueData.u32Data;
107 }
108 
109 /**
110  * Method used to print messages.
111  * Called by the CM_DEBUG() macro.
112  * @param Priority of the message
113  * @param Source file where the method has been called (set equal to __FILE__
114  *           by the DEBUG macro)
115  * @param Source line where the method has been called (set equal to __LINE__
116           by the macro)
117  * @param Message
118  */
Print(const unsigned int verbosityLevel,const std::string & file,const int line,const std::string & message)119 void CmLogger::Print(const unsigned int verbosityLevel,
120                      const std::string &file,
121                      const int line,
122                      const std::string &message)
123 {
124     CmLogger::Lock();
125 
126     if (verbosityLevel <= m_verbosityLevel)
127     {
128         switch (verbosityLevel)
129         {
130         case CM_LOG_LEVEL_DEBUG:
131             m_streamOut << "[DEBUG]";
132             break;
133 
134         case CM_LOG_LEVEL_INFO:
135             m_streamOut << "[INFO]";
136             break;
137 
138         case CM_LOG_LEVEL_WARN:
139             m_streamOut << "[WARN]";
140             break;
141 
142         case CM_LOG_LEVEL_ERROR:
143             m_streamOut << "[ERROR]";
144             break;
145         }
146 
147         m_streamOut << "[PID :" << CmGetCurProcessId() << "][TID :" << CmGetCurThreadId() << "]"
148                     << "[" << file << ":" << line << "] @ "
149                     << ":" << message << std::endl;
150     }
151 
152     CmLogger::Unlock();
153 }
154 
LogDataArrayHex(std::ostringstream & oss,unsigned char * data,unsigned int size)155 void CmLogger::LogDataArrayHex(std::ostringstream &oss, unsigned char *data, unsigned int size)
156 {
157     std::ios::fmtflags f(oss.flags());  // store the oss flags
158 
159     oss << "Data[Hex] : ";
160 
161     for (unsigned int i = 0; i < size; ++i)
162     {
163         oss << std::setfill('0') << std::setw(2) << std::hex << static_cast<short>(data[i]);
164     }
165     oss << std::endl;
166 
167     oss.flags(f);  // restore the flags
168 }
169 
Lock()170 void CmLogger::Lock()
171 {
172     globalCmLogLock.Acquire();
173 }
174 
Unlock()175 void CmLogger::Unlock()
176 {
177     globalCmLogLock.Release();
178 }
179 
180 #endif  // #if CM_LOG_ON
181