根据两个参数为问题创建ID

时间:2016-07-14 14:08:41

标签: php

有一段时间我一直试图改变我的ACRA后端以包含问题ID,因此每个问题都不会显示为单个问题,但会显示为报告计数的一个问题。因此,我解决了这个问题:

// Finds in array
function array_find($needle, $haystack) {
    foreach($haystack as $k => $v) {
        if (strstr($v, $needle) !== FALSE) {
            return $k;
        }
    }
    return FALSE;
}
function bicou_short_stack_trace($stack_trace, $package) {
    $lines = explode("\n", $stack_trace);
    if (array_find(": ", $lines) === FALSE && array_find($package, $lines) === FALSE) {
        $value = $lines[0];
    } else {
        $value = "";
        foreach ($lines as $id => $line) {
        if (/*strpos($line, ": ") !== FALSE || */strpos($line, $package) !== FALSE
             || strpos($line, "Error") !== FALSE || strpos($line, "ACRA caught a") !== FALSE) {
                $value .= $line . "<br />";
            }
        }
    }
    return $value;
}

function bicou_issue_id($stack_trace, $package) {
    return md5(bicou_short_stack_trace($stack_trace, $package));
}

这应该采用堆栈跟踪和包并将其转换为问题ID,无论何时报告,都可以反复使用相同问题。所以我在我的应用程序中导致错误传输:

