Logger.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public class Logger
  5. {
  6. public static bool Enabled = true;
  7. private static Logger m_instance = null;
  8. private static string ApplicationDirectory
  9. {
  10. get
  11. {
  12. string path = Application.dataPath;
  13. if (Application.platform == RuntimePlatform.OSXPlayer)
  14. {
  15. path += "/../../";
  16. }
  17. else if (Application.platform == RuntimePlatform.WindowsPlayer)
  18. {
  19. path += "/../";
  20. }
  21. return path;
  22. }
  23. }
  24. public string LogFilePath {get; private set;}
  25. public static Logger instance
  26. {
  27. get
  28. {
  29. if (m_instance == null)
  30. {
  31. m_instance = new Logger();
  32. }
  33. return m_instance;
  34. }
  35. }
  36. public Logger()
  37. {
  38. if(!Enabled){return;}
  39. Debug.Log("Starting logger @ " + ApplicationDirectory);
  40. if(LogFilePath == null){
  41. LogFilePath = ApplicationDirectory + "Log.txt";
  42. }
  43. File.WriteAllText(LogFilePath, "Logger initiated at " + DateTime.Now + "\n\n");
  44. }
  45. public void log(string message){
  46. if(!Enabled){return;}
  47. File.AppendAllText(LogFilePath,$"[{DateTime.Now}] {message}\n");
  48. Debug.Log(message);
  49. }
  50. public static void Log(string message){
  51. instance.log(message);
  52. }
  53. public static void SetFileName(string fileName){
  54. instance.LogFilePath = ApplicationDirectory+fileName + ".txt";
  55. }
  56. public static void SetFilePath(string path){
  57. instance.LogFilePath= path;
  58. }
  59. }