Skip to content

Commit 2f22d6e

Browse files
committed
Logger: add a log system instead of std::cout
Add a logger with level from ERROR to DEBUG Signed-off-by: Stéphane Cerveau <scerveau@igalia.com>
1 parent 8c8c80d commit 2f22d6e

25 files changed

+446
-317
lines changed

common/include/Logger.h

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef LOGGER_H_
2+
#define LOGGER_H_
3+
4+
#include <iostream>
5+
#include <fstream>
6+
#include <string>
7+
#include <stdarg.h>
8+
9+
// Enum for log levels
10+
enum LogLevel {
11+
NONE = 0, // Use this to disable logging
12+
ERROR,
13+
WARNING,
14+
INFO,
15+
DEBUG
16+
};
17+
18+
#define LOG_S_DEBUG Logger::instance()(LogLevel::DEBUG)
19+
#define LOG_S_INFO Logger::instance()(LogLevel::INFO)
20+
#define LOG_S_WARN Logger::instance()(LogLevel::WARNING)
21+
#define LOG_S_ERROR Logger::instance()(LogLevel::ERROR)
22+
23+
#define LOG_DEBUG(ARGS ...) Logger::instance().printf(LogLevel::DEBUG, ## ARGS)
24+
#define LOG_INFO(ARGS...) Logger::instance().printf(LogLevel::INFO, ## ARGS)
25+
#define LOG_WARN(ARGS...) Logger::instance().printf(LogLevel::WARNING, ## ARGS)
26+
#define LOG_ERROR(ARGS...) Logger::instance().printf(LogLevel::ERROR, ## ARGS)
27+
28+
class Logger {
29+
private:
30+
std::ostream& os; // The output stream (e.g., std::cout or std::ofstream)
31+
std::ostream& err; // The error stream (e.g., std::cerr)
32+
LogLevel currentLevel; // Current log level
33+
LogLevel messageLevel; // The log level for the current message
34+
35+
public:
36+
static Logger &instance ()
37+
{
38+
static Logger instance;
39+
return instance;
40+
}
41+
// Constructor to set the output stream and log level (default is INFO)
42+
Logger(std::ostream& outStream = std::cout, std::ostream& errStream = std::cerr, LogLevel level = LogLevel::INFO)
43+
: os(outStream), err(errStream), currentLevel(level), messageLevel(LogLevel::INFO) {}
44+
45+
// Set the log level for the logger
46+
void setLogLevel(int level) {
47+
if (level > DEBUG)
48+
level = DEBUG;
49+
currentLevel = static_cast<LogLevel>(level);
50+
}
51+
52+
// Set the log level for the current message
53+
Logger& operator()(LogLevel level) {
54+
messageLevel = level;
55+
return *this;
56+
}
57+
58+
// Overload the << operator for generic types
59+
template<typename T>
60+
Logger& operator<<(const T& data) {
61+
if (messageLevel <= currentLevel) {
62+
if (messageLevel == ERROR)
63+
err << data;
64+
else
65+
os << data;
66+
}
67+
return *this;
68+
}
69+
70+
// Overload for stream manipulators (like std::endl)
71+
typedef std::ostream& (*StreamManipulator)(std::ostream&);
72+
Logger& operator<<(StreamManipulator manip) {
73+
if (messageLevel <= currentLevel) {
74+
if (messageLevel == ERROR)
75+
err << manip;
76+
else
77+
os << manip; // Handle std::endl, std::flush, etc.
78+
}
79+
return *this;
80+
}
81+
82+
void printf(LogLevel level, const char* format, ...) {
83+
if (level <= currentLevel) {
84+
va_list args;
85+
va_start(args, format);
86+
if (level == ERROR)
87+
vfprintf(stderr, format, args);
88+
else
89+
vfprintf(stdout,format, args);
90+
va_end(args);
91+
}
92+
}
93+
};
94+
95+
96+
#endif

common/include/VkVideoCore/VulkanVideoCapabilities.h

