Skip to content

Instantly share code, notes, and snippets.

@fwal
Created November 20, 2013 07:57
Show Gist options
  • Save fwal/7559379 to your computer and use it in GitHub Desktop.
Save fwal/7559379 to your computer and use it in GitHub Desktop.
A way of logging to the iOS console from Unity3D
#include <stdio.h>
#include <stdlib.h>
#include <asl.h>
static aslclient client;
void CloseLogToConsole()
{
asl_close(client);
}
void OpenLogToConsole()
{
client = asl_open(/*ident*/ NULL, /*facility*/ "MyApp" /*or other reasonable facility string*/, ASL_OPT_STDERR);
atexit(CloseLogToConsole);
}
void LogToConsoleDebug(char* message)
{
asl_log(client, /*msg*/ NULL, ASL_LEVEL_WARNING, "%s", message);
}
void LogToConsoleError(char* message)
{
asl_log(client, /*msg*/ NULL, ASL_LEVEL_ERR, "%s", message);
}
void LogToConsoleException(char* message)
{
asl_log(client, /*msg*/ NULL, ASL_LEVEL_CRIT, "%s", message);
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;
public class DebugFunctions
{
[DllImport("__Internal")]
private static extern void OpenLogToConsole();
[DllImport("__Internal")]
private static extern void LogToConsoleDebug( string message );
[DllImport("__Internal")]
private static extern void LogToConsoleError( string message );
[DllImport("__Internal")]
private static extern void LogToConsoleException( string message );
class TextureInfo
{
public TextureInfo(Texture2D textureIn, int memoryIn)
{
texture = textureIn;
memory = memoryIn;
}
public Texture2D texture;
public int memory;
}
public static void ShowLog(string error)
{
if (!Debug.isDebugBuild && !Application.isEditor)
return;
GameObject errorLog = GameObject.Find("ErrorLog");
if (errorLog == null)
return;
UILabel label = errorLog.GetComponent<UILabel>();
if (label == null)
return;
if (error == null || error == "")
{
label.text = "";
label.enabled = false;
}
else
{
label.enabled = true;
label.text = error;
}
}
#if !UNITY_EDITOR
static void HandleLog(string logString, string stackTrace, LogType type)
{
string timeNow = System.DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
if (type == LogType.Exception)
{
ShowLog(logString);
LogToConsoleException( String.Format("[{0}] EXCEPTION. {1} <> {2}\n", timeNow, logString, stackTrace) );
}
else if (type == LogType.Error)
{
ShowLog(logString);
LogToConsoleError( String.Format("[{0}] {1} ERROR. <> {2}\n", timeNow, logString, stackTrace) );
}
else if (type == LogType.Warning)
{
if (debugBuild)
{
LogToConsoleDebug( String.Format("[{0}] WARNING. {1} <> {2}\n", timeNow, logString, stackTrace) );
}
}
else
{
if (debugBuild)
{
LogToConsoleDebug( String.Format("[{0}] MISC. {1} <> {2}\n", timeNow, logString, stackTrace) );
}
}
}
#endif
#if UNITY_EDITOR
static bool debugBuild = true;
#else
static bool debugBuild = false;
#endif
public static void DebugLogInit()
{
#if !UNITY_EDITOR
OpenLogToConsole();
Application.RegisterLogCallback(HandleLog);
#endif
if (debugBuild == false)
{
debugBuild = Debug.isDebugBuild;
}
}
public static void DebugLog(string s)
{
if (debugBuild)
{
Debug.Log(s);
}
}
public static void OutputDebugInfo()
{
UnityEngine.Object[] textures = Resources.FindObjectsOfTypeAll(typeof(Texture2D));
List<TextureInfo> textureInfo = new List<TextureInfo>(textures.Length);
int totalSize = 0;
for (int i = 0; i < textures.Length; ++i)
{
Texture2D texture = (Texture2D)textures[i];
int memory = texture.width * texture.height;
switch (texture.format)
{
case TextureFormat.ARGB32:
case TextureFormat.RGBA32:
memory *= 4;
break;
case TextureFormat.RGB565:
case TextureFormat.ARGB4444:
memory *= 2;
break;
case TextureFormat.PVRTC_RGB2:
case TextureFormat.PVRTC_RGBA2:
memory /= 4;
break;
case TextureFormat.PVRTC_RGB4:
case TextureFormat.PVRTC_RGBA4:
memory /= 2;
break;
case TextureFormat.RGB24:
memory *= 3;
break;
case TextureFormat.Alpha8:
break;
}
if (texture.mipmapCount > 1)
memory += memory / 3;
totalSize += memory;
textureInfo.Add( new TextureInfo(texture, memory) );
}
textureInfo.Sort(delegate(TextureInfo a, TextureInfo b) { return b.memory.CompareTo(a.memory); } );
System.Console.WriteLine("Textures:");
foreach(TextureInfo ti in textureInfo)
{
System.Console.Write((((int)(ti.memory/1024)).ToString()+"KB ").PadLeft(8));
System.Console.Write(ti.texture.name.PadRight(60));
System.Console.Write((ti.texture.width + "x" + ti.texture.height + " ").ToString().PadLeft(12));
System.Console.Write(("mips:" + ti.texture.mipmapCount).ToString().PadRight(10));
System.Console.WriteLine(("format:" + ti.texture.format.ToString()).ToString().PadRight(10));
}
textureInfo.Clear();
System.Console.WriteLine("[[ " + totalSize/1024 + "KB Total Size ]]");
UnityEngine.Object[] objects = Resources.FindObjectsOfTypeAll(typeof(GameObject));
System.Console.WriteLine("[[ " + objects.Length + " :: GameObjects ]]");
System.Console.WriteLine("[[ " + System.GC.GetTotalMemory(false)/1024 + "KB :: TotalMemory ]]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment