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.

219 lines
7.6KB

  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. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. public static void SystemLog(string msg)
  115. {
  116. Svelto.Console.SystemLog(msg);
  117. }
  118. /// <summary>
  119. /// Write a message to stdout (ie the terminal, like Command Prompt or PowerShell)
  120. /// </summary>
  121. /// <param name="obj">The object to log</param>
  122. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. public static void SystemLog(object obj)
  124. {
  125. Svelto.Console.SystemLog(obj.ToString());
  126. }
  127. // descriptive logging
  128. /// <summary>
  129. /// Write a descriptive message to Gamecraft's log only when the API is a Debug build
  130. /// </summary>
  131. /// <param name="obj">The object to log</param>
  132. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  133. public static void MetaDebugLog(object obj)
  134. {
  135. #if DEBUG
  136. MetaLog($"[MetaDebug]{obj.ToString()}");
  137. #endif
  138. }
  139. /// <summary>
  140. /// Write a descriptive message to Gamecraft's log including the current time and the calling method's name
  141. /// </summary>
  142. /// <param name="obj">The object to log</param>
  143. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  144. public static void MetaLog(object obj)
  145. {
  146. var method = (new StackTrace()).GetFrame(1).GetMethod();
  147. Log($"[{DateTime.Now.ToString()}][{method.DeclaringType.FullName}.{method.Name}]{obj.ToString()}");
  148. }
  149. // CLI logging
  150. /// <summary>
  151. /// Write a message to Gamecraft's command line
  152. /// </summary>
  153. /// <param name="obj">The object to log</param>
  154. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  155. public static void CommandLog(object obj)
  156. {
  157. CommandLog(obj.ToString());
  158. }
  159. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  160. public static void CommandLog(string msg)
  161. {
  162. uREPL.Log.Output(msg);
  163. }
  164. /// <summary>
  165. /// Write an error message to Gamecraft's command line
  166. /// </summary>
  167. /// <param name="obj">The object to log</param>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. public static void CommandLogError(object obj)
  170. {
  171. CommandLogError(obj.ToString());
  172. }
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. public static void CommandLogError(string msg)
  175. {
  176. uREPL.Log.Error(msg);
  177. }
  178. /// <summary>
  179. /// Write a warning message to Gamecraft's command line
  180. /// </summary>
  181. /// <param name="obj">The object to log</param>
  182. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  183. public static void CommandLogWarning(object obj)
  184. {
  185. CommandLogWarning(obj.ToString());
  186. }
  187. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  188. public static void CommandLogWarning(string msg)
  189. {
  190. uREPL.Log.Warn(msg);
  191. }
  192. }
  193. }