Skip to content

Commit

Permalink
improve android AppMetrica
Browse files Browse the repository at this point in the history
improve android error logs
  • Loading branch information
irov committed Sep 13, 2023
1 parent 0fc29b4 commit 14a9b28
Show file tree
Hide file tree
Showing 25 changed files with 156 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,19 @@ protected void onCreate(Bundle savedInstanceState) {
for (MenginePluginActivityListener l : listeners) {
String pluginName = l.getPluginName();

MengineLog.logMessage(TAG, "onCreate plugin: %s"
, pluginName
);

long plugin_init_start_timestamp = MengineAnalytics.buildEvent("mng_activity_init_plugin_start")
.addParameterString("name", pluginName)
.flush();

try {
l.onCreate(this, savedInstanceState);
} catch (MenginePluginInvalidInitializeException e) {
MengineLog.logError(TAG, "invalid plugin %s callback onCreate exception: %s"
, e.getPluginName()
MengineLog.logError(TAG, "[ERROR] onCreate plugin: %s exception: %s"
, l.getPluginName()
, e.getLocalizedMessage()
);

Expand All @@ -283,10 +287,14 @@ protected void onCreate(Bundle savedInstanceState) {

for (MenginePlugin p : plugins) {
try {
MengineLog.logMessage(TAG, "onExtensionInitialize plugin: %s"
, p.getPluginName()
);

p.onExtensionInitialize(this);
} catch (MenginePluginInvalidInitializeException e) {
MengineLog.logError(TAG, "invalid plugin %s callback onExtensionInitialize exception: %s"
, e.getPluginName()
MengineLog.logError(TAG, "[ERROR] onExtensionInitialize plugin %s: exception: %s"
, p.getPluginName()
, e.getLocalizedMessage()
);

Expand Down Expand Up @@ -638,7 +646,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
MengineLog.logInfo(TAG, "dispatchKeyEvent action: %d key code: %d scan code: %d"
MengineLog.logInfo(TAG, "dispatchKeyEvent action: %d key: %d scan: %d"
, event.getAction()
, event.getKeyCode()
, event.getScanCode()
Expand Down Expand Up @@ -689,7 +697,7 @@ public int genRequestCode(String name) {
**********************************************************************************************/

public void pythonInitializePlugins() {
MengineLog.logInfo(TAG, "python initialize");
MengineLog.logInfo(TAG, "pythonInitializePlugins");

m_initializePython = true;

Expand Down Expand Up @@ -727,7 +735,7 @@ public void pythonFinalizePlugins() {

public void pythonCall(String plugin, String method, Object ... args) {
if (m_initializePython == false) {
MengineLog.logWarning(TAG,"invalid python plugin [%s] method [%s] args [%s] call before embedding"
MengineLog.logWarning(TAG,"pythonCall call before embedding plugin: %s method: %s args: %s"
, plugin
, method
, args
Expand All @@ -754,7 +762,7 @@ public void addPythonPlugin(String name, Object plugin) {
return;
}

MengineLog.logMessage(TAG, "addPythonPlugin [%s] plugin: %s"
MengineLog.logMessage(TAG, "addPythonPlugin name: %s plugin: %s"
, name
, plugin
);
Expand All @@ -771,7 +779,7 @@ public void activateSemaphore(String name) {
return;
}

MengineLog.logMessage(TAG, "activate semaphore [%s]"
MengineLog.logMessage(TAG, "activateSemaphore semaphore: %s"
, name
);

Expand Down Expand Up @@ -807,7 +815,7 @@ public void waitSemaphore(String name, MengineFunctorVoid cb) {
}

public void waitAndroidSemaphore(String name) {
MengineLog.logMessage(TAG, "wait android semaphore [%s]"
MengineLog.logMessage(TAG, "waitAndroidSemaphore semaphore: %s"
, name
);

Expand All @@ -823,7 +831,7 @@ public void waitAndroidSemaphore(String name) {
**********************************************************************************************/

public boolean linkingOpenURL(String url) {
MengineLog.logMessage(TAG, "linking open url [%s]"
MengineLog.logMessage(TAG, "linkingOpenURL url: %s"
, url
);

Expand All @@ -841,7 +849,7 @@ public boolean linkingOpenURL(String url) {
}

public boolean linkingOpenMail(String email, String subject, String body) {
MengineLog.logMessage(TAG, "linking open mail [%s] subject [%s] body: %s"
MengineLog.logMessage(TAG, "linkingOpenMail mail: %s subject: %s body: %s"
, email
, subject
, body
Expand Down Expand Up @@ -878,7 +886,7 @@ public boolean linkingOpenMail(String email, String subject, String body) {
File accountZipFile = MengineUtils.createTempFile(context, "mng_account_", ".zip");

if (accountZipFile == null) {
MengineLog.logWarning(TAG, "linking open mail [%s] subject [%s] invalid create temp file 'mng_account_***.zip'"
MengineLog.logWarning(TAG, "linkingOpenMail invalid create temp file 'mng_account_***.zip' mail: %s subject: %s"
, email
, subject
);
Expand All @@ -893,10 +901,10 @@ public boolean linkingOpenMail(String email, String subject, String body) {
return false;
}

MengineLog.logInfo(TAG, "linking open mail [%s] subject [%s] attach: %s"
MengineLog.logInfo(TAG, "linkingOpenMail attach file '%s' mail: %s subject: %s"
, accountZIPUri
, email
, subject
, accountZIPUri
);

fileUris.add(accountZIPUri);
Expand All @@ -905,7 +913,7 @@ public boolean linkingOpenMail(String email, String subject, String body) {
File logFile = MengineUtils.createTempFile(context, "mng_log_", ".log");

if (logFile == null) {
MengineLog.logWarning(TAG, "linking open mail [%s] subject [%s] invalid create temp file 'mng_log_***.log'"
MengineLog.logWarning(TAG, "linkingOpenMail invalid create temp file 'mng_log_***.log' mail: %s subject: %s"
, email
, subject
);
Expand All @@ -928,10 +936,10 @@ public boolean linkingOpenMail(String email, String subject, String body) {
return false;
}

MengineLog.logInfo(TAG, "linking open mail [%s] subject [%s] attach: %s"
MengineLog.logInfo(TAG, "linkingOpenMail attach file '%s' mail: %s subject: %s"
, logZipFileUri
, email
, subject
, logZipFileUri
);

fileUris.add(logZipFileUri);
Expand All @@ -941,7 +949,7 @@ public boolean linkingOpenMail(String email, String subject, String body) {
File oldLogFile = MengineUtils.createTempFile(context, "mng_old_log_", ".log");

if (oldLogFile == null) {
MengineLog.logWarning(TAG, "linking open mail [%s] subject [%s] invalid create temp file 'mng_old_log_***.log'"
MengineLog.logWarning(TAG, "linkingOpenMail invalid create temp file 'mng_old_log_***.log' mail: %s subject: %s"
, email
, subject
);
Expand All @@ -964,10 +972,10 @@ public boolean linkingOpenMail(String email, String subject, String body) {
return false;
}

MengineLog.logInfo(TAG, "linking open mail [%s] subject [%s] attach: %s"
MengineLog.logInfo(TAG, "linkingOpenMail attach file '%s' mail: %s subject: %s"
, oldLogZipFileUri
, email
, subject
, oldLogZipFileUri
);

fileUris.add(oldLogZipFileUri);
Expand All @@ -976,7 +984,7 @@ public boolean linkingOpenMail(String email, String subject, String body) {

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris);
} catch (IOException e) {
MengineLog.logError(TAG, "linking open mail [%s] subject [%s] failed attachs file exception: %s"
MengineLog.logError(TAG, "[ERROR] linkingOpenMail failed attachs file mail: %s subject: %s exception: %s"
, email
, subject
, e.getLocalizedMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Bundle getMetaDataBundle() {

return bundle;
} catch (PackageManager.NameNotFoundException e) {
MengineLog.logError(TAG, "Unable to load meta-data: %s"
MengineLog.logError(TAG, "[ERROR] Unable to load meta-data: %s"
, e.getLocalizedMessage()
);
}
Expand Down Expand Up @@ -373,13 +373,13 @@ public <T> T getPlugin(Class<T> cls) {

name = (String)PLUGIN_NAME.get(null);
} catch (NoSuchFieldException ex) {
MengineLog.logError(TAG, "plugin not found field PLUGIN_NAME: %s"
MengineLog.logError(TAG, "[ERROR] plugin not found field PLUGIN_NAME: %s"
, cls.getName()
);

return null;
} catch (IllegalAccessException ex) {
MengineLog.logError(TAG, "plugin invalid field PLUGIN_NAME: %s"
MengineLog.logError(TAG, "[ERROR] plugin invalid field PLUGIN_NAME: %s"
, cls.getName()
);

Expand Down Expand Up @@ -411,21 +411,21 @@ protected boolean createPlugin(String type) {

name = (String)PLUGIN_NAME.get(plugin);
} catch (NoSuchFieldException ex) {
MengineLog.logError(TAG, "plugin [%s] not found field PLUGIN_NAME"
MengineLog.logError(TAG, "[ERROR] plugin [%s] not found field PLUGIN_NAME"
, type
);

return false;
} catch (IllegalAccessException ex) {
MengineLog.logError(TAG, "plugin [%s] invalid field PLUGIN_NAME"
MengineLog.logError(TAG, "[ERROR] plugin [%s] invalid field PLUGIN_NAME"
, type
);

return false;
}

if (plugin.onInitialize(this, name) == false) {
MengineLog.logError(TAG, "plugin [%s] invalid initialize"
MengineLog.logError(TAG, "[ERROR] plugin [%s] invalid initialize"
, type
);

Expand Down Expand Up @@ -590,23 +590,31 @@ private boolean isMainProcess() {
public void onCreate() {
super.onCreate();

if (this.isMainProcess() == false) {
return;
}
boolean isMainProcess = this.isMainProcess();

ArrayList<MenginePluginApplicationListener> applicationListeners = this.getApplicationListeners();

for (MenginePluginApplicationListener l : applicationListeners) {
try {
l.onAppInit(this);
MengineLog.logMessage(TAG, "onAppInit plugin: %s isMainProcess: %b"
, l.getPluginName()
, isMainProcess
);

l.onAppInit(this, isMainProcess);
} catch (MenginePluginInvalidInitializeException e) {
this.invalidInitialize("invalid plugin %s onAppCreate exception: %s"
, e.getPluginName()
, e.getLocalizedMessage()
this.invalidInitialize("onAppCreate plugin: %s exception: %s"
, l.getPluginName()
, e.getLocalizedMessage()
);
}
}

if (isMainProcess == false) {
return;
}


this.setState("build.debug", BuildConfig.DEBUG);

this.setState("application.init", "started");
Expand Down Expand Up @@ -726,10 +734,14 @@ public Long get() {

for (MenginePluginApplicationListener l : applicationListeners) {
try {
MengineLog.logMessage(TAG, "onAppPrepare plugin: %s"
, l.getPluginName()
);

l.onAppPrepare(this);
} catch (MenginePluginInvalidInitializeException e) {
this.invalidInitialize("invalid plugin %s onAppCreate exception: %s"
, e.getPluginName()
this.invalidInitialize("onAppPrepare plugin: %s exception: %s"
, l.getPluginName()
, e.getLocalizedMessage()
);
}
Expand All @@ -746,6 +758,10 @@ public Long get() {
try {
String pluginName = l.getPluginName();

MengineLog.logMessage(TAG, "onAppCreate plugin: %s"
, pluginName
);

this.setState("application.init", pluginName);

long app_init_plugin_start_timestamp = MengineAnalytics.buildEvent("mng_app_init_plugin_start")
Expand All @@ -759,9 +775,9 @@ public Long get() {
.addParameterLong("time", MengineUtils.getDurationTimestamp(app_init_plugin_start_timestamp))
.flush();
} catch (MenginePluginInvalidInitializeException e) {
this.invalidInitialize("invalid plugin %s callback onAppCreate exception: %s"
this.invalidInitialize("onAppCreate plugin: %s exception: %s"
, l.getPluginName()
, e.getPluginName()
, e.getLocalizedMessage()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String getPrivateSettingString(String name, String defaultValue) {
protected void invalidInitialize(String format, Object ... args) throws MenginePluginInvalidInitializeException {
String message = MengineLog.buildTotalMsg(format, args);

throw new MenginePluginInvalidInitializeException(m_pluginName, message);
throw new MenginePluginInvalidInitializeException(message);
}

public void sendEvent(MengineEvent event, Object ... args) {
Expand Down Expand Up @@ -159,7 +159,7 @@ public boolean createExtension(MengineActivity activity, String type) {
MenginePluginExtension extension = m_application.createPluginExtension(activity, this, type);

if (extension == null) {
this.logError("plugin [%s] invalid create extension: %s"
this.logError("[ERROR] plugin [%s] invalid create extension: %s"
, m_pluginName
, type
);
Expand Down Expand Up @@ -189,14 +189,14 @@ public void onExtensionInitialize(MengineActivity activity) throws MenginePlugin
} catch (NoSuchFieldException e) {
return;
} catch (NullPointerException e) {
this.invalidInitialize("plugin [%s] invalid get MENGINE_GRADLE_ANDROID_PLUGIN_EXTENSIONS exception: %s [NullPointerException]"
this.invalidInitialize("invalid get MENGINE_GRADLE_ANDROID_PLUGIN_EXTENSIONS plugin: %s exception: %s [NullPointerException]"
, m_pluginName
, e.getLocalizedMessage()
);

return;
} catch (SecurityException e) {
this.invalidInitialize("plugin [%s] invalid get MENGINE_GRADLE_ANDROID_PLUGIN_EXTENSIONS exception: %s [SecurityException]"
this.invalidInitialize("invalid get MENGINE_GRADLE_ANDROID_PLUGIN_EXTENSIONS plugin: %s exception: %s [SecurityException]"
, m_pluginName
, e.getLocalizedMessage()
);
Expand All @@ -209,14 +209,14 @@ public void onExtensionInitialize(MengineActivity activity) throws MenginePlugin
try {
extension = f.get(this);
} catch (IllegalArgumentException e) {
this.invalidInitialize("plugin [%s] invalid get extension from this exception: %s [IllegalArgumentException]"
this.invalidInitialize("invalid get extension from this plugin: %s exception: %s [IllegalArgumentException]"
, m_pluginName
, e.getLocalizedMessage()
);

return;
} catch (IllegalAccessException e) {
this.invalidInitialize("plugin [%s] invalid get extension from this exception: %s [IllegalAccessException]"
this.invalidInitialize("invalid get extension from this plugin: %s exception: %s [IllegalAccessException]"
, m_pluginName
, e.getLocalizedMessage()
);
Expand All @@ -230,7 +230,7 @@ public void onExtensionInitialize(MengineActivity activity) throws MenginePlugin

for (String extensionName : (String[])extension) {
if (this.createExtension(activity, extensionName) == false) {
this.invalidInitialize("invalid create extension %s"
this.invalidInitialize("[ERROR] invalid create extension %s"
, extensionName
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import androidx.annotation.NonNull;

public interface MenginePluginApplicationListener extends MenginePluginInterface {
default void onAppInit(MengineApplication application) throws MenginePluginInvalidInitializeException {
default void onAppInit(MengineApplication application, boolean isMainProcess) throws MenginePluginInvalidInitializeException {
//Empty
}

Expand Down
Loading

0 comments on commit 14a9b28

Please sign in to comment.