diff --git a/trunk/src/app/srs_app_log.cpp b/trunk/src/app/srs_app_log.cpp index 89885cd6a..38c10c4ff 100644 --- a/trunk/src/app/srs_app_log.cpp +++ b/trunk/src/app/srs_app_log.cpp @@ -18,6 +18,7 @@ #include #include #include +#include // 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; } diff --git a/trunk/src/app/srs_app_log.hpp b/trunk/src/app/srs_app_log.hpp index a18662834..fc0cbcccd 100644 --- a/trunk/src/app/srs_app_log.hpp +++ b/trunk/src/app/srs_app_log.hpp @@ -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(); diff --git a/trunk/src/app/srs_app_srt_server.cpp b/trunk/src/app/srs_app_srt_server.cpp index f3ef03d61..40c55a698 100644 --- a/trunk/src/app/srs_app_srt_server.cpp +++ b/trunk/src/app/srs_app_srt_server.cpp @@ -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) { diff --git a/trunk/src/protocol/srs_protocol_srt.cpp b/trunk/src/protocol/srs_protocol_srt.cpp index 53b77c2c9..d92adfb3a 100644 --- a/trunk/src/protocol/srs_protocol_srt.cpp +++ b/trunk/src/protocol/srs_protocol_srt.cpp @@ -16,6 +16,9 @@ using namespace std; #include +// 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; diff --git a/trunk/src/protocol/srs_protocol_srt.hpp b/trunk/src/protocol/srs_protocol_srt.hpp index a2f4189fe..219e12731 100644 --- a/trunk/src/protocol/srs_protocol_srt.hpp +++ b/trunk/src/protocol/srs_protocol_srt.hpp @@ -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();