运行 iPhone 模拟器时,您可以将 Xcode 控制台的日志输出重定向到终端吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1910177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you redirect log output of Xcode console to Terminal when running iPhone Simulator?
提问by dan
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.
在模拟器中(或在设备上)调试 iPhone 应用程序时,我不喜欢使用 Xcode 的控制台输出窗口。我希望能够使用 Unix 工具箱并执行诸如使用 grep 过滤日志输出之类的操作。但要做到这一点,我需要让 Xcode 将正在运行的 iPhone 应用程序的日志输出发送到终端。
Is there any way to accomplish this?
有什么办法可以做到这一点吗?
采纳答案by Azeem.Butt
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);
回答by EightyEight
In XCode 6, the logs are pipped here: /Users/<username>/Library/Logs/CoreSimulator/<app guid>/system.log
. usernameis of course your system username, and app guidis something that the XCode generates for the simulator.
在 XCode 6 中,日志在此处:/Users/<username>/Library/Logs/CoreSimulator/<app guid>/system.log
. username当然是你的系统用户名,app guid是 XCode 为模拟器生成的东西。
The easiest way to determine what the app guidis, is to build your app for the simulator and see which directory was last changed:
确定应用程序 guid的最简单方法是为模拟器构建应用程序并查看上次更改的目录:
~ ??? 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-8C3695E350EFis 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.
显然425D8E41-0842-4F2D-BC22-8C3695E350EF是最后修改的目录,因此现在您可以tail -f ~/Library/Logs/CoreSimulator/425D8E41-0842-4F2D-BC22-8C3695E350EF/system.log | grep keyword
对实时日志执行或执行任何其他操作。
回答by joxl
I found the this answerto 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:
我删除了模拟器检查并在重定向之前添加了一个 NSLog 语句,以便 XCode 控制台吐出文件的位置:
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. CocoaLumberHyman ) and configure it with whatever destination you like.
我认为最强大的解决方案是使用日志库(例如CocoaLumberHyman )并将其配置为您喜欢的任何目的地。
回答by dassimon
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).
我可以在终端中获得设备输出,但无法同时在 xcode 中连接调试会话(我猜是因为终端有调试连接)。
Start the app from a terminal using idevicedebug.
使用 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.
其中 com.myco.apps.myapp 是捆绑 ID,可以在设备窗口的“已安装应用”窗格中看到。
I thinkidevicedebug is installed with ideviceinstaller and can be done with brew
我认为idevicedebug 是用 ideviceinstaller 安装的,可以用 brew 来完成
brew install ideviceinstaller
But I recommend searching for ideviceinstaller and libimobiledevice (the library that this uses) just to be sure.
但我建议搜索 ideviceinstaller 和 libimobiledevice(它使用的库)只是为了确定。
It's a small annoyance but the best way I've found to get the program output in a terminal.
这是一个小烦恼,但我发现在终端中获得程序输出的最佳方式。
回答by rustyMagnet
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.
为什么不使用Console.app
?查看和 grep 日志的好方法,而不是 xCode 的调试窗口或终端。默认情况下,这是 macOS 的一部分。
If you have C printf statements then the following SO has lots of useful info: Get printf on Console.app
如果你有 C printf 语句,那么下面的 SO 有很多有用的信息: 在 Console.app 上获取 printf
回答by Joshua Lang
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
在 Xcode 6 中它已经做到了。打开与您当前使用的模拟器构建关联的 System.log 文件。对我来说,它在这里:/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.
它将保留所有内容,但我注意到如果在我的 SIM 卡运行时打开它,它也会自行重新加载。