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