Merge pull request #5440 from alexzatsepin/MAPSME-3795-bind-core-debug-logs-to-setting

[android] Bound core debug logs to 'Enable logging' setting
This commit is contained in:
Arsentiy Milchakov 2017-02-17 14:23:38 +03:00 committed by GitHub
commit d94f46d17b
9 changed files with 76 additions and 26 deletions

View file

@ -110,6 +110,7 @@ LOCAL_SRC_FILES := \
com/mapswithme/util/HttpClient.cpp \
com/mapswithme/util/StringUtils.cpp \
com/mapswithme/util/statistics/PushwooshHelper.cpp \
com/mapswithme/util/LoggerFactory.cpp \
com/mapswithme/util/NetworkPolicy.cpp \

View file

@ -24,6 +24,7 @@ jclass g_platformSocketClazz;
jclass g_utilsClazz;
jclass g_bannerClazz;
jclass g_arrayListClazz;
jclass g_loggerFactoryClazz;
extern "C"
{
@ -44,6 +45,7 @@ JNI_OnLoad(JavaVM * jvm, void *)
g_platformSocketClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/location/PlatformSocket");
g_utilsClazz = jni::GetGlobalClassRef(env, "com/mapswithme/util/Utils");
g_bannerClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/bookmarks/data/Banner");
g_loggerFactoryClazz = jni::GetGlobalClassRef(env, "com/mapswithme/util/log/LoggerFactory");
return JNI_VERSION_1_6;
}
@ -62,6 +64,7 @@ JNI_OnUnload(JavaVM *, void *)
env->DeleteGlobalRef(g_platformSocketClazz);
env->DeleteGlobalRef(g_utilsClazz);
env->DeleteGlobalRef(g_bannerClazz);
env->DeleteGlobalRef(g_loggerFactoryClazz);
}
} // extern "C"

View file

