Skip to content

Commit

Permalink
specify log level on initialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Aug 29, 2019
1 parent 4042e63 commit 4567381
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ extern "C" JNIEXPORT JNICALL jint JNI_OnLoad(JavaVM *vm, void *reserved) {

namespace mmkv {

MMKV_JNI void jniInitialize(JNIEnv *env, jobject obj, jstring rootDir) {
MMKV_JNI void jniInitialize(JNIEnv *env, jobject obj, jstring rootDir, jint logLevel) {
if (!rootDir) {
return;
}
const char *kstr = env->GetStringUTFChars(rootDir, nullptr);
if (kstr) {
g_currentLogLevel = (MMKVLogLevel) logLevel;

MMKV::initializeMMKV(kstr);
env->ReleaseStringUTFChars(rootDir, kstr);
}
Expand Down Expand Up @@ -778,7 +780,7 @@ static JNINativeMethod g_methods[] = {
{"isFileValid", "(Ljava/lang/String;)Z", (void *) mmkv::isFileValid},
{"ashmemFD", "()I", (void *) mmkv::ashmemFD},
{"ashmemMetaFD", "()I", (void *) mmkv::ashmemMetaFD},
{"jniInitialize", "(Ljava/lang/String;)V", (void *) mmkv::jniInitialize},
{"jniInitialize", "(Ljava/lang/String;I)V", (void *) mmkv::jniInitialize},
{"getMMKVWithID", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)J",
(void *) mmkv::getMMKVWithID},
{"getMMKVWithIDAndSize", "(Ljava/lang/String;IILjava/lang/String;)J",
Expand Down
31 changes: 26 additions & 5 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,30 @@ public interface LibLoader { void loadLibrary(String libName); }
// call on program start
public static String initialize(Context context) {
String root = context.getFilesDir().getAbsolutePath() + "/mmkv";
return initialize(root, null);
MMKVLogLevel logLevel =
BuildConfig.DEBUG ? MMKVLogLevel.LevelDebug : MMKVLogLevel.LevelInfo;
return initialize(root, null, logLevel);
}
public static String initialize(Context context, MMKVLogLevel logLevel) {
String root = context.getFilesDir().getAbsolutePath() + "/mmkv";
return initialize(root, null, logLevel);
}

public static String initialize(String rootDir) {
return initialize(rootDir, null);
MMKVLogLevel logLevel =
BuildConfig.DEBUG ? MMKVLogLevel.LevelDebug : MMKVLogLevel.LevelInfo;
return initialize(rootDir, null, logLevel);
}
public static String initialize(String rootDir, MMKVLogLevel logLevel) {
return initialize(rootDir, null, logLevel);
}

public static String initialize(String rootDir, LibLoader loader) {
MMKVLogLevel logLevel =
BuildConfig.DEBUG ? MMKVLogLevel.LevelDebug : MMKVLogLevel.LevelInfo;
return initialize(rootDir, loader, logLevel);
}
public static String initialize(String rootDir, LibLoader loader, MMKVLogLevel logLevel) {
if (loader != null) {
if (BuildConfig.FLAVOR.equals("SharedCpp")) {
loader.loadLibrary("c++_shared");
Expand All @@ -84,7 +100,7 @@ public static String initialize(String rootDir, LibLoader loader) {
System.loadLibrary("mmkv");
}
MMKV.rootDir = rootDir;
jniInitialize(MMKV.rootDir);
jniInitialize(MMKV.rootDir, LogLevel2Int(logLevel));
return rootDir;
}

Expand All @@ -93,7 +109,7 @@ public static String getRootDir() {
return rootDir;
}

public static void setLogLevel(MMKVLogLevel level) {
private static int LogLevel2Int(MMKVLogLevel level) {
int realLevel;
switch (level) {
case LevelDebug:
Expand All @@ -115,6 +131,11 @@ public static void setLogLevel(MMKVLogLevel level) {
realLevel = 1;
break;
}
return realLevel;
}

public static void setLogLevel(MMKVLogLevel level) {
int realLevel = LogLevel2Int(level);
setLogLevel(realLevel);
}

Expand Down Expand Up @@ -812,7 +833,7 @@ private MMKV(long handle) {
nativeHandle = handle;
}

private static native void jniInitialize(String rootDir);
private static native void jniInitialize(String rootDir, int level);

private native static long
getMMKVWithID(String mmapID, int mode, String cryptKey, String relativePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void loadLibrary(String libName) {
ReLinker.loadLibrary(MainActivity.this, libName);
}
});
}, MMKVLogLevel.LevelInfo);
Log.i("MMKV", "mmkv root: " + rootDir);

// set log level
Expand Down

0 comments on commit 4567381

Please sign in to comment.