LogManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SuperTux Editor
  2. // Copyright (C) 2007 Arvid Norlander <anmaster AT berlios DOT de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. /// <summary>
  18. /// Loglevels for logging.
  19. /// </summary>
  20. /// <seealso cref="LogManager"/>
  21. public enum LogLevel {
  22. /// <summary>
  23. /// Debug messages for developers.
  24. /// </summary>
  25. Debug,
  26. /// <summary>
  27. /// Warnings/Errors for developers, they should fix the issue.
  28. /// These <strong>should</strong> never show up in a release.
  29. /// </summary>
  30. DebugWarning,
  31. Info,
  32. Warning,
  33. Error,
  34. /// <summary>
  35. /// The world will end (or at least this part of it), maybe with
  36. /// emergency save of level, maybe not.
  37. /// </summary>
  38. Fatal
  39. }
  40. public static class LogManager {
  41. /// <summary>
  42. /// Returns string to use as prefix for <paramref name="loglevel"/>
  43. /// </summary>
  44. /// <param name="loglevel">The loglevel</param>
  45. /// <returns>The prefix to use in the format of "XXX: ".</returns>
  46. private static string GetLevelString(LogLevel loglevel) {
  47. switch (loglevel) {
  48. case LogLevel.Debug:
  49. return "DEBUG: ";
  50. case LogLevel.DebugWarning:
  51. return "DEBUGWARN: ";
  52. case LogLevel.Info:
  53. return "INFO: ";
  54. case LogLevel.Warning:
  55. return "WARN: ";
  56. case LogLevel.Error:
  57. return "ERROR: ";
  58. case LogLevel.Fatal:
  59. return "FATAL: ";
  60. default:
  61. return loglevel.ToString();
  62. }
  63. }
  64. /// <summary>
  65. /// Log a message with <paramref name="loglevel"/>
  66. /// </summary>
  67. /// <remarks>
  68. /// Currently this logs to STDERR for <see cref="LogLevel.Error"/> and
  69. /// <see cref="LogLevel.Fatal"/> and other levels to STDOUT.
  70. /// </remarks>
  71. /// <param name="loglevel">The log level of this message.</param>
  72. /// <param name="message">The message to log</param>
  73. public static void Log(LogLevel loglevel, string message) {
  74. if (loglevel == LogLevel.Fatal || loglevel == LogLevel.Error)
  75. Console.Error.WriteLine(loglevel.ToString() + ": " + message);
  76. Console.WriteLine(GetLevelString(loglevel) + message);
  77. }
  78. /// <summary>
  79. /// Log a message with <paramref name="loglevel"/>
  80. /// </summary>
  81. /// <remarks>
  82. /// Currently this logs to STDERR for <see cref="LogLevel.Error"/> and
  83. /// <see cref="LogLevel.Fatal"/> and other levels to STDOUT.
  84. /// </remarks>
  85. /// <param name="loglevel">The log level of this message.</param>
  86. /// <param name="format">A format string.</param>
  87. /// <param name="arg0">First object for format string</param>
  88. public static void Log(LogLevel loglevel, string format, object arg0) {
  89. if (loglevel == LogLevel.Fatal || loglevel == LogLevel.Error)
  90. Console.Error.WriteLine(loglevel.ToString() + ": " + format, arg0);
  91. Console.WriteLine(GetLevelString(loglevel) + format, arg0);
  92. }
  93. /// <summary>
  94. /// Log a message with <paramref name="loglevel"/>
  95. /// </summary>
  96. /// <remarks>
  97. /// Currently this logs to STDERR for <see cref="LogLevel.Error"/> and
  98. /// <see cref="LogLevel.Fatal"/> and other levels to STDOUT.
  99. /// </remarks>
  100. /// <param name="loglevel">The log level of this message.</param>
  101. /// <param name="format">A format string.</param>
  102. /// <param name="arg0">First object for format string</param>
  103. /// <param name="arg1">Second object for format string</param>
  104. public static void Log(LogLevel loglevel, string format, object arg0, object arg1) {
  105. if (loglevel == LogLevel.Fatal || loglevel == LogLevel.Error)
  106. Console.Error.WriteLine(loglevel.ToString() + ": " + format, arg0, arg1);
  107. Console.WriteLine(GetLevelString(loglevel) + format, arg0, arg1);
  108. }
  109. /// <summary>
  110. /// Log a message with <paramref name="loglevel"/>
  111. /// </summary>
  112. /// <remarks>
  113. /// Currently this logs to STDERR for <see cref="LogLevel.Error"/> and
  114. /// <see cref="LogLevel.Fatal"/> and other levels to STDOUT.
  115. /// </remarks>
  116. /// <param name="loglevel">The log level of this message.</param>
  117. /// <param name="format">A format string.</param>
  118. /// <param name="args">Array of object for format string</param>
  119. public static void Log(LogLevel loglevel, string format, params object[] args) {
  120. if (loglevel == LogLevel.Fatal || loglevel == LogLevel.Error)
  121. Console.Error.WriteLine(loglevel.ToString() + ": " + format, args);
  122. Console.WriteLine(GetLevelString(loglevel) + format, args);
  123. }
  124. }
  125. /* EOF */