ConsoleBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.IO;
  3. namespace OpenSim.Framework.Console
  4. {
  5. public enum LogPriority : int
  6. {
  7. CRITICAL,
  8. HIGH,
  9. MEDIUM,
  10. NORMAL,
  11. LOW,
  12. VERBOSE,
  13. EXTRAVERBOSE
  14. }
  15. public class ConsoleBase
  16. {
  17. StreamWriter Log;
  18. public conscmd_callback cmdparser;
  19. public string componentname;
  20. private bool m_silent;
  21. public ConsoleBase(string LogFile, string componentname, conscmd_callback cmdparser, bool silent )
  22. {
  23. this.componentname = componentname;
  24. this.cmdparser = cmdparser;
  25. this.m_silent = silent;
  26. System.Console.WriteLine("ServerConsole.cs - creating new local console");
  27. System.Console.WriteLine("Logs will be saved to current directory in " + LogFile);
  28. Log = File.AppendText(LogFile);
  29. Log.WriteLine("========================================================================");
  30. Log.WriteLine(componentname + " Started at " + DateTime.Now.ToString());
  31. }
  32. public void Close()
  33. {
  34. Log.WriteLine("Shutdown at " + DateTime.Now.ToString());
  35. Log.Close();
  36. }
  37. public void Write(string format, params object[] args)
  38. {
  39. WriteLine(LogPriority.NORMAL,format,args);
  40. return;
  41. }
  42. [Obsolete("WriteLine(msg,args) has been depreciated, use WriteLine(priority,msg,args) instead.")]
  43. public void WriteLine(string format, params object[] args)
  44. {
  45. Log.WriteLine(format, args);
  46. Log.Flush();
  47. if(!m_silent)
  48. {
  49. System.Console.WriteLine(format, args);
  50. }
  51. return;
  52. }
  53. public void WriteLine(LogPriority importance, string format, params object[] args)
  54. {
  55. Log.WriteLine(format, args);
  56. Log.Flush();
  57. if (!m_silent)
  58. {
  59. System.Console.WriteLine(format, args);
  60. }
  61. return;
  62. }
  63. public string ReadLine()
  64. {
  65. string TempStr = System.Console.ReadLine();
  66. Log.WriteLine(TempStr);
  67. return TempStr;
  68. }
  69. public int Read()
  70. {
  71. int TempInt = System.Console.Read();
  72. Log.Write((char)TempInt);
  73. return TempInt;
  74. }
  75. // Displays a prompt and waits for the user to enter a string, then returns that string
  76. // Done with no echo and suitable for passwords
  77. public string PasswdPrompt(string prompt)
  78. {
  79. // FIXME: Needs to be better abstracted
  80. Log.WriteLine(prompt);
  81. this.Write(prompt);
  82. ConsoleColor oldfg = System.Console.ForegroundColor;
  83. System.Console.ForegroundColor = System.Console.BackgroundColor;
  84. string temp = System.Console.ReadLine();
  85. System.Console.ForegroundColor = oldfg;
  86. return temp;
  87. }
  88. // Displays a command prompt and waits for the user to enter a string, then returns that string
  89. public string CmdPrompt(string prompt)
  90. {
  91. this.Write(String.Format("{0}: ", prompt));
  92. return this.ReadLine();
  93. }
  94. // Displays a command prompt and returns a default value if the user simply presses enter
  95. public string CmdPrompt(string prompt, string defaultresponse)
  96. {
  97. string temp = CmdPrompt(String.Format( "{0} [{1}]", prompt, defaultresponse ));
  98. if (temp == "")
  99. {
  100. return defaultresponse;
  101. }
  102. else
  103. {
  104. return temp;
  105. }
  106. }
  107. // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
  108. public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB)
  109. {
  110. bool itisdone = false;
  111. string temp = CmdPrompt(prompt, defaultresponse);
  112. while (itisdone == false)
  113. {
  114. if ((temp == OptionA) || (temp == OptionB))
  115. {
  116. itisdone = true;
  117. }
  118. else
  119. {
  120. this.WriteLine(LogPriority.MEDIUM,"Valid options are " + OptionA + " or " + OptionB);
  121. temp = CmdPrompt(prompt, defaultresponse);
  122. }
  123. }
  124. return temp;
  125. }
  126. // Runs a command with a number of parameters
  127. public Object RunCmd(string Cmd, string[] cmdparams)
  128. {
  129. cmdparser.RunCmd(Cmd, cmdparams);
  130. return null;
  131. }
  132. // Shows data about something
  133. public void ShowCommands(string ShowWhat)
  134. {
  135. cmdparser.Show(ShowWhat);
  136. }
  137. public void MainConsolePrompt()
  138. {
  139. string[] tempstrarray;
  140. string tempstr = this.CmdPrompt(this.componentname + "# ");
  141. tempstrarray = tempstr.Split(' ');
  142. string cmd = tempstrarray[0];
  143. Array.Reverse(tempstrarray);
  144. Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1);
  145. Array.Reverse(tempstrarray);
  146. string[] cmdparams = (string[])tempstrarray;
  147. RunCmd(cmd, cmdparams);
  148. }
  149. }
  150. }