Skip to content

Commit

Permalink
logging: add configurable log-level to startup params (#173)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Schore <[email protected]>

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 <[email protected]>
  • Loading branch information
goaway authored and jpsim committed Nov 28, 2022
1 parent 6059228 commit 4a9b00e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mobile/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ IndentWidth: 2
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
PointerAlignment: Right
SortIncludes: false
...

Expand Down
2 changes: 1 addition & 1 deletion mobile/examples/objective-c/hello_world/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow* window;
@property (strong, nonatomic) UIWindow *window;

@end
5 changes: 3 additions & 2 deletions mobile/library/common/jni_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions mobile/library/common/main_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Envoy::MainCommon> 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.
Expand All @@ -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<Envoy::MainCommon>(3, envoy_argv);
main_common = std::make_unique<Envoy::MainCommon>(5, envoy_argv);
} catch (const Envoy::NoServingException& e) {
return EXIT_SUCCESS;
} catch (const Envoy::MalformedArgvException& e) {
Expand Down
4 changes: 2 additions & 2 deletions mobile/library/common/main_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 4 additions & 3 deletions mobile/library/kotlin/io/envoyproxy/envoymobile/Envoy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,7 +22,7 @@ class Envoy(
load(context)

runner = Thread(Runnable {
EnvoyEngine.run(config.trim())
EnvoyEngine.run(config.trim(), logLevel)
})

runner.start()
Expand Down
7 changes: 6 additions & 1 deletion mobile/library/objective-c/Envoy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 14 additions & 3 deletions mobile/library/objective-c/Envoy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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};
Expand Down

0 comments on commit 4a9b00e

Please sign in to comment.