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.

203 lines
7.1KB

  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 TechbloxModdingAPI.Utility
  9. {
  10. /// <summary>
  11. /// Utility class to access Techblox's built-in logging capabilities.
  12. /// The log is saved to %APPDATA%\..\LocalLow\FreeJam\Techblox\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 Techblox'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 Techblox'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 Techblox'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 Techblox'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 Techblox's log and to the screen and exit game
  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 Techblox's log and to the screen and exit game
  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 Techblox'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. // descriptive logging
  114. /// <summary>
  115. /// Write a descriptive message to Techblox's log only when the API is a Debug build
  116. /// </summary>
  117. /// <param name="obj">The object to log</param>
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. public static void MetaDebugLog(object obj)
  120. {
  121. #if DEBUG
  122. MetaLog($"[MetaDebug]{obj.ToString()}");
  123. #endif
  124. }
  125. /// <summary>
  126. /// Write a descriptive message to Techblox's log including the current time and the calling method's name
  127. /// </summary>
  128. /// <param name="obj">The object to log</param>
  129. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  130. public static void MetaLog(object obj)
  131. {
  132. var method = (new StackTrace()).GetFrame(1).GetMethod();
  133. Log($"[{DateTime.Now.ToString()}][{method.DeclaringType.FullName}.{method.Name}]{obj.ToString()}");
  134. }
  135. // CLI logging
  136. /// <summary>
  137. /// Write a message to Techblox's command line
  138. /// </summary>
  139. /// <param name="obj">The object to log</param>
  140. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  141. public static void CommandLog(object obj)
  142. {
  143. CommandLog(obj.ToString());
  144. }
  145. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146. public static void CommandLog(string msg)
  147. {
  148. Log(msg);
  149. }
  150. /// <summary>
  151. /// Write an error message to Techblox's command line
  152. /// </summary>
  153. /// <param name="obj">The object to log</param>
  154. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  155. public static void CommandLogError(object obj)
  156. {
  157. CommandLogError(obj.ToString());
  158. }
  159. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  160. public static void CommandLogError(string msg)
  161. {
  162. LogError(msg);
  163. }
  164. /// <summary>
  165. /// Write a warning message to Techblox's command line
  166. /// </summary>
  167. /// <param name="obj">The object to log</param>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. public static void CommandLogWarning(object obj)
  170. {
  171. CommandLogWarning(obj.ToString());
  172. }
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. public static void CommandLogWarning(string msg)
  175. {
  176. LogWarning(msg);
  177. }
  178. }
  179. }