1

There's an app I've started working on, that regularly logs to the console a lot of stuff, and it's really not convenient to use the console for additional debug logs. I don't want to erase these logs, because there are a few people that are maintaining these logs, and it might be important to them.

So I actually need to to write my debug stuff to a different place. one option is to write it to a file and watch it in Terminal using tail command, but an iOS app can only write inside its folder which, when using a simulator, is always changing each time I run the app. and I don't want to change the path int the tail command every time - I want a fast process.

Does anyone have an idea for such external log place that I can use easily?

Nimrod Yizhar
  • 381
  • 4
  • 14
  • Possible duplicate of [Logging Framework for iOS?](https://stackoverflow.com/questions/3130614/logging-framework-for-ios) – Tamás Sengel Apr 04 '18 at 14:49
  • 1
    Not a duplicate. Logging frameworks don't cover how to do this, and that SO question is closed anyhow. – Smartcat Apr 04 '18 at 16:42

1 Answers1

1

Here's how to make it easier to find and tail your log file when running in Simulator (I use this myself):

1) Add the following code to your home directory's .bashrc then log out and back in again.

xcodelog()
{
    find . -name xcode.log -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "
}

2) Start your app in Xcode's simulator, such that at least something gets logged to your file. Oh, and the file your app is logging to needs to be named "xcode.log" unless you want to change the filename in the above code.

3) Open terminal and switch to your ~/Library/Developer/CoreSimulator directory. Then perform the following command (it displays the last 100 lines of it along with anything new you dump to it).

tail -n 100 -f $(xcodelog)

So the above command hunts for that file among all your simulator devices and their apps, hunting down the most recent "xcode.log" file you've written to (among all apps and devices in entire CoreSimulator subdirectory system).

To clear the most recent xcode.log file, you can do this command:

cat /dev/null > $(xcodelog)

I switched to this approach for all my logging when Xcode 8 lost support for plugins, along with the very fine XcodeColors plugin that would do ANSI color logging into Xcode's console. So I changed my log system to output colors that terminal would support when doing a tail of a file. So I can spot errors in red, warnings in orange, user step logging in yellow, and various degrees of important other info in progressive shades of gray. :)

Smartcat
  • 2,834
  • 1
  • 13
  • 25
  • That's a great solution! But I get this error message when I use the tail command: find: ./Library/Saved Application State/com.bitrock.appinstaller.savedState: Permission denied. after this message, I see the content of the log file. – Nimrod Yizhar Apr 08 '18 at 11:56
  • No idea. Sounds like some kind of conflict with something else you have set up. Try on another Mac as a test? – Smartcat Apr 08 '18 at 22:29
  • Ok, that doesn't matter. the bigger problem is that it takes some time for the file to be found. sometimes I need to wait 20 seconds. any Idea how to make it faster? – Nimrod Yizhar Apr 09 '18 at 15:22
  • Yes, clear out your devices or if you keep some, reduce the number of apps (reset simulator). You probably have a lot of devices for several versions of iOS/Xcode. I periodically trim them so this script doesn’t struggle. – Smartcat Apr 09 '18 at 15:24