Get Debug.Log output in the working iOS build
I've got a question.
I have an application that is uploaded to AppStore. And I have a device (iPhone, jailbroken) with this app installed. Is there any way to see what "Debug.Log" is outputting in the build?
I've found only way to see the console through "iPhone Configuration Utility", but there are only some system outputs in that console.
Is there any chance to see something similar to what I see when I build a project directly to my phone and then look at xCode's "all output" ?
Answer by Mozgoid · Dec 05, 2014 at 10:24 AM
You can use log callbacks to save log to file, or send it to your server.
public class LogHandler : MonoBehaviour
{
private StreamWriter _writer;
void Awake()
{
_writer = File.AppendText("log.txt");
_writer.Write("\n\n=============== Game started ================\n\n");
DontDestroyOnLoad(gameObject);
Application.RegisterLogCallback(HandleLog);
}
private void HandleLog(string condition, string stackTrace, LogType type)
{
var logEntry = string.Format("\n {0} {1} \n {2}\n {3}"
, DateTime.Now, type, condition, stackTrace);
MyServer.SendLog(logEntry);
_writer.Write(logEntry);
}
void OnDestroy()
{
_writer.Close();
}
}
Answer by sunshineblocks · Nov 13, 2016 at 10:43 PM
Hey so -- good answer by @Mozgold but -- well, first off let me note that Application.RegisterLogCallback has now been replaced -- any one looking to use @Mozgold's answer should use Application.logMessageReceivedThreaded or Application.logMessageReceived -- see this thread http://answers.unity3d.com/questions/1009660/how-to-use-applicationlogmessagereceived-for-loggi.html
I had the same problem, but preferred instead to use a bridge to pipe Debug.Log messages to NSLog. The downside of this approach is that, in XCode's console, you see the messages twice. The advantage is that, when running a TestFlight'd build, I see the messages live as they happen in XCode's Window / Devices log when the app is running on my selected device.
STEP 1 -- register a callback so that Debug.Log's are piped to method in my iOS plugin
public void OnEnable() {
// Debug.LogWarning("------ ------ Registered log callback----- ---------");
Application.logMessageReceived += IOSPlugin.LogToiOS;
}
public void OnDisable() {
Application.logMessageReceived -= IOSPlugin.LogToiOS;
}
STEP 2 -- implement method in my iOSPlugin to pipe these calls to the native app
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class IOSPlugin : MonoBehaviour {
#if UNITY_IPHONE
[DllImport ("__Internal")]
private static extern void _logToiOS(string debugMessage);
#endif
public static void LogToiOS(string logString, string stackTrace, LogType type) {
// We check for UNITY_IPHONE again so we don't try this if it isn't iOS platform.
#if UNITY_IPHONE
// Now we check that it's actually an iOS device/simulator, not the Unity Player. You only get plugins on the actual device or iOS Simulator.
if (Application.platform == RuntimePlatform.IPhonePlayer) {
_logToiOS(logString + "\n===============\n" + stackTrace);
}
#endif
}
}
STEP 3 -- implement a method in my iOS app's AppController.mm to pipe this to NSLog -- you should be able to just directly drop this in your AppController.mm and be all set
extern "C" {
void _logToiOS(const char* debugMessage) {
NSLog(@"Received _logToiOS %@", [NSString stringWithUTF8String:debugMessage]);
}
}
Sweet solution, thanks! In my case step 3 had to go in UnityAppController.mm
not jailbreak : you can use mac to view log(https://github.com/libimobiledevice/libimobiledevice) jailbreak : install oslog in cydia(cydia.saurik.com/package/net.limneos.oslog),and use ssh to connect your phone
Answer by unimechanic · Nov 22, 2013 at 03:10 PM
First build your iOS app with "Development Build" enabled. In Unity 4, Debug.Log() output is shown in the XCode's debug Console. Notice that this is NOT the Console in the Organizer, which shows the device events, but it's the Console that is shown at the bottom of the main window, which is called Debug area. The Organizer's Device Logs is useful to obtain the crash logs when the app is executing out of XCode.
I think the OP knows that.... the question is how to make messages pop up in the XCode / Devices console to, for example, debug an issue that only shows in a TestFlight'd build (or some other build that you can't attach the standard debug console to).
Answer by yashesh · Mar 14, 2013 at 11:32 AM
You can check that with another option. Connect you phone to your MAC which have xCode install. Now open xCode and from xCode open Organizer. From Organizer select **Devices*tab and in that you can see you device in that. And from your selected device select Console* in that you can see the device log.
No, that's helpless, sorry. This console is the same console you can see in iPhone Configuration Utility. And there are only system messages, but no my Debug.Log outputs :(
Answer by Deepscorn · Apr 08, 2020 at 09:01 PM
You can redirect NSLog (and so, what you write with Debug.Log, will also be redirected) to file adding something like that to your native ios code:
- (void)redirectLogToDocument
{
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"yourFile.txt"];
NSLog(@"redirectLogToDocument: %@", pathForLog);
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stdout);
}
// usage:
if (!isatty(STDERR_FILENO)) { // if debugger not attached
[self redirectLogToDocument]; // redirect logs
}
Then you can take your file using Window -> Devices & Simulators -> select your device -> select your app -> press settings button below -> select "download container" from dropdown. xcappdata file will be saved. Reveil in finder, click right mouse button, click "show package contents", then go to Documents folder. There will be your file.
Screenshot better describes that: https://stackoverflow.com/a/6121649/884195
Based on: https://stackoverflow.com/a/8379047/884195 https://stackoverflow.com/a/9620429/884195
Your answer
Follow this Question
Related Questions
Does anyone have this Problem? - iOS build crashes on Unity 2020.1 1 Answer
How to prepare your scene to build and run? 1 Answer
Launch Xcode build from iOS application project. 0 Answers
BuildPlayer for iOS - progress bar never closes 1 Answer
IOS Unity Cloud build failed : Invalid bitcode version 1 Answer