From 4a9b00e622578520307ccb1b9e9779fb6651d726 Mon Sep 17 00:00:00 2001 From: Mike Schore Date: Tue, 25 Jun 2019 15:59:42 -0700 Subject: [PATCH] logging: add configurable log-level to startup params (#173) Signed-off-by: Mike Schore Description: Adds configurable log-level to common interface, and support to both iOS and Android interfaces. Defaults to log-level info. Risk Level: Low Testing: Locally compiled and tested. Signed-off-by: JP Simard --- mobile/.clang-format | 2 +- .../objective-c/hello_world/AppDelegate.h | 2 +- mobile/library/common/jni_interface.cc | 5 +++-- mobile/library/common/main_interface.cc | 7 ++++--- mobile/library/common/main_interface.h | 4 ++-- .../envoymobile/engine/EnvoyEngine.java | 2 +- .../kotlin/io/envoyproxy/envoymobile/Envoy.kt | 7 ++++--- mobile/library/objective-c/Envoy.h | 7 ++++++- mobile/library/objective-c/Envoy.mm | 17 ++++++++++++++--- 9 files changed, 36 insertions(+), 17 deletions(-) diff --git a/mobile/.clang-format b/mobile/.clang-format index 4885c0b508d5..83c65db769f2 100644 --- a/mobile/.clang-format +++ b/mobile/.clang-format @@ -26,7 +26,7 @@ IndentWidth: 2 ObjCBlockIndentWidth: 2 ObjCSpaceAfterProperty: true ObjCSpaceBeforeProtocolList: true -PointerAlignment: Left +PointerAlignment: Right SortIncludes: false ... diff --git a/mobile/examples/objective-c/hello_world/AppDelegate.h b/mobile/examples/objective-c/hello_world/AppDelegate.h index d2a509a751c5..e1fe1bf304bb 100644 --- a/mobile/examples/objective-c/hello_world/AppDelegate.h +++ b/mobile/examples/objective-c/hello_world/AppDelegate.h @@ -3,6 +3,6 @@ @interface AppDelegate : UIResponder -@property (strong, nonatomic) UIWindow* window; +@property (strong, nonatomic) UIWindow *window; @end diff --git a/mobile/library/common/jni_interface.cc b/mobile/library/common/jni_interface.cc index 92cc4222a5e7..aa7ce20dcfb9 100644 --- a/mobile/library/common/jni_interface.cc +++ b/mobile/library/common/jni_interface.cc @@ -23,8 +23,9 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) { extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_EnvoyEngine_run(JNIEnv* env, jobject, // this - jstring config) { - return run_envoy(env->GetStringUTFChars(config, nullptr)); + jstring config, jstring log_level) { + return run_envoy(env->GetStringUTFChars(config, nullptr), + env->GetStringUTFChars(log_level, nullptr)); } extern "C" JNIEXPORT jint JNICALL diff --git a/mobile/library/common/main_interface.cc b/mobile/library/common/main_interface.cc index 3f324ee35413..b936cc3a6252 100644 --- a/mobile/library/common/main_interface.cc +++ b/mobile/library/common/main_interface.cc @@ -14,10 +14,11 @@ /** * External entrypoint for library. */ -extern "C" int run_envoy(const char* config) { +extern "C" int run_envoy(const char* config, const char* log_level) { std::unique_ptr main_common; - char* envoy_argv[] = {strdup("envoy"), strdup("--config-yaml"), strdup(config), nullptr}; + char* envoy_argv[] = {strdup("envoy"), strdup("--config-yaml"), strdup(config), + strdup("-l"), strdup(log_level), nullptr}; // Ensure static factory registration occurs on time. // Envoy's static factory registration happens when main is run. @@ -41,7 +42,7 @@ extern "C" int run_envoy(const char* config) { // This is a known problem, and will be addressed by: // https://github.com/lyft/envoy-mobile/issues/34 try { - main_common = std::make_unique(3, envoy_argv); + main_common = std::make_unique(5, envoy_argv); } catch (const Envoy::NoServingException& e) { return EXIT_SUCCESS; } catch (const Envoy::MalformedArgvException& e) { diff --git a/mobile/library/common/main_interface.h b/mobile/library/common/main_interface.h index 42232fb1e889..6255df914855 100644 --- a/mobile/library/common/main_interface.h +++ b/mobile/library/common/main_interface.h @@ -6,7 +6,7 @@ * External entrypoint for library. */ #ifdef __cplusplus -extern "C" int run_envoy(const char* config); +extern "C" int run_envoy(const char* config, const char* log_level); #else -int run_envoy(const char* config); +int run_envoy(const char* config, const char* log_level); #endif diff --git a/mobile/library/java/io/envoyproxy/envoymobile/engine/EnvoyEngine.java b/mobile/library/java/io/envoyproxy/envoymobile/engine/EnvoyEngine.java index d83c8eafdeea..4e2047e937c3 100644 --- a/mobile/library/java/io/envoyproxy/envoymobile/engine/EnvoyEngine.java +++ b/mobile/library/java/io/envoyproxy/envoymobile/engine/EnvoyEngine.java @@ -35,5 +35,5 @@ public static void load(Context context) { private static native boolean isAresInitialized(); - public static native int run(String config); + public static native int run(String config, String logLevel); } diff --git a/mobile/library/kotlin/io/envoyproxy/envoymobile/Envoy.kt b/mobile/library/kotlin/io/envoyproxy/envoymobile/Envoy.kt index 6725e010fbbf..f1c9d7e4c9af 100644 --- a/mobile/library/kotlin/io/envoyproxy/envoymobile/Envoy.kt +++ b/mobile/library/kotlin/io/envoyproxy/envoymobile/Envoy.kt @@ -4,9 +4,10 @@ import android.content.Context import io.envoyproxy.envoymobile.engine.EnvoyEngine // Wrapper class that allows for easy calling of Envoy's JNI interface in native Java. -class Envoy( +class Envoy @JvmOverloads constructor( context: Context, - config: String + config: String, + logLevel: String = "info" ) { // Dedicated thread for running this instance of Envoy. @@ -21,7 +22,7 @@ class Envoy( load(context) runner = Thread(Runnable { - EnvoyEngine.run(config.trim()) + EnvoyEngine.run(config.trim(), logLevel) }) runner.start() diff --git a/mobile/library/objective-c/Envoy.h b/mobile/library/objective-c/Envoy.h index 4b6edceb24b2..5ea32a1dc593 100644 --- a/mobile/library/objective-c/Envoy.h +++ b/mobile/library/objective-c/Envoy.h @@ -10,5 +10,10 @@ /// Create a new Envoy instance. The Envoy runner NSThread is started as part of instance /// initialization with the configuration provided. -- (instancetype)initWithConfig:(NSString*)config; +- (instancetype)initWithConfig:(NSString *)config; + +/// Create a new Envoy instance. The Envoy runner NSThread is started as part of instance +/// initialization with the configuration provided. +- (instancetype)initWithConfig:(NSString *)config logLevel:(NSString *)logLevel; + @end diff --git a/mobile/library/objective-c/Envoy.mm b/mobile/library/objective-c/Envoy.mm index 83843e9dde5f..95df35ab533b 100644 --- a/mobile/library/objective-c/Envoy.mm +++ b/mobile/library/objective-c/Envoy.mm @@ -2,6 +2,8 @@ #import "library/common/main_interface.h" +static NSString *const kConfig = @"config"; +static NSString *const kLogLevel = @"logLevel"; @interface Envoy () @property (nonatomic, strong) NSThread *runner; @@ -12,9 +14,18 @@ @implementation Envoy @synthesize runner; - (instancetype)initWithConfig:(NSString *)config { + self = [self initWithConfig:config logLevel:@"info"]; + return self; +} + +- (instancetype)initWithConfig:(NSString *)config logLevel:(NSString *) logLevel { self = [super init]; if (self) { - self.runner = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:config]; + NSDictionary *args = @{ + kConfig: config, + kLogLevel: logLevel, + }; + self.runner = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:args]; [self.runner start]; } return self; @@ -30,9 +41,9 @@ - (BOOL)isTerminated { #pragma mark private -- (void)run:(NSString *)config { +- (void)run:(NSDictionary *)args { try { - run_envoy(config.UTF8String); + run_envoy([args[kConfig] UTF8String], [args[kLogLevel] UTF8String]); } catch (NSException *e) { NSLog(@"Envoy exception: %@", e); NSDictionary *userInfo = @{ @"exception": e};