Logging NSNotifications
Asked Answered
C

2

5

I would like to log any NSNotifications posted by a single NSNotificationCenter shared accross my application. I have tried subclassing NSNotificationCenter with the intention of adding logging code to the three post methods, but it returns an instance of CFNotification center instead of my subclass.

Surely there is a way of monitoring NSNotification posting?

EDIT/UPDATE

As two answers below correctly point out I could listen to all notifications and log them in a handler, but the sequence the handler would receive these notifications is far from guaranteed to be the same as the sequence in which they were dispatched. If I could be sure the handler would always be the first hander to be notified this would work, but I cannot: 'The order in which observers receive notifications is undefined' From NSNotification Docs

Crosswind answered 17/4, 2012 at 15:7 Comment(0)
E
10

By using - addObserver:selector:name:object: and passing nil for both the name and the object, you will get notified about any notification.

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(log:) name:nil object:nil];
    }
    return self;
}

- (void)log:(NSNotification *)notification
{
    NSLog(@"%@", notification);
}

Edit: if you want to get the real order of the notifications being send, try subclassing NSNotificationCenter and overriding the following methods:

– postNotification:
– postNotificationName:object:
– postNotificationName:object:userInfo:

If subclassing is no option for you, you might consider defining a category on NSNotificationCenter where you override these methods with calling the super implementation. (you will need to swizzle methods to call super within a category). Tell me if you need help doing so.

Essary answered 17/4, 2012 at 15:51 Comment(4)
Thanks. But the problem there is that I can't log pre-send. I am left to log notifications after they are dispatched as an observer at an arbitrary position in the sequence of observers being notified. If I can log at point of dispatch I get a much clearer idea of notification sequence. This kind of global listener might not be notified until a previous handler has triggered a sequence of events, meaning I would be unable to see the order of dispatch, only the order this particular observer received them.Crosswind
Thanks. Problem is that when you alloc/init an NSNotificationCenter you don't get an NSNotification center back. You get a CFNotificationCenter. The same is true of a subclass of NSNotificationCenter. So surely adding a category to NSNotificationCenter will not help?Crosswind
Just tested it, subclassing works just fine. You indeed get a CFNotificationCenter back, but it still works. Many NS types are internally represented by their corresponding CoreFoundation types.Essary
So it does. Must be losing my mind. Thanks for setting me right.Crosswind
C
0

You should be able to use [addObserver:self selector:@selector(whatever:) name:nil object:nil] and just put your logging code in the whatever: method. This observer should get all notifications posted by your app (at least all those posted by the default center).

Corrigan answered 17/4, 2012 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.