mirror of
				https://github.com/ossrs/srs.git
				synced 2025-03-09 15:49:59 +00:00 
			
		
		
		
	SRT: add srt log handle, srs log supoort multithread
This commit is contained in:
		
							parent
							
								
									d03c6793b8
								
							
						
					
					
						commit
						910b5945af
					
				
					 5 changed files with 61 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -18,6 +18,7 @@
 | 
			
		|||
#include <srs_kernel_error.hpp>
 | 
			
		||||
#include <srs_app_utility.hpp>
 | 
			
		||||
#include <srs_kernel_utility.hpp>
 | 
			
		||||
#include <srs_core_lock.hpp>
 | 
			
		||||
 | 
			
		||||
// the max size of a line of log.
 | 
			
		||||
#define LOG_MAX_SIZE 8192
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +36,8 @@ SrsFileLog::SrsFileLog()
 | 
			
		|||
    fd = -1;
 | 
			
		||||
    log_to_file_tank = false;
 | 
			
		||||
    utc = false;
 | 
			
		||||
 | 
			
		||||
    pthread_mutex_init(&mutex_, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SrsFileLog::~SrsFileLog()
 | 
			
		||||
| 
						 | 
				
			
			@ -49,6 +52,8 @@ SrsFileLog::~SrsFileLog()
 | 
			
		|||
    if (_srs_config) {
 | 
			
		||||
        _srs_config->unsubscribe(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pthread_mutex_destroy(&mutex_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
srs_error_t SrsFileLog::initialize()
 | 
			
		||||
| 
						 | 
				
			
			@ -79,6 +84,8 @@ void SrsFileLog::reopen()
 | 
			
		|||
 | 
			
		||||
void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    SrsScopeLock sl(&mutex_);
 | 
			
		||||
 | 
			
		||||
    if (level > SrsLogLevelVerbose) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -99,6 +106,8 @@ void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* f
 | 
			
		|||
 | 
			
		||||
void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    SrsScopeLock sl(&mutex_);
 | 
			
		||||
 | 
			
		||||
    if (level > SrsLogLevelInfo) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -119,6 +128,8 @@ void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt,
 | 
			
		|||
 | 
			
		||||
void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    SrsScopeLock sl(&mutex_);
 | 
			
		||||
 | 
			
		||||
    if (level > SrsLogLevelTrace) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -139,6 +150,8 @@ void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt
 | 
			
		|||
 | 
			
		||||
void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    SrsScopeLock sl(&mutex_);
 | 
			
		||||
 | 
			
		||||
    if (level > SrsLogLevelWarn) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -159,6 +172,8 @@ void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt,
 | 
			
		|||
 | 
			
		||||
void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    SrsScopeLock sl(&mutex_);
 | 
			
		||||
 | 
			
		||||
    if (level > SrsLogLevelError) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,6 +39,9 @@ private:
 | 
			
		|||
    bool log_to_file_tank;
 | 
			
		||||
    // Whether use utc time.
 | 
			
		||||
    bool utc;
 | 
			
		||||
    // TODO: FIXME: use macro define like SRS_MULTI_THREAD_LOG to switch enable log mutex or not.
 | 
			
		||||
    // Mutex for multithread log.
 | 
			
		||||
    pthread_mutex_t mutex_;
 | 
			
		||||
public:
 | 
			
		||||
    SrsFileLog();
 | 
			
		||||
    virtual ~SrsFileLog();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -267,6 +267,10 @@ srs_error_t SrsSrtServerAdapter::initialize()
 | 
			
		|||
{
 | 
			
		||||
    srs_error_t err = srs_success;
 | 
			
		||||
 | 
			
		||||
    if ((err = srs_srt_log_initialie()) != srs_success) {
 | 
			
		||||
        return srs_error_wrap(err, "srt log initialize");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    _srt_eventloop = new SrsSrtEventLoop();
 | 
			
		||||
 | 
			
		||||
    if ((err = _srt_eventloop->initialize()) != srs_success) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,9 @@ using namespace std;
 | 
			
		|||
 | 
			
		||||
#include <srt/srt.h>
 | 
			
		||||
 | 
			
		||||
// TODO: FIXME: protocol could no include app's header file, so define TAG_SRT in this file.
 | 
			
		||||
#define TAG_SRT "SRT"
 | 
			
		||||
 | 
			
		||||
#define SET_SRT_OPT_STR(srtfd, optname, buf, size)                                  \
 | 
			
		||||
    if (srt_setsockflag(srtfd, optname, buf, size) == SRT_ERROR) {                  \
 | 
			
		||||
        std::stringstream ss;                                                       \
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +46,7 @@ using namespace std;
 | 
			
		|||
        }                                                                       \
 | 
			
		||||
    } while (0)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static srs_error_t do_srs_srt_listen(srs_srt_t srt_fd, addrinfo* r)
 | 
			
		||||
{
 | 
			
		||||
    srs_error_t err = srs_success;
 | 
			
		||||
| 
						 | 
				
			
			@ -72,6 +76,39 @@ static srs_error_t do_srs_srt_get_streamid(srs_srt_t srt_fd, string& streamid)
 | 
			
		|||
    return srs_success;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void srs_srt_log_handler(void* opaque, int level, const char* file, int line, const char* area, const char* message)
 | 
			
		||||
{
 | 
			
		||||
    switch (level) {
 | 
			
		||||
        case srt_logging::LogLevel::debug:
 | 
			
		||||
            srs_info2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
 | 
			
		||||
            break;
 | 
			
		||||
        case srt_logging::LogLevel::note:
 | 
			
		||||
            srs_trace2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
 | 
			
		||||
            break;
 | 
			
		||||
        case srt_logging::LogLevel::warning:
 | 
			
		||||
            srs_warn2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
 | 
			
		||||
            break;
 | 
			
		||||
        case srt_logging::LogLevel::error:
 | 
			
		||||
        case srt_logging::LogLevel::fatal:
 | 
			
		||||
            srs_error2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            srs_trace2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
srs_error_t srs_srt_log_initialie()
 | 
			
		||||
{
 | 
			
		||||
    srs_error_t err = srs_success;
 | 
			
		||||
 | 
			
		||||
    srt_setlogflags(0 | SRT_LOGF_DISABLE_TIME | SRT_LOGF_DISABLE_SEVERITY |
 | 
			
		||||
        SRT_LOGF_DISABLE_THREADNAME | SRT_LOGF_DISABLE_EOL);
 | 
			
		||||
    srt_setloghandler(NULL, srs_srt_log_handler);
 | 
			
		||||
 | 
			
		||||
    return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
srs_srt_t srs_srt_socket_invalid()
 | 
			
		||||
{
 | 
			
		||||
    return SRT_INVALID_SOCK;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,8 @@
 | 
			
		|||
 | 
			
		||||
class SrsSrtSocket;
 | 
			
		||||
 | 
			
		||||
extern srs_error_t srs_srt_log_initialie();
 | 
			
		||||
 | 
			
		||||
typedef int srs_srt_t;
 | 
			
		||||
extern srs_srt_t srs_srt_socket_invalid();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue