Terraria ModLoader  0.11.6.2
A framework for Terraria mods
ErrorLogger.cs
Go to the documentation of this file.
1 using System;
2 using System.Diagnostics;
3 using System.IO;
4 using System.Reflection;
5 using System.Text;
6 
7 namespace Terraria.ModLoader
8 {
13  [Obsolete("This class is deprecated. Use Terraria.ModLoader.Logging instead (see ExampleMod for example)", false)]
14  public static class ErrorLogger
15  {
20  [Obsolete("Please use Terraria.ModLoader.Logging.LogDir instead", false)]
21  public static readonly string LogPath = Logging.LogDir;
22 
23  private static Object logExceptionLock = new Object();
24 
25  private static Object logLock = new Object();
30  [Obsolete("Please use your own ILog instead, see ExampleMod for an example", false)]
31  public static void Log(string message) {
32  string callerName = GetCallerName();
33  Mod callerMod = ModLoader.GetMod(callerName);
34  if (callerMod == null) {
35  Logging.tML.WarnFormat("Tried to forward ErrorLogger.Log for mod {0} but failed (mod not found!)", callerName);
36  }
37  else {
38  callerMod.Logger.Info(message);
39  }
40  }
41 
48  [Obsolete("Please use your own ILog instead, see ExampleMod for an example", false)]
49  public static void Log(object param, bool alternateOutput = false) {
50  string getParamString() {
51  StringBuilder sb = new StringBuilder();
52  sb.AppendLine("Object type: " + param.GetType());
53  foreach (PropertyInfo property in param.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
54  sb.AppendLine("PROPERTY " + property.Name + " = " + property.GetValue(param, null) + "\n");
55  }
56 
57  foreach (FieldInfo field in param.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
58  sb.AppendLine("FIELD " + field.Name + " = " + (field.GetValue(param).ToString() != "" ? field.GetValue(param) : "(Field value not found)") + "\n");
59  }
60 
61  foreach (MethodInfo method in param.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
62  sb.AppendLine("METHOD " + method.Name + "\n");
63  }
64 
65  int temp = 0;
66 
67  foreach (ConstructorInfo constructor in param.GetType().GetConstructors(BindingFlags.Public | BindingFlags.NonPublic)) {
68  temp++;
69  sb.AppendLine("CONSTRUCTOR " + temp + " : " + constructor.Name + "\n");
70  }
71 
72  return sb.ToString();
73  }
74 
75  string callerName = GetCallerName();
76  Mod callerMod = ModLoader.GetMod(callerName);
77  if (callerMod == null) {
78  Logging.tML.WarnFormat("Tried to forward ErrorLogger.Log for mod {0} but failed (mod not found!)", callerName);
79  }
80  else {
81  callerMod.Logger.Info(!alternateOutput ? param.ToString() : getParamString());
82  }
83  }
84 
85  private static string GetCallerName() {
86  StackTrace stackTrace = new StackTrace();
87  Assembly asm = stackTrace.GetFrame(2).GetMethod().DeclaringType.Assembly;
88  string name = asm.GetName().Name;
89  return name.Contains("_") ? name.Split('_')[0] : name;
90  }
91 
96  [Obsolete("Please ue Terraria.ModLoader.Logging instead", false)]
97  public static void ClearLogs() {
98  lock (logLock) {
99  if (!Directory.Exists(Logging.LogDir))
100  Directory.CreateDirectory(Logging.LogDir);
101 
102  string[] files = new string[] {
103  LogPath + Path.DirectorySeparatorChar + "Logs.txt",
104  LogPath + Path.DirectorySeparatorChar + "Network Error.txt",
105  LogPath + Path.DirectorySeparatorChar + "Runtime Error.txt",
106  };
107  foreach (var file in files) {
108  try {
109  File.Delete(file);
110  }
111  catch (Exception) {
112  // Don't worry about it, modder or player might have the file open in tail or notepad.
113  }
114  }
115  }
116  }
117  }
118 }
static void Log(string message)
NOTE: Deprecated. Please use your own ILog instead, see ExampleMod for an example You can use this me...
Definition: ErrorLogger.cs:31
This serves as the central class which loads mods. It contains many static fields and methods related...
Definition: ModLoader.cs:26
static void Log(object param, bool alternateOutput=false)
NOTE: Deprecated. Please use your own ILog instead, see ExampleMod for an example Allows you to log a...
Definition: ErrorLogger.cs:49
ILog Logger
A logger with this mod's name for easy logging.
Definition: Mod.cs:37
NOTE: This class is deprecated. Use Terraria.ModLoader.Logging instead (see ExampleMod for example) T...
Definition: ErrorLogger.cs:14
static string GetCallerName()
Definition: ErrorLogger.cs:85
static readonly string LogDir
Definition: Logging.cs:25
Mod is an abstract class that you will override. It serves as a central place from which the mod's co...
Definition: Mod.cs:24
static Mod GetMod(string name)
Gets the instance of the Mod with the specified name.
Definition: ModLoader.cs:86
static void ClearLogs()
NOTE: Deprecated. Deletes all log files.
Definition: ErrorLogger.cs:97