Log.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Log.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using System.IO.IsolatedStorage;
  12. using System.Threading;
  13. using System.Diagnostics;
  14. namespace RacingGame.Helpers
  15. {
  16. /// <summary>
  17. /// Log will create automatically a log file and write
  18. /// log/error/debug info for simple runtime error checking, very useful
  19. /// for minor errors, such as missing files.
  20. /// The application can still continue working, but this log provides
  21. /// an easy support to find out what files are missing (in this example).
  22. ///
  23. /// Note: I don't use this class anymore for big projects, but its small
  24. /// and handy for smaller projects and nice to log non-debugable stuff.
  25. ///
  26. /// Also, there's no reason to do this on the Xbox 360, since you cannot
  27. /// pull the log file back down to your computer.
  28. /// </summary>
  29. public static class Log
  30. {
  31. #if !XBOX360
  32. /// <summary>
  33. /// Writer
  34. /// </summary>
  35. private static StreamWriter writer = null;
  36. /// <summary>
  37. /// Log filename
  38. /// </summary>
  39. private const string LogFilename = "Log.txt";
  40. #endif
  41. /// <summary>
  42. /// Static constructor
  43. /// </summary>
  44. public static void Initialize()
  45. {
  46. #if !XBOX360 && !NETFX_CORE && !XBOXONE
  47. try
  48. {
  49. IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain();
  50. FileStream file;
  51. if (!isolatedStorageFile.FileExists(LogFilename))
  52. file = isolatedStorageFile.CreateFile(LogFilename);
  53. else
  54. {
  55. file = isolatedStorageFile.OpenFile(LogFilename, FileMode.OpenOrCreate,
  56. FileAccess.Write, FileShare.ReadWrite);
  57. }
  58. // Check if file is too big (more than 2 MB),
  59. // in this case we just kill it and create a new one :)
  60. if (file.Length > 2 * 1024 * 1024)
  61. {
  62. file.Close();
  63. file = isolatedStorageFile.CreateFile(LogFilename);
  64. }
  65. // Associate writer with that, when writing to a new file,
  66. // make sure UTF-8 sign is written, else don't write it again!
  67. if (file.Length == 0)
  68. writer = new StreamWriter(file,
  69. System.Text.Encoding.UTF8);
  70. else
  71. writer = new StreamWriter(file);
  72. // Go to end of file
  73. writer.BaseStream.Seek(0, SeekOrigin.End);
  74. // Enable auto flush (always be up to date when reading!)
  75. writer.AutoFlush = true;
  76. // Add some info about this session
  77. writer.WriteLine("");
  78. writer.WriteLine("/// Session started at: " + DateTime.Now.ToString());
  79. writer.WriteLine("/// RacingGame");
  80. writer.WriteLine("");
  81. }
  82. catch (IOException)
  83. {
  84. // Ignore any file exceptions, if file is not
  85. // createable (e.g. on a CD-Rom) it doesn't matter.
  86. }
  87. catch (UnauthorizedAccessException)
  88. {
  89. // Ignore any file exceptions, if file is not
  90. // createable (e.g. on a CD-Rom) it doesn't matter.
  91. }
  92. #endif
  93. }
  94. /// <summary>
  95. /// Writes a LogType and info/error message string to the Log file
  96. /// </summary>
  97. static public void Write(string message)
  98. {
  99. #if !XBOX360 && !XBOXONE
  100. // Can't continue without valid writer
  101. if (writer == null)
  102. return;
  103. try
  104. {
  105. DateTime ct = DateTime.Now;
  106. string s = "[" + ct.Hour.ToString("00") + ":" +
  107. ct.Minute.ToString("00") + ":" +
  108. ct.Second.ToString("00") + "] " +
  109. message;
  110. writer.WriteLine(s);
  111. #if DEBUG
  112. // In debug mode write that message to the console as well!
  113. System.Diagnostics.Debug.WriteLine(s);
  114. #endif
  115. }
  116. catch (IOException)
  117. {
  118. // Ignore any file exceptions, if file is not
  119. // createable (e.g. on a CD-Rom) it doesn't matter.
  120. }
  121. catch (UnauthorizedAccessException)
  122. {
  123. // Ignore any file exceptions, if file is not
  124. // createable (e.g. on a CD-Rom) it doesn't matter.
  125. }
  126. #endif
  127. }
  128. }
  129. }