Skip to content

Commit 05eb8f3

Browse files
committed
Logger: add a log system instead of std::cout or printf
Add a logger with level from ERROR to DEBUG Signed-off-by: Stéphane Cerveau <[email protected]>
1 parent 0b4eb47 commit 05eb8f3

34 files changed

+738
-616
lines changed

common/include/Logger.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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_CAT_LEVEL(LEVEL, CAT, FMT, ARGS ...) Logger::instance().printf(LEVEL, CAT "" FMT"\n", ## ARGS)
24+
25+
#define LOG_DEBUG_CAT(CAT, ARGS ...) LOG_CAT_LEVEL(LogLevel::DEBUG, CAT, ##ARGS)
26+
#define LOG_INFO_CAT(CAT, ARGS ...) LOG_CAT_LEVEL(LogLevel::INFO, CAT, ##ARGS)
27+
#define LOG_WARN_CAT(CAT, ARGS ...) LOG_CAT_LEVEL(LogLevel::WARNING, CAT, ##ARGS)
28+
#define LOG_ERROR_CAT(CAT, ARGS ...) LOG_CAT_LEVEL(LogLevel::ERROR, CAT, ##ARGS)
29+
30+
#define LOG_DEBUG(ARGS ...) LOG_DEBUG_CAT("", ## ARGS)
31+
#define LOG_INFO(ARGS...) LOG_INFO_CAT("", ## ARGS)
32+
#define LOG_WARN(ARGS...) LOG_WARN_CAT("", ## ARGS)
33+
#define LOG_ERROR(ARGS...) LOG_DEBUG_CAT("", ## ARGS)
34+
35+
#define LOG_DEBUG_CONFIG(ARGS ...) LOG_DEBUG_CAT("config:\t", ## ARGS)
36+
#define LOG_INFO_CONFIG(ARGS...) LOG_INFO_CAT("config:\t", ## ARGS)
37+
#define LOG_WARN_CONFIG(ARGS...) LOG_WARN_CAT("config:\t", ## ARGS)
38+
#define LOG_ERROR_CONFIG(ARGS...) LOG_DEBUG_CAT("config:\t", ## ARGS)
39+
40+
class Logger {
41+
private:
42+
std::ostream& os; // The output stream (e.g., std::cout or std::ofstream)
43+
std::ostream& err; // The error stream (e.g., std::cerr)
44+
LogLevel currentLevel; // Current log level
45+
LogLevel messageLevel; // The log level for the current message
46+
47+
public:
48+
static Logger &instance ()
49+
{
50+
static Logger instance;
51+
return instance;
52+
}
53+
// Constructor to set the output stream and log level (default is INFO)
54+
Logger(std::ostream& outStream = std::cout, std::ostream& errStream = std::cerr, LogLevel level = LogLevel::INFO)
55+
: os(outStream), err(errStream), currentLevel(level), messageLevel(LogLevel::INFO) {}
56+
57+
// Set the log level for the logger
58+
void setLogLevel(int level) {
59+
if (level > DEBUG)
60+
level = DEBUG;
61+
currentLevel = static_cast<LogLevel>(level);
62+
}
63+
64+
// Set the log level for the current message
65+
Logger& operator()(LogLevel level) {
66+
messageLevel = level;
67+
return *this;
68+
}
69+
70+
// Overload the << operator for generic types
71+
template<typename T>
72+
Logger& operator<<(const T& data) {
73+
if (messageLevel <= currentLevel) {
74+
if (messageLevel == ERROR)
75+
err << data;
76+
else
77+
os << data;
78+
}
79+
return *this;
80+
}
81+
82+
// Overload for stream manipulators (like std::endl)
83+
typedef std::ostream& (*StreamManipulator)(std::ostream&);
84+
Logger& operator<<(StreamManipulator manip) {
85+
if (messageLevel <= currentLevel) {
86+
if (messageLevel == ERROR)
87+
err << manip;
88+
else
89+
os << manip; // Handle std::endl, std::flush, etc.
90+
}
91+
return *this;
92+
}
93+
94+
void printf(LogLevel level, const char* format, ...) {
95+
if (level <= currentLevel) {
96+
va_list args;
97+
va_start(args, format);
98+
if (level == ERROR)
99+
vfprintf(stderr, format, args);
100+
else
101+
vfprintf(stdout,format, args);
102+
va_end(args);
103+
}
104+
}
105+
};
106+
107+
108+
#endif

common/include/VkVideoCore/VkVideoCoreProfile.h

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "vulkan/vulkan.h"
2828
#include "nvidia_utils/vulkan/ycbcr_utils.h"
29+
#include "Logger.h"
2930

3031
typedef enum StdChromaFormatIdc {
3132
chroma_format_idc_monochrome = STD_VIDEO_H264_CHROMA_FORMAT_IDC_MONOCHROME,
@@ -621,58 +622,58 @@ class VkVideoCoreProfile
621622
{
622623
// formatProfile info based on supported chroma_format_idc
623624
if (pVideoProfile->chromaSubsampling & VK_VIDEO_CHROMA_SUBSAMPLING_MONOCHROME_BIT_KHR) {
624-
std::cout << "MONO, ";
625+
LOG_S_DEBUG << "MONO, ";
625626
}
626627
if (pVideoProfile->chromaSubsampling & VK_VIDEO_CHROMA_SUBSAMPLING_420_BIT_KHR) {
627-
std::cout << " 420, ";
628+
LOG_S_DEBUG << " 420, ";
628629
}
629630
if (pVideoProfile->chromaSubsampling & VK_VIDEO_CHROMA_SUBSAMPLING_422_BIT_KHR) {
630-
std::cout << " 422, ";
631+
LOG_S_DEBUG << " 422, ";
631632
}
632633
if (pVideoProfile->chromaSubsampling & VK_VIDEO_CHROMA_SUBSAMPLING_444_BIT_KHR) {
633-
std::cout << " 444, ";
634+
LOG_S_DEBUG << " 444, ";
634635
}
635636

636637
// Profile info based on max bit_depth_luma_minus8
637638
if (pVideoProfile->lumaBitDepth & VK_VIDEO_COMPONENT_BIT_DEPTH_8_BIT_KHR) {
638-
std::cout << "LUMA: 8-bit, ";
639+
LOG_S_DEBUG << "LUMA: 8-bit, ";
639640
}
640641
if (pVideoProfile->lumaBitDepth & VK_VIDEO_COMPONENT_BIT_DEPTH_10_BIT_KHR) {
641-
std::cout << "LUMA: 10-bit, ";
642+
LOG_S_DEBUG << "LUMA: 10-bit, ";
642643
}
643644
if (pVideoProfile->lumaBitDepth & VK_VIDEO_COMPONENT_BIT_DEPTH_12_BIT_KHR) {
644-
std::cout << "LUMA: 12-bit, ";
645+
LOG_S_DEBUG << "LUMA: 12-bit, ";
645646
}
646647

647648
// Profile info based on max bit_depth_chroma_minus8
648649
if (pVideoProfile->chromaBitDepth & VK_VIDEO_COMPONENT_BIT_DEPTH_8_BIT_KHR) {
649-
std::cout << "CHROMA: 8-bit, ";
650+
LOG_S_DEBUG << "CHROMA: 8-bit, ";
650651
}
651652
if (pVideoProfile->chromaBitDepth & VK_VIDEO_COMPONENT_BIT_DEPTH_10_BIT_KHR) {
652-
std::cout << "CHROMA:10-bit, ";
653+
LOG_S_DEBUG << "CHROMA:10-bit, ";
653654
}
654655
if (pVideoProfile->chromaBitDepth & VK_VIDEO_COMPONENT_BIT_DEPTH_12_BIT_KHR) {
655-
std::cout << "CHROMA:12-bit,";
656+
LOG_S_DEBUG << "CHROMA:12-bit,";
656657
}
657658
}
658659

659660
static void DumpH264Profiles(VkVideoDecodeH264ProfileInfoKHR* pH264Profiles)
660661
{
661662
switch (pH264Profiles->stdProfileIdc) {
662663
case STD_VIDEO_H264_PROFILE_IDC_BASELINE:
663-
std::cout << "BASELINE, ";
664+
LOG_S_DEBUG << "BASELINE, ";
664665
break;
665666
case STD_VIDEO_H264_PROFILE_IDC_MAIN:
666-
std::cout << "MAIN, ";
667+
LOG_S_DEBUG << "MAIN, ";
667668
break;
668669
case STD_VIDEO_H264_PROFILE_IDC_HIGH:
669-
std::cout << "HIGH, ";
670+
LOG_S_DEBUG << "HIGH, ";
670671
break;
671672
case STD_VIDEO_H264_PROFILE_IDC_HIGH_444_PREDICTIVE:
672-
std::cout << "HIGH_444_PREDICTIVE, ";
673+
LOG_S_DEBUG << "HIGH_444_PREDICTIVE, ";
673674
break;
674675
default:
675-
std::cout << "UNKNOWN PROFILE, ";
676+
LOG_S_DEBUG << "UNKNOWN PROFILE, ";
676677
break;
677678
}
678679
}
@@ -681,22 +682,22 @@ class VkVideoCoreProfile
681682
{
682683
switch (pH265Profiles->stdProfileIdc) {
683684
case STD_VIDEO_H265_PROFILE_IDC_MAIN:
684-
std::cout << "MAIN, ";
685+
LOG_S_DEBUG << "MAIN, ";
685686
break;
686687
case STD_VIDEO_H265_PROFILE_IDC_MAIN_10:
687-
std::cout << "MAIN_10, ";
688+
LOG_S_DEBUG << "MAIN_10, ";
688689
break;
689690
case STD_VIDEO_H265_PROFILE_IDC_MAIN_STILL_PICTURE:
690-
std::cout << "MAIN_STILL_PICTURE, ";
691+
LOG_S_DEBUG << "MAIN_STILL_PICTURE, ";
691692
break;
692693
case STD_VIDEO_H265_PROFILE_IDC_FORMAT_RANGE_EXTENSIONS:
693-
std::cout << "FORMAT_RANGE_EXTENSIONS, ";
694+
LOG_S_DEBUG << "FORMAT_RANGE_EXTENSIONS, ";
694695
break;
695696
case STD_VIDEO_H265_PROFILE_IDC_SCC_EXTENSIONS:
696-
std::cout << "SCC_EXTENSIONS, ";
697+
LOG_S_DEBUG << "SCC_EXTENSIONS, ";
697698
break;
698699
default:
699-
std::cout << "UNKNOWN PROFILE, ";
700+
LOG_S_DEBUG << "UNKNOWN PROFILE, ";
700701
break;
701702
}
702703
}

common/include/VkVideoCore/VulkanVideoCapabilities.h

Lines changed: 19 additions & 18 deletions
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
{
@@ -52,7 +53,7 @@ class VulkanVideoCapabilities
5253
VkResult result = GetVideoCapabilities(vkDevCtx, videoProfile, &videoCapabilities);
5354
assert(result == VK_SUCCESS);
5455
if (result != VK_SUCCESS) {
55-
fprintf(stderr, "\nERROR: Input is not supported. GetVideoCapabilities() result: 0x%x\n", result);
56+
LOG_ERROR("\nERROR: Input is not supported. GetVideoCapabilities() result: 0x%x\n", result);
5657
}
5758
return result;
5859
}
@@ -76,7 +77,7 @@ class VulkanVideoCapabilities
7677
VkResult result = GetVideoCapabilities(vkDevCtx, videoProfile, &videoCapabilities);
7778
assert(result == VK_SUCCESS);
7879
if (result != VK_SUCCESS) {
79-
fprintf(stderr, "\nERROR: Input is not supported. GetVideoCapabilities() result: 0x%x\n", result);
80+
LOG_ERROR("\nERROR: Input is not supported. GetVideoCapabilities() result: 0x%x\n", result);
8081
}
8182
return result;
8283
}
@@ -118,13 +119,13 @@ class VulkanVideoCapabilities
118119
pictureFormat = supportedOutFormats[0];
119120

120121
} else {
121-
fprintf(stderr, "\nERROR: Unsupported decode capability flags.");
122+
LOG_ERROR("ERROR: Unsupported decode capability flags.");
122123
return VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR;
123124
}
124125

125126
assert(result == VK_SUCCESS);
126127
if (result != VK_SUCCESS) {
127-
fprintf(stderr, "\nERROR: GetVideoFormats() result: 0x%x\n", result);
128+
LOG_ERROR("ERROR: GetVideoFormats() result: 0x%x\n", result);
128129
}
129130

130131
assert((referencePicturesFormat != VK_FORMAT_UNDEFINED) && (pictureFormat != VK_FORMAT_UNDEFINED));
@@ -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

0 commit comments

Comments
 (0)