Skip to content

Commit 11843fd

Browse files
cjhopmanfacebook-github-bot-5
authored and
facebook-github-bot-5
committed
Initialize glog in react native.
Reviewed By: mhorowitz Differential Revision: D2967180 fb-gh-sync-id: fe59b7f7c7dab8b5e7f3a449e72b467e1c2c2c67 shipit-source-id: fe59b7f7c7dab8b5e7f3a449e72b467e1c2c2c67
1 parent a759131 commit 11843fd

File tree

8 files changed

+205
-3
lines changed

8 files changed

+205
-3
lines changed

ReactAndroid/DEFS

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import os
88

9-
109
# Example: react_native_target('java/com/facebook/react/common:common')
1110
def react_native_target(path):
1211
return '//ReactAndroid/src/main/' + path
@@ -34,6 +33,8 @@ JSC_INTERNAL_DEPS = [
3433
'//native/third-party/jsc-internal:jsc_legacy_profiler',
3534
]
3635

36+
FBGLOGINIT_TARGET = '//ReactAndroid/src/main/jni/first-party/fbgloginit:fbgloginit'
37+
3738
INTERNAL_APP = 'PUBLIC'
3839

3940
# React property preprocessor
@@ -96,4 +97,4 @@ def robolectric3_test(name, deps, vm_args=None, *args, **kwargs):
9697
vm_args=vm_args + extra_vm_args,
9798
*args,
9899
**kwargs
99-
)
100+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
LOCAL_PATH:= $(call my-dir)
2+
include $(CLEAR_VARS)
3+
4+
LOCAL_SRC_FILES:= \
5+
glog_init.cpp
6+
7+
LOCAL_C_INCLUDES := $(LOCAL_PATH)
8+
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
9+
10+
LOCAL_CFLAGS := -fexceptions -fno-omit-frame-pointer
11+
LOCAL_CFLAGS += -Wall -Werror
12+
13+
CXX11_FLAGS := -std=gnu++11
14+
LOCAL_CFLAGS += $(CXX11_FLAGS)
15+
16+
LOCAL_EXPORT_CPPFLAGS := $(CXX11_FLAGS)
17+
18+
LOCAL_LDLIBS := -llog
19+
20+
LOCAL_SHARED_LIBRARIES := libglog
21+
22+
LOCAL_MODULE := libglog_init
23+
24+
include $(BUILD_SHARED_LIBRARY)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cxx_library(
2+
name = 'fbgloginit',
3+
force_static = True,
4+
srcs = [
5+
'glog_init.cpp',
6+
],
7+
exported_headers = ['fb/glog_init.h'],
8+
compiler_flags = [
9+
'-fexceptions',
10+
'-fno-omit-frame-pointer',
11+
],
12+
linker_flags = [
13+
'-llog',
14+
],
15+
deps=[
16+
'//xplat/third-party/glog:glog',
17+
],
18+
visibility=['PUBLIC'],
19+
)
20+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
#pragma once
4+
5+
#include <string>
6+
7+
namespace facebook { namespace gloginit {
8+
9+
void initialize(const char* tag = "ReactNativeJNI");
10+
11+
}}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
#include "fb/glog_init.h"
4+
5+
#include <iostream>
6+
#include <mutex>
7+
#include <stdexcept>
8+
9+
#include <glog/logging.h>
10+
11+
#ifdef __ANDROID__
12+
13+
#include <android/log.h>
14+
15+
static int toAndroidLevel(google::LogSeverity severity) {
16+
switch (severity) {
17+
case google::GLOG_INFO:
18+
return ANDROID_LOG_INFO;
19+
case google::GLOG_WARNING:
20+
return ANDROID_LOG_WARN;
21+
case google::GLOG_ERROR:
22+
return ANDROID_LOG_ERROR;
23+
case google::GLOG_FATAL:
24+
return ANDROID_LOG_FATAL;
25+
default:
26+
return ANDROID_LOG_FATAL;
27+
}
28+
}
29+
30+
/**
31+
* Sends GLog output to adb logcat.
32+
*/
33+
class LogcatSink : public google::LogSink {
34+
public:
35+
void send(
36+
google::LogSeverity severity,
37+
const char* full_filename,
38+
const char* base_filename,
39+
int line,
40+
const struct ::tm* tm_time,
41+
const char* message,
42+
size_t message_len) override {
43+
auto level = toAndroidLevel(severity);
44+
__android_log_print(
45+
level,
46+
base_filename,
47+
"%.*s",
48+
(int)message_len,
49+
message);
50+
}
51+
};
52+
53+
/**
54+
* Sends GLog output to adb logcat.
55+
*/
56+
class TaggedLogcatSink : public google::LogSink {
57+
const std::string tag_;
58+
59+
public:
60+
TaggedLogcatSink(const std::string &tag) : tag_{tag} {}
61+
62+
void send(
63+
google::LogSeverity severity,
64+
const char* full_filename,
65+
const char* base_filename,
66+
int line,
67+
const struct ::tm* tm_time,
68+
const char* message,
69+
size_t message_len) override {
70+
auto level = toAndroidLevel(severity);
71+
__android_log_print(
72+
level,
73+
tag_.c_str(),
74+
"%.*s",
75+
(int)message_len,
76+
message);
77+
}
78+
};
79+
80+
static google::LogSink* make_sink(const std::string& tag) {
81+
if (tag.empty()) {
82+
return new LogcatSink{};
83+
} else {
84+
return new TaggedLogcatSink{tag};
85+
}
86+
}
87+
88+
static void sendGlogOutputToLogcat(const char* tag) {
89+
google::AddLogSink(make_sink(tag));
90+
91+
// Disable logging to files
92+
for (auto i = 0; i < google::NUM_SEVERITIES; ++i) {
93+
google::SetLogDestination(i, "");
94+
}
95+
}
96+
97+
#endif // __ANDROID__
98+
99+
static void lastResort(const char* tag, const char* msg, const char* arg = nullptr) {
100+
#ifdef __ANDROID__
101+
if (!arg) {
102+
__android_log_write(ANDROID_LOG_ERROR, tag, msg);
103+
} else {
104+
__android_log_print(ANDROID_LOG_ERROR, tag, "%s: %s", msg, arg);
105+
}
106+
#else
107+
std::cerr << msg;
108+
if (arg) {
109+
std::cerr << ": " << arg;
110+
}
111+
std::cerr << std::endl;
112+
#endif
113+
}
114+
115+
namespace facebook { namespace gloginit {
116+
117+
void initialize(const char* tag) {
118+
static std::once_flag flag{};
119+
static auto failed = false;
120+
121+
std::call_once(flag, [tag] {
122+
try {
123+
google::InitGoogleLogging(tag);
124+
125+
#ifdef __ANDROID__
126+
sendGlogOutputToLogcat(tag);
127+
#endif
128+
} catch (std::exception& ex) {
129+
lastResort(tag, "Failed to initialize glog", ex.what());
130+
failed = true;
131+
} catch (...) {
132+
lastResort(tag, "Failed to initialize glog");
133+
failed = true;
134+
}
135+
});
136+
137+
if (failed) {
138+
throw std::runtime_error{"Failed to initialize glog"};
139+
}
140+
}
141+
142+
}}

ReactAndroid/src/main/jni/react/jni/Android.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ LOCAL_CFLAGS += $(CXX11_FLAGS)
2222
LOCAL_EXPORT_CPPFLAGS := $(CXX11_FLAGS)
2323

2424
LOCAL_LDLIBS += -landroid
25-
LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libjsc
25+
LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libjsc libglog_init
2626
LOCAL_STATIC_LIBRARIES := libreactnative
2727

2828
include $(BUILD_SHARED_LIBRARY)
2929

3030
$(call import-module,react)
3131
$(call import-module,jsc)
3232
$(call import-module,folly)
33+
$(call import-module,fbgloginit)
3334
$(call import-module,jni)
3435
$(call import-module,jsc)

ReactAndroid/src/main/jni/react/jni/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include_defs('//ReactAndroid/DEFS')
44
SUPPORTED_PLATFORMS = '^android-(armv7|x86)$'
55

66
DEPS = [
7+
FBGLOGINIT_TARGET,
78
'//native/jni:jni',
89
'//native/third-party/android-ndk:android',
910
'//xplat/folly:molly',

ReactAndroid/src/main/jni/react/jni/OnLoad.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <android/asset_manager_jni.h>
44
#include <android/input.h>
55
#include <fb/log.h>
6+
#include <fb/glog_init.h>
67
#include <folly/json.h>
78
#include <jni/Countable.h>
89
#include <jni/Environment.h>
@@ -845,6 +846,7 @@ jmethodID getLogMarkerMethod() {
845846

846847
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
847848
return initialize(vm, [] {
849+
facebook::gloginit::initialize();
848850
// Inject some behavior into react/
849851
ReactMarker::logMarker = bridge::logMarker;
850852
WebWorkerUtil::createWebWorkerThread = WebWorkers::createWebWorkerThread;

0 commit comments

Comments
 (0)