aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Console/LogBase.cs
diff options
context:
space:
mode:
authorJeff Ames2008-02-05 19:44:27 +0000
committerJeff Ames2008-02-05 19:44:27 +0000
commit6ed5283bc06a62f38eb517e67b975832b603bf61 (patch)
treee5f635018789b73a99ddeca0883a68368fa5eece /OpenSim/Framework/Console/LogBase.cs
parentCut down on the number of packets sent during terraforming. Terraforming shou... (diff)
downloadopensim-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/LogBase.cs')
-rw-r--r--OpenSim/Framework/Console/LogBase.cs511
1 files changed, 0 insertions, 511 deletions
diff --git a/OpenSim/Framework/Console/LogBase.cs b/OpenSim/Framework/Console/LogBase.cs
deleted file mode 100644
index a2c4b3a..0000000
--- a/OpenSim/Framework/Console/LogBase.cs
+++ /dev/null
@@ -1,511 +0,0 @@
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*/
28using System;
29using System.Collections.Generic;
30using System.Diagnostics;
31using System.IO;
32using System.Net;
33
34namespace OpenSim.Framework.Console
35{
36 public enum LogPriority : int
37 {
38 CRITICAL,
39 HIGH,
40 MEDIUM,
41 NORMAL,
42 LOW,
43 VERBOSE,
44 EXTRAVERBOSE
45 }
46
47 public class LogBase
48 {
49 private object m_syncRoot = new object();
50
51 private StreamWriter Log;
52 public conscmd_callback cmdparser;
53 public string componentname;
54 private bool m_verbose;
55
56 public LogBase(string LogFile, string componentname, conscmd_callback cmdparser, bool verbose)
57 {
58 this.componentname = componentname;
59 this.cmdparser = cmdparser;
60 m_verbose = verbose;
61 System.Console.WriteLine("Creating new local console");
62
63 if (String.IsNullOrEmpty(LogFile))
64 {
65 LogFile = componentname + ".log";
66 }
67
68 System.Console.WriteLine("Logs will be saved to current directory in " + LogFile);
69
70 try
71 {
72 Log = File.AppendText(LogFile);
73 }
74 catch (Exception ex)
75 {
76 System.Console.WriteLine("Unable to open log file. Do you already have another copy of OpenSim running? Permission problem?");
77 System.Console.WriteLine(ex.Message);
78 System.Console.WriteLine("");
79 System.Console.WriteLine("Application is terminating.");
80 System.Console.WriteLine("");
81 System.Threading.Thread.CurrentThread.Abort();
82 }
83 Log.WriteLine("========================================================================");
84 Log.WriteLine(componentname + " Started at " + DateTime.Now.ToString());
85 }
86
87 public void Close()
88 {
89 Log.WriteLine("Shutdown at " + DateTime.Now.ToString());
90 Log.Close();
91 }
92
93 /// <summary>
94 /// derive an ansi color from a string, ignoring the darker colors.
95 /// This is used to help automatically bin component tags with colors
96 /// in various print functions.
97 /// </summary>
98 /// <param name="input">arbitrary string for input</param>
99 /// <returns>an ansii color</returns>
100 private ConsoleColor DeriveColor(string input)
101 {
102 int colIdx = (input.ToUpper().GetHashCode()%6) + 9;
103 return (ConsoleColor) colIdx;
104 }
105
106 /// <summary>
107 /// Sends a warning to the current log output
108 /// </summary>
109 /// <param name="format">The message to send</param>
110 /// <param name="args">WriteLine-style message arguments</param>
111 public void Warn(string format, params object[] args)
112 {
113 WriteNewLine(ConsoleColor.Yellow, format, args);
114 return;
115 }
116
117 /// <summary>
118 /// Sends a warning to the current log output
119 /// </summary>
120 /// <param name="sender">The module that sent this message</param>
121 /// <param name="format">The message to send</param>
122 /// <param name="args">WriteLine-style message arguments</param>
123 public void Warn(string sender, string format, params object[] args)
124 {
125 WritePrefixLine(DeriveColor(sender), sender);
126 WriteNewLine(ConsoleColor.Yellow, format, args);
127 return;
128 }
129
130 /// <summary>
131 /// Sends a notice to the current log output
132 /// </summary>
133 /// <param name="format">The message to send</param>
134 /// <param name="args">WriteLine-style message arguments</param>
135 public void Notice(string format, params object[] args)
136 {
137 WriteNewLine(ConsoleColor.White, format, args);
138 return;
139 }
140
141 /// <summary>
142 /// Sends a notice to the current log output
143 /// </summary>
144 /// <param name="sender">The module that sent this message</param>
145 /// <param name="format">The message to send</param>
146 /// <param name="args">WriteLine-style message arguments</param>
147 public void Notice(string sender, string format, params object[] args)
148 {
149 WritePrefixLine(DeriveColor(sender), sender);
150 WriteNewLine(ConsoleColor.White, format, args);
151 return;
152 }
153
154 /// <summary>
155 /// Sends an error to the current log output
156 /// </summary>
157 /// <param name="format">The message to send</param>
158 /// <param name="args">WriteLine-style message arguments</param>
159 public void Error(string format, params object[] args)
160 {
161 WriteNewLine(ConsoleColor.Red, format, args);
162 return;
163 }
164
165 /// <summary>
166 /// Sends an error to the current log output
167 /// </summary>
168 /// <param name="sender">The module that sent this message</param>
169 /// <param name="format">The message to send</param>
170 /// <param name="args">WriteLine-style message arguments</param>
171 public void Error(string sender, string format, params object[] args)
172 {
173 WritePrefixLine(DeriveColor(sender), sender);
174 Error(format, args);
175 return;
176 }
177
178 /// <summary>
179 /// Sends an informational message to the current log output
180 /// </summary>
181 /// <param name="sender">The module that sent this message</param>
182 /// <param name="format">The message to send</param>
183 /// <param name="args">WriteLine-style message arguments</param>
184 public void Verbose(string sender, string format, params object[] args)
185 {
186 if (m_verbose)
187 {
188 WritePrefixLine(DeriveColor(sender), sender);
189 WriteNewLine(ConsoleColor.Gray, format, args);
190 return;
191 }
192 }
193
194 /// <summary>
195 /// Sends a status message to the current log output
196 /// </summary>
197 /// <param name="format">The message to send</param>
198 /// <param name="args">WriteLine-style message arguments</param>
199 public void Status(string format, params object[] args)
200 {
201 WriteNewLine(ConsoleColor.Blue, format, args);
202 return;
203 }
204
205 /// <summary>
206 /// Sends a status message to the current log output
207 /// </summary>
208 /// <param name="sender">The module that sent this message</param>
209 /// <param name="format">The message to send</param>
210 /// <param name="args">WriteLine-style message arguments</param>
211 public void Status(string sender, string format, params object[] args)
212 {
213 WritePrefixLine(DeriveColor(sender), sender);
214 WriteNewLine(ConsoleColor.Blue, format, args);
215 return;
216 }
217
218 [Conditional("DEBUG")]
219 public void Debug(string format, params object[] args)
220 {
221 WriteNewLine(ConsoleColor.Gray, format, args);
222 return;
223 }
224
225 [Conditional("DEBUG")]
226 public void Debug(string sender, string format, params object[] args)
227 {
228 WritePrefixLine(DeriveColor(sender), sender);
229 WriteNewLine(ConsoleColor.Gray, format, args);
230 return;
231 }
232
233 private void WriteNewLine(ConsoleColor color, string format, params object[] args)
234 {
235 try
236 {
237 lock (m_syncRoot)
238 {
239 string now = DateTime.Now.ToString("[MM-dd HH:mm:ss] ");
240 Log.Write(now);
241 try
242 {
243 Log.WriteLine(format, args);
244 Log.Flush();
245 }
246
247 catch (FormatException)
248 {
249 System.Console.WriteLine(args);
250 }
251 System.Console.Write(now);
252 try
253 {
254 if (color != ConsoleColor.White)
255 System.Console.ForegroundColor = color;
256
257 System.Console.WriteLine(format, args);
258 System.Console.ResetColor();
259 }
260 catch (ArgumentNullException)
261 {
262 // Some older systems dont support coloured text.
263 System.Console.WriteLine(format, args);
264 }
265 catch (FormatException)
266 {
267 // Some older systems dont support coloured text.
268 System.Console.WriteLine(args);
269 }
270
271 return;
272 }
273 }
274 catch (ObjectDisposedException)
275 {
276 return;
277 }
278 }
279
280 private void WritePrefixLine(ConsoleColor color, string sender)
281 {
282 try
283 {
284 lock (m_syncRoot)
285 {
286 sender = sender.ToUpper();
287
288 Log.WriteLine("[" + sender + "] ");
289
290
291 Log.Flush();
292
293 System.Console.Write("[");
294
295 try
296 {
297 System.Console.ForegroundColor = color;
298 System.Console.Write(sender);
299 System.Console.ResetColor();
300 }
301 catch (ArgumentNullException)
302 {
303 // Some older systems dont support coloured text.
304 System.Console.WriteLine(sender);
305 }
306
307 System.Console.Write("] \t");
308
309 return;
310 }
311 }
312 catch (ObjectDisposedException)
313 {
314 return;
315 }
316 }
317
318
319 public string ReadLine()
320 {
321 try
322 {
323 string TempStr = System.Console.ReadLine();
324 Log.WriteLine(TempStr);
325 return TempStr;
326 }
327 catch (Exception e)
328 {
329 MainLog.Instance.Error("Console", "System.Console.ReadLine exception " + e.ToString());
330 return String.Empty;
331 }
332 }
333
334 public int Read()
335 {
336 int TempInt = System.Console.Read();
337 Log.Write((char) TempInt);
338 return TempInt;
339 }
340
341 public IPAddress CmdPromptIPAddress(string prompt, string defaultvalue)
342 {
343 IPAddress address;
344 string addressStr;
345
346 while (true)
347 {
348 addressStr = MainLog.Instance.CmdPrompt(prompt, defaultvalue);
349 if (IPAddress.TryParse(addressStr, out address))
350 {
351 break;
352 }
353 else
354 {
355 MainLog.Instance.Error("Illegal address. Please re-enter.");
356 }
357 }
358
359 return address;
360 }
361
362 public uint CmdPromptIPPort(string prompt, string defaultvalue)
363 {
364 uint port;
365 string portStr;
366
367 while (true)
368 {
369 portStr = MainLog.Instance.CmdPrompt(prompt, defaultvalue);
370 if (uint.TryParse(portStr, out port))
371 {
372 if (port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort)
373 {
374 break;
375 }
376 }
377
378 MainLog.Instance.Error("Illegal address. Please re-enter.");
379 }
380
381 return port;
382 }
383
384 // Displays a prompt and waits for the user to enter a string, then returns that string
385 // Done with no echo and suitable for passwords
386 public string PasswdPrompt(string prompt)
387 {
388 // FIXME: Needs to be better abstracted
389 Log.WriteLine(prompt);
390 Notice(prompt);
391 ConsoleColor oldfg = System.Console.ForegroundColor;
392 System.Console.ForegroundColor = System.Console.BackgroundColor;
393 string temp = System.Console.ReadLine();
394 System.Console.ForegroundColor = oldfg;
395 return temp;
396 }
397
398 // Displays a command prompt and waits for the user to enter a string, then returns that string
399 public string CmdPrompt(string prompt)
400 {
401 Notice(String.Format("{0}: ", prompt));
402 return ReadLine();
403 }
404
405 // Displays a command prompt and returns a default value if the user simply presses enter
406 public string CmdPrompt(string prompt, string defaultresponse)
407 {
408 string temp = CmdPrompt(String.Format("{0} [{1}]", prompt, defaultresponse));
409 if (temp == String.Empty)
410 {
411 return defaultresponse;
412 }
413 else
414 {
415 return temp;
416 }
417 }
418
419 // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
420 public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB)
421 {
422 bool itisdone = false;
423 string temp = CmdPrompt(prompt, defaultresponse);
424 while (itisdone == false)
425 {
426 if ((temp == OptionA) || (temp == OptionB))
427 {
428 itisdone = true;
429 }
430 else
431 {
432 Notice("Valid options are " + OptionA + " or " + OptionB);
433 temp = CmdPrompt(prompt, defaultresponse);
434 }
435 }
436 return temp;
437 }
438
439 // Runs a command with a number of parameters
440 public Object RunCmd(string Cmd, string[] cmdparams)
441 {
442 cmdparser.RunCmd(Cmd, cmdparams);
443 return null;
444 }
445
446 // Shows data about something
447 public void ShowCommands(string ShowWhat)
448 {
449 cmdparser.Show(ShowWhat);
450 }
451
452 public void MainLogPrompt()
453 {
454 string tempstr = CmdPrompt(componentname + "# ");
455 MainLogRunCommand(tempstr);
456 }
457
458 public void MainLogRunCommand(string command)
459 {
460 string[] tempstrarray;
461 tempstrarray = command.Split(' ');
462 string cmd = tempstrarray[0];
463 Array.Reverse(tempstrarray);
464 Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1);
465 Array.Reverse(tempstrarray);
466 string[] cmdparams = (string[]) tempstrarray;
467 try
468 {
469 RunCmd(cmd, cmdparams);
470 }
471 catch (Exception e)
472 {
473 MainLog.Instance.Error("Console", "Command failed with exception " + e.ToString());
474 }
475 }
476
477 public string LineInfo
478 {
479 get
480 {
481 string result = String.Empty;
482
483 string stacktrace = Environment.StackTrace;
484 List<string> lines = new List<string>(stacktrace.Split(new string[] {"at "}, StringSplitOptions.None));
485
486 if (lines.Count > 4)
487 {
488 lines.RemoveRange(0, 4);
489
490 string tmpLine = lines[0];
491
492 int inIndex = tmpLine.IndexOf(" in ");
493
494 if (inIndex > -1)
495 {
496 result = tmpLine.Substring(0, inIndex);
497
498 int lineIndex = tmpLine.IndexOf(":line ");
499
500 if (lineIndex > -1)
501 {
502 lineIndex += 6;
503 result += ", line " + tmpLine.Substring(lineIndex, (tmpLine.Length - lineIndex) - 5);
504 }
505 }
506 }
507 return result;
508 }
509 }
510 }
511}