码迷,mamicode.com
首页 > 其他好文 > 详细

请求失败

时间:2016-03-24 18:16:42      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

// 连接错误报错的方法
- (void)getErrorMethod:(NSError *)error{
    // 获取当前时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString* date = [formatter stringFromDate:[NSDate date]];
    // 获取错误的所在类
    NSString *errorClass = NSStringFromClass([self class]);
    // 获取错误所在的方法
    NSString *errorMethod = NSStringFromSelector(_cmd);
    // 获取错误所在的行数
    NSString *lineNumber = [NSString stringWithFormat:@"%d",__LINE__];
    NSString *excpCnt = [NSString stringWithFormat:
                         @"date: %@ \n errorClass: %@ \n errorMethod: %@ \n lineNumber: %@ \n\n",
                         date,errorClass,errorMethod,lineNumber];
    [self saveErrorLog:excpCnt];
}


// 保存日志的方法
- (void)saveErrorLog:(NSString *)excpCnt{
    NSArray  *dirArr  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dirPath = dirArr[0];
    NSString *logDir = [dirPath stringByAppendingString:@"/CrashLog"];
    
    BOOL isExistLogDir = YES;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:logDir]) {
        isExistLogDir = [fileManager createDirectoryAtPath:logDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    if (isExistLogDir) {
        // 此处可扩展
        NSString *logPath = [logDir stringByAppendingString:@"/crashLog.txt"];
        // 没有log.txt文件就新建一个
        if (![fileManager fileExistsAtPath:logPath]) {
            [excpCnt writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
        // 有的话就追加文字
        else{
            NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:logPath];
            [fileHandle seekToEndOfFile];  //将节点跳到文件的末尾
            NSData* stringData  = [excpCnt dataUsingEncoding:NSUTF8StringEncoding];
            [fileHandle writeData:stringData]; //追加写入数据
            [fileHandle closeFile];
        }
    }
    
}

 

请求失败

标签:

原文地址:http://www.cnblogs.com/zpt1011/p/5316391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!