Skip to content

Commit 5ac9c7c

Browse files
author
Clement Champetier
committed
Add log files
Include log.hpp in common.hpp
1 parent e06636c commit 5ac9c7c

File tree

4 files changed

+130
-108
lines changed

4 files changed

+130
-108
lines changed

src/AvTranscoder/common.cpp

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
extern "C" {
44
#include <libavformat/avformat.h>
5+
#include <libavutil/error.h>
56
}
67

78
namespace avtranscoder
@@ -19,65 +20,4 @@ std::string getDescriptionFromErrorCode( const int code )
1920
return std::string( err );
2021
}
2122

22-
void Logger::setLogLevel( const int level )
23-
{
24-
av_log_set_level( level );
25-
}
26-
27-
void Logger::log( const int level, const std::string& msg )
28-
{
29-
std::string avTranscoderMsg( "[avTranscoder - " );
30-
31-
std::string levelStr;
32-
switch( level )
33-
{
34-
case AV_LOG_DEBUG:
35-
levelStr = "debug";
36-
break;
37-
case AV_LOG_INFO:
38-
levelStr = "info";
39-
break;
40-
case AV_LOG_WARNING:
41-
levelStr = "warning";
42-
break;
43-
case AV_LOG_ERROR:
44-
levelStr = "error";
45-
break;
46-
default:
47-
break;
48-
}
49-
50-
avTranscoderMsg += levelStr;
51-
avTranscoderMsg += "] ";
52-
avTranscoderMsg += msg;
53-
avTranscoderMsg += "\n";
54-
av_log( NULL, level, avTranscoderMsg.c_str() );
55-
}
56-
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-
8323
}

src/AvTranscoder/common.hpp

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ extern "C" {
1010
#define UINT64_C(c) (c ## ULL)
1111
#endif
1212
#include <libavcodec/version.h>
13-
#include <libavutil/error.h>
1413
#include <libavutil/rational.h>
15-
#include <libavutil/log.h>
1614
}
1715

1816
#include <string>
19-
#include <cstring>
20-
#include <sstream>
21-
#include <fstream>
2217

