Loggers don't flush in Command Line Tool #63

Closed
JaSpa opened this Issue Oct 2, 2012 · 10 comments

Comments

Projects
None yet
4 participants

JaSpa commented Oct 2, 2012

I use CocoaLumberjack in an Command Line Tool using Xcode 4.5. In code I add a tty and an asl logger. I do some logging with DDLogCInfo(). The documentation says the loggers flush when the application terminates but they don't – maybe sometimes. So I added the following code in main() after I add the loggers and it worked:

atexit_b(^{ @autoreleasepool {
    [DDLog flushLog];
}})

I removed it again and there was only one logged line – the first one, even if I called the program from command line. I looked a bit in your code and set an breakpoint in +[DDLog applicationWillTerminate:] -- and it wasn't called!

Member

bpoplauschi commented Nov 1, 2013

@JaSpa it this still an issue?

JaSpa commented Nov 1, 2013

@bpoplauschi Yes, it is. I just tried. There are the following cases:

  • No explicit flushing
    • Inside Xcode: no output
    • From Terminal: sometimes output
  • Explicit flushing (via [DDLog flushLog])
    • Always output

+[DDLog applicationWillTerminate:] still won't get called.

Maybe worth mentioning—I tried using the newest version from CocoaPods (CocoaLumberjack v1.6.2)

Member

bpoplauschi commented Nov 1, 2013

@JaSpa thanks, we will look into this asap

Member

rivera-ernesto commented Nov 5, 2013

Probably related to #153

Member

bpoplauschi commented Dec 2, 2013

@JaSpa the issue here is the flushing mechanism is built using NSApplicationWillTerminateNotification. In the case of Command Line Tool, there is no NSApplication so I assume that is the reason the notification is not fired

Member

bpoplauschi commented Dec 2, 2013

We could try to replace the notification with atexit_b, but I'm not sure what's the impact on the other mac apps. Any comments?

JaSpa commented Dec 2, 2013

@bpoplauschi Maybe it would work the following way:

  • check if NSApp equals nil.
    • if it does use atexit_b()
    • otherwise go the “notification way”?
Member

dvor commented Dec 2, 2013

@bpoplauschi @JaSpa We can use NSAppKitVersionNumber to see if AppKit is available or if it's command line tool. I'll commit some code for review.

Member

dvor commented Dec 2, 2013

No, I was wrong, NSAppKitVersionNumber doesn't help. It seems that checking NSApp is the only way

@dvor dvor added a commit to dvor/CocoaLumberjack that referenced this issue Dec 2, 2013

@dvor dvor Issue #63. If running Command Line Tool app terminateNotification isn…
…'t called, so use atexit_b instead
4475551
Member

dvor commented Dec 2, 2013

Created pull request #184

@rivera-ernesto rivera-ernesto added a commit that referenced this issue Dec 3, 2013

@rivera-ernesto rivera-ernesto Merge pull request #184 from dvor/issue63--flush-loggers-in-command-l…
…ine-tool-app

Fixing issue #63.
4104f99

@dvor dvor added a commit to dvor/CocoaLumberjack that referenced this issue Feb 28, 2014

@dvor dvor DDLog: calling atexit_b in CLI applications, that use Foundation fram…
…ework.

Related to #63, #184. Fixes #233.
b0fc0c6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment