自己動手寫一個Bug管理工具

knightG發表於2017-10-09

timg.jpeg
timg.jpeg

##功能

  • 非Crash Bug 在App內可截圖新增描述併傳送
  • Crash Bug 在App第二次啟動時提取Crash log新增描述併傳送

##分析
非Crash的Bug:字型不對、顏色不對、資料不對、佈局不對。
Crash Bug:系統Crash、處理signal

場景互動:發現非Crash Bug時候搖一搖手機,彈出郵件,圖片帶入郵件,點選傳送即可。有Crash Bug的時候第二次啟動App,彈出郵件,Crash log帶入郵件,點選傳送即可。

需要用到NSSetUncaughtExceptionHandler,MFMailComposeViewController,沙盒,NSFileManager。

##實現
截圖的功能,考慮到並不是所有的頁面都需要使用所以寫在了分類裡。需要用的時候直接引入標頭檔案即可。

//這三個方法分別在搖一搖的時候回撥用,開始,需要,結束。他們的父類是UIResponsder在UIKit中。
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self screenShot];
}

-(void)screenShot
{
    UIWindow *screen = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screen.frame.size);
    [screen.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsGetCurrentContext();
    NSData *screenData = UIImagePNGRepresentation(image);
    [screenData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] atomically:YES];   
}複製程式碼

傳送郵件的功能,也寫在了分類裡面,需要用的時候引入即可。

@interface UIViewController (send)<MFMailComposeViewControllerDelegate>

//傳送郵件的方法,傳入標題,描述資訊,data, 接收人
-(void)sendMail:(MFMailComposeViewController*)mf andSubject:(NSString*)subject andMessageBody:(NSString*)message andData:(NSData*)data  andRecipients:(NSArray*)recipients
{
    if([MFMailComposeViewController canSendMail]){
        mf.mailComposeDelegate = self;
        [mf setSubject:subject];
        [mf setToRecipients:recipients];
        [mf addAttachmentData:data mimeType:@"image/jpeg" fileName:@"error"];
        [mf setMessageBody:message isHTML:YES];
        [self presentViewController:mf animated:YES completion:nil];
    }else{
        [self alertView:@"不能呼叫郵箱" andDesc:@"請嘗試下載App原生郵箱,並配置"];
    }
}

//MFMailComposeViewControllerDelegate的代理方法,可以在這個方法裡面寫一些回撥
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result) {
        case MFMailComposeResultSent:

            [self alertView:@"傳送成功" andDesc:nil];
            self.success();
            break;
        case MFMailComposeResultSaved:
            [self alertView:@"儲存成功" andDesc:nil];
            break;
        case MFMailComposeResultFailed:
            self.faild();
            [self alertView:error.domain andDesc:[NSString stringWithFormat:@"%@",error.userInfo]];
            break;
        case MFMailComposeResultCancelled:
            [self alertView:@"取消傳送" andDesc:nil];
            break;
        default:
            [self alertView:@"為什麼不傳送" andDesc:nil];
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}複製程式碼

異常捕獲

這兩個為函式方法,匯入類名,直接可呼叫不用初始化
void CrashExceptionHandler(void)
{
    NSSetUncaughtExceptionHandler(&ExceptionLog);
}

void ExceptionLog(NSException *exception)
{
    NSDate *date_current = [NSDate date];
    NSDictionary *dictInfo = [[NSBundle mainBundle]infoDictionary];
    NSString *name_App = [dictInfo objectForKey:@"CFBundleDisplayName"];
    NSString *verson_App = [dictInfo objectForKey:@"CFBundleShortVersionString"];
    NSString *build_App = [dictInfo objectForKey:@"CFBundleVersion"];
    NSArray *ecp = exception.callStackSymbols;
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    NSString *exceptionInfo = [NSString stringWithFormat:
                               @"\n\n ******************************異常日誌****************************** \n時間:%@\nApp名稱:%@\nApp版本:%@\nBuild版本:%@\n異常名稱:%@\n異常原因:%@\n堆疊資訊:%@",date_current,name_App,verson_App,build_App,name,reason,ecp];
    [CrashHandler saveLog:exceptionInfo andDate:date_current];
#ifdef DEBUG
    NSLog(@"%@",exceptionInfo);
#else

#endif

}

@implementation CrashHandler
+(void)saveLog:(NSString *)crashLog andDate:(NSDate *)date
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingString:@"/Crash"];
    if(![[NSFileManager defaultManager]fileExistsAtPath:path])
    {
        [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *logPath = [path stringByAppendingFormat:@"/%@.log",date];
    [crashLog writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
@end複製程式碼

檢測Crash log 功能在App開啟的第一個頁面去呼叫就好

-(void)crashLog
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingString:@"/Crash"];
    NSFileManager *mf = [NSFileManager defaultManager];
    if(![mf fileExistsAtPath:path])
    {
        return;
    }
    NSArray *array = [mf contentsOfDirectoryAtPath:path error:nil]; 
 }複製程式碼

以上程式碼均為區域性程式碼,具體程式碼請移步github

##為什麼要寫

###此處廢話,可忽略

最近找工作面技術的時候經常會聊到App中bug的處理,我前公司Web端業務繁重領導並不太關心App,只有一個全域性的異常捕獲還是我軟磨硬泡加進去的。我只有實話實說,告訴他我們的Bug統計平臺,又胡謅一些個人意見可加入第三方Bug管理工具(OneAPM,Bugly)。這並不是滿意的答案,他們的Bug是自己寫的工具,所以我只能繼續求職中。

我們前公司的Bug管理用過禪道、OSChina、Jira。(經歷過四個技術總監)

  • 禪道
    我們之前的測試老大用公司伺服器搭建的,中規中矩,我覺得很好用啊,配合著jekins還能去看後臺的日誌。
  • OSChina
    第三個技術總監用的,省事,拿過來直接用就好,不用搭建伺服器什麼的。
  • jira
    第四任技術總監搭建的,功能最為強大,正版的jira很貴,我們用的破解版。

以上都有著完整的專案管理系統,包括了任務安排,Bug追蹤系統等等,日常工作夠用了。當有Bug的時候測試人員需要在手機上手動截圖,將圖片匯入PC後再上傳平臺上,再由平臺發給對應的開發人員。如果有崩潰的Bug還要想著復現,握著資料線插,拿著手機走來走去,是不是很麻煩。所以寫了一個小工具,關於訊號量的異常捕獲,有待日後完善,見笑了。

相關文章