Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to Debug Notificaction Functionalilty

How to Debug Notificaction Functionalilty
Thread Tools
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 1, 2003, 10:09 AM
 
Is there any way to inspect the default NSNotificationCenter as to see who is registred and for what. Also, I would like to see what notifications are on queue.

Ideally, I would put a breakpoint in my code and inspect the contents NSNotificationCenter and its queue at that point.

Is this possible? If not then, what are good ways to debug notification functionality?

Thanks!
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 1, 2003, 03:38 PM
 
What you want to do is not easily possible, as far as I know. NSNotificationCenter doesn't have any apparent interface to its list of observers (except just to count them).

But why do you want to try to debug Foundation? Unless you're working for Apple, you can't fix any bugs you find anyway. Looking to reverse-engineer NSNotificationCenter?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 1, 2003, 04:23 PM
 
Originally posted by Chuckit:
But why do you want to try to debug Foundation? Unless you're working for Apple, you can't fix any bugs you find anyway. Looking to reverse-engineer NSNotificationCenter?
Thanks for posting. And I am way to green to dig that deep... I am sure it works as advertized. Here's what I'm up to.

I have been working on an app that detaches a thread and within that thread it a launches NSTask to go and try to "tail -f" a log file asynchronously. I'm using notifications to communicate between the embedded NSTask and other parts of my app.

At this point I know that the necessary objects are not being prematurely deallocated so my next step was to see if the notifications were working or hanging up somewhere. I strongly believe that's where the it's hanging up since the callback method is not being triggered.

So there's, no easy way to look into the notification center's data ah? Oh well, gonna have to try some other things.

     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 1, 2003, 04:47 PM
 
It sounds like what you want are distributed notifications.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Senior User
Join Date: Dec 2001
Status: Offline
Reply With Quote
Jun 1, 2003, 08:10 PM
 
Why are using a thread and launching another process to monitor a file for changes? Why not just have a timer fire every 0.5 seconds that checks the file's size? Just store the current file size and if the size changes, just read from currentFileSize+1 to EOF?
"Think Different. Like The Rest Of Us."

iBook G4/1.2GHz | 1.25GB | 60GB | Mac OS X 10.4.2
Athlon XP 2500+/1.83GHz | 1GB PC3200 | 120GB | Windows XP
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 1, 2003, 08:21 PM
 
Originally posted by Chuckit:
It sounds like what you want are distributed notifications.
Uhm.. Interesting. I will explore those.

Thanks for the tip.


     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 1, 2003, 08:38 PM
 
Originally posted by macmike42:
Why are using a thread and launching another process to monitor a file for changes? Why not just have a timer fire every 0.5 seconds that checks the file's size? Just store the current file size and if the size changes, just read from currentFileSize+1 to EOF?
That didn't cross my mind, thanks. You see I'm in a fairly idealistic phase (no time pressure to finish) and hoping for a snappy UI. Those two factors steared me towards an additional thread. However, I see your point. Maybe I can find a timer setting that would still allow for a responsive UI.

Thanks!

     
Senior User
Join Date: Dec 2001
Status: Offline
Reply With Quote
Jun 1, 2003, 11:28 PM
 
Originally posted by DaGuy:
That didn't cross my mind, thanks. You see I'm in a fairly idealistic phase (no time pressure to finish) and hoping for a snappy UI. Those two factors steared me towards an additional thread. However, I see your point. Maybe I can find a timer setting that would still allow for a responsive UI.

Thanks!

A timer firing every 0.5 seconds that just stat()'s the file to check the size will use barely a millisecond of CPU time on even the slowest machine. The overhead of a separate process and extra thread will have a much more adverse effect on the system.
"Think Different. Like The Rest Of Us."

iBook G4/1.2GHz | 1.25GB | 60GB | Mac OS X 10.4.2
Athlon XP 2500+/1.83GHz | 1GB PC3200 | 120GB | Windows XP
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 2, 2003, 01:16 AM
 
Originally posted by macmike42:
A timer firing every 0.5 seconds that just stat()'s the file to check the size will use barely a millisecond of CPU time on even the slowest machine. The overhead of a separate process and extra thread will have a much more adverse effect on the system.
I was just thinking about that. I agree with you. This is a neat technique, thanks very much.



     
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Jun 2, 2003, 06:22 PM
 
For the record, you can also use NSTask and tail without a timer and without a separate thread. What you want to do is to create a pipe that will send out a notification whenever the tail process outputs data to stdout. Then you simply observe that notification and process the data as it is spit out by tail. Here is a code snippet (I haven't checked this to see if it compiles, but it should demonstrate the method OK):

Code:
- (void)startTailProcess { NSString* tailPath = @"/usr/bin/tail"; NSTask* tailTask = [[NSTask alloc] init]; NSPipe* dataPipe; NSFileHandle* outputFileHandle; dataPipe = [[NSPipe alloc] init]; outputFileHandle = [dataPipe fileHandleForReading]; [outputFileHandle readInBackgroundAndNotify]; [tailTask setStandardOutput:dataPipe]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tailDidOutputData:) name:NSFileHandleReadCompletionNotification object:outputFileHandle]; [tailTask setLaunchPath:tailPath]; [tailTask setArguments:[NSArray arrayWithObjects:@"-f", @"/var/tmp/console.log", nil]]; [tailTask launch]; [tailTask release]; [dataPipe release]; } - (void)tailDidOutputData:(NSNotification *)notification { NSData* data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]; if ([data length] > 0) { //process the data [[notification object] readInBackgroundAndNotify]; } else { //the process has closed and is no longer outputting data } }
So basically what happens is you launch the task, and what puts out to stdout gets piped to the file handle, which you observe for notifications when it has read data. Each time you get a chunk of data, you must tell the file handle again to read data in the background and notify you when it has more. If the notifications gets called with a 0 length data object, this means that there is no more data, which in this case would only happen when the tail process quit. It all gets done in the background in the main thread, without the need for polling with a timer.
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 3, 2003, 01:56 AM
 
see below -I made a boo boo while editing.
(Last edited by DaGuy; Jun 3, 2003 at 02:10 AM. )
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 3, 2003, 02:09 AM
 
Thanks very much for the snipplet. I had something similar working at the beginning of my project but then I added additional asynchronous NSTasks (also using pipes) to display stdout data in a different NSTextView. Well, things got really messy...

Juggling these notifications and routing the associated outputs to the correct NSTextViews became a royal pain. The approach just didn't scale too well (at N=3, bummer); hence, I decided to rethink my approach. I blame the sloppy mess on trying to run everything of the main thread and so I decided to use detach a thread solely dedicated to tailing a log file while the main thread would handle all the rest -which includes heavy UI usage.

The folks on this forum gave me some new insights and I'm currently still hammering to get a reasonably clean solution. But, more than anything, what I'm really interested in is improving my meager Cocoa ability. So far this project has proven quite rich in obstacles.



In any event, I'm not discarding using notifications.

Thanks again!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:38 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2