A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace GamecraftModdingAPI.Utility
  9. {
  10. /// <summary>
  11. /// Utility class to access Gamecraft's built-in logging capabilities.
  12. /// The log is saved to %APPDATA%\..\LocalLow\FreeJam\Gamecraft\Player.Log
  13. /// </summary>
  14. public static class Logging
  15. {
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. public static void Log(string msg)
  18. {
  19. Svelto.Console.Log(msg);
  20. }
  21. /// <summary>
  22. /// Write a regular message to Gamecraft's log
  23. /// </summary>
  24. /// <param name="obj">The object to log</param>
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public static void Log(object obj)
  27. {
  28. Svelto.Console.Log(obj.ToString());
  29. }
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public static void LogDebug(string msg)
  32. {
  33. Svelto.Console.LogDebug(msg);
  34. }
  35. /// <summary>
  36. /// Write a debug message to Gamecraft's log
  37. /// </summary>
  38. /// <param name="obj">The object to log</param>
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static void LogDebug(object obj)
  41. {
  42. Svelto.Console.LogDebug(obj.ToString());
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public static void LogDebug<T>(string msg, T extraDebug)
  46. {
  47. Svelto.Console.LogDebug<T>(msg, extraDebug);
  48. }
  49. /// <summary>
  50. /// Write a debug message and object to Gamecraft's log
  51. /// The reason this method exists in Svelto.Console is beyond my understanding
  52. /// </summary>
  53. /// <typeparam name="T">The type of the extra debug object</typeparam>
  54. /// <param name="obj">The object to log</param>
  55. /// <param name="extraDebug">The extra object to log</param>
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public static void LogDebug<T>(object obj, T extraDebug)
  58. {
  59. Svelto.Console.LogDebug<T>(obj.ToString(), extraDebug);
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. public static void LogError(string msg, Dictionary<string, string> extraData = null)
  63. {
  64. Svelto.Console.LogError(msg, extraData);
  65. }
  66. /// <summary>
  67. /// Write an error message to Gamecraft's log
  68. /// </summary>
  69. /// <param name="obj">The object to log</param>
  70. /// <param name="extraData">The extra data to pass to the ILogger</param>
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public static void LogError(object obj, Dictionary<string, string> extraData = null)
  73. {
  74. Svelto.Console.LogError(obj.ToString(), extraData);
  75. }
  76. /// <summary>
  77. /// Write an exception to Gamecraft's log
  78. /// </summary>
  79. /// <param name="e">The exception to log</param>
  80. /// <param name="extraData">The extra data to pass to the ILogger.
  81. /// This is automatically populated with "OuterException#" and "OuterStacktrace#" entries</param>
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public static void LogException(Exception e, string msg = null, Dictionary<string, string> extraData = null)
  84. {
  85. Svelto.Console.LogException(e, msg, extraData);
  86. }
  87. /// <summary>
  88. /// Write an exception message to Gamecraft's log
  89. /// </summary>
  90. /// <param name="obj">The object to log</param>
  91. /// <param name="e">The exception to log</param>
  92. /// <param name="extraData">The extra data to pass to the ILogger.
  93. /// This is implemented similar to LogException(Exception e, Dictionary extraData)'s extraData</param>
  94. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  95. public static void LogException(Exception e, object obj, Dictionary<string, string> extraData = null)
  96. {
  97. Svelto.Console.LogException(e, obj.ToString(), extraData);
  98. }
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public static void LogWarning(string msg)
  101. {
  102. Svelto.Console.LogWarning(msg);
  103. }
  104. /// <summary>
  105. /// Write a warning message to Gamecraft's log
  106. /// </summary>
  107. /// <param name="obj">The object to log</param>
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. public static void LogWarning(object obj)
  110. {
  111. Svelto.Console.LogWarning(obj.ToString());
  112. }
  113. [Obsolete("SystemLog was removed from Svelto.Common")]
  114. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  115. public static void SystemLog(string msg)
  116. {
  117. Svelto.Console.Log(msg);
  118. }
  119. /// <summary>
  120. /// Write a message to stdout (ie the terminal, like Command Prompt or PowerShell)
  121. /// </summary>
  122. /// <param name="obj">The object to log</param>
  123. [Obsolete("SystemLog was removed from Svelto.Common")]
  124. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  125. public static void SystemLog(object obj)
  126. {
  127. Svelto.Console.Log(obj.ToString());
  128. }
  129. // descriptive logging
  130. /// <summary>
  131. /// Write a descriptive message to Gamecraft's log only when the API is a Debug build
  132. /// </summary>
  133. /// <param name="obj">The object to log</param>
  134. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135. public static void MetaDebugLog(object obj)
  136. {
  137. #if DEBUG
  138. MetaLog($"[MetaDebug]{obj.ToString()}");
  139. #endif
  140. }
  141. /// <summary>
  142. /// Write a descriptive message to Gamecraft's log including the current time and the calling method's name
  143. /// </summary>
  144. /// <param name="obj">The object to log</param>
  145. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146. public static void MetaLog(object obj)
  147. {
  148. var method = (new StackTrace()).GetFrame(1).GetMethod();
  149. Log($"[{DateTime.Now.ToString()}][{method.DeclaringType.FullName}.{method.Name}]{obj.ToString()}");
  150. }
  151. // CLI logging
  152. /// <summary>
  153. /// Write a message to Gamecraft's command line
  154. /// </summary>
  155. /// <param name="obj">The object to log</param>
  156. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  157. public static void CommandLog(object obj)
  158. {
  159. CommandLog(obj.ToString());
  160. }
  161. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  162. public static void CommandLog(string msg)
  163. {
  164. uREPL.Log.Output(msg);
  165. }
  166. /// <summary>
  167. /// Write an error message to Gamecraft's command line
  168. /// </summary>
  169. /// <param name="obj">The object to log</param>
  170. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  171. public static void CommandLogError(object obj)
  172. {
  173. CommandLogError(obj.ToString());
  174. }
  175. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  176. public static void CommandLogError(string msg)
  177. {
  178. uREPL.Log.Error(msg);
  179. }
  180. /// <summary>
  181. /// Write a warning message to Gamecraft's command line
  182. /// </summary>
  183. /// <param name="obj">The object to log</param>
  184. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  185. public static void CommandLogWarning(object obj)
  186. {
  187. CommandLogWarning(obj.ToString());
  188. }
  189. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  190. public static void CommandLogWarning(string msg)
  191. {
  192. uREPL.Log.Warn(msg);
  193. }
  194. }
  195. }