PageRenderTime 258ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/obs-qsv11/libmfx/src/mfx_dispatcher_log.cpp

https://gitlab.com/Ornim/obs-old
C++ | 449 lines | 338 code | 66 blank | 45 comment | 37 complexity | 3e96f8f86711f524dbf4174918d7963d MD5 | raw file
  1. /* ****************************************************************************** *\
  2. Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. - Neither the name of Intel Corporation nor the names of its contributors
  11. may be used to endorse or promote products derived from this software
  12. without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
  14. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. File Name: mfx_dispatcher_log.h
  24. \* ****************************************************************************** */
  25. #if defined(MFX_DISPATCHER_LOG)
  26. #include "mfx_dispatcher_log.h"
  27. #include "mfxstructures.h"
  28. #if defined(_WIN32) || defined(_WIN64)
  29. #include <windows.h>
  30. #if defined(DISPATCHER_LOG_REGISTER_EVENT_PROVIDER)
  31. #include <evntprov.h>
  32. #include <winmeta.h>
  33. #endif
  34. #endif // #if defined(_WIN32) || defined(_WIN64)
  35. #include <stdarg.h>
  36. #include <algorithm>
  37. #include <string>
  38. #include <sstream>
  39. struct CodeStringTable
  40. {
  41. int code;
  42. const char *string;
  43. } LevelStrings []=
  44. {
  45. {DL_INFO, "INFO: "},
  46. {DL_WRN, "WARNING:"},
  47. {DL_ERROR, "ERROR: "}
  48. };
  49. #define DEFINE_CODE(code)\
  50. {code, #code}
  51. static CodeStringTable StringsOfImpl[] = {
  52. DEFINE_CODE(MFX_IMPL_AUTO),
  53. DEFINE_CODE(MFX_IMPL_SOFTWARE),
  54. DEFINE_CODE(MFX_IMPL_HARDWARE),
  55. DEFINE_CODE(MFX_IMPL_AUTO_ANY),
  56. DEFINE_CODE(MFX_IMPL_HARDWARE_ANY),
  57. DEFINE_CODE(MFX_IMPL_HARDWARE2),
  58. DEFINE_CODE(MFX_IMPL_HARDWARE3),
  59. DEFINE_CODE(MFX_IMPL_HARDWARE4),
  60. DEFINE_CODE(MFX_IMPL_UNSUPPORTED)
  61. };
  62. static CodeStringTable StringsOfImplVIA[] = {
  63. DEFINE_CODE(MFX_IMPL_VIA_ANY),
  64. DEFINE_CODE(MFX_IMPL_VIA_D3D9),
  65. DEFINE_CODE(MFX_IMPL_VIA_D3D11),
  66. };
  67. static CodeStringTable StringsOfStatus[] =
  68. {
  69. DEFINE_CODE(MFX_ERR_NONE ),
  70. DEFINE_CODE(MFX_ERR_UNKNOWN ),
  71. DEFINE_CODE(MFX_ERR_NULL_PTR ),
  72. DEFINE_CODE(MFX_ERR_UNSUPPORTED ),
  73. DEFINE_CODE(MFX_ERR_MEMORY_ALLOC ),
  74. DEFINE_CODE(MFX_ERR_NOT_ENOUGH_BUFFER ),
  75. DEFINE_CODE(MFX_ERR_INVALID_HANDLE ),
  76. DEFINE_CODE(MFX_ERR_LOCK_MEMORY ),
  77. DEFINE_CODE(MFX_ERR_NOT_INITIALIZED ),
  78. DEFINE_CODE(MFX_ERR_NOT_FOUND ),
  79. DEFINE_CODE(MFX_ERR_MORE_DATA ),
  80. DEFINE_CODE(MFX_ERR_MORE_SURFACE ),
  81. DEFINE_CODE(MFX_ERR_ABORTED ),
  82. DEFINE_CODE(MFX_ERR_DEVICE_LOST ),
  83. DEFINE_CODE(MFX_ERR_INCOMPATIBLE_VIDEO_PARAM),
  84. DEFINE_CODE(MFX_ERR_INVALID_VIDEO_PARAM ),
  85. DEFINE_CODE(MFX_ERR_UNDEFINED_BEHAVIOR ),
  86. DEFINE_CODE(MFX_ERR_DEVICE_FAILED ),
  87. DEFINE_CODE(MFX_WRN_IN_EXECUTION ),
  88. DEFINE_CODE(MFX_WRN_DEVICE_BUSY ),
  89. DEFINE_CODE(MFX_WRN_VIDEO_PARAM_CHANGED ),
  90. DEFINE_CODE(MFX_WRN_PARTIAL_ACCELERATION ),
  91. DEFINE_CODE(MFX_WRN_INCOMPATIBLE_VIDEO_PARAM),
  92. DEFINE_CODE(MFX_WRN_VALUE_NOT_CHANGED ),
  93. DEFINE_CODE(MFX_WRN_OUT_OF_RANGE ),
  94. };
  95. #define CODE_TO_STRING(code, array)\
  96. CodeToString(code, array, sizeof(array)/sizeof(array[0]))
  97. const char* CodeToString(int code, CodeStringTable array[], int len )
  98. {
  99. for (int i = 0 ; i < len; i++)
  100. {
  101. if (array[i].code == code)
  102. return array[i].string;
  103. }
  104. return "undef";
  105. }
  106. std::string DispatcherLog_GetMFXImplString(int impl)
  107. {
  108. std::string str1 = CODE_TO_STRING(impl & ~(-MFX_IMPL_VIA_ANY), StringsOfImpl);
  109. std::string str2 = CODE_TO_STRING(impl & (-MFX_IMPL_VIA_ANY), StringsOfImplVIA);
  110. return str1 + (str2 == "undef" ? "" : "|"+str2);
  111. }
  112. const char *DispatcherLog_GetMFXStatusString(int sts)
  113. {
  114. return CODE_TO_STRING(sts, StringsOfStatus);
  115. }
  116. //////////////////////////////////////////////////////////////////////////
  117. void DispatcherLogBracketsHelper::Write(const char * str, ...)
  118. {
  119. va_list argsptr;
  120. va_start(argsptr, str);
  121. DispatchLog::get().Write(m_level, m_opcode, str, argsptr);
  122. va_end(argsptr);
  123. }
  124. void DispatchLogBlockHelper::Write(const char * str, ...)
  125. {
  126. va_list argsptr;
  127. va_start(argsptr, str);
  128. DispatchLog::get().Write(m_level, DL_EVENT_START, str, argsptr);
  129. va_end(argsptr);
  130. }
  131. DispatchLogBlockHelper::~DispatchLogBlockHelper()
  132. {
  133. DispatchLog::get().Write(m_level, DL_EVENT_STOP, NULL, NULL);
  134. }
  135. //////////////////////////////////////////////////////////////////////////
  136. DispatchLog::DispatchLog()
  137. : m_DispatcherLogSink(DL_SINK_PRINTF)
  138. {
  139. }
  140. void DispatchLog::SetSink(int nSink, IMsgHandler * pHandler)
  141. {
  142. DetachAllSinks();
  143. AttachSink(nSink, pHandler);
  144. }
  145. void DispatchLog::AttachSink(int nsink, IMsgHandler *pHandler)
  146. {
  147. m_DispatcherLogSink |= nsink;
  148. if (NULL != pHandler)
  149. m_Recepients.push_back(pHandler);
  150. }
  151. void DispatchLog::DetachSink(int nsink, IMsgHandler *pHandler)
  152. {
  153. if (nsink & DL_SINK_IMsgHandler)
  154. {
  155. m_Recepients.remove(pHandler);
  156. }
  157. m_DispatcherLogSink &= ~nsink;
  158. }
  159. void DispatchLog::ExchangeSink(int nsink, IMsgHandler *oldHdl, IMsgHandler *newHdl)
  160. {
  161. if (nsink & DL_SINK_IMsgHandler)
  162. {
  163. std::list<IMsgHandler*> :: iterator it = std::find(m_Recepients.begin(), m_Recepients.end(), oldHdl);
  164. //cannot exchange in that case
  165. if (m_Recepients.end() == it)
  166. return;
  167. *it = newHdl;
  168. }
  169. }
  170. void DispatchLog::DetachAllSinks()
  171. {
  172. m_Recepients.clear();
  173. m_DispatcherLogSink = DL_SINK_NULL;
  174. }
  175. void DispatchLog::Write(int level, int opcode, const char * msg, va_list argptr)
  176. {
  177. int sinkTable[] =
  178. {
  179. DL_SINK_PRINTF,
  180. DL_SINK_IMsgHandler,
  181. };
  182. for (size_t i = 0; i < sizeof(sinkTable) / sizeof(sinkTable[0]); i++)
  183. {
  184. switch(m_DispatcherLogSink & sinkTable[i])
  185. {
  186. case DL_SINK_NULL:
  187. break;
  188. case DL_SINK_PRINTF:
  189. {
  190. char msg_formated[8048] = {0};
  191. if (NULL != msg && level != DL_LOADED_LIBRARY)
  192. {
  193. #if _MSC_VER >= 1400
  194. vsprintf_s(msg_formated, sizeof(msg_formated)/sizeof(msg_formated[0]), msg, argptr);
  195. #else
  196. vsnprintf(msg_formated, sizeof(msg_formated)/sizeof(msg_formated[0]), msg, argptr);
  197. #endif
  198. //TODO: improve this , add opcode handling
  199. printf("%s %s", CODE_TO_STRING(level, LevelStrings), msg_formated);
  200. }
  201. break;
  202. }
  203. case DL_SINK_IMsgHandler:
  204. {
  205. std::list<IMsgHandler*>::iterator it;
  206. for (it = m_Recepients.begin(); it != m_Recepients.end(); ++it)
  207. {
  208. (*it)->Write(level, opcode, msg, argptr);
  209. }
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. #if defined(DISPATCHER_LOG_REGISTER_EVENT_PROVIDER)
  216. class ETWHandler : public IMsgHandler
  217. {
  218. public:
  219. ETWHandler(const wchar_t * guid_str)
  220. : m_bUseFormatter(DISPATCHER_LOG_USE_FORMATING)
  221. , m_EventHandle()
  222. , m_bProviderEnable()
  223. {
  224. GUID rguid = GUID_NULL;
  225. if (FAILED(CLSIDFromString(guid_str, &rguid)))
  226. {
  227. return;
  228. }
  229. EventRegister(&rguid, NULL, NULL, &m_EventHandle);
  230. m_bProviderEnable = 0 != EventProviderEnabled(m_EventHandle, 1,0);
  231. }
  232. ~ETWHandler()
  233. {
  234. if (m_EventHandle)
  235. {
  236. EventUnregister(m_EventHandle);
  237. }
  238. }
  239. virtual void Write(int level, int opcode, const char * msg, va_list argptr)
  240. {
  241. //event not registered
  242. if (0==m_EventHandle)
  243. {
  244. return;
  245. }
  246. if (!m_bProviderEnable)
  247. {
  248. return;
  249. }
  250. if (level == DL_LOADED_LIBRARY)
  251. {
  252. return;
  253. }
  254. char msg_formated[1024];
  255. EVENT_DESCRIPTOR descriptor;
  256. EVENT_DATA_DESCRIPTOR data_descriptor;
  257. EventDescZero(&descriptor);
  258. descriptor.Opcode = (UCHAR)opcode;
  259. descriptor.Level = (UCHAR)level;
  260. if (m_bUseFormatter)
  261. {
  262. if (NULL != msg)
  263. {
  264. #if _MSC_VER >= 1400
  265. vsprintf_s(msg_formated, sizeof (msg_formated) / sizeof (msg_formated[0]), msg, argptr);
  266. #else
  267. vsnprintf(msg_formated, sizeof (msg_formated) / sizeof (msg_formated[0]), msg, argptr);
  268. #endif
  269. EventDataDescCreate(&data_descriptor, msg_formated, (ULONG)(strlen(msg_formated) + 1));
  270. }else
  271. {
  272. EventDataDescCreate(&data_descriptor, NULL, 0);
  273. }
  274. }else
  275. {
  276. //TODO: non formated events supports under zbb
  277. }
  278. EventWrite(m_EventHandle, &descriptor, 1, &data_descriptor);
  279. }
  280. protected:
  281. //we may not use formatter in some cases described in dispatch_log macro
  282. //it significantly increases performance by eliminating any vsprintf operations
  283. bool m_bUseFormatter;
  284. //consumer is attached, dispatcher trace to reduce formating overhead
  285. //submits event only if consumer attached
  286. bool m_bProviderEnable;
  287. REGHANDLE m_EventHandle;
  288. };
  289. //
  290. IMsgHandler *ETWHandlerFactory::GetSink(const wchar_t* sguid)
  291. {
  292. _storage_type::iterator it;
  293. it = m_storage.find(sguid);
  294. if (it == m_storage.end())
  295. {
  296. ETWHandler * handler = new ETWHandler(sguid);
  297. _storage_type::_Pairib it_bool = m_storage.insert(_storage_type::value_type(sguid, handler));
  298. it = it_bool.first;
  299. }
  300. return it->second;
  301. }
  302. ETWHandlerFactory::~ETWHandlerFactory()
  303. {
  304. for each(_storage_type::value_type val in m_storage)
  305. {
  306. delete val.second;
  307. }
  308. }
  309. class EventRegistrator : public IMsgHandler
  310. {
  311. const wchar_t * m_sguid;
  312. public:
  313. EventRegistrator(const wchar_t* sguid = DISPATCHER_LOG_EVENT_GUID)
  314. :m_sguid(sguid)
  315. {
  316. DispatchLog::get().AttachSink( DL_SINK_IMsgHandler
  317. , this);
  318. }
  319. virtual void Write(int level, int opcode, const char * msg, va_list argptr)
  320. {
  321. //we cannot call attach sink since we may have been called from iteration
  322. //we axchanging preserve that placeholding
  323. IMsgHandler * pSink = NULL;
  324. DispatchLog::get().ExchangeSink(DL_SINK_IMsgHandler,
  325. this,
  326. pSink = ETWHandlerFactory::get().GetSink(m_sguid));
  327. //need to call only once here all next calls will be done inside dispatcherlog
  328. if (NULL != pSink)
  329. {
  330. pSink->Write(level, opcode, msg, argptr);
  331. }
  332. }
  333. };
  334. #endif
  335. template <class TSink>
  336. class SinkRegistrator
  337. {
  338. };
  339. #if defined(DISPATCHER_LOG_REGISTER_EVENT_PROVIDER)
  340. template <>
  341. class SinkRegistrator<ETWHandlerFactory>
  342. {
  343. public:
  344. SinkRegistrator(const wchar_t* sguid = DISPATCHER_LOG_EVENT_GUID)
  345. {
  346. DispatchLog::get().AttachSink( DL_SINK_IMsgHandler
  347. , ETWHandlerFactory::get().GetSink(sguid));
  348. }
  349. };
  350. #endif
  351. #if defined(DISPATCHER_LOG_REGISTER_FILE_WRITER)
  352. template <>
  353. class SinkRegistrator<FileSink>
  354. {
  355. public:
  356. SinkRegistrator()
  357. {
  358. DispatchLog::get().AttachSink( DL_SINK_IMsgHandler, &FileSink::get(DISPACTHER_LOG_FW_PATH));
  359. }
  360. };
  361. void FileSink::Write(int level, int /*opcode*/, const char * msg, va_list argptr)
  362. {
  363. if (NULL != m_hdl && NULL != msg)
  364. {
  365. fprintf(m_hdl, "%s", CODE_TO_STRING(level, LevelStrings));
  366. vfprintf(m_hdl, msg, argptr);
  367. }
  368. }
  369. #endif
  370. //////////////////////////////////////////////////////////////////////////
  371. //singletons initialization section
  372. #ifdef DISPATCHER_LOG_REGISTER_EVENT_PROVIDER
  373. static SinkRegistrator<ETWHandlerFactory> g_registrator1;
  374. #endif
  375. #ifdef DISPATCHER_LOG_REGISTER_FILE_WRITER
  376. static SinkRegistrator<FileSink> g_registrator2;
  377. #endif
  378. #endif//(MFX_DISPATCHER_LOG)