Skip to content

Commit

Permalink
Logger: add a log system instead of std::cout
Browse files Browse the repository at this point in the history
Add a logger with level from ERROR to DEBUG

Signed-off-by: Stéphane Cerveau <[email protected]>
  • Loading branch information
dabrain34 committed Nov 26, 2024
1 parent 8c8c80d commit 2f22d6e
Show file tree
Hide file tree
Showing 25 changed files with 446 additions and 317 deletions.
96 changes: 96 additions & 0 deletions common/include/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef LOGGER_H_
#define LOGGER_H_

#include <iostream>
#include <fstream>
#include <string>
#include <stdarg.h>

// Enum for log levels
enum LogLevel {
NONE = 0, // Use this to disable logging
ERROR,
WARNING,
INFO,
DEBUG
};

#define LOG_S_DEBUG Logger::instance()(LogLevel::DEBUG)
#define LOG_S_INFO Logger::instance()(LogLevel::INFO)
#define LOG_S_WARN Logger::instance()(LogLevel::WARNING)
#define LOG_S_ERROR Logger::instance()(LogLevel::ERROR)

#define LOG_DEBUG(ARGS ...) Logger::instance().printf(LogLevel::DEBUG, ## ARGS)
#define LOG_INFO(ARGS...) Logger::instance().printf(LogLevel::INFO, ## ARGS)
#define LOG_WARN(ARGS...) Logger::instance().printf(LogLevel::WARNING, ## ARGS)
#define LOG_ERROR(ARGS...) Logger::instance().printf(LogLevel::ERROR, ## ARGS)

class Logger {
private:
std::ostream& os; // The output stream (e.g., std::cout or std::ofstream)
std::ostream& err; // The error stream (e.g., std::cerr)
LogLevel currentLevel; // Current log level
LogLevel messageLevel; // The log level for the current message

public:
static Logger &instance ()
{
static Logger instance;
return instance;
}
// Constructor to set the output stream and log level (default is INFO)
Logger(std::ostream& outStream = std::cout, std::ostream& errStream = std::cerr, LogLevel level = LogLevel::INFO)
: os(outStream), err(errStream), currentLevel(level), messageLevel(LogLevel::INFO) {}

// Set the log level for the logger
void setLogLevel(int level) {
if (level > DEBUG)
level = DEBUG;
currentLevel = static_cast<LogLevel>(level);
}

// Set the log level for the current message
Logger& operator()(LogLevel level) {
messageLevel = level;
return *this;
}

// Overload the << operator for generic types
template<typename T>
Logger& operator<<(const T& data) {
if (messageLevel <= currentLevel) {
if (messageLevel == ERROR)
err << data;
else
os << data;
}
return *this;
}

// Overload for stream manipulators (like std::endl)
typedef std::ostream& (*StreamManipulator)(std::ostream&);
Logger& operator<<(StreamManipulator manip) {
if (messageLevel <= currentLevel) {
if (messageLevel == ERROR)
err << manip;
else
os << manip; // Handle std::endl, std::flush, etc.
}
return *this;
}

void printf(LogLevel level, const char* format, ...) {
if (level <= currentLevel) {
va_list args;
va_start(args, format);
if (level == ERROR)
vfprintf(stderr, format, args);
else
vfprintf(stdout,format, args);
va_end(args);
}
}
};


#endif
29 changes: 15 additions & 14 deletions common/include/VkVideoCore/VulkanVideoCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "VkCodecUtils/VulkanDeviceContext.h"
#include "VkCodecUtils/Helpers.h"
#include "VkVideoCore/VkVideoCoreProfile.h"
#include "Logger.h"

class VulkanVideoCapabilities
{
Expand Down Expand Up @@ -219,24 +220,24 @@ class VulkanVideoCapabilities
}

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

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

