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.

225 lines
7.8KB

  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, Dictionary<string, string> extraData = null)
  84. {
  85. Svelto.Console.LogException(e, extraData);
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static void LogException(string msg, Exception e, Dictionary<string, string> extraData = null)
  89. {
  90. Svelto.Console.LogException(msg, e, extraData);
  91. }
  92. /// <summary>
  93. /// Write an exception message to Gamecraft's log
  94. /// </summary>
  95. /// <param name="obj">The object to log</param>
  96. /// <param name="e">The exception to log</param>
  97. /// <param name="extraData">The extra data to pass to the ILogger.
  98. /// This is implemented similar to LogException(Exception e, Dictionary extraData)'s extraData</param>
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public static void LogException(object obj, Exception e, Dictionary<string, string> extraData = null)
  101. {
  102. Svelto.Console.LogException(obj.ToString(), e, extraData);
  103. }
  104. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  105. public static void LogWarning(string msg)
  106. {
  107. Svelto.Console.LogWarning(msg);
  108. }
  109. /// <summary>
  110. /// Write a warning message to Gamecraft's log
  111. /// </summary>
  112. /// <param name="obj">The object to log</param>
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. public static void LogWarning(object obj)
  115. {
  116. Svelto.Console.LogWarning(obj.ToString());
  117. }
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. public static void SystemLog(string msg)
  120. {
  121. Svelto.Console.SystemLog(msg);
  122. }
  123. /// <summary>
  124. /// Write a message to stdout (ie the terminal, like Command Prompt or PowerShell)
  125. /// </summary>
  126. /// <param name="obj">The object to log</param>
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. public static void SystemLog(object obj)
  129. {
  130. Svelto.Console.SystemLog(obj.ToString());
  131. }
  132. // descriptive logging
  133. /// <summary>
  134. /// Write a descriptive message to Gamecraft's log only when the API is a Debug build
  135. /// </summary>
  136. /// <param name="obj">The object to log</param>
  137. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  138. public static void MetaDebugLog(object obj)
  139. {
  140. #if DEBUG
  141. MetaLog($"[MetaDebug]{obj.ToString()}");
  142. #endif
  143. }
  144. /// <summary>
  145. /// Write a descriptive message to Gamecraft's log including the current time and the calling method's name
  146. /// </summary>
  147. /// <param name="obj">The object to log</param>
  148. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  149. public static void MetaLog(object obj)
  150. {
  151. var method = (new StackTrace()).GetFrame(1).GetMethod();
  152. Log($"[{DateTime.Now.ToString()}][{method.DeclaringType.FullName}.{method.Name}]{obj.ToString()}");
  153. }
  154. // CLI logging
  155. /// <summary>
  156. /// Write a message to Gamecraft's command line
  157. /// </summary>
  158. /// <param name="obj">The object to log</param>
  159. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  160. public static void CommandLog(object obj)
  161. {
  162. CommandLog(obj.ToString());
  163. }
  164. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  165. public static void CommandLog(string msg)
  166. {
  167. uREPL.Log.Output(msg);
  168. }
  169. /// <summary>
  170. /// Write an error message to Gamecraft's command line
  171. /// </summary>
  172. /// <param name="obj">The object to log</param>
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. public static void CommandLogError(object obj)
  175. {
  176. CommandLogError(obj.ToString());
  177. }
  178. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  179. public static void CommandLogError(string msg)
  180. {
  181. uREPL.Log.Error(msg);
  182. }
  183. /// <summary>
  184. /// Write a warning message to Gamecraft's command line
  185. /// </summary>
  186. /// <param name="obj">The object to log</param>
  187. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  188. public static void CommandLogWarning(object obj)
  189. {
  190. CommandLogWarning(obj.ToString());
  191. }
  192. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  193. public static void CommandLogWarning(string msg)
  194. {
  195. uREPL.Log.Warn(msg);
  196. }
  197. }
  198. }