This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Logger.cs
93 lines (84 loc) · 2.75 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.IO;
using System.Text;
using UnityEngine;
namespace Oxide
{
/// <summary>
/// Utility class that assists logging messages
/// </summary>
public static class Logger
{
/// <summary>
/// Gets the filename for today's log
/// </summary>
private static string Logfile
{
get
{
string filename = "logs/oxide_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt";
return Main.GetPath(filename);
}
}
/// <summary>
/// Writes the specified message to the log file
/// </summary>
/// <param name="message"></param>
private static void WriteToLogfile(string message)
{
string toappend = string.Format("{0}: {1}{2}", DateTime.Now.ToShortTimeString(), message, Environment.NewLine);
File.AppendAllText(Logfile, toappend);
}
/// <summary>
/// Logs the specified message to console and file
/// </summary>
/// <param name="message"></param>
public static void Message(string message)
{
Debug.Log(string.Format("[Oxide] {0}", message));
WriteToLogfile(message);
}
/// <summary>
/// Logs the specified error to console and file
/// </summary>
/// <param name="message"></param>
public static void Error(string message)
{
Debug.LogError(string.Format("[Oxide] {0}", message));
WriteToLogfile(string.Format("ERROR: {0}", message));
}
/// <summary>
/// Converts an exception to a string
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
private static string ParseError(Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(ex.Source + ": " + ex.ToString());
sb.AppendLine(ex.StackTrace);
if (ex.InnerException != null) sb.Append(ParseError(ex.InnerException));
return sb.ToString();
}
/// <summary>
/// Logs the specified error to console and file
/// </summary>
/// <param name="ex"></param>
public static void Error(Exception ex)
{
Error(ParseError(ex));
}
/// <summary>
/// Logs the specified error to console and file
/// </summary>
/// <param name="message"></param>
/// <param name="ex"></param>
public static void Error(string message, Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(message);
sb.AppendLine(ParseError(ex));
Error(sb.ToString());
}
}
}