std::cout << "\t\t\t" << "minBitstreamBufferOffsetAlignment: " << pVideoCapabilities->minBitstreamBufferOffsetAlignment << std::endl;
std::cout << "\t\t\t" << "minBitstreamBufferSizeAlignment: " << pVideoCapabilities->minBitstreamBufferSizeAlignment << std::endl;
std::cout << "\t\t\t" << "pictureAccessGranularity: " << pVideoCapabilities->pictureAccessGranularity.width << " x " << pVideoCapabilities->pictureAccessGranularity.height << std::endl;
std::cout << "\t\t\t" << "minCodedExtent: " << pVideoCapabilities->minCodedExtent.width << " x " << pVideoCapabilities->minCodedExtent.height << std::endl;
std::cout << "\t\t\t" << "maxCodedExtent: " << pVideoCapabilities->maxCodedExtent.width << " x " << pVideoCapabilities->maxCodedExtent.height << std::endl;
std::cout << "\t\t\t" << "maxDpbSlots: " << pVideoCapabilities->maxDpbSlots << std::endl;
std::cout << "\t\t\t" << "maxActiveReferencePictures: " << pVideoCapabilities->maxActiveReferencePictures << std::endl;
LOG_S_DEBUG << "\t\t\t" << "minBitstreamBufferOffsetAlignment: " << pVideoCapabilities->minBitstreamBufferOffsetAlignment << std::endl;
LOG_S_DEBUG << "\t\t\t" << "minBitstreamBufferSizeAlignment: " << pVideoCapabilities->minBitstreamBufferSizeAlignment << std::endl;
LOG_S_DEBUG << "\t\t\t" << "pictureAccessGranularity: " << pVideoCapabilities->pictureAccessGranularity.width << " x " << pVideoCapabilities->pictureAccessGranularity.height << std::endl;
LOG_S_DEBUG << "\t\t\t" << "minCodedExtent: " << pVideoCapabilities->minCodedExtent.width << " x " << pVideoCapabilities->minCodedExtent.height << std::endl;
LOG_S_DEBUG << "\t\t\t" << "maxCodedExtent: " << pVideoCapabilities->maxCodedExtent.width << " x " << pVideoCapabilities->maxCodedExtent.height << std::endl;
LOG_S_DEBUG << "\t\t\t" << "maxDpbSlots: " << pVideoCapabilities->maxDpbSlots << std::endl;
LOG_S_DEBUG << "\t\t\t" << "maxActiveReferencePictures: " << pVideoCapabilities->maxActiveReferencePictures << std::endl;

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