@ -18,6 +18,7 @@ extern jclass g_httpHeaderClazz;
extern jclass g_platformSocketClazz;
extern jclass g_utilsClazz;
extern jclass g_bannerClazz;
extern jclass g_loggerFactoryClazz;
namespace jni
{

View file

@ -1,14 +1,11 @@
#include "logging.hpp"
#include "platform/platform.hpp"
#include "base/exception.hpp"
#include "base/logging.hpp"
#include "coding/file_writer.hpp"
#include "platform/file_logging.hpp"
#include "platform/platform.hpp"
#include "../util/crashlytics.h"
#include "com/mapswithme/core/jni_helper.hpp"
#include "com/mapswithme/core/logging.hpp"
#include "com/mapswithme/util/crashlytics.h"
#include <android/log.h>
#include <cassert>
@ -28,17 +25,22 @@ void AndroidMessage(LogLevel level, SrcPoint const & src, string const & s)
switch (level)
{
case LINFO: pr = ANDROID_LOG_INFO; break;
case LDEBUG: pr = ANDROID_LOG_DEBUG; break;
case LWARNING: pr = ANDROID_LOG_WARN; break;
case LERROR: pr = ANDROID_LOG_ERROR; break;
case LCRITICAL: pr = ANDROID_LOG_FATAL; break;
case LINFO: pr = ANDROID_LOG_INFO; break;
case LDEBUG: pr = ANDROID_LOG_DEBUG; break;
case LWARNING: pr = ANDROID_LOG_WARN; break;
case LERROR: pr = ANDROID_LOG_ERROR; break;
case LCRITICAL: pr = ANDROID_LOG_ERROR; break;
}
JNIEnv * env = jni::GetEnv();
static jmethodID const logCoreMsgMethod = jni::GetStaticMethodID(env, g_loggerFactoryClazz,
"logCoreMessage", "(ILjava/lang/String;)V");
string const out = DebugPrint(src) + " " + s;
jni::TScopedLocalRef msg(env, jni::ToJavaString(env, out));
env->CallStaticVoidMethod(g_loggerFactoryClazz, logCoreMsgMethod, pr, msg.get());
if (g_crashlytics)
g_crashlytics->log(g_crashlytics, out.c_str());
__android_log_write(pr, "MapsWithMe_JNI", out.c_str());
}
void AndroidLogMessage(LogLevel level, SrcPoint const & src, string const & s)
@ -49,12 +51,7 @@ void AndroidLogMessage(LogLevel level, SrcPoint const & src, string const & s)
void AndroidAssertMessage(SrcPoint const & src, string const & s)
{
#ifdef MWM_LOG_TO_FILE
LogMessageFile(LCRITICAL, src, s);
#else
AndroidMessage(LCRITICAL, src, s);
#endif
#ifdef DEBUG
assert(false);
#else
@ -64,11 +61,7 @@ void AndroidAssertMessage(SrcPoint const & src, string const & s)
void InitSystemLog()
{
#ifdef MWM_LOG_TO_FILE
SetLogMessageFn(&LogMessageFile);
#else
SetLogMessageFn(&AndroidLogMessage);
#endif
}
void InitAssertLog()
@ -76,6 +69,13 @@ void InitAssertLog()
SetAssertFunction(&AndroidAssertMessage);
}
void ToggleDebugLogs(bool enabled)
{
if (enabled)
g_LogLevel = LDEBUG;
else
g_LogLevel = LINFO;
}
}
extern "C" {

View file

@ -4,4 +4,5 @@ namespace jni
{
void InitSystemLog();
void InitAssertLog();
void ToggleDebugLogs(bool enabled);
}

View file

@ -0,0 +1,11 @@
#include <jni.h>
#include "com/mapswithme/core/logging.hpp"
extern "C" {
JNIEXPORT void JNICALL
Java_com_mapswithme_util_log_LoggerFactory_nativeToggleCoreDebugLogs(
JNIEnv * /*env*/, jclass /*clazz*/, jboolean enabled)
{
jni::ToggleDebugLogs(enabled);
}
} // extern "C"

View file

@ -12,7 +12,7 @@ import com.mapswithme.maps.BuildConfig;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.util.StorageUtils;
import com.mapswithme.util.Utils;
import net.jcip.annotations.NotThreadSafe;
import net.jcip.annotations.Immutable;
import java.io.File;
import java.io.FileWriter;
@ -23,7 +23,7 @@ import java.util.Date;
import java.util.Locale;
import java.util.concurrent.Executor;
@NotThreadSafe
@Immutable
class FileLoggerStrategy implements LoggerStrategy
{
private static final String TAG = FileLoggerStrategy.class.getSimpleName();

View file

@ -3,7 +3,9 @@ package com.mapswithme.util.log;
import android.util.Log;
import com.mapswithme.maps.BuildConfig;
import net.jcip.annotations.Immutable;
@Immutable
class LogCatStrategy implements LoggerStrategy
{
@Override
@ -83,7 +85,7 @@ class LogCatStrategy implements LoggerStrategy
Log.e(tag, msg, tr);
}
private boolean canLog()
private static boolean canLog()
{
return BuildConfig.DEBUG;
}

View file

@ -4,6 +4,7 @@ import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import com.mapswithme.maps.BuildConfig;
import com.mapswithme.maps.MwmApplication;
@ -22,7 +23,8 @@ public class LoggerFactory
{
public enum Type
{
MISC, LOCATION, TRAFFIC, GPS_TRACKING, TRACK_RECORDER, ROUTING, NETWORK, STORAGE, DOWNLOADER
MISC, LOCATION, TRAFFIC, GPS_TRACKING, TRACK_RECORDER, ROUTING, NETWORK, STORAGE, DOWNLOADER,
CORE
}
public interface OnZipCompletedListener
@ -41,6 +43,7 @@ public class LoggerFactory
@NonNull
@GuardedBy("this")
private final EnumMap<Type, BaseLogger> mLoggers = new EnumMap<>(Type.class);
private final static String CORE_TAG = "Core";
@Nullable
@GuardedBy("this")
private ExecutorService mFileLoggerExecutor;
@ -58,6 +61,7 @@ public class LoggerFactory
public void setFileLoggingEnabled(boolean enabled)
{
nativeToggleCoreDebugLogs(enabled);
SharedPreferences prefs = MwmApplication.prefs();
SharedPreferences.Editor editor = prefs.edit();
String enableLoggingKey = MwmApplication.get().getString(R.string.pref_enable_logging);
@ -113,6 +117,7 @@ public class LoggerFactory
{
if (isFileLoggingEnabled())
{
nativeToggleCoreDebugLogs(true);
String logsFolder = StorageUtils.getLogsFolder();
if (!TextUtils.isEmpty(logsFolder))
return new FileLoggerStrategy(logsFolder + File.separator
@ -129,4 +134,30 @@ public class LoggerFactory
mFileLoggerExecutor = Executors.newSingleThreadExecutor();
return mFileLoggerExecutor;
}
// Called from JNI.
@SuppressWarnings("unused")
private static void logCoreMessage(int level, String msg)
{
Logger logger = INSTANCE.getLogger(Type.CORE);
switch (level)
{
case Log.DEBUG:
logger.d(CORE_TAG, msg);
break;
case Log.INFO:
logger.i(CORE_TAG, msg);
break;
case Log.WARN:
logger.w(CORE_TAG, msg);
break;
case Log.ERROR:
logger.e(CORE_TAG, msg);
break;
default:
logger.v(CORE_TAG, msg);
}
}
private static native void nativeToggleCoreDebugLogs(boolean enabled);
}