FileLog.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace Harmony
  9. {
  10. public static class FileLog
  11. {
  12. public static string logPath;
  13. public static char indentChar = '\t';
  14. public static int indentLevel = 0;
  15. static List<string> buffer = new List<string>();
  16. static FileLog()
  17. {
  18. logPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Path.DirectorySeparatorChar + "harmony.log.txt";
  19. }
  20. static string IndentString()
  21. {
  22. return new string(indentChar, indentLevel);
  23. }
  24. public static void ChangeIndent(int delta)
  25. {
  26. indentLevel = Math.Max(0, indentLevel + delta);
  27. }
  28. // use this method only if you are sure that FlushBuffer will be called
  29. // or else logging information is incomplete in case of a crash
  30. //
  31. public static void LogBuffered(string str)
  32. {
  33. lock (logPath)
  34. {
  35. buffer.Add(IndentString() + str);
  36. }
  37. }
  38. public static void FlushBuffer()
  39. {
  40. lock (logPath)
  41. {
  42. if (buffer.Count > 0)
  43. {
  44. using (var writer = File.AppendText(logPath))
  45. {
  46. foreach (var str in buffer)
  47. writer.WriteLine(str);
  48. }
  49. buffer.Clear();
  50. }
  51. }
  52. }
  53. // this is the slower method that flushes changes directly to the file
  54. // to prevent missing information in case of a cache
  55. //
  56. public static void Log(string str)
  57. {
  58. lock (logPath)
  59. {
  60. using (var writer = File.AppendText(logPath))
  61. {
  62. writer.WriteLine(IndentString() + str);
  63. }
  64. }
  65. }
  66. public static void Reset()
  67. {
  68. lock (logPath)
  69. {
  70. var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Path.DirectorySeparatorChar + "harmony.log.txt";
  71. File.Delete(path);
  72. }
  73. }
  74. public static unsafe void LogBytes(long ptr, int len)
  75. {
  76. lock (logPath)
  77. {
  78. var p = (byte*)ptr;
  79. var s = "";
  80. for (var i = 1; i <= len; i++)
  81. {
  82. if (s == "") s = "# ";
  83. s = s + (*p).ToString("X2") + " ";
  84. if (i > 1 || len == 1)
  85. {
  86. if (i % 8 == 0 || i == len)
  87. {
  88. Log(s);
  89. s = "";
  90. }
  91. else if (i % 4 == 0)
  92. s = s + " ";
  93. }
  94. p++;
  95. }
  96. var arr = new byte[len];
  97. Marshal.Copy((IntPtr)ptr, arr, 0, len);
  98. var md5Hash = MD5.Create();
  99. var hash = md5Hash.ComputeHash(arr);
  100. #pragma warning disable XS0001
  101. var sBuilder = new StringBuilder();
  102. #pragma warning restore XS0001
  103. for (var i = 0; i < hash.Length; i++)
  104. sBuilder.Append(hash[i].ToString("X2"));
  105. Log("HASH: " + sBuilder);
  106. }
  107. }
  108. }
  109. }