if (strncmp(pVideoCapabilities->stdHeaderVersion.extensionName,
VK_STD_VULKAN_VIDEO_CODEC_H264_DECODE_EXTENSION_NAME,
Expand All @@ -247,7 +248,7 @@ class VulkanVideoCapabilities
}
} else if (videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR) {
const VkVideoDecodeH265CapabilitiesKHR* pH265DecCapabilities = (VkVideoDecodeH265CapabilitiesKHR*)pVideoDecodeCapabilities->pNext;
std::cout << "\t\t\t" << "maxLevelIdc: " << pH265DecCapabilities->maxLevelIdc << std::endl;
LOG_S_DEBUG << "\t\t\t" << "maxLevelIdc: " << pH265DecCapabilities->maxLevelIdc << std::endl;
if (strncmp(pVideoCapabilities->stdHeaderVersion.extensionName,
VK_STD_VULKAN_VIDEO_CODEC_H265_DECODE_EXTENSION_NAME,
sizeof (pVideoCapabilities->stdHeaderVersion.extensionName) - 1U) ||
Expand Down Expand Up @@ -301,9 +302,9 @@ class VulkanVideoCapabilities
result = vkDevCtx->GetPhysicalDeviceVideoFormatPropertiesKHR(vkDevCtx->getPhysicalDevice(), &videoFormatInfo, &supportedFormatCount, pSupportedFormats);
assert(result == VK_SUCCESS);
if (dumpData) {
std::cout << "\t\t\t" << ((videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ? "h264" : "h265") << "decode formats: " << std::endl;
LOG_S_DEBUG << "\t\t\t" << ((videoProfile.GetCodecType() == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ? "h264" : "h265") << "decode formats: " << std::endl;
for (uint32_t fmt = 0; fmt < supportedFormatCount; fmt++) {
std::cout << "\t\t\t " << fmt << ": " << std::hex << pSupportedFormats[fmt].format << std::dec << std::endl;
LOG_S_DEBUG << "\t\t\t " << fmt << ": " << std::hex << pSupportedFormats[fmt].format << std::dec << std::endl;
}
}

Expand Down
13 changes: 7 additions & 6 deletions common/libs/VkCodecUtils/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <assert.h>
#include <iostream>
#include "HelpersDispatchTable.h"
#include "Logger.h"

namespace vk {

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

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

printf("\nERROR: GetQueryPoolResults() result: 0x%x\n", queryResult);
std::cout << "\t +++++++++++++++++++++++++++< " << pictureIndex
LOG_ERROR("\nERROR: GetQueryPoolResults() result: 0x%x\n", queryResult);
LOG_S_WARN << "\t +++++++++++++++++++++++++++< " << pictureIndex
<< " >++++++++++++++++++++++++++++++" << std::endl;
std::cout << "\t => Decode Status for CurrPicIdx: " << pictureIndex << std::endl
LOG_S_WARN << "\t => Decode Status for CurrPicIdx: " << pictureIndex << std::endl
<< "\t\tdecodeStatus: " << decodeStatus << std::endl;

if (queryResult == VK_ERROR_DEVICE_LOST) {
std::cout << "\t Dropping frame" << std::endl;
LOG_S_WARN << "\t Dropping frame" << std::endl;
break;
}

if ((queryResult == VK_SUCCESS) && (decodeStatus == VK_QUERY_RESULT_STATUS_ERROR_KHR)) {
std::cout << "\t Decoding of the frame failed." << std::endl;
LOG_S_ERROR << "\t Decoding of the frame failed." << std::endl;
break;
}
}
Expand Down
13 changes: 10 additions & 3 deletions common/libs/VkCodecUtils/ProgramConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
#include <vector>
#include <functional>
#include <algorithm>
#include <iomanip>
#include <iomanip>
#include <sstream>
#include "vulkan_interfaces.h"
#include "Logger.h"

struct ProgramConfig {

Expand Down Expand Up @@ -83,7 +84,7 @@ struct ProgramConfig {
}

using ProgramArgs = std::vector<ArgSpec>;
static bool showHelp(const char ** argv, const ProgramArgs &spec) {
static bool showHelp(const char ** argv, const ProgramArgs &spec) {
std::cout << argv[0] << std::endl;
for ( auto& flag : spec ) {
std::stringstream ss;
Expand Down Expand Up @@ -112,6 +113,12 @@ struct ProgramConfig {
exit(EXIT_SUCCESS);
return rtn;
}},
{"--logLevel", "-l", 1, "Set the log level",
[this](const char **args, const ProgramArgs &a) {
int logLevel = std::atoi(args[0]);
Logger::instance().setLogLevel(logLevel);
return true;
}},
{"--enableStrDemux", nullptr, 0, "Enable stream demuxing",
[this](const char **, const ProgramArgs &a) {
enableStreamDemuxing = true;
Expand Down Expand Up @@ -347,7 +354,7 @@ struct ProgramConfig {
std::cerr << "Missing arguments for \"" << argv[i] << "\"" << std::endl;
exit(EXIT_FAILURE);
}
disableValueCheck = true;
disableValueCheck = true;
i++;
}

Expand Down
2 changes: 1 addition & 1 deletion common/libs/VkCodecUtils/VulkanComputePipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ VkResult VulkanComputePipeline::CreatePipeline(const VulkanDeviceContext* vkDevC

const bool verbose = false;

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

DestroyShaderModule();
m_shaderModule = shaderCompiler.BuildGlslShader(shaderCode,
Expand Down
Loading

0 comments on commit 2f22d6e

Please sign in to comment.