The options and position arguments do not work in the 'entriesEnumeratorWithOptions:position:predicate:error:' method of the OSLogStore object.

  1. When I set the option parameter to OSLogEnumeratorReverse, the iteration order of OSLogEnumerator is still from front to back in time

  2. When I set the options parameter to 0 and the position parameter to the first 5 seconds of the current time, OSLogEnumerator can still iterate over the previous 5 seconds


#import "ViewController.h"
#import <OSLog/OSLog.h>

@interface ViewController ()

@property(strong, nonatomic)OSLogStore *logStore;
@property(strong, nonatomic)NSDateFormatter *formatter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *err = nil;
    self.logStore = [OSLogStore storeWithScope:OSLogStoreCurrentProcessIdentifier error:&err];
    if (!self.logStore || err) {
        NSLog(@"error: %@", err);
        NSAssert(0, @"");
    }
    self.formatter = [[NSDateFormatter alloc] init];
    [self.formatter setDateFormat:@"[yyyy-MM-dd HH:mm:ss:SSS]"];
}

- (IBAction)addLog:(id)sender {
    static int i = 0;
    NSLog(@"[test] %@  this is a log with index:%d", [self.formatter stringFromDate:[NSDate date]], i++);
}


- (IBAction)printLogWithReverse:(id)sender {
    NSError *err = nil;
    NSPredicate *preeicate = [NSPredicate predicateWithFormat:@"composedMessage contains %@" argumentArray:@[@"[test]"]];
    OSLogEnumerator *enumer = [self.logStore entriesEnumeratorWithOptions:OSLogEnumeratorReverse position:nil predicate:preeicate error:&err];
    if (err) {
        NSLog(@"enumer error:%@", err);
        NSAssert(0, @"");
    }
    
    OSLogEntryLog *entry = nil;
    while (entry = [enumer nextObject]) {
        NSString *message = [entry composedMessage];
        printf("log: %s\n", message.UTF8String);
    }
}


- (IBAction)printLogWithPosition:(id)sender {
    NSError *err = nil;
    NSPredicate *preeicate = [NSPredicate predicateWithFormat:@"composedMessage contains %@" argumentArray:@[@"[test]"]];
    NSDate *posDate = [NSDate dateWithTimeIntervalSinceNow:-5];
    OSLogPosition *pos = [self.logStore positionWithDate:posDate];
    OSLogEnumerator *enumer = [self.logStore entriesEnumeratorWithOptions:0 position:pos predicate:preeicate error:&err];
    if (err) {
        NSLog(@"enumer error:%@", err);
        NSAssert(0, @"");
    }
    
    const char *now = [self.formatter stringFromDate:[NSDate date]].UTF8String;
    const char *posStart = [self.formatter stringFromDate:posDate].UTF8String;
    OSLogEntryLog *entry = nil;
    while (entry = [enumer nextObject]) {
        NSString *message = [entry composedMessage];
        printf("log(now:%s, pos:%s): %s\n", now, posStart, message.UTF8String);
    }
}

@end

The method of - (IBAction)printLogWithReverse:(id)sender print result not reversed by time.

log: [test] [2025-02-18 17:35:50:175]  this is a log with index:0
log: [test] [2025-02-18 17:35:51:040]  this is a log with index:1
log: [test] [2025-02-18 17:35:51:174]  this is a log with index:2
log: [test] [2025-02-18 17:35:51:323]  this is a log with index:3
log: [test] [2025-02-18 17:35:51:473]  this is a log with index:4
log: [test] [2025-02-18 17:35:51:640]  this is a log with index:5
log: [test] [2025-02-18 17:35:51:773]  this is a log with index:6
log: [test] [2025-02-18 17:35:51:923]  this is a log with index:7

The method of - (IBAction)printLogWithPosition:(id) print result should not contain the log from 5 seconds ago because I set the start time position in the position argument

[test] [2025-02-18 17:43:58:741]  this is a log with index:0
[test] [2025-02-18 17:43:58:940]  this is a log with index:1
[test] [2025-02-18 17:43:59:458]  this is a log with index:2
[test] [2025-02-18 17:43:59:923]  this is a log with index:3
log(now:[2025-02-18 17:44:51:132], pos:[2025-02-18 17:44:46:032]): [test] [2025-02-18 17:43:58:741]  this is a log with index:0
log(now:[2025-02-18 17:44:51:132], pos:[2025-02-18 17:44:46:032]): [test] [2025-02-18 17:43:58:940]  this is a log with index:1
log(now:[2025-02-18 17:44:51:132], pos:[2025-02-18 17:44:46:032]): [test] [2025-02-18 17:43:59:458]  this is a log with index:2
log(now:[2025-02-18 17:44:51:132], pos:[2025-02-18 17:44:46:032]): [test] [2025-02-18 17:43:59:923]  this is a log with index:3
Answered by DTS Engineer in 825488022

These are known issues with the .currentProcessIdentifier scope implementation. I talk about this in Your Friend the System Log (search for “87622922”). Unfortunately, I’m not aware of any workaround, other than the obvious: Get all the events and filter them yourself.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

These are known issues with the .currentProcessIdentifier scope implementation. I talk about this in Your Friend the System Log (search for “87622922”). Unfortunately, I’m not aware of any workaround, other than the obvious: Get all the events and filter them yourself.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The options and position arguments do not work in the 'entriesEnumeratorWithOptions:position:predicate:error:' method of the OSLogStore object.
 
 
Q