1b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org/*
2b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *
4b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *  Use of this source code is governed by a BSD-style license
5b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org */
10b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
11b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org#include "webrtc/system_wrappers/interface/logcat_trace_context.h"
12b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
13b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org#include <android/log.h>
14b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org#include <assert.h>
15b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
16b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org#include "webrtc/system_wrappers/interface/logging.h"
17b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
18b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.orgnamespace webrtc {
19b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
20b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.orgstatic android_LogPriority AndroidLogPriorityFromWebRtcLogLevel(
21b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    TraceLevel webrtc_level) {
22b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  // NOTE: this mapping is somewhat arbitrary.  StateInfo and Info are mapped
23b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  // to DEBUG because they are highly verbose in webrtc code (which is
24b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  // unfortunate).
25b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  switch (webrtc_level) {
26b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceStateInfo: return ANDROID_LOG_DEBUG;
27b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceWarning: return ANDROID_LOG_WARN;
28b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceError: return ANDROID_LOG_ERROR;
29b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceCritical: return ANDROID_LOG_FATAL;
30b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceApiCall: return ANDROID_LOG_VERBOSE;
31b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceModuleCall: return ANDROID_LOG_VERBOSE;
32b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceMemory: return ANDROID_LOG_VERBOSE;
33b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceTimer: return ANDROID_LOG_VERBOSE;
34b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceStream: return ANDROID_LOG_VERBOSE;
35b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceDebug: return ANDROID_LOG_DEBUG;
36b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceInfo: return ANDROID_LOG_DEBUG;
37b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    case webrtc::kTraceTerseInfo: return ANDROID_LOG_INFO;
38b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    default:
39b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org      LOG(LS_ERROR) << "Unexpected log level" << webrtc_level;
40b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org      return ANDROID_LOG_FATAL;
41b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  }
42b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org}
43b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
44b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.orgLogcatTraceContext::LogcatTraceContext() {
45b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  webrtc::Trace::CreateTrace();
46b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  if (webrtc::Trace::SetTraceCallback(this) != 0)
47b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    assert(false);
48b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org}
49b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
50b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.orgLogcatTraceContext::~LogcatTraceContext() {
51b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  if (webrtc::Trace::SetTraceCallback(NULL) != 0)
52b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org    assert(false);
53b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  webrtc::Trace::ReturnTrace();
54b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org}
55b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
56b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.orgvoid LogcatTraceContext::Print(TraceLevel level,
57b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org                               const char* message,
58b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org                               int length) {
59b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org  __android_log_print(AndroidLogPriorityFromWebRtcLogLevel(level),
60b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org                      "WEBRTC", "%.*s", length, message);
61b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org}
62b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org
63b82f683898b5d38ea1b864b9b584ab3b9e5b6914fischman@webrtc.org}  // namespace webrtc
64