some idiot
some idiot

Reputation: 191

How do I render debug.log messages before crashing unity

When unity crashes debug.log s from the previous event don't render. Which happens to be the very event that crashed unity (probably via a loop)

So far, I've found Application.Quit(), but obviously, I can only use that if I know precisely when unity would crash (for example for finding out which of some (non nested) loops crashes)

(I also found a lot of info that didn't actually do anything, like opening the console window before starting the game. not listing all of that though)

I guess this example code should describe the problem well enough:

while (condition) {
  DoSomething(); //contains a loop too
  Debug.Log (value);
  Application.Quit ();
}

obviously, only the first iteration happens. But the inner loop usually won't crash for the first few times. So I'd like to log some values every time the inner loop terminates.

Upvotes: 0

Views: 1702

Answers (3)

Enz
Enz

Reputation: 185

You can add a listener to log message's event (Application.logMessageReceived) in unity and store it in a file or send it via the network to prevent losing them.

void Awake() 
{
    DontDestroyOnLoad(this);
    Application.logMessageReceived += HandleLog;
}

void OnApplicationQuit()
{
    Application.logMessageReceived -= HandleLog;
}

void HandleLog(string logString, string stackTrace, LogType type) 
{
    File.AppendAllText ("D:/Log.txt" /*Path to your log folder*/, logString + stackTrace);
}

Upvotes: 2

Muhammad Farhan Aqeel
Muhammad Farhan Aqeel

Reputation: 717

You can use Adb logs in cmd.

If you're using Unity with android, you must have already installed adb through Android SDK. Given that, yeah, it should be simple and obvious. And Ben is right. You didn't seem to google enough or take any effort here... But I just hate simple unanswered questions.

There are just a couple things to assure:

Make sure USB debugging is enabled on the Android device (check under Settings -> Development). - Run adb through the command prompt (with command line argument logcat) that comes with the Android SDK while running the game on the Android, still connected via USB to the computer.

 adb logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG

Finally, if the Android Debug Bridge can't help you, I'm not sure what could. There was this one time, actually, it couldn't help me... But I was ignoring a unity warning, so I did the debugging checklist in the wrong order.

Source of the answer

Upvotes: 1

Immorality
Immorality

Reputation: 2174

You could surround the block in question with a try catch. If it throws an error, you can log it in the catch block.

try 
{

    while (condition) {
      DoSomething(); //contains a loop too
      Debug.Log (value);
    }
}
catch
{
    Debug.Log(value);
}

Upvotes: 0

Related Questions