diff options
author | Jeff Ames | 2008-02-05 19:44:27 +0000 |
---|---|---|
committer | Jeff Ames | 2008-02-05 19:44:27 +0000 |
commit | 6ed5283bc06a62f38eb517e67b975832b603bf61 (patch) | |
tree | e5f635018789b73a99ddeca0883a68368fa5eece /OpenSim/Framework/Console/ConsoleBase.cs | |
parent | Cut down on the number of packets sent during terraforming. Terraforming shou... (diff) | |
download | opensim-SC_OLD-6ed5283bc06a62f38eb517e67b975832b603bf61.zip opensim-SC_OLD-6ed5283bc06a62f38eb517e67b975832b603bf61.tar.gz opensim-SC_OLD-6ed5283bc06a62f38eb517e67b975832b603bf61.tar.bz2 opensim-SC_OLD-6ed5283bc06a62f38eb517e67b975832b603bf61.tar.xz |
Converted logging to use log4net.
Changed LogBase to ConsoleBase, which handles console I/O.
This is mostly an in-place conversion, so lots of refactoring can still be done.
Diffstat (limited to 'OpenSim/Framework/Console/ConsoleBase.cs')
-rw-r--r-- | OpenSim/Framework/Console/ConsoleBase.cs | 433 |
1 files changed, 433 insertions, 0 deletions
diff --git a/OpenSim/Framework/Console/ConsoleBase.cs b/OpenSim/Framework/Console/ConsoleBase.cs new file mode 100644 index 0000000..3f68e50 --- /dev/null +++ b/OpenSim/Framework/Console/ConsoleBase.cs | |||
@@ -0,0 +1,433 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.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.Collections.Generic; | ||
30 | using System.Diagnostics; | ||
31 | using System.IO; | ||
32 | using System.Net; | ||
33 | |||
34 | namespace OpenSim.Framework.Console | ||
35 | { | ||
36 | public class ConsoleBase | ||
37 | { | ||
38 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
39 | |||
40 | private object m_syncRoot = new object(); | ||
41 | |||
42 | public conscmd_callback m_cmdParser; | ||
43 | public string m_componentName; | ||
44 | |||
45 | public ConsoleBase(string componentname, conscmd_callback cmdparser) | ||
46 | { | ||
47 | m_componentName = componentname; | ||
48 | m_cmdParser = cmdparser; | ||
49 | |||
50 | System.Console.WriteLine("Creating new local console"); | ||
51 | |||
52 | m_log.Info("[" + m_componentName + "]: Started at " + DateTime.Now.ToString()); | ||
53 | } | ||
54 | |||
55 | public void Close() | ||
56 | { | ||
57 | m_log.Info("[" + m_componentName + "]: Shutdown at " + DateTime.Now.ToString()); | ||
58 | } | ||
59 | |||
60 | /// <summary> | ||
61 | /// derive an ansi color from a string, ignoring the darker colors. | ||
62 | /// This is used to help automatically bin component tags with colors | ||
63 | /// in various print functions. | ||
64 | /// </summary> | ||
65 | /// <param name="input">arbitrary string for input</param> | ||
66 | /// <returns>an ansii color</returns> | ||
67 | private ConsoleColor DeriveColor(string input) | ||
68 | { | ||
69 | int colIdx = (input.ToUpper().GetHashCode() % 6) + 9; | ||
70 | return (ConsoleColor) colIdx; | ||
71 | } | ||
72 | |||
73 | /// <summary> | ||
74 | /// Sends a warning to the current console output | ||
75 | /// </summary> | ||
76 | /// <param name="format">The message to send</param> | ||
77 | /// <param name="args">WriteLine-style message arguments</param> | ||
78 | public void Warn(string format, params object[] args) | ||
79 | { | ||
80 | WriteNewLine(ConsoleColor.Yellow, format, args); | ||
81 | } | ||
82 | |||
83 | /// <summary> | ||
84 | /// Sends a warning to the current console output | ||
85 | /// </summary> | ||
86 | /// <param name="sender">The module that sent this message</param> | ||
87 | /// <param name="format">The message to send</param> | ||
88 | /// <param name="args">WriteLine-style message arguments</param> | ||
89 | public void Warn(string sender, string format, params object[] args) | ||
90 | { | ||
91 | WritePrefixLine(DeriveColor(sender), sender); | ||
92 | WriteNewLine(ConsoleColor.Yellow, format, args); | ||
93 | } | ||
94 | |||
95 | /// <summary> | ||
96 | /// Sends a notice to the current console output | ||
97 | /// </summary> | ||
98 | /// <param name="format">The message to send</param> | ||
99 | /// <param name="args">WriteLine-style message arguments</param> | ||
100 | public void Notice(string format, params object[] args) | ||
101 | { | ||
102 | WriteNewLine(ConsoleColor.White, format, args); | ||
103 | } | ||
104 | |||
105 | /// <summary> | ||
106 | /// Sends a notice to the current console output | ||
107 | /// </summary> | ||
108 | /// <param name="sender">The module that sent this message</param> | ||
109 | /// <param name="format">The message to send</param> | ||
110 | /// <param name="args">WriteLine-style message arguments</param> | ||
111 | public void Notice(string sender, string format, params object[] args) | ||
112 | { | ||
113 | WritePrefixLine(DeriveColor(sender), sender); | ||
114 | WriteNewLine(ConsoleColor.White, format, args); | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// Sends an error to the current console output | ||
119 | /// </summary> | ||
120 | /// <param name="format">The message to send</param> | ||
121 | /// <param name="args">WriteLine-style message arguments</param> | ||
122 | public void Error(string format, params object[] args) | ||
123 | { | ||
124 | WriteNewLine(ConsoleColor.Red, format, args); | ||
125 | } | ||
126 | |||
127 | /// <summary> | ||
128 | /// Sends an error to the current console output | ||
129 | /// </summary> | ||
130 | /// <param name="sender">The module that sent this message</param> | ||
131 | /// <param name="format">The message to send</param> | ||
132 | /// <param name="args">WriteLine-style message arguments</param> | ||
133 | public void Error(string sender, string format, params object[] args) | ||
134 | { | ||
135 | WritePrefixLine(DeriveColor(sender), sender); | ||
136 | Error(format, args); | ||
137 | } | ||
138 | |||
139 | /// <summary> | ||
140 | /// Sends a status message to the current console output | ||
141 | /// </summary> | ||
142 | /// <param name="format">The message to send</param> | ||
143 | /// <param name="args">WriteLine-style message arguments</param> | ||
144 | public void Status(string format, params object[] args) | ||
145 | { | ||
146 | WriteNewLine(ConsoleColor.Blue, format, args); | ||
147 | } | ||
148 | |||
149 | /// <summary> | ||
150 | /// Sends a status message to the current console output | ||
151 | /// </summary> | ||
152 | /// <param name="sender">The module that sent this message</param> | ||
153 | /// <param name="format">The message to send</param> | ||
154 | /// <param name="args">WriteLine-style message arguments</param> | ||
155 | public void Status(string sender, string format, params object[] args) | ||
156 | { | ||
157 | WritePrefixLine(DeriveColor(sender), sender); | ||
158 | WriteNewLine(ConsoleColor.Blue, format, args); | ||
159 | } | ||
160 | |||
161 | [Conditional("DEBUG")] | ||
162 | public void Debug(string format, params object[] args) | ||
163 | { | ||
164 | WriteNewLine(ConsoleColor.Gray, format, args); | ||
165 | } | ||
166 | |||
167 | [Conditional("DEBUG")] | ||
168 | public void Debug(string sender, string format, params object[] args) | ||
169 | { | ||
170 | WritePrefixLine(DeriveColor(sender), sender); | ||
171 | WriteNewLine(ConsoleColor.Gray, format, args); | ||
172 | } | ||
173 | |||
174 | private void WriteNewLine(ConsoleColor color, string format, params object[] args) | ||
175 | { | ||
176 | try | ||
177 | { | ||
178 | lock (m_syncRoot) | ||
179 | { | ||
180 | try | ||
181 | { | ||
182 | System.Console.WriteLine(format, args); | ||
183 | } | ||
184 | |||
185 | catch (FormatException) | ||
186 | { | ||
187 | System.Console.WriteLine(args); | ||
188 | } | ||
189 | |||
190 | try | ||
191 | { | ||
192 | if (color != ConsoleColor.White) | ||
193 | System.Console.ForegroundColor = color; | ||
194 | |||
195 | System.Console.WriteLine(format, args); | ||
196 | System.Console.ResetColor(); | ||
197 | } | ||
198 | catch (ArgumentNullException) | ||
199 | { | ||
200 | // Some older systems dont support coloured text. | ||
201 | System.Console.WriteLine(format, args); | ||
202 | } | ||
203 | catch (FormatException) | ||
204 | { | ||
205 | // Some older systems dont support coloured text. | ||
206 | System.Console.WriteLine(args); | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | catch (ObjectDisposedException) | ||
211 | { | ||
212 | } | ||
213 | } | ||
214 | |||
215 | private void WritePrefixLine(ConsoleColor color, string sender) | ||
216 | { | ||
217 | try | ||
218 | { | ||
219 | lock (m_syncRoot) | ||
220 | { | ||
221 | sender = sender.ToUpper(); | ||
222 | |||
223 | System.Console.WriteLine("[" + sender + "] "); | ||
224 | |||
225 | System.Console.Write("["); | ||
226 | |||
227 | try | ||
228 | { | ||
229 | System.Console.ForegroundColor = color; | ||
230 | System.Console.Write(sender); | ||
231 | System.Console.ResetColor(); | ||
232 | } | ||
233 | catch (ArgumentNullException) | ||
234 | { | ||
235 | // Some older systems dont support coloured text. | ||
236 | System.Console.WriteLine(sender); | ||
237 | } | ||
238 | |||
239 | System.Console.Write("] \t"); | ||
240 | } | ||
241 | } | ||
242 | catch (ObjectDisposedException) | ||
243 | { | ||
244 | } | ||
245 | } | ||
246 | |||
247 | public string ReadLine() | ||
248 | { | ||
249 | try | ||
250 | { | ||
251 | return System.Console.ReadLine(); | ||
252 | } | ||
253 | catch (Exception e) | ||
254 | { | ||
255 | m_log.Error("[Console]: System.Console.ReadLine exception " + e.ToString()); | ||
256 | return String.Empty; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | public int Read() | ||
261 | { | ||
262 | return System.Console.Read(); | ||
263 | } | ||
264 | |||
265 | public IPAddress CmdPromptIPAddress(string prompt, string defaultvalue) | ||
266 | { | ||
267 | IPAddress address; | ||
268 | string addressStr; | ||
269 | |||
270 | while (true) | ||
271 | { | ||
272 | addressStr = CmdPrompt(prompt, defaultvalue); | ||
273 | if (IPAddress.TryParse(addressStr, out address)) | ||
274 | { | ||
275 | break; | ||
276 | } | ||
277 | else | ||
278 | { | ||
279 | m_log.Error("Illegal address. Please re-enter."); | ||
280 | } | ||
281 | } | ||
282 | |||
283 | return address; | ||
284 | } | ||
285 | |||
286 | public uint CmdPromptIPPort(string prompt, string defaultvalue) | ||
287 | { | ||
288 | uint port; | ||
289 | string portStr; | ||
290 | |||
291 | while (true) | ||
292 | { | ||
293 | portStr = CmdPrompt(prompt, defaultvalue); | ||
294 | if (uint.TryParse(portStr, out port)) | ||
295 | { | ||
296 | if (port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort) | ||
297 | { | ||
298 | break; | ||
299 | } | ||
300 | } | ||
301 | |||
302 | m_log.Error("Illegal address. Please re-enter."); | ||
303 | } | ||
304 | |||
305 | return port; | ||
306 | } | ||
307 | |||
308 | // Displays a prompt and waits for the user to enter a string, then returns that string | ||
309 | // Done with no echo and suitable for passwords | ||
310 | public string PasswdPrompt(string prompt) | ||
311 | { | ||
312 | // FIXME: Needs to be better abstracted | ||
313 | ConsoleColor oldfg = System.Console.ForegroundColor; | ||
314 | System.Console.ForegroundColor = System.Console.BackgroundColor; | ||
315 | string temp = System.Console.ReadLine(); | ||
316 | System.Console.ForegroundColor = oldfg; | ||
317 | return temp; | ||
318 | } | ||
319 | |||
320 | // Displays a command prompt and waits for the user to enter a string, then returns that string | ||
321 | public string CmdPrompt(string prompt) | ||
322 | { | ||
323 | System.Console.WriteLine(String.Format("{0}: ", prompt)); | ||
324 | return ReadLine(); | ||
325 | } | ||
326 | |||
327 | // Displays a command prompt and returns a default value if the user simply presses enter | ||
328 | public string CmdPrompt(string prompt, string defaultresponse) | ||
329 | { | ||
330 | string temp = CmdPrompt(String.Format("{0} [{1}]", prompt, defaultresponse)); | ||
331 | if (temp == String.Empty) | ||
332 | { | ||
333 | return defaultresponse; | ||
334 | } | ||
335 | else | ||
336 | { | ||
337 | return temp; | ||
338 | } | ||
339 | } | ||
340 | |||
341 | // Displays a command prompt and returns a default value, user may only enter 1 of 2 options | ||
342 | public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB) | ||
343 | { | ||
344 | bool itisdone = false; | ||
345 | string temp = CmdPrompt(prompt, defaultresponse); | ||
346 | while (itisdone == false) | ||
347 | { | ||
348 | if ((temp == OptionA) || (temp == OptionB)) | ||
349 | { | ||
350 | itisdone = true; | ||
351 | } | ||
352 | else | ||
353 | { | ||
354 | System.Console.WriteLine("Valid options are " + OptionA + " or " + OptionB); | ||
355 | temp = CmdPrompt(prompt, defaultresponse); | ||
356 | } | ||
357 | } | ||
358 | return temp; | ||
359 | } | ||
360 | |||
361 | // Runs a command with a number of parameters | ||
362 | public Object RunCmd(string Cmd, string[] cmdparams) | ||
363 | { | ||
364 | m_cmdParser.RunCmd(Cmd, cmdparams); | ||
365 | return null; | ||
366 | } | ||
367 | |||
368 | // Shows data about something | ||
369 | public void ShowCommands(string ShowWhat) | ||
370 | { | ||
371 | m_cmdParser.Show(ShowWhat); | ||
372 | } | ||
373 | |||
374 | public void Prompt() | ||
375 | { | ||
376 | string tempstr = CmdPrompt(m_componentName + "# "); | ||
377 | RunCommand(tempstr); | ||
378 | } | ||
379 | |||
380 | public void RunCommand(string command) | ||
381 | { | ||
382 | string[] tempstrarray; | ||
383 | tempstrarray = command.Split(' '); | ||
384 | string cmd = tempstrarray[0]; | ||
385 | Array.Reverse(tempstrarray); | ||
386 | Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1); | ||
387 | Array.Reverse(tempstrarray); | ||
388 | string[] cmdparams = (string[]) tempstrarray; | ||
389 | try | ||
390 | { | ||
391 | RunCmd(cmd, cmdparams); | ||
392 | } | ||
393 | catch (Exception e) | ||
394 | { | ||
395 | m_log.Error("[Console]: Command failed with exception " + e.ToString()); | ||
396 | } | ||
397 | } | ||
398 | |||
399 | public string LineInfo | ||
400 | { | ||
401 | get | ||
402 | { | ||
403 | string result = String.Empty; | ||
404 | |||
405 | string stacktrace = Environment.StackTrace; | ||
406 | List<string> lines = new List<string>(stacktrace.Split(new string[] {"at "}, StringSplitOptions.None)); | ||
407 | |||
408 | if (lines.Count > 4) | ||
409 | { | ||
410 | lines.RemoveRange(0, 4); | ||
411 | |||
412 | string tmpLine = lines[0]; | ||
413 | |||
414 | int inIndex = tmpLine.IndexOf(" in "); | ||
415 | |||
416 | if (inIndex > -1) | ||
417 | { | ||
418 | result = tmpLine.Substring(0, inIndex); | ||
419 | |||
420 | int lineIndex = tmpLine.IndexOf(":line "); | ||
421 | |||
422 | if (lineIndex > -1) | ||
423 | { | ||
424 | lineIndex += 6; | ||
425 | result += ", line " + tmpLine.Substring(lineIndex, (tmpLine.Length - lineIndex) - 5); | ||
426 | } | ||
427 | } | ||
428 | } | ||
429 | return result; | ||
430 | } | ||
431 | } | ||
432 | } | ||
433 | } | ||