Can you redirect log output of Xcode console to Terminal when running iPhone Simulator?
I don't like using Xcode's console output window when debugging an iPhone app in the Simulator (or on a device for that matter). I'd like to be able to use the Unix toolbox and开发者_高级运维 do things like filter the logging output with grep. But to do this I need to get Xcode to send the logging output for the running iPhone app to the Terminal.
Is there any way to accomplish this?
Couldn't say how it'd work in the simulator, but redirecting stdout is not terribly difficult. Say you wanted to pipe it into your own view:
#include <unistd.h>
stderr->_write = RedirectOutputToView;
stdout->_write = RedirectOutputToView;
And use the prototype:
int RedirectOutputToView(void *inFD, const char *buffer, int size);
In XCode 6, the logs are pipped here: /Users/<username>/Library/Logs/CoreSimulator/<app guid>/system.log
. username is of course your system username, and app guid is something that the XCode generates for the simulator.
The easiest way to determine what the app guid is, is to build your app for the simulator and see which directory was last changed:
~ ❯❯❯ cd ~/Library/Logs/CoreSimulator
~/L/L/CoreSimulator ❯❯❯ ls -latr
total 64
-rw-r--r-- 1 x staff 519 27 Aug 21:54 iOS Simulator.log
drwxr-xr-x 13 x staff 442 27 Aug 21:54 D283605A-0BA9-43B3-AB6B-F4858BE6E45E
drwxr-xr-x 15 x staff 510 8 Oct 03:56 425D8E41-0842-4F2D-BC22-8C3695E350EF
Clearly 425D8E41-0842-4F2D-BC22-8C3695E350EF is the last modified directory, so now you can do tail -f ~/Library/Logs/CoreSimulator/425D8E41-0842-4F2D-BC22-8C3695E350EF/system.log | grep keyword
or whatever else you might want to do with the live log.
I found the this answer to work well for my purpose, albeit it requires running the simulator in order to stream it live in a terminal:
I removed the simulator check and added an NSLog statement just before the redirect so the XCode console spits out the location of the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
NSLog(@"redirecting STDERR: %@", logPath);
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
I think the most robust solution would be to use a logging library (e.g. CocoaLumberjack ) and configure it with whatever destination you like.
I can get the device output in a terminal but haven't been able to connect a debug session in xcode simultaneously (I guess because the terminal has a debug connection).
Start the app from a terminal using idevicedebug.
idevicedebug -u <device uuid> run com.myco.apps.myapp
Where com.myco.apps.myapp is the bundle id which can be seen in the 'installed apps' pane in the devices window.
I think idevicedebug is installed with ideviceinstaller and can be done with brew
brew install ideviceinstaller
But I recommend searching for ideviceinstaller and libimobiledevice (the library that this uses) just to be sure.
It's a small annoyance but the best way I've found to get the program output in a terminal.
Why not use Console.app
? A nice way to view and grep logs, instead of xCode's debug window or terminal. This is part of macOS by default.
If you have C printf statements then the following SO has lots of useful info: Get printf on Console.app
In Xcode 6 it already does this. Open the System.log file associated with the simulators build you are currently using. For me it is here: /Users/josh.lang/Library/Logs/CoreSimulator/3BB4CBFB-5A67-4E93-91B5-78E6E3658A16/system.log
It will keep it all, but I noticed if I have it open while my sim is running it also reloads on its own.
精彩评论