LogBase.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.IO;
  30. using System.Net;
  31. namespace OpenSim.Framework.Console
  32. {
  33. public enum LogPriority : int
  34. {
  35. CRITICAL,
  36. HIGH,
  37. MEDIUM,
  38. NORMAL,
  39. LOW,
  40. VERBOSE,
  41. EXTRAVERBOSE
  42. }
  43. public class LogBase
  44. {
  45. StreamWriter Log;
  46. public conscmd_callback cmdparser;
  47. public string componentname;
  48. private bool m_silent;
  49. public LogBase(string LogFile, string componentname, conscmd_callback cmdparser, bool silent)
  50. {
  51. this.componentname = componentname;
  52. this.cmdparser = cmdparser;
  53. this.m_silent = silent;
  54. System.Console.WriteLine("ServerConsole.cs - creating new local console");
  55. if (String.IsNullOrEmpty(LogFile))
  56. {
  57. LogFile = componentname + ".log";
  58. }
  59. System.Console.WriteLine("Logs will be saved to current directory in " + LogFile);
  60. Log = File.AppendText(LogFile);
  61. Log.WriteLine("========================================================================");
  62. Log.WriteLine(componentname + " Started at " + DateTime.Now.ToString());
  63. }
  64. public void Close()
  65. {
  66. Log.WriteLine("Shutdown at " + DateTime.Now.ToString());
  67. Log.Close();
  68. }
  69. public void Write(string format, params object[] args)
  70. {
  71. Notice(format, args);
  72. return;
  73. }
  74. public void WriteLine(LogPriority importance, string format, params object[] args)
  75. {
  76. Log.WriteLine(format, args);
  77. Log.Flush();
  78. if (!m_silent)
  79. {
  80. System.Console.WriteLine(format, args);
  81. }
  82. return;
  83. }
  84. public void Warn(string format, params object[] args)
  85. {
  86. WriteNewLine(ConsoleColor.Yellow, format, args);
  87. return;
  88. }
  89. public void Notice(string format, params object[] args)
  90. {
  91. WriteNewLine(ConsoleColor.White, format, args);
  92. return;
  93. }
  94. public void Error(string format, params object[] args)
  95. {
  96. WriteNewLine(ConsoleColor.Red, format, args);
  97. return;
  98. }
  99. public void Verbose(string format, params object[] args)
  100. {
  101. WriteNewLine(ConsoleColor.Gray, format, args);
  102. return;
  103. }
  104. public void Status(string format, params object[] args)
  105. {
  106. WriteNewLine(ConsoleColor.Blue, format, args);
  107. return;
  108. }
  109. private void WriteNewLine(ConsoleColor color, string format, params object[] args)
  110. {
  111. Log.WriteLine(format, args);
  112. Log.Flush();
  113. if (!m_silent)
  114. {
  115. try
  116. {
  117. System.Console.ForegroundColor = color;
  118. System.Console.WriteLine(format, args);
  119. System.Console.ResetColor();
  120. }
  121. catch (ArgumentNullException)
  122. {
  123. // Some older systems dont support coloured text.
  124. System.Console.WriteLine(format, args);
  125. }
  126. }
  127. return;
  128. }
  129. public string ReadLine()
  130. {
  131. string TempStr = System.Console.ReadLine();
  132. Log.WriteLine(TempStr);
  133. return TempStr;
  134. }
  135. public int Read()
  136. {
  137. int TempInt = System.Console.Read();
  138. Log.Write((char)TempInt);
  139. return TempInt;
  140. }
  141. public IPAddress CmdPromptIPAddress(string prompt, string defaultvalue)
  142. {
  143. IPAddress address;
  144. string addressStr;
  145. while (true)
  146. {
  147. addressStr = MainLog.Instance.CmdPrompt(prompt, defaultvalue);
  148. if (IPAddress.TryParse(addressStr, out address))
  149. {
  150. break;
  151. }
  152. else
  153. {
  154. MainLog.Instance.Error("Illegal address. Please re-enter.");
  155. }
  156. }
  157. return address;
  158. }
  159. public int CmdPromptIPPort(string prompt, string defaultvalue)
  160. {
  161. int port;
  162. string portStr;
  163. while (true)
  164. {
  165. portStr = MainLog.Instance.CmdPrompt(prompt, defaultvalue);
  166. if (int.TryParse(portStr, out port))
  167. {
  168. if (port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort)
  169. {
  170. break;
  171. }
  172. }
  173. MainLog.Instance.Error("Illegal address. Please re-enter.");
  174. }
  175. return port;
  176. }
  177. // Displays a prompt and waits for the user to enter a string, then returns that string
  178. // Done with no echo and suitable for passwords
  179. public string PasswdPrompt(string prompt)
  180. {
  181. // FIXME: Needs to be better abstracted
  182. Log.WriteLine(prompt);
  183. this.Write(prompt);
  184. ConsoleColor oldfg = System.Console.ForegroundColor;
  185. System.Console.ForegroundColor = System.Console.BackgroundColor;
  186. string temp = System.Console.ReadLine();
  187. System.Console.ForegroundColor = oldfg;
  188. return temp;
  189. }
  190. // Displays a command prompt and waits for the user to enter a string, then returns that string
  191. public string CmdPrompt(string prompt)
  192. {
  193. this.Write(String.Format("{0}: ", prompt));
  194. return this.ReadLine();
  195. }
  196. // Displays a command prompt and returns a default value if the user simply presses enter
  197. public string CmdPrompt(string prompt, string defaultresponse)
  198. {
  199. string temp = CmdPrompt(String.Format("{0} [{1}]", prompt, defaultresponse));
  200. if (temp == "")
  201. {
  202. return defaultresponse;
  203. }
  204. else
  205. {
  206. return temp;
  207. }
  208. }
  209. // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
  210. public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB)
  211. {
  212. bool itisdone = false;
  213. string temp = CmdPrompt(prompt, defaultresponse);
  214. while (itisdone == false)
  215. {
  216. if ((temp == OptionA) || (temp == OptionB))
  217. {
  218. itisdone = true;
  219. }
  220. else
  221. {
  222. Notice("Valid options are " + OptionA + " or " + OptionB);
  223. temp = CmdPrompt(prompt, defaultresponse);
  224. }
  225. }
  226. return temp;
  227. }
  228. // Runs a command with a number of parameters
  229. public Object RunCmd(string Cmd, string[] cmdparams)
  230. {
  231. cmdparser.RunCmd(Cmd, cmdparams);
  232. return null;
  233. }
  234. // Shows data about something
  235. public void ShowCommands(string ShowWhat)
  236. {
  237. cmdparser.Show(ShowWhat);
  238. }
  239. public void MainLogPrompt()
  240. {
  241. string[] tempstrarray;
  242. string tempstr = this.CmdPrompt(this.componentname + "# ");
  243. tempstrarray = tempstr.Split(' ');
  244. string cmd = tempstrarray[0];
  245. Array.Reverse(tempstrarray);
  246. Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1);
  247. Array.Reverse(tempstrarray);
  248. string[] cmdparams = (string[])tempstrarray;
  249. RunCmd(cmd, cmdparams);
  250. }
  251. }
  252. }