+15-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "VkCodecUtils/VulkanDeviceContext.h"
2323
#include "VkCodecUtils/Helpers.h"
2424
#include "VkVideoCore/VkVideoCoreProfile.h"
25+
#include "Logger.h"
2526

2627
class VulkanVideoCapabilities
2728
{
@@ -219,24 +220,24 @@ class VulkanVideoCapabilities
219220
}
220221

221222
if (dumpData) {
222-
std::cout << "\t\t\t" << ((videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ? "h264" : "h265") << "decode capabilities: " << std::endl;
223+
LOG_S_DEBUG << "\t\t\t" << ((videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ? "h264" : "h265") << "decode capabilities: " << std::endl;
223224

224225
if (pVideoCapabilities->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR) {
225-
std::cout << "\t\t\t" << "Use separate reference images" << std::endl;
226+
LOG_S_DEBUG << "\t\t\t" << "Use separate reference images" << std::endl;
226227
}
227228

228-
std::cout << "\t\t\t" << "minBitstreamBufferOffsetAlignment: " << pVideoCapabilities->minBitstreamBufferOffsetAlignment << std::endl;
229-
std::cout << "\t\t\t" << "minBitstreamBufferSizeAlignment: " << pVideoCapabilities->minBitstreamBufferSizeAlignment << std::endl;
230-
std::cout << "\t\t\t" << "pictureAccessGranularity: " << pVideoCapabilities->pictureAccessGranularity.width << " x " << pVideoCapabilities->pictureAccessGranularity.height << std::endl;
231-
std::cout << "\t\t\t" << "minCodedExtent: " << pVideoCapabilities->minCodedExtent.width << " x " << pVideoCapabilities->minCodedExtent.height << std::endl;
232-
std::cout << "\t\t\t" << "maxCodedExtent: " << pVideoCapabilities->maxCodedExtent.width << " x " << pVideoCapabilities->maxCodedExtent.height << std::endl;
233-
std::cout << "\t\t\t" << "maxDpbSlots: " << pVideoCapabilities->maxDpbSlots << std::endl;
234-
std::cout << "\t\t\t" << "maxActiveReferencePictures: " << pVideoCapabilities->maxActiveReferencePictures << std::endl;
229+
LOG_S_DEBUG << "\t\t\t" << "minBitstreamBufferOffsetAlignment: " << pVideoCapabilities->minBitstreamBufferOffsetAlignment << std::endl;
230+
LOG_S_DEBUG << "\t\t\t" << "minBitstreamBufferSizeAlignment: " << pVideoCapabilities->minBitstreamBufferSizeAlignment << std::endl;
231+
LOG_S_DEBUG << "\t\t\t" << "pictureAccessGranularity: " << pVideoCapabilities->pictureAccessGranularity.width << " x " << pVideoCapabilities->pictureAccessGranularity.height << std::endl;
232+
LOG_S_DEBUG << "\t\t\t" << "minCodedExtent: " << pVideoCapabilities->minCodedExtent.width << " x " << pVideoCapabilities->minCodedExtent.height << std::endl;
233+
LOG_S_DEBUG << "\t\t\t" << "maxCodedExtent: " << pVideoCapabilities->maxCodedExtent.width << " x " << pVideoCapabilities->maxCodedExtent.height << std::endl;
234+
LOG_S_DEBUG << "\t\t\t" << "maxDpbSlots: " << pVideoCapabilities->maxDpbSlots << std::endl;
235+
LOG_S_DEBUG << "\t\t\t" << "maxActiveReferencePictures: " << pVideoCapabilities->maxActiveReferencePictures << std::endl;
235236

236237
if (videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) {
237238
const VkVideoDecodeH264CapabilitiesKHR* pH264DecCapabilities = (VkVideoDecodeH264CapabilitiesKHR*)pVideoDecodeCapabilities->pNext;
238-
std::cout << "\t\t\t" << "maxLevelIdc: " << pH264DecCapabilities->maxLevelIdc << std::endl;
239-
std::cout << "\t\t\t" << "fieldOffsetGranularity: " << pH264DecCapabilities->fieldOffsetGranularity.x << " x " << pH264DecCapabilities->fieldOffsetGranularity.y << std::endl;
239+
LOG_S_DEBUG << "\t\t\t" << "maxLevelIdc: " << pH264DecCapabilities->maxLevelIdc << std::endl;
240+
LOG_S_DEBUG << "\t\t\t" << "fieldOffsetGranularity: " << pH264DecCapabilities->fieldOffsetGranularity.x << " x " << pH264DecCapabilities->fieldOffsetGranularity.y << std::endl;
240241

241242
if (strncmp(pVideoCapabilities->stdHeaderVersion.extensionName,
242243
VK_STD_VULKAN_VIDEO_CODEC_H264_DECODE_EXTENSION_NAME,
@@ -247,7 +248,7 @@ class VulkanVideoCapabilities
247248
}
248249
} else if (videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR) {
249250
const VkVideoDecodeH265CapabilitiesKHR* pH265DecCapabilities = (VkVideoDecodeH265CapabilitiesKHR*)pVideoDecodeCapabilities->pNext;
250-
std::cout << "\t\t\t" << "maxLevelIdc: " << pH265DecCapabilities->maxLevelIdc << std::endl;
251+
LOG_S_DEBUG << "\t\t\t" << "maxLevelIdc: " << pH265DecCapabilities->maxLevelIdc << std::endl;
251252
if (strncmp(pVideoCapabilities->stdHeaderVersion.extensionName,
252253
VK_STD_VULKAN_VIDEO_CODEC_H265_DECODE_EXTENSION_NAME,
253254
sizeof (pVideoCapabilities->stdHeaderVersion.extensionName) - 1U) ||
@@ -301,9 +302,9 @@ class VulkanVideoCapabilities
301302
result = vkDevCtx->GetPhysicalDeviceVideoFormatPropertiesKHR(vkDevCtx->getPhysicalDevice(), &videoFormatInfo, &supportedFormatCount, pSupportedFormats);
302303
assert(result == VK_SUCCESS);
303304
if (dumpData) {
304-
std::cout << "\t\t\t" << ((videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ? "h264" : "h265") << "decode formats: " << std::endl;
305+
LOG_S_DEBUG << "\t\t\t" << ((videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ? "h264" : "h265") << "decode formats: " << std::endl;
305306
for (uint32_t fmt = 0; fmt < supportedFormatCount; fmt++) {
306-
std::cout << "\t\t\t " << fmt << ": " << std::hex << pSupportedFormats[fmt].format << std::dec << std::endl;
307+
LOG_S_DEBUG << "\t\t\t " << fmt << ": " << std::hex << pSupportedFormats[fmt].format << std::dec << std::endl;
307308
}
308309
}
309310

common/libs/VkCodecUtils/Helpers.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <assert.h>
2424
#include <iostream>
2525
#include "HelpersDispatchTable.h"
26+
#include "Logger.h"
2627

2728
namespace vk {
2829

@@ -282,7 +283,7 @@ inline VkResult WaitAndGetStatus(const VkInterfaceFunctions* vkIf, VkDevice devi
282283
do {
283284
result = WaitAndResetFence(vkIf, device, fence, resetAfterWait, fenceName, fenceWaitTimeout, fenceTotalWaitTimeout);
284285
if (result != VK_SUCCESS) {
285-
std::cout << "WaitForFences timeout " << fenceWaitTimeout
286+
LOG_S_WARN << "WaitForFences timeout " << fenceWaitTimeout
286287
<< " result " << result << " retry " << retryCount << std::endl << std::flush;
287288

288289
VkQueryResultStatusKHR decodeStatus = VK_QUERY_RESULT_STATUS_NOT_READY_KHR;
@@ -295,19 +296,19 @@ inline VkResult WaitAndGetStatus(const VkInterfaceFunctions* vkIf, VkDevice devi
295296
sizeof(decodeStatus),
296297
VK_QUERY_RESULT_WITH_STATUS_BIT_KHR);
297298

298-
printf("\nERROR: GetQueryPoolResults() result: 0x%x\n", queryResult);
299-
std::cout << "\t +++++++++++++++++++++++++++< " << pictureIndex
299+
LOG_ERROR("\nERROR: GetQueryPoolResults() result: 0x%x\n", queryResult);
300+
LOG_S_WARN << "\t +++++++++++++++++++++++++++< " << pictureIndex
300301
<< " >++++++++++++++++++++++++++++++" << std::endl;
301-
std::cout << "\t => Decode Status for CurrPicIdx: " << pictureIndex << std::endl
302+
LOG_S_WARN << "\t => Decode Status for CurrPicIdx: " << pictureIndex << std::endl
302303
<< "\t\tdecodeStatus: " << decodeStatus << std::endl;
303304

304305
if (queryResult == VK_ERROR_DEVICE_LOST) {
305-
std::cout << "\t Dropping frame" << std::endl;
306+
LOG_S_WARN << "\t Dropping frame" << std::endl;
306307
break;
307308
}
308309

309310
if ((queryResult == VK_SUCCESS) && (decodeStatus == VK_QUERY_RESULT_STATUS_ERROR_KHR)) {
310-
std::cout << "\t Decoding of the frame failed." << std::endl;
311+
LOG_S_ERROR << "\t Decoding of the frame failed." << std::endl;
311312
break;
312313
}
313314
}

common/libs/VkCodecUtils/ProgramConfig.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
#include <vector>
2727
#include <functional>
2828
#include <algorithm>
29-
#include <iomanip>
29+
#include <iomanip>
3030
#include <sstream>
3131
#include "vulkan_interfaces.h"
32+
#include "Logger.h"
3233

3334
struct ProgramConfig {
3435

@@ -83,7 +84,7 @@ struct ProgramConfig {
8384
}
8485

8586
using ProgramArgs = std::vector<ArgSpec>;
86-
static bool showHelp(const char ** argv, const ProgramArgs &spec) {
87+
static bool showHelp(const char ** argv, const ProgramArgs &spec) {
8788
std::cout << argv[0] << std::endl;
8889
for ( auto& flag : spec ) {
8990
std::stringstream ss;
@@ -112,6 +113,12 @@ struct ProgramConfig {
112113
exit(EXIT_SUCCESS);
113114
return rtn;
114115
}},
116+
{"--logLevel", "-l", 1, "Set the log level",
117+
[this](const char **args, const ProgramArgs &a) {
118+
int logLevel = std::atoi(args[0]);
119+
Logger::instance().setLogLevel(logLevel);
120+
return true;
121+
}},
115122
{"--enableStrDemux", nullptr, 0, "Enable stream demuxing",
116123
[this](const char **, const ProgramArgs &a) {
117124
enableStreamDemuxing = true;
@@ -347,7 +354,7 @@ struct ProgramConfig {
347354
std::cerr << "Missing arguments for \"" << argv[i] << "\"" << std::endl;
348355
exit(EXIT_FAILURE);
349356
}
350-
disableValueCheck = true;
357+
disableValueCheck = true;
351358
i++;
352359
}
353360

common/libs/VkCodecUtils/VulkanComputePipeline.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ VkResult VulkanComputePipeline::CreatePipeline(const VulkanDeviceContext* vkDevC
4545

4646
const bool verbose = false;
4747

48-
if (verbose) printf("\nCompute shader code:\n %s", shaderCode);
48+
if (verbose) LOG_DEBUG("\nCompute shader code:\n %s", shaderCode);
4949

5050
DestroyShaderModule();
5151
m_shaderModule = shaderCompiler.BuildGlslShader(shaderCode,

0 commit comments

Comments
 (0)