Unofficial CardLife revival project, pronounced like "celery"
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.

LogEndpoints.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.IO;
  2. using System.Net;
  3. using System.Reflection;
  4. using System.Text;
  5. using HarmonyLib;
  6. namespace CLre_server.WebStatus
  7. {
  8. public class LogEndpoints
  9. {
  10. [WebEndpoint("/l/current")]
  11. private static void FullLog(HttpListenerContext ctx)
  12. {
  13. if (CustomLogger_GetFileNameToUse_Patch.currentLogFile == null)
  14. {
  15. byte[] output = Encoding.UTF8.GetBytes("No log file available");
  16. ctx.Response.OutputStream.Write(output, 0, output.Length);
  17. return;
  18. }
  19. // copy file because log is already open for writing
  20. string copyFilename = CustomLogger_GetFileNameToUse_Patch.currentLogFile + ".copy";
  21. File.Copy(CustomLogger_GetFileNameToUse_Patch.currentLogFile, copyFilename, true);
  22. FileStream logFile = new FileStream(copyFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
  23. logFile.CopyTo(ctx.Response.OutputStream);
  24. logFile.Close();
  25. }
  26. }
  27. [HarmonyPatch]
  28. class CustomLogger_GetFileNameToUse_Patch
  29. {
  30. internal static string currentLogFile = null;
  31. [HarmonyPostfix]
  32. public static void AfterMethodCall(string __result)
  33. {
  34. #if DEBUG
  35. API.Utility.Logging.MetaLog($"Current logfile is {__result}");
  36. #endif
  37. currentLogFile = __result;
  38. }
  39. [HarmonyTargetMethod]
  40. public static MethodBase Target()
  41. {
  42. return AccessTools.Method("CustomLogger:GetFileNameToUse");
  43. }
  44. }
  45. }