PACKAGE_NAME = com.my.package
APP_VERSION_NAME = 1.1.5
LOGCAT = 07-14 15:51:20.891 V/MediaPlayer(31408): isPlaying: no active player
07-14 15:51:20.891 V/MediaPlayer-JNI(31408): isPlaying: 0
07-14 15:51:20.911 V/MediaPlayer(31408): isPlaying: no active player
07-14 15:51:20.911 V/MediaPlayer-JNI(31408): isPlaying: 0
07-14 15:51:20.931 V/MediaPlayer(31408): isPlaying: no active player
07-14 15:51:20.931 V/MediaPlayer-JNI(31408): isPlaying: 0
07-14 15:51:20.941 V/MediaPlayer(31408): isPlaying: no active player
07-14 15:51:20.941 V/MediaPlayer-JNI(31408): isPlaying: 0
07-14 15:51:20.961 V/MediaPlayer(31408): isPlaying: no active player
07-14 15:51:20.961 V/MediaPlayer-JNI(31408): isPlaying: 0
07-14 15:51:20.981 V/MediaPlayer(31408): isPlaying: no active player
07-14 15:51:20.981 V/MediaPlayer-JNI(31408): isPlaying: 0
07-14 15:51:22.651 W/SELinux (  566): SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=com.my.package
07-14 15:51:22.661 I/art     (  566): Late-enabling -Xcheck:jni
07-14 15:51:22.681 D/TimaKeyStoreProvider(  566): TimaSignature is unavailable
07-14 15:51:22.681 D/ActivityThread(  566): Added TimaKeyStore provider
--------- beginning of system
07-14 15:51:22.711 W/ResourcesManager(  566): getTopLevelResources: /data/app/com.my.package-2/base.apk / 1.0 running in com.my.package rsrc of package com.my.package
07-14 15:51:22.731 I/InjectionManager(  566): Inside getClassLibPath + mLibMap{0=, 1=}
07-14 15:51:22.741 I/InjectionManager(  566): Inside getClassLibPath caller
07-14 15:51:22.741 D/ResourcesManager(  566): For user 0 new overlays fetched Null
07-14 15:51:22.751 W/System  (  566): ClassLoader referenced unknown path: /data/app/com.my.package-2/lib/arm64
07-14 15:51:22.791 I/ACRA    (  566): ACRA is enabled for com.my.package, initializing...
07-14 15:51:22.851 D/InjectionManager(  566): InjectionManager
07-14 15:51:22.851 D/InjectionManager(  566): fillFeatureStoreMap com.my.package
07-14 15:51:22.851 I/InjectionManager(  566): Constructor com.my.package, Feature store :{}
07-14 15:51:22.851 I/InjectionManager(  566): featureStore :{}
07-14 15:51:22.851 W/SELinux (  613): SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=com.my.package:acra
07-14 15:51:22.861 I/art     (  613): Late-enabling -Xcheck:jni
07-14 15:51:22.861 W/ResourcesManager(  566): getTopLevelResources: /data/app/com.my.package-2/base.apk / 1.0 running in com.my.package rsrc of package com.my.package
07-14 15:51:22.861 W/ResourcesManager(  566): getTopLevelResources: /data/app/com.my.package-2/base.apk / 1.0 running in com.my.package rsrc of package com.my.package
07-14 15:51:22.881 D/TimaKeyStoreProvider(  613): TimaSignature is unavailable
07-14 15:51:22.881 D/ActivityThread(  613): Added TimaKeyStore provider
07-14 15:51:22.901 I/InjectionManager(  613): Inside getClassLibPath + mLibMap{0=, 1=}
07-14 15:51:22.901 W/ResourcesManager(  613): getTopLevelResources: /data/app/com.my.package-2/base.apk / 1.0 running in com.my.package rsrc of package com.my.package
07-14 15:51:22.901 D/ResourcesManager(  613): For user 0 new overlays fetched Null
07-14 15:51:22.911 I/InjectionManager(  613): Inside getClassLibPath caller
07-14 15:51:22.911 W/System  (  613): ClassLoader referenced unknown path: /data/app/com.my.package-2/lib/arm64
07-14 15:51:22.931 D/Activity(  566): performCreate Call Injection manager
07-14 15:51:22.941 I/InjectionManager(  566): dispatchOnViewCreated > Target : com.my.package.Splash isFragment :false
07-14 15:51:22.941 D/InjectionManager(  613): InjectionManager
07-14 15:51:22.941 D/InjectionManager(  613): fillFeatureStoreMap com.my.package
07-14 15:51:22.941 I/InjectionManager(  613): Constructor com.my.package, Feature store :{}
07-14 15:51:22.941 I/InjectionManager(  613): featureStore :{}
07-14 15:51:22.941 D/ViewRootImpl(  566): #1 mView = com.android.internal.policy.PhoneWindow$DecorView{f80c5e I.E...... R.....ID 0,0-0,0}
07-14 15:51:22.941 D/SecWifiDisplayUtil(  566): Metadata value : SecSettings2
07-14 15:51:22.951 D/OpenGLRenderer(  566): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-14 15:51:23.051 D/libEGL  (  566): loaded /vendor/lib64/egl/libGLES_mali.so
07-14 15:51:23.071 D/libEGL  (  566): eglInitialize EGLDisplay = 0x7f97440178
07-14 15:51:23.071 I/OpenGLRenderer(  566): Initialized EGL, version 1.4
07-14 15:51:23.071 D/        (  566): ro.exynos.dss isEnabled: 0
07-14 15:51:23.081 D/mali_winsys(  566): new_window_surface returns 0x3000,  [1440x2560]-format:1
07-14 15:51:23.121 D/ViewRootImpl(  566): MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
07-14 15:51:23.181 D/ViewRootImpl(  566): #3 mView = null
07-14 15:51:23.191 E/ViewRootImpl(  566): sendUserActionEvent() mView == null
07-14 15:51:23.301 E/ViewRootImpl(  566): sendUserActionEvent() mView == null
07-14 15:51:25.941 I/Timeline(  566): Timeline: Activity_launch_request id:com.my.package time:414449072
07-14 15:51:26.021 W/ResourcesManager(  566): getTopLevelResources: /data/app/com.my.package-2/base.apk / 1.0 running in com.my.package rsrc of package com.my.package
07-14 15:51:26.171 D/AndroidRuntime(  566): Shutting down VM
07-14 15:51:26.171 E/ACRA    (  566): ACRA caught a RuntimeException for com.my.package
07-14 15:51:26.171 E/ACRA    (  566): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package/com.my.package.Game}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class com.my.package.Clicker
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.ActivityThread.access$1100(ActivityThread.java:222)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
07-14 15:51:26.171 E/ACRA    (  566):   at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 15:51:26.171 E/ACRA    (  566):   at android.os.Looper.loop(Looper.java:158)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.ActivityThread.main(ActivityThread.java:7229)
07-14 15:51:26.171 E/ACRA    (  566):   at java.lang.reflect.Method.invoke(Native Method)
07-14 15:51:26.171 E/ACRA    (  566):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
07-14 15:51:26.171 E/ACRA    (  566):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-14 15:51:26.171 E/ACRA    (  566): Caused by: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class com.my.package.Clicker
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
07-14 15:51:26.171 E/ACRA    (  566):   at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.Activity.setContentView(Activity.java:2387)
07-14 15:51:26.171 E/ACRA    (  566):   at com.my.package.Game.onCreate(Game.java:62)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.Activity.performCreate(Activity.java:6876)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
07-14 15:51:26.171 E/ACRA    (  566):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
07-14 15:51:26.171 E/ACRA    (  566):   ... 9 more
07-14 15:51:26.171 E/ACRA    (  566): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.my.package.Clicker
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.createView(LayoutInflater.java:657)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
07-14 15:51:26.171 E/ACRA    (  566):   ... 17 more
07-14 15:51:26.171 E/ACRA    (  566): Caused by: java.lang.reflect.InvocationTargetException
07-14 15:51:26.171 E/ACRA    (  566):   at java.lang.reflect.Constructor.newInstance(Native Method)
07-14 15:51:26.171 E/ACRA    (  566):   at android.view.LayoutInflater.createView(LayoutInflater.java:631)
07-14 15:51:26.171 E/ACRA    (  566):   ... 24 more
07-14 15:51:26.171 E/ACRA    (  566): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
07-14 15:51:26.171 E/ACRA    (  566):   at com.my.package.Clicker.init(Clicker.java:241)
07-14 15:51:26.171 E/ACRA    (  566):   at com.my.package.Clicker.<init>(Clicker.java:217)
07-14 15:51:26.171 E/ACRA    (  566):   ... 26 more
PHONE_MODEL = SM-G925F
ANDROID_VERSION = 6.0.1

