Skip to content

Commit edc291a

Browse files
committed
SRT: add srt log handle, srs log supoort multithread
1 parent b7aad03 commit edc291a

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

trunk/src/app/srs_app_log.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <srs_kernel_error.hpp>
1919
#include <srs_app_utility.hpp>
2020
#include <srs_kernel_utility.hpp>
21+
#include <srs_core_lock.hpp>
2122

2223
// the max size of a line of log.
2324
#define LOG_MAX_SIZE 8192
@@ -35,6 +36,8 @@ SrsFileLog::SrsFileLog()
3536
fd = -1;
3637
log_to_file_tank = false;
3738
utc = false;
39+
40+
pthread_mutex_init(&mutex_, NULL);
3841
}
3942

4043
SrsFileLog::~SrsFileLog()
@@ -49,6 +52,8 @@ SrsFileLog::~SrsFileLog()
4952
if (_srs_config) {
5053
_srs_config->unsubscribe(this);
5154
}
55+
56+
pthread_mutex_destroy(&mutex_);
5257
}
5358

5459
srs_error_t SrsFileLog::initialize()
@@ -79,6 +84,8 @@ void SrsFileLog::reopen()
7984

8085
void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
8186
{
87+
SrsScopeLock sl(&mutex_);
88+
8289
if (level > SrsLogLevelVerbose) {
8390
return;
8491
}
@@ -99,6 +106,8 @@ void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* f
99106

100107
void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
101108
{
109+
SrsScopeLock sl(&mutex_);
110+
102111
if (level > SrsLogLevelInfo) {
103112
return;
104113
}
@@ -119,6 +128,8 @@ void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt,
119128

120129
void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
121130
{
131+
SrsScopeLock sl(&mutex_);
132+
122133
if (level > SrsLogLevelTrace) {
123134
return;
124135
}
@@ -139,6 +150,8 @@ void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt
139150

140151
void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
141152
{
153+
SrsScopeLock sl(&mutex_);
154+
142155
if (level > SrsLogLevelWarn) {
143156
return;
144157
}
@@ -159,6 +172,8 @@ void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt,
159172

160173
void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
161174
{
175+
SrsScopeLock sl(&mutex_);
176+
162177
if (level > SrsLogLevelError) {
163178
return;
164179
}

trunk/src/app/srs_app_log.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class SrsFileLog : public ISrsLog, public ISrsReloadHandler
3939
bool log_to_file_tank;
4040
// Whether use utc time.
4141
bool utc;
42+
// TODO: FIXME: use macro define like SRS_MULTI_THREAD_LOG to switch enable log mutex or not.
43+
// Mutex for multithread log.
44+
pthread_mutex_t mutex_;
4245
public:
4346
SrsFileLog();
4447
virtual ~SrsFileLog();

trunk/src/app/srs_app_srt_server.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ srs_error_t SrsSrtServerAdapter::initialize()
267267
{
268268
srs_error_t err = srs_success;
269269

270+
if ((err = srs_srt_log_initialie()) != srs_success) {
271+
return srs_error_wrap(err, "srt log initialize");
272+
}
273+
270274
_srt_eventloop = new SrsSrtEventLoop();
271275

272276
if ((err = _srt_eventloop->initialize()) != srs_success) {

trunk/src/protocol/srs_protocol_srt.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ using namespace std;
1616

1717
#include <srt/srt.h>
1818

19+
// TODO: FIXME: protocol could no include app's header file, so define TAG_SRT in this file.
20+
#define TAG_SRT "SRT"
21+
1922
#define SET_SRT_OPT_STR(srtfd, optname, buf, size) \
2023
if (srt_setsockflag(srtfd, optname, buf, size) == SRT_ERROR) { \
2124
std::stringstream ss; \
@@ -43,6 +46,7 @@ using namespace std;
4346
} \
4447
} while (0)
4548

49+
4650
static srs_error_t do_srs_srt_listen(srs_srt_t srt_fd, addrinfo* r)
4751
{
4852
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)
7276
return srs_success;
7377
}
7478

79+
static void srs_srt_log_handler(void* opaque, int level, const char* file, int line, const char* area, const char* message)
80+
{
81+
switch (level) {
82+
case srt_logging::LogLevel::debug:
83+
srs_info2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
84+
break;
85+
case srt_logging::LogLevel::note:
86+
srs_trace2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
87+
break;
88+
case srt_logging::LogLevel::warning:
89+
srs_warn2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
90+
break;
91+
case srt_logging::LogLevel::error:
92+
case srt_logging::LogLevel::fatal:
93+
srs_error2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
94+
break;
95+
default:
96+
srs_trace2(TAG_SRT, "%s:%d(%s) # %s", file, line, area, message);
97+
break;
98+
}
99+
}
100+
101+
srs_error_t srs_srt_log_initialie()
102+
{
103+
srs_error_t err = srs_success;
104+
105+
srt_setlogflags(0 | SRT_LOGF_DISABLE_TIME | SRT_LOGF_DISABLE_SEVERITY |
106+
SRT_LOGF_DISABLE_THREADNAME | SRT_LOGF_DISABLE_EOL);
107+
srt_setloghandler(NULL, srs_srt_log_handler);
108+
109+
return err;
110+
}
111+
75112
srs_srt_t srs_srt_socket_invalid()
76113
{
77114
return SRT_INVALID_SOCK;

trunk/src/protocol/srs_protocol_srt.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class SrsSrtSocket;
1717

18+
extern srs_error_t srs_srt_log_initialie();
19+
1820
typedef int srs_srt_t;
1921
extern srs_srt_t srs_srt_socket_invalid();
2022

0 commit comments

Comments
 (0)