From f3999a431caaf0d067ed7abb9e29b3b50175a64c Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Fri, 20 Dec 2024 00:00:00 +0000 Subject: [PATCH] feat(logger): improve FFmpeg logging format Signed-off-by: k4yt3x --- src/logger_manager.cpp | 15 ++++----- tools/video2x/src/video2x.cpp | 58 +++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/logger_manager.cpp b/src/logger_manager.cpp index 2722feb..51e130d 100644 --- a/src/logger_manager.cpp +++ b/src/logger_manager.cpp @@ -26,19 +26,20 @@ static spdlog::level::level_enum ffmpeg_level_to_spdlog(int av_level) { } } -static void ffmpeg_log_callback(void *, int av_level, const char *fmt, va_list vargs) { - // Format the message into a buffer - char buffer[1024]; - vsnprintf(buffer, sizeof(buffer), fmt, vargs); +static void ffmpeg_log_callback(void *avcl, int level, const char *fmt, va_list vargs) { + // Format the message the same way as the default callback + char line[1024]; + int print_prefix = 1; + av_log_format_line(avcl, level, fmt, vargs, line, sizeof(line), &print_prefix); // Trim trailing newlines - std::string message = buffer; + std::string message = line; while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) { message.pop_back(); } - // Forward FFmpeg log message to the logger instance - video2x::logger()->log(ffmpeg_level_to_spdlog(av_level), message); + // Forward the formatted FFmpeg log message to the logger + video2x::logger()->log(ffmpeg_level_to_spdlog(level), "[FFmpeg] {}", message); } namespace video2x { diff --git a/tools/video2x/src/video2x.cpp b/tools/video2x/src/video2x.cpp index 7df8084..86b0e72 100644 --- a/tools/video2x/src/video2x.cpp +++ b/tools/video2x/src/video2x.cpp @@ -53,7 +53,14 @@ int wmain(int argc, wchar_t *argv[]) { #else int main(int argc, char **argv) { #endif - // Initialize arguments structures + // Initialize newline-safe logger with custom formatting pattern + std::shared_ptr logger_sink = std::make_shared(); + std::vector sinks = {logger_sink}; + video2x::logger_manager::LoggerManager::instance().reconfigure_logger( + "video2x", sinks, "[%Y-%m-%d %H:%M:%S] [%^%l%$] %v" + ); + + // Initialize argument and configuration structs Arguments arguments; video2x::processors::ProcessorConfig proc_cfg; video2x::encoder::EncoderConfig enc_cfg; @@ -76,11 +83,6 @@ int main(int argc, char **argv) { proc_cfg, enc_cfg, arguments.vk_device_index, arguments.hw_device_type, arguments.benchmark ); - // Register a newline-safe log sink - std::shared_ptr logger_sink = std::make_shared(); - std::vector sinks = {logger_sink}; - video2x::logger_manager::LoggerManager::instance().reconfigure_logger("video2x", sinks); - // Create a thread for video processing int proc_ret = 0; std::atomic completed = false; // Use atomic for thread-safe updates @@ -207,6 +209,7 @@ int main(int argc, char **argv) { // Print a newline if progress bar was displayed if (logger_sink->get_needs_newline()) { + logger_sink->set_needs_newline(false); std::cout << '\n'; } @@ -222,28 +225,31 @@ int main(int argc, char **argv) { video2x::logger()->info("Video processed successfully"); } - // Calculate statistics - int64_t processed_frames = video_processor.get_processed_frames(); - int time_elapsed = static_cast(timer.get_elapsed_time() / 1000); - auto [hours_elapsed, minutes_elapsed, seconds_elapsed] = - calculate_time_components(time_elapsed); - float average_speed_fps = static_cast(processed_frames) / - (time_elapsed > 0 ? static_cast(time_elapsed) : 1); + // Print the processing summary if the log level is info or lower + if (video2x::logger()->level() <= spdlog::level::info) { + // Calculate statistics + int64_t processed_frames = video_processor.get_processed_frames(); + int time_elapsed = static_cast(timer.get_elapsed_time() / 1000); + auto [hours_elapsed, minutes_elapsed, seconds_elapsed] = + calculate_time_components(time_elapsed); + float average_speed_fps = static_cast(processed_frames) / + (time_elapsed > 0 ? static_cast(time_elapsed) : 1); - // Print processing summary - std::cout << "====== Video2X " << (arguments.benchmark ? "Benchmark" : "Processing") - << " summary ======" << std::endl; - std::cout << "Video file processed: " << arguments.in_fname.u8string() << std::endl; - std::cout << "Total frames processed: " << processed_frames << std::endl; - std::cout << "Total time taken: " << std::setw(2) << std::setfill('0') << hours_elapsed << ":" - << std::setw(2) << std::setfill('0') << minutes_elapsed << ":" << std::setw(2) - << std::setfill('0') << seconds_elapsed << std::endl; - std::cout << "Average processing speed: " << std::fixed << std::setprecision(2) - << average_speed_fps << " FPS" << std::endl; + // Print processing summary + std::cout << "====== Video2X " << (arguments.benchmark ? "Benchmark" : "Processing") + << " summary ======" << std::endl; + std::cout << "Video file processed: " << arguments.in_fname.u8string() << std::endl; + std::cout << "Total frames processed: " << processed_frames << std::endl; + std::cout << "Total time taken: " << std::setw(2) << std::setfill('0') << hours_elapsed + << ":" << std::setw(2) << std::setfill('0') << minutes_elapsed << ":" + << std::setw(2) << std::setfill('0') << seconds_elapsed << std::endl; + std::cout << "Average processing speed: " << std::fixed << std::setprecision(2) + << average_speed_fps << " FPS" << std::endl; - // Print additional information if not in benchmark mode - if (!arguments.benchmark) { - std::cout << "Output written to: " << arguments.out_fname.u8string() << std::endl; + // Print additional information if not in benchmark mode + if (!arguments.benchmark) { + std::cout << "Output written to: " << arguments.out_fname.u8string() << std::endl; + } } return 0;