如何在ios中使用ASL获取控制台日志?

时间:2014-03-03 12:29:20

标签: ios iphone nslog asl

我想使用ASL获取控制台日志。我已经编写了以下代码,并且我在iPhone模拟器中获得了所有控制台日志,但是当我使用iPhone 4.1和iPad时,从开始起,我得到的控制台就不那么完全了。我已经尝试了很多。

- (NSMutableArray *)getAppLogByApp:(NSString *)appName
{
    NSMutableArray      *lObjAppLogsPtr = (NSMutableArray *)nil;
    LogInfo         *lObjLogInfoPtr = (LogInfo *)nil;

    aslmsg q, m;
    int i;
    const char *key, *val, *lAppName;
    NSDateFormatter *lObjDateFormatter = [[NSDateFormatter alloc] init];
    [lObjDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


    q = asl_new(ASL_TYPE_QUERY);

    //also set the log level to fetch if is set to the global variable m_cObjLogMessageLevel
    const char* lObjASLLogLevel = [[NSString stringWithFormat:@"%d", m_cLogMessageLevel] UTF8String];
    asl_set_query(q, ASL_KEY_LEVEL, lObjASLLogLevel, ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
    //asl_set_query(q, ASL_KEY_LEVEL, lObjASLLogLevel, ASL_QUERY_OP_EQUAL | ASL_QUERY_OP_NUMERIC);
    //if appName passed to it then fetch the logs for this application only
    if ((NSString *)nil != appName && 0 < appName.length)
    {
        lAppName = [appName UTF8String];
        asl_set_query(q, ASL_KEY_SENDER, lAppName, ASL_QUERY_OP_EQUAL);
    }

    aslresponse r = asl_search(NULL, q);

    while (NULL != (m = aslresponse_next(r)))
    {
        if ((NSMutableArray *)nil == lObjAppLogsPtr)
        {
            lObjAppLogsPtr = [[NSMutableArray alloc] init];
        }

        for (i = 0; (NULL != (key = asl_key(m, i))); i++)
        {
            NSString *keyString = [NSString stringWithUTF8String:(char *)key];

            val = asl_get(m, key);

            NSString *lObjValString = nil;
            if (nil != val)
            {
                lObjValString = [NSString stringWithUTF8String:val];
            }
            else
            {
                //NSLog(@"Val in nil");
                lObjValString = @"";
            }

            if ((LogInfo *)nil == lObjLogInfoPtr)
            {
                lObjLogInfoPtr = [[LogInfo alloc] init];
            }


            if(YES == [keyString isEqualToString:@"Level"])
            {
                lObjLogInfoPtr.m_cObjLevel = lObjValString;
            }
            else if(YES == [keyString isEqualToString:@"Time"])
            {
                //convert the time to YYYY-MM-DD HH:MM:SS format
                time_t time = strtol(val, NULL, 0);

                NSDate *lObjDatePtr = [[NSDate dateWithTimeIntervalSince1970:time] copy];
                NSString *lObjDateStringPtr = [lObjDateFormatter stringFromDate:lObjDatePtr];
                lObjLogInfoPtr.m_cObjTime = lObjDateStringPtr;
            }
            else if(YES == [keyString isEqualToString:@"Sender"])
            {
                lObjLogInfoPtr.m_cObjSender = lObjValString;
            }
            else if(YES == [keyString isEqualToString:@"Message"])
            {
                lObjLogInfoPtr.m_cObjMessage = lObjValString;
            }
        }

        //Add the loginfo array
        [lObjAppLogsPtr addObject:lObjLogInfoPtr];
        //Release the loginfo and set it to nil for further use
         [lObjLogInfoPtr release];
        lObjLogInfoPtr = (LogInfo *)nil;
    }

    if (NULL != r)
    {
        aslresponse_free(r);
    }

    //free the als msg object
    asl_free(q);
    [lObjDateFormatter release];


    return lObjAppLogsPtr;
}

此处LogIngoNSObject的子类。 请帮帮我。我一直在尝试一周。

1 个答案:

答案 0 :(得分:4)

Mac上的行为(因此在iOS模拟器中)是不同的;在Mac上,默认情况下,进程可以读取其他进程生成的日志条目。在iOS设备上,ASL将返回您的进程生成的日志条目,并且仅当您明确将ASL_KEY_READ_UID设置为&#34; {{1 }}&#34;

此外,日志在设备上进行了相当严格的修剪,并且可能只包含当前运行的应用程序所做的条目。 (如果您希望将日志条目保留在其之后并且您正在使用Swift,则可能需要查看更强大的日志记录包,例如CleanroomLogger。)

如果您正在使用Swift,那么有一个名为CleanroomASL的新开源项目,它提供了一个类型安全的API,用于读取和写入Apple System Log条目。使用CleanroomASL,搜索ASL日志条目如下:

-1

您可以通过设置其他查询键来约束搜索查询:

let client = ASLClient()
let query = ASLQueryObject()

client.search(query) { record in
    if let record = record {
        // we've gotten a log entry for the search.
        // 'record' is of type ASLQueryObject.ResultRecord
    }
    else {
        // there are no more log entries to process
    }
    // return true while we still want to get results
    // or return false when we don't want to process more
    return true
}

上面的代码将返回过去5分钟内记录的所有ASL日志条目,这些日志条目也具有query.setQueryKey(.Message, value: nil, operation: .KeyExists, modifiers: .None) query.setQueryKey(.Time, value: Int(NSDate().timeIntervalSince1970 - (60 * 60)), operation: .GreaterThanOrEqualTo, modifiers: .None) 属性的值。