每次都是完全相同的,除了每行前面定义的时间。

对于问题:每次输入完全相同,但每次都会产生不同的ID。我引起了3次,这里是ID:

69b30e6cff3768ff3e3616d85a4b2ea8
e2c090b737a4e854106c373fa408779c
038e70fe97be4e23cac2d6bb42df751c

堆栈跟踪不包含在.txt文件的前两行和后两行中。

我该如何解决这个问题?它真的很烦人,因为上面的代码需要很长时间才能解决它并且不起作用。

要求:

如果stacktrace相同,我希望输出(ID)相同,但它似乎不起作用。

修改

感谢Samurai8,我设法得到了这个:

$reworkedLog = $LOG;
$reworkedLog = str_replace('LOGCAT =', '', $reworkedLog);

$reworkedLog = preg_replace("'\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d*'", '', $reworkedLog);
$reworkedLog = preg_replace("'$\d*'", '', $reworkedLog);
$reworkedLog = preg_replace("'0x[0-9a-f]*'", '', $reworkedLog);
$reworkedLog = preg_replace("'(\d{5})'",'', $reworkedLog);
$reworkedLog = preg_replace("'... \d{1} more'", '', $reworkedLog);
$reworkedLog = preg_replace("'... \d{2} more'", '', $reworkedLog);

$issue_id = bicou_issue_id($reworkedLog, $package) . '<br>';

这使得错误md5每次都相同

1 个答案:

答案 0 :(得分:2)

你应该牢记三件事:

  • md5是一个哈希函数。这意味着许多输入具有相同的输出。永远不能保证两个完全不同的堆栈跟踪会产生不同的ID。这意味着即使id已经在系统中,也不能盲目地忽略堆栈跟踪。
  • 输入的微小差异将导致不同的哈希值。向堆栈跟踪添加单个空格或更改单个字符将导致生成不同的哈希,从而为您提供不同的ID。
  • 堆栈跟踪通常包含执行的日期和时间代码,对象的内存位置和实例化编号。在您的情况下,您的logcat包含所有这些:

    07-14 15:51:20.891
    

    eglInitialize EGLDisplay = 0x7f97440178
    

    at android.app.ActivityThread.access$1100
    

    堆栈跟踪本身只包含日期和实例化编号,但足以创建不同的ID。

你能做什么?我们可以从生成id的堆栈跟踪中剥离它们。我们可能希望将它们留在实际的堆栈跟踪中,因为它提供了一些上下文。只需preg_replace(..)使用以下正则表达式:

  • \d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d*日期和时间
  • \$\d*匿名课程
  • 0x[0-9a-f]*表示十六进制值,可能是每次运行都会更改的内存位置。

当然这会增加id不唯一的机会,但我们已经确定你必须记录每个错误。给出相同内容的机会应该是最小的,但要确保无论你在哪里记录这些错误,都可以拆分错误报告,以防两个不同的错误记录在相同的id下。或者,为每个崩溃报告使用唯一ID,并根据计算出的ID自动建议重复。