feat(logger): improve FFmpeg logging format

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
k4yt3x 2024-12-20 00:00:00 +00:00
parent 6676cd2439
commit f3999a431c
No known key found for this signature in database
2 changed files with 40 additions and 33 deletions

View File

@ -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) { static void ffmpeg_log_callback(void *avcl, int level, const char *fmt, va_list vargs) {
// Format the message into a buffer // Format the message the same way as the default callback
char buffer[1024]; char line[1024];
vsnprintf(buffer, sizeof(buffer), fmt, vargs); int print_prefix = 1;
av_log_format_line(avcl, level, fmt, vargs, line, sizeof(line), &print_prefix);
// Trim trailing newlines // Trim trailing newlines
std::string message = buffer; std::string message = line;
while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) { while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) {
message.pop_back(); message.pop_back();
} }
// Forward FFmpeg log message to the logger instance // Forward the formatted FFmpeg log message to the logger
video2x::logger()->log(ffmpeg_level_to_spdlog(av_level), message); video2x::logger()->log(ffmpeg_level_to_spdlog(level), "[FFmpeg] {}", message);
} }
namespace video2x { namespace video2x {

View File

@ -53,7 +53,14 @@ int wmain(int argc, wchar_t *argv[]) {
#else #else
int main(int argc, char **argv) { int main(int argc, char **argv) {
#endif #endif
// Initialize arguments structures // Initialize newline-safe logger with custom formatting pattern
std::shared_ptr<newline_safe_sink> logger_sink = std::make_shared<newline_safe_sink>();
std::vector<spdlog::sink_ptr> 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; Arguments arguments;
video2x::processors::ProcessorConfig proc_cfg; video2x::processors::ProcessorConfig proc_cfg;
video2x::encoder::EncoderConfig enc_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 proc_cfg, enc_cfg, arguments.vk_device_index, arguments.hw_device_type, arguments.benchmark
); );
// Register a newline-safe log sink
std::shared_ptr<newline_safe_sink> logger_sink = std::make_shared<newline_safe_sink>();
std::vector<spdlog::sink_ptr> sinks = {logger_sink};
video2x::logger_manager::LoggerManager::instance().reconfigure_logger("video2x", sinks);
// Create a thread for video processing // Create a thread for video processing
int proc_ret = 0; int proc_ret = 0;
std::atomic<bool> completed = false; // Use atomic for thread-safe updates std::atomic<bool> 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 // Print a newline if progress bar was displayed
if (logger_sink->get_needs_newline()) { if (logger_sink->get_needs_newline()) {
logger_sink->set_needs_newline(false);
std::cout << '\n'; std::cout << '\n';
} }
@ -222,28 +225,31 @@ int main(int argc, char **argv) {
video2x::logger()->info("Video processed successfully"); video2x::logger()->info("Video processed successfully");
} }
// Calculate statistics // Print the processing summary if the log level is info or lower
int64_t processed_frames = video_processor.get_processed_frames(); if (video2x::logger()->level() <= spdlog::level::info) {
int time_elapsed = static_cast<int>(timer.get_elapsed_time() / 1000); // Calculate statistics
auto [hours_elapsed, minutes_elapsed, seconds_elapsed] = int64_t processed_frames = video_processor.get_processed_frames();
calculate_time_components(time_elapsed); int time_elapsed = static_cast<int>(timer.get_elapsed_time() / 1000);
float average_speed_fps = static_cast<float>(processed_frames) / auto [hours_elapsed, minutes_elapsed, seconds_elapsed] =
(time_elapsed > 0 ? static_cast<float>(time_elapsed) : 1); calculate_time_components(time_elapsed);
float average_speed_fps = static_cast<float>(processed_frames) /
(time_elapsed > 0 ? static_cast<float>(time_elapsed) : 1);
// Print processing summary // Print processing summary
std::cout << "====== Video2X " << (arguments.benchmark ? "Benchmark" : "Processing") std::cout << "====== Video2X " << (arguments.benchmark ? "Benchmark" : "Processing")
<< " summary ======" << std::endl; << " summary ======" << std::endl;
std::cout << "Video file processed: " << arguments.in_fname.u8string() << 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 frames processed: " << processed_frames << std::endl;
std::cout << "Total time taken: " << std::setw(2) << std::setfill('0') << hours_elapsed << ":" 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::setw(2) << std::setfill('0') << minutes_elapsed << ":"
<< std::setfill('0') << seconds_elapsed << std::endl; << std::setw(2) << std::setfill('0') << seconds_elapsed << std::endl;
std::cout << "Average processing speed: " << std::fixed << std::setprecision(2) std::cout << "Average processing speed: " << std::fixed << std::setprecision(2)
<< average_speed_fps << " FPS" << std::endl; << average_speed_fps << " FPS" << std::endl;
// Print additional information if not in benchmark mode // Print additional information if not in benchmark mode
if (!arguments.benchmark) { if (!arguments.benchmark) {
std::cout << "Output written to: " << arguments.out_fname.u8string() << std::endl; std::cout << "Output written to: " << arguments.out_fname.u8string() << std::endl;
}
} }
return 0; return 0;