2318
#ifdef SWIG
2419
#define AvExport
@@ -50,6 +45,8 @@ extern "C" {
5045
#endif
5146
#endif
5247

48+
#include <AvTranscoder/log.hpp>
49+
5350
namespace avtranscoder
5451
{
5552

@@ -63,48 +60,6 @@ void AvExport preloadCodecsAndFormats();
6360
/// Get the string description corresponding to the error code provided by ffmpeg/libav
6461
std::string AvExport getDescriptionFromErrorCode( const int code );
6562

66-
#define LOG_DEBUG( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_DEBUG, os.str() ); }
67-
#define LOG_INFO( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_INFO, os.str() ); }
68-
#define LOG_WARN( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_WARNING, os.str() ); }
69-
#define LOG_ERROR( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_ERROR, os.str() ); }
70-
71-
/// Logger class which contains static functions to use ffmpeg/libav log system
72-
class AvExport Logger
73-
{
74-
public:
75-
/**
76-
* @brief Set the log level of ffmpeg/libav.
77-
* @param level: refer to define AV_LOG_xxx (from AV_LOG_QUIET to AV_LOG_DEBUG)
78-
* @see SWIG interface avLogLevel.i
79-
*/
80-
static void setLogLevel( const int level );
81-
82-
/**
83-
* @brief Log with the ffmpeg/libav log system
84-
* @note use define LOG_* to log at DEBUG/INFO/WARN/ERROR level
85-
* @param msg: the message will be prefixed by '[avTranscoder - <level>]'
86-
* @param msg: the message will be suffixed by '\n'
87-
*/
88-
static void log( const 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
106-
};
107-
10863
}
10964

11065
#endif

src/AvTranscoder/log.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "log.hpp"
2+
3+
namespace avtranscoder
4+
{
5+
6+
void callbackToWriteInFile( void *ptr, int level, const char *fmt, va_list vl )
7+
{
8+
// Format a line of log the same way as the default callback
9+
char line[1024];
10+
static int print_prefix = 1;
11+
av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
12+
13+
// Print line in log file
14+
std::ofstream outputFile;
15+
outputFile.open( Logger::getLogFileName().c_str(), std::ios::out | std::ios::app );
16+
outputFile << line;
17+
outputFile.close();
18+
}
19+
20+
std::string Logger::_logFileName( "avtranscoder.log" );
21+
22+
void Logger::setLogLevel( const int level )
23+
{
24+
av_log_set_level( level );
25+
}
26+
27+
void Logger::log( const int level, const std::string& msg )
28+
{
29+
std::string avTranscoderMsg( "[avTranscoder - " );
30+
31+
std::string levelStr;
32+
switch( level )
33+
{
34+
case AV_LOG_DEBUG:
35+
levelStr = "debug";
36+
break;
37+
case AV_LOG_INFO:
38+
levelStr = "info";
39+
break;
40+
case AV_LOG_WARNING:
41+
levelStr = "warning";
42+
break;
43+
case AV_LOG_ERROR:
44+
levelStr = "error";
45+
break;
46+
default:
47+
break;
48+
}
49+
50+
avTranscoderMsg += levelStr;
51+
avTranscoderMsg += "] ";
52+
avTranscoderMsg += msg;
53+
avTranscoderMsg += "\n";
54+
av_log( NULL, level, avTranscoderMsg.c_str() );
55+
}
56+
57+
void Logger::logInFile()
58+
{
59+
av_log_set_callback( callbackToWriteInFile );
60+
61+
// clean log file
62+
std::ofstream outputFile;
63+
outputFile.open( Logger::getLogFileName().c_str() );
64+
outputFile.close();
65+
}
66+
67+
}

src/AvTranscoder/log.hpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef _AV_TRANSCODER_LOG_HPP
2+
#define _AV_TRANSCODER_LOG_HPP
3+
4+
#include <AvTranscoder/common.hpp>
5+
6+
extern "C" {
7+
#include <libavutil/log.h>
8+
}
9+
10+
#include <sstream>
11+
#include <fstream>
12+
13+
namespace avtranscoder
14+
{
15+
16+
#define LOG_DEBUG( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_DEBUG, os.str() ); }
17+
#define LOG_INFO( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_INFO, os.str() ); }
18+
#define LOG_WARN( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_WARNING, os.str() ); }
19+
#define LOG_ERROR( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_ERROR, os.str() ); }
20+
21+
/// Logger class which contains static functions to use ffmpeg/libav log system
22+
class AvExport Logger
23+
{
24+
public:
25+
/**
26+
* @brief Set the log level of ffmpeg/libav.
27+
* @param level: refer to define AV_LOG_xxx (from AV_LOG_QUIET to AV_LOG_DEBUG)
28+
* @see SWIG interface avLogLevel.i
29+
*/
30+
static void setLogLevel( const int level );
31+
32+
/**
33+
* @brief Log with the ffmpeg/libav log system
34+
* @note use define LOG_* to log at DEBUG/INFO/WARN/ERROR level
35+
* @param msg: the message will be prefixed by '[avTranscoder - <level>]'
36+
* @param msg: the message will be suffixed by '\n'
37+
*/
38+
static void log( const int level, const std::string& msg );
39+
40+
/**
41+
* @brief Log ffmpeg/libav and avtranscoder informations in a text file.
42+
* @note Default log filename is avtranscoder.log
43+
* @see getLogFileName
44+
* @see setLogFileName
45+
*/
46+
static void logInFile();
47+
48+
///@{
49+
/// @warning Need to set the expected log filename before calling logInFile
50+
static std::string& getLogFileName() { return _logFileName; }
51+
static void setLogFileName( const std::string& newLogFileName ) { _logFileName = newLogFileName; }
52+
///@}
53+
54+
private:
55+
static std::string _logFileName; ///< Name of the log file
56+
};
57+
58+
}
59+
60+
#endif

0 commit comments

Comments
 (0)