summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/common/browser_logging/WebRtcLog.cpp
blob: 0f3769604eae582a2642ca00e33cb6233506b1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebRtcLog.h"

#include "mozilla/Logging.h"
#include "mozilla/StaticPtr.h"
#include "prenv.h"
#include "rtc_base/logging.h"

#include "nscore.h"
#include "nsString.h"
#include "nsXULAppAPI.h"
#include "mozilla/Preferences.h"

#include "nsIFile.h"
#include "nsDirectoryServiceUtils.h"
#include "nsDirectoryServiceDefs.h"
#include "nsNativeCharsetUtils.h"

using mozilla::LogLevel;

static mozilla::LazyLogModule sWebRtcLog("webrtc_trace");
static mozilla::LazyLogModule sLogAEC("AEC");

class LogSinkImpl : public rtc::LogSink {
 public:
  LogSinkImpl() {}

 private:
  void OnLogMessage(const std::string& message) override {
    MOZ_LOG(sWebRtcLog, LogLevel::Debug, ("%s", message.data()));
  }
};

// For RTC_LOG()
static mozilla::StaticAutoPtr<LogSinkImpl> sSink;

void GetWebRtcLogPrefs() {
  rtc::LogMessage::set_aec_debug_size(
      mozilla::Preferences::GetUint("media.webrtc.debug.aec_dump_max_size"));
}

mozilla::LogLevel CheckOverrides() {
  mozilla::LogModule* log_info = sWebRtcLog;
  mozilla::LogLevel log_level = log_info->Level();

  log_info = sLogAEC;
  if (sLogAEC && (log_info->Level() != mozilla::LogLevel::Disabled)) {
    rtc::LogMessage::set_aec_debug(true);
  }

  return log_level;
}

void ConfigWebRtcLog(mozilla::LogLevel level) {
  rtc::LoggingSeverity log_level;
  switch (level) {
    case mozilla::LogLevel::Verbose:
      log_level = rtc::LoggingSeverity::LS_VERBOSE;
      break;
    case mozilla::LogLevel::Debug:
    case mozilla::LogLevel::Info:
      log_level = rtc::LoggingSeverity::LS_INFO;
      break;
    case mozilla::LogLevel::Warning:
      log_level = rtc::LoggingSeverity::LS_WARNING;
      break;
    case mozilla::LogLevel::Error:
      log_level = rtc::LoggingSeverity::LS_ERROR;
      break;
    case mozilla::LogLevel::Disabled:
      log_level = rtc::LoggingSeverity::LS_NONE;
      break;
    default:
      MOZ_ASSERT(false);
      break;
  }
  rtc::LogMessage::LogToDebug(log_level);
  if (level != mozilla::LogLevel::Disabled) {
    // always capture LOG(...) << ... logging in webrtc.org code to nspr logs
    if (!sSink) {
      sSink = new LogSinkImpl();
      rtc::LogMessage::AddLogToStream(sSink, log_level);
      // it's ok if this leaks to program end
    }
  } else if (sSink) {
    rtc::LogMessage::RemoveLogToStream(sSink);
    sSink = nullptr;
  }
}

void StartWebRtcLog(mozilla::LogLevel log_level) {
  if (log_level == mozilla::LogLevel::Disabled) {
    return;
  }

  GetWebRtcLogPrefs();
  mozilla::LogLevel level = CheckOverrides();

  ConfigWebRtcLog(level);
}

void EnableWebRtcLog() {
  GetWebRtcLogPrefs();
  mozilla::LogLevel level = CheckOverrides();
  ConfigWebRtcLog(level);
}

// Called when we destroy the singletons from PeerConnectionCtx or if the
// user changes logging in about:webrtc
void StopWebRtcLog() {
  if (sSink) {
    rtc::LogMessage::RemoveLogToStream(sSink);
    sSink = nullptr;
  }
}

nsCString ConfigAecLog() {
  nsCString aecLogDir;
  if (rtc::LogMessage::aec_debug()) {
    return ""_ns;
  }
#if defined(ANDROID)
  const char* default_tmp_dir = "/dev/null";
  aecLogDir.Assign(default_tmp_dir);
#else
  nsCOMPtr<nsIFile> tempDir;
  nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tempDir));
  if (NS_SUCCEEDED(rv)) {
#  ifdef XP_WIN
    // WebRTC wants a path encoded in the native charset, not UTF-8.
    nsAutoString temp;
    tempDir->GetPath(temp);
    NS_CopyUnicodeToNative(temp, aecLogDir);
#  else
    tempDir->GetNativePath(aecLogDir);
#  endif
  }
#endif
  rtc::LogMessage::set_aec_debug_filename(aecLogDir.get());

  return aecLogDir;
}

nsCString StartAecLog() {
  nsCString aecLogDir;
  if (rtc::LogMessage::aec_debug()) {
    return ""_ns;
  }

  GetWebRtcLogPrefs();
  CheckOverrides();
  aecLogDir = ConfigAecLog();

  rtc::LogMessage::set_aec_debug(true);

  return aecLogDir;
}

void StopAecLog() { rtc::LogMessage::set_aec_debug(false); }