| 1 |
/*############################################################################ |
| 2 |
# Copyright (C) Intel Corporation |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
############################################################################*/ |
| 6 |
|
| 7 |
#include "vpl/mfx_dispatcher_vpl_log.h" |
| 8 |
|
| 9 |
DispatcherLogVPL::DispatcherLogVPL() : m_logLevel(0), m_logFileName(), m_logFile(nullptr) {} |
| 10 |
|
| 11 |
DispatcherLogVPL::~DispatcherLogVPL() { |
| 12 |
if (!m_logFileName.empty() && m_logFile) |
| 13 |
fclose(m_logFile); |
| 14 |
m_logFile = nullptr; |
| 15 |
} |
| 16 |
|
| 17 |
mfxStatus DispatcherLogVPL::Init(mfxU32 logLevel, const std::string &logFileName) { |
| 18 |
// avoid leaking file handle if Init is accidentally called more than once |
| 19 |
if (m_logFile) |
| 20 |
return MFX_ERR_UNSUPPORTED; |
| 21 |
|
| 22 |
m_logLevel = logLevel; |
| 23 |
m_logFileName = logFileName; |
| 24 |
|
| 25 |
// append to file if it already exists, otherwise create a new one |
| 26 |
// m_logFile will be closed in dtor |
| 27 |
if (m_logLevel) { |
| 28 |
if (m_logFileName.empty()) { |
| 29 |
m_logFile = stdout; |
| 30 |
} |
| 31 |
else { |
| 32 |
#if defined(_WIN32) || defined(_WIN64) |
| 33 |
fopen_s(&m_logFile, m_logFileName.c_str(), "a"); |
| 34 |
#else |
| 35 |
m_logFile = fopen(m_logFileName.c_str(), "a"); |
| 36 |
#endif |
| 37 |
if (!m_logFile) { |
| 38 |
m_logFile = stdout; |
| 39 |
fprintf(m_logFile, |
| 40 |
"Warning - unable to create logfile %s\n", |
| 41 |
m_logFileName.c_str()); |
| 42 |
fprintf(m_logFile, "Log output will be sent to stdout\n"); |
| 43 |
m_logFileName.clear(); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return MFX_ERR_NONE; |
| 49 |
} |
| 50 |
|
| 51 |
mfxStatus DispatcherLogVPL::LogMessage(const char *msg, ...) { |
| 52 |
if (!m_logLevel || !m_logFile) |
| 53 |
return MFX_ERR_NONE; |
| 54 |
|
| 55 |
va_list args; |
| 56 |
va_start(args, msg); |
| 57 |
vfprintf(m_logFile, msg, args); |
| 58 |
va_end(args); |
| 59 |
|
| 60 |
fprintf(m_logFile, "\n"); |
| 61 |
|
| 62 |
return MFX_ERR_NONE; |
| 63 |
} |