Skip to content

Commit 041996d

Browse files
Slash333Igor Ponomarenkocloudwebrtc
authored
add logging api (#117)
* add logging api * Update rtc_logging.cc --------- Co-authored-by: Igor Ponomarenko <[email protected]> Co-authored-by: CloudWebRTC <[email protected]>
1 parent 1d15983 commit 041996d

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ rtc_shared_library("libwebrtc") {
9494
"include/rtc_video_renderer.h",
9595
"include/rtc_video_source.h",
9696
"include/rtc_video_track.h",
97+
"include/rtc_logging.h",
9798
"include/helper.h",
9899
"src/helper.cc",
99100
"src/base/portable.cc",
@@ -160,6 +161,7 @@ rtc_shared_library("libwebrtc") {
160161
"src/rtc_video_source_impl.h",
161162
"src/rtc_video_track_impl.cc",
162163
"src/rtc_video_track_impl.h",
164+
"src/rtc_logging.cc",
163165
]
164166

165167
# intel media sdk

include/rtc_logging.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef LIB_WEBRTC_RTC_LOGGING_HXX
2+
#define LIB_WEBRTC_RTC_LOGGING_HXX
3+
4+
#include "rtc_types.h"
5+
6+
namespace libwebrtc {
7+
8+
enum RTCLoggingSeverity {
9+
Verbose,
10+
Info,
11+
Warning,
12+
Error,
13+
None,
14+
};
15+
16+
typedef void (*RTCCallbackLoggerMessageHandler)(const string& message);
17+
18+
class LibWebRTCLogging {
19+
public:
20+
LIB_WEBRTC_API static void setMinDebugLogLevel(RTCLoggingSeverity severity);
21+
LIB_WEBRTC_API static void setLogSink(RTCLoggingSeverity severity, RTCCallbackLoggerMessageHandler callbackHandler);
22+
};
23+
} // namespace libwebrtc
24+
25+
#endif // LIB_WEBRTC_RTC_LOGGING_HXX

src/rtc_logging.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "rtc_logging.h"
2+
#include "rtc_base/logging.h"
3+
#include "rtc_base/log_sinks.h"
4+
5+
#include <memory>
6+
using std::unique_ptr;
7+
8+
namespace libwebrtc {
9+
10+
inline rtc::LoggingSeverity getNativeLoggingSeverity(RTCLoggingSeverity severity) {
11+
switch (severity) {
12+
case Verbose:
13+
return rtc::LS_VERBOSE;
14+
case Info:
15+
return rtc::LS_INFO;
16+
case Warning:
17+
return rtc::LS_WARNING;
18+
case Error:
19+
return rtc::LS_ERROR;
20+
case None:
21+
return rtc::LS_NONE;
22+
}
23+
}
24+
25+
class CallbackLogSink final : public rtc::LogSink {
26+
public:
27+
CallbackLogSink(RTCCallbackLoggerMessageHandler _callbackHandler)
28+
: callback_handler(_callbackHandler) {}
29+
30+
void OnLogMessage(const std::string& message) override {
31+
callback_handler(string(message));
32+
}
33+
34+
private:
35+
RTCCallbackLoggerMessageHandler callback_handler;
36+
};
37+
38+
static std::unique_ptr<CallbackLogSink> log_sink;
39+
40+
void LibWebRTCLogging::setMinDebugLogLevel(RTCLoggingSeverity severity) {
41+
rtc::LogMessage::LogToDebug(getNativeLoggingSeverity(severity));
42+
}
43+
44+
void LibWebRTCLogging::setLogSink(RTCLoggingSeverity severity, RTCCallbackLoggerMessageHandler callbackHandler) {
45+
log_sink.reset(new CallbackLogSink(callbackHandler));
46+
rtc::LogMessage::AddLogToStream(log_sink.get(), getNativeLoggingSeverity(severity));
47+
}
48+
} // namespace libwebrtc

0 commit comments

Comments
 (0)