• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "RTCCallbackLogger.h"
12
13#include <memory>
14
15#include "rtc_base/checks.h"
16#include "rtc_base/log_sinks.h"
17#include "rtc_base/logging.h"
18
19class CallbackLogSink : public rtc::LogSink {
20 public:
21  CallbackLogSink(RTCCallbackLoggerMessageHandler callbackHandler)
22      : callback_handler_(callbackHandler) {}
23
24  void OnLogMessage(const std::string &message) override {
25    if (callback_handler_) {
26      callback_handler_([NSString stringWithUTF8String:message.c_str()]);
27    }
28  }
29
30 private:
31  RTCCallbackLoggerMessageHandler callback_handler_;
32};
33
34class CallbackWithSeverityLogSink : public rtc::LogSink {
35 public:
36  CallbackWithSeverityLogSink(RTCCallbackLoggerMessageAndSeverityHandler callbackHandler)
37      : callback_handler_(callbackHandler) {}
38
39  void OnLogMessage(const std::string& message) override { RTC_NOTREACHED(); }
40
41  void OnLogMessage(const std::string& message, rtc::LoggingSeverity severity) override {
42    if (callback_handler_) {
43      RTCLoggingSeverity loggingSeverity = NativeSeverityToObjcSeverity(severity);
44      callback_handler_([NSString stringWithUTF8String:message.c_str()], loggingSeverity);
45    }
46  }
47
48 private:
49  static RTCLoggingSeverity NativeSeverityToObjcSeverity(rtc::LoggingSeverity severity) {
50    switch (severity) {
51      case rtc::LS_VERBOSE:
52        return RTCLoggingSeverityVerbose;
53      case rtc::LS_INFO:
54        return RTCLoggingSeverityInfo;
55      case rtc::LS_WARNING:
56        return RTCLoggingSeverityWarning;
57      case rtc::LS_ERROR:
58        return RTCLoggingSeverityError;
59      case rtc::LS_NONE:
60        return RTCLoggingSeverityNone;
61    }
62  }
63
64  RTCCallbackLoggerMessageAndSeverityHandler callback_handler_;
65};
66
67@implementation RTC_OBJC_TYPE (RTCCallbackLogger) {
68  BOOL _hasStarted;
69  std::unique_ptr<rtc::LogSink> _logSink;
70}
71
72@synthesize severity = _severity;
73
74- (instancetype)init {
75  self = [super init];
76  if (self != nil) {
77    _severity = RTCLoggingSeverityInfo;
78  }
79  return self;
80}
81
82- (void)dealloc {
83  [self stop];
84}
85
86- (void)start:(nullable RTCCallbackLoggerMessageHandler)handler {
87  if (_hasStarted) {
88    return;
89  }
90
91  _logSink.reset(new CallbackLogSink(handler));
92
93  rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]);
94  _hasStarted = YES;
95}
96
97- (void)startWithMessageAndSeverityHandler:
98        (nullable RTCCallbackLoggerMessageAndSeverityHandler)handler {
99  if (_hasStarted) {
100    return;
101  }
102
103  _logSink.reset(new CallbackWithSeverityLogSink(handler));
104
105  rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]);
106  _hasStarted = YES;
107}
108
109- (void)stop {
110  if (!_hasStarted) {
111    return;
112  }
113  RTC_DCHECK(_logSink);
114  rtc::LogMessage::RemoveLogToStream(_logSink.get());
115  _hasStarted = NO;
116  _logSink.reset();
117}
118
119#pragma mark - Private
120
121- (rtc::LoggingSeverity)rtcSeverity {
122  switch (_severity) {
123    case RTCLoggingSeverityVerbose:
124      return rtc::LS_VERBOSE;
125    case RTCLoggingSeverityInfo:
126      return rtc::LS_INFO;
127    case RTCLoggingSeverityWarning:
128      return rtc::LS_WARNING;
129    case RTCLoggingSeverityError:
130      return rtc::LS_ERROR;
131    case RTCLoggingSeverityNone:
132      return rtc::LS_NONE;
133  }
134}
135
136@end
137