LogManager.cs 4.8 KB

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