Skip to content

Commit 5f1daf6

Browse files
author
Clement Champetier
committed
Logger: add logInFile
1 parent 7278daf commit 5f1daf6

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/AvTranscoder/common.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,30 @@ void Logger::log( int level, const std::string& msg )
5454
av_log( NULL, level, avTranscoderMsg.c_str() );
5555
}
5656

57+
void callbackToWriteInFile( void *ptr, int level, const char *fmt, va_list vl )
58+
{
59+
// Format a line of log the same way as the default callback
60+
char line[1024];
61+
static int print_prefix = 1;
62+
av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
63+
64+
// Print line in log file
65+
std::ofstream outputFile;
66+
outputFile.open( Logger::getLogFileName().c_str(), std::ios::out | std::ios::app );
67+
outputFile << line;
68+
outputFile.close();
69+
}
70+
71+
void Logger::logInFile()
72+
{
73+
av_log_set_callback( callbackToWriteInFile );
74+
75+
// clean log file
76+
std::ofstream outputFile;
77+
outputFile.open( Logger::getLogFileName().c_str() );
78+
outputFile.close();
79+
}
80+
81+
std::string Logger::_logFileName( "avtranscoder.log" );
82+
5783
}

src/AvTranscoder/common.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern "C" {
1818
#include <string>
1919
#include <cstring>
2020
#include <sstream>
21+
#include <fstream>
2122

2223
#ifdef SWIG
2324
#define AvExport
@@ -85,6 +86,23 @@ class AvExport Logger
8586
* @param msg: the message will be suffixed by '\n'
8687
*/
8788
static void log( int level, const std::string& msg );
89+
90+
/**
91+
* @brief Log ffmpeg/libav and avtranscoder informations in a text file.
92+
* @note Default log filename is avtranscoder.log
93+
* @see getLogFileName
94+
* @see setLogFileName
95+
*/
96+
static void logInFile();
97+
98+
///@{
99+
/// @warning Need to set the expected log filename before calling logInFile
100+
static std::string& getLogFileName() { return _logFileName; }
101+
static void setLogFileName( const std::string& newLogFileName ) { _logFileName = newLogFileName; }
102+
///@}
103+
104+
private:
105+
static std::string _logFileName; ///< Name of the log file
88106
};
89107

90108
}

0 commit comments

Comments
 (0)