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.

172 lines
6.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Reflection;
  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. 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. /// <summary>
  133. /// Write a descriptive message to Gamecraft's log only when the API is a Debug build
  134. /// </summary>
  135. /// <param name="obj">The object to log</param>
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. public static void MetaDebugLog(object obj)
  138. {
  139. #if DEBUG
  140. MetaLog($"[MetaDebug]{obj.ToString()}");
  141. #endif
  142. }
  143. /// <summary>
  144. /// Write a descriptive message to Gamecraft's log including the current time and the DLL's name
  145. /// </summary>
  146. /// <param name="obj">The object to log</param>
  147. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  148. public static void MetaLog(object obj)
  149. {
  150. var callAsm = Assembly.GetCallingAssembly();
  151. Log($"[{DateTime.Now.ToString()}][{callAsm.GetName().Name}]{obj.ToString()}");
  152. }
  153. }
  154. }