-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviewed By: mhorowitz Differential Revision: D2967180 fb-gh-sync-id: fe59b7f7c7dab8b5e7f3a449e72b467e1c2c2c67 shipit-source-id: fe59b7f7c7dab8b5e7f3a449e72b467e1c2c2c67
- Loading branch information
Showing
8 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ReactAndroid/src/main/jni/first-party/fbgloginit/Android.mk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
LOCAL_PATH:= $(call my-dir) | ||
include $(CLEAR_VARS) | ||
|
||
LOCAL_SRC_FILES:= \ | ||
glog_init.cpp | ||
|
||
LOCAL_C_INCLUDES := $(LOCAL_PATH) | ||
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) | ||
|
||
LOCAL_CFLAGS := -fexceptions -fno-omit-frame-pointer | ||
LOCAL_CFLAGS += -Wall -Werror | ||
|
||
CXX11_FLAGS := -std=gnu++11 | ||
LOCAL_CFLAGS += $(CXX11_FLAGS) | ||
|
||
LOCAL_EXPORT_CPPFLAGS := $(CXX11_FLAGS) | ||
|
||
LOCAL_LDLIBS := -llog | ||
|
||
LOCAL_SHARED_LIBRARIES := libglog | ||
|
||
LOCAL_MODULE := libglog_init | ||
|
||
include $(BUILD_SHARED_LIBRARY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
cxx_library( | ||
name = 'fbgloginit', | ||
force_static = True, | ||
srcs = [ | ||
'glog_init.cpp', | ||
], | ||
exported_headers = ['fb/glog_init.h'], | ||
compiler_flags = [ | ||
'-fexceptions', | ||
'-fno-omit-frame-pointer', | ||
], | ||
linker_flags = [ | ||
'-llog', | ||
], | ||
deps=[ | ||
'//xplat/third-party/glog:glog', | ||
], | ||
visibility=['PUBLIC'], | ||
) | ||
|
11 changes: 11 additions & 0 deletions
11
ReactAndroid/src/main/jni/first-party/fbgloginit/fb/glog_init.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace facebook { namespace gloginit { | ||
|
||
void initialize(const char* tag = "ReactNativeJNI"); | ||
|
||
}} |
142 changes: 142 additions & 0 deletions
142
ReactAndroid/src/main/jni/first-party/fbgloginit/glog_init.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#include "fb/glog_init.h" | ||
|
||
#include <iostream> | ||
#include <mutex> | ||
#include <stdexcept> | ||
|
||
#include <glog/logging.h> | ||
|
||
#ifdef __ANDROID__ | ||
|
||
#include <android/log.h> | ||
|
||
static int toAndroidLevel(google::LogSeverity severity) { | ||
switch (severity) { | ||
case google::GLOG_INFO: | ||
return ANDROID_LOG_INFO; | ||
case google::GLOG_WARNING: | ||
return ANDROID_LOG_WARN; | ||
case google::GLOG_ERROR: | ||
return ANDROID_LOG_ERROR; | ||
case google::GLOG_FATAL: | ||
return ANDROID_LOG_FATAL; | ||
default: | ||
return ANDROID_LOG_FATAL; | ||
} | ||
} | ||
|
||
/** | ||
* Sends GLog output to adb logcat. | ||
*/ | ||
class LogcatSink : public google::LogSink { | ||
public: | ||
void send( | ||
google::LogSeverity severity, | ||
const char* full_filename, | ||
const char* base_filename, | ||
int line, | ||
const struct ::tm* tm_time, | ||
const char* message, | ||
size_t message_len) override { | ||
auto level = toAndroidLevel(severity); | ||
__android_log_print( | ||
level, | ||
base_filename, | ||
"%.*s", | ||
(int)message_len, | ||
message); | ||
} | ||
}; | ||
|
||
/** | ||
* Sends GLog output to adb logcat. | ||
*/ | ||
class TaggedLogcatSink : public google::LogSink { | ||
const std::string tag_; | ||
|
||
public: | ||
TaggedLogcatSink(const std::string &tag) : tag_{tag} {} | ||
|
||
void send( | ||
google::LogSeverity severity, | ||
const char* full_filename, | ||
const char* base_filename, | ||
int line, | ||
const struct ::tm* tm_time, | ||
const char* message, | ||
size_t message_len) override { | ||
auto level = toAndroidLevel(severity); | ||
__android_log_print( | ||
level, | ||
tag_.c_str(), | ||
"%.*s", | ||
(int)message_len, | ||
message); | ||
} | ||
}; | ||
|
||
static google::LogSink* make_sink(const std::string& tag) { | ||
if (tag.empty()) { | ||
return new LogcatSink{}; | ||
} else { | ||
return new TaggedLogcatSink{tag}; | ||
} | ||
} | ||
|
||
static void sendGlogOutputToLogcat(const char* tag) { | ||
google::AddLogSink(make_sink(tag)); | ||
|
||
// Disable logging to files | ||
for (auto i = 0; i < google::NUM_SEVERITIES; ++i) { | ||
google::SetLogDestination(i, ""); | ||
} | ||
} | ||
|
||
#endif // __ANDROID__ | ||
|
||
static void lastResort(const char* tag, const char* msg, const char* arg = nullptr) { | ||
#ifdef __ANDROID__ | ||
if (!arg) { | ||
__android_log_write(ANDROID_LOG_ERROR, tag, msg); | ||
} else { | ||
__android_log_print(ANDROID_LOG_ERROR, tag, "%s: %s", msg, arg); | ||
} | ||
#else | ||
std::cerr << msg; | ||
if (arg) { | ||
std::cerr << ": " << arg; | ||
} | ||
std::cerr << std::endl; | ||
#endif | ||
} | ||
|
||
namespace facebook { namespace gloginit { | ||
|
||
void initialize(const char* tag) { | ||
static std::once_flag flag{}; | ||
static auto failed = false; | ||
|
||
std::call_once(flag, [tag] { | ||
try { | ||
google::InitGoogleLogging(tag); | ||
|
||
#ifdef __ANDROID__ | ||
sendGlogOutputToLogcat(tag); | ||
#endif | ||
} catch (std::exception& ex) { | ||
lastResort(tag, "Failed to initialize glog", ex.what()); | ||
failed = true; | ||
} catch (...) { | ||
lastResort(tag, "Failed to initialize glog"); | ||
failed = true; | ||
} | ||
}); | ||
|
||
if (failed) { | ||
throw std::runtime_error{"Failed to initialize glog"}; | ||
} | ||
} | ||
|
||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters