Game Development Reference
In-Depth Information
38 //------------------------------------------------
39 //Log exception to a text file
40 void HandleLog(string logString, string stackTrace,
LogType type)
41 {
42 //If an exception or error, then log to file
43 if(type == LogType.Exception || type == LogType.Error)
44 {
45
SW.WriteLine
("Logged at: " +
System.DateTime.Now.ToString() +
" - Log Desc: " + logString + " -
Trace: " + stackTrace + " - Type: " +
type.ToString());
46 }
47 }
48 //------------------------------------------------
49 //Called when object is destroyed
50 void OnDestroy()
51 {
52 //Close file
53 SW.Close();
54 }
55 //------------------------------------------------
56 }
57 //------------------------------------------------
The following are the comments for code sample 2-7:
•
Line 22
: A new
StreamWriter
object is created to write debug strings
to a file on the computer. The file is created inside
Application.
persistentDataPath
; this points to a system location that is always writable.
•
Line 30
: The
Application.RegisterLogCallBack
method is invoked with a
function reference to
HandleLog
as an argument. This relies on delegates. In
short, a reference to the
HandleLog
function is passed, and this will be called
when an error or exception occurs that allows us to write details to a logfile.