aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Console/LogBase.cs
diff options
context:
space:
mode:
authorAdam Frisby2007-07-11 08:10:25 +0000
committerAdam Frisby2007-07-11 08:10:25 +0000
commite2ff441e31328e60c8bb1d4bb32fa4ac64f91978 (patch)
tree8405b6cef57b66a58f31a24c859846085d0b81f7 /OpenSim/Framework/Console/LogBase.cs
parent* Wiping trunk in prep for Sugilite (diff)
parent* Applying dalien's patches from bug#177 and #179 (diff)
downloadopensim-SC_OLD-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.zip
opensim-SC_OLD-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.tar.gz
opensim-SC_OLD-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.tar.bz2
opensim-SC_OLD-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.tar.xz
* Bringing Sugilite in to trunk
Diffstat (limited to 'OpenSim/Framework/Console/LogBase.cs')
-rw-r--r--OpenSim/Framework/Console/LogBase.cs282
1 files changed, 282 insertions, 0 deletions
diff --git a/OpenSim/Framework/Console/LogBase.cs b/OpenSim/Framework/Console/LogBase.cs
new file mode 100644
index 0000000..f73e15e
--- /dev/null
+++ b/OpenSim/Framework/Console/LogBase.cs
@@ -0,0 +1,282 @@
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*/
28using System;
29using System.IO;
30using System.Net;
31
32namespace OpenSim.Framework.Console
33{
34 public enum LogPriority : int
35 {
36 CRITICAL,
37 HIGH,
38 MEDIUM,
39 NORMAL,
40 LOW,
41 VERBOSE,
42 EXTRAVERBOSE
43 }
44
45 public class LogBase
46 {
47 StreamWriter Log;
48 public conscmd_callback cmdparser;
49 public string componentname;
50 private bool m_silent;
51
52 public LogBase(string LogFile, string componentname, conscmd_callback cmdparser, bool silent)
53 {
54 this.componentname = componentname;
55 this.cmdparser = cmdparser;
56 this.m_silent = silent;
57 System.Console.WriteLine("ServerConsole.cs - creating new local console");
58
59 if (String.IsNullOrEmpty(LogFile))
60 {
61 LogFile = componentname + ".log";
62 }
63
64 System.Console.WriteLine("Logs will be saved to current directory in " + LogFile);
65 Log = File.AppendText(LogFile);
66 Log.WriteLine("========================================================================");
67 Log.WriteLine(componentname + " Started at " + DateTime.Now.ToString());
68 }
69
70 public void Close()
71 {
72 Log.WriteLine("Shutdown at " + DateTime.Now.ToString());
73 Log.Close();
74 }
75
76 public void Write(string format, params object[] args)
77 {
78 Notice(format, args);
79 return;
80 }
81
82 public void WriteLine(LogPriority importance, string format, params object[] args)
83 {
84 Log.WriteLine(format, args);
85 Log.Flush();
86 if (!m_silent)
87 {
88 System.Console.WriteLine(format, args);
89 }
90 return;
91 }
92
93 public void Warn(string format, params object[] args)
94 {
95 WriteNewLine(ConsoleColor.Yellow, format, args);
96 return;
97 }
98
99 public void Notice(string format, params object[] args)
100 {
101 WriteNewLine(ConsoleColor.White, format, args);
102 return;
103 }
104
105 public void Error(string format, params object[] args)
106 {
107 WriteNewLine(ConsoleColor.Red, format, args);
108 return;
109 }
110
111 public void Verbose(string format, params object[] args)
112 {
113 WriteNewLine(ConsoleColor.Gray, format, args);
114 return;
115 }
116
117 public void Status(string format, params object[] args)
118 {
119 WriteNewLine(ConsoleColor.Blue, format, args);
120 return;
121 }
122
123 private void WriteNewLine(ConsoleColor color, string format, params object[] args)
124 {
125 Log.WriteLine(format, args);
126 Log.Flush();
127 if (!m_silent)
128 {
129 try
130 {
131 System.Console.ForegroundColor = color;
132 System.Console.WriteLine(format, args);
133 System.Console.ResetColor();
134 }
135 catch (ArgumentNullException)
136 {
137 // Some older systems dont support coloured text.
138 System.Console.WriteLine(format, args);
139 }
140 }
141 return;
142 }
143
144 public string ReadLine()
145 {
146 string TempStr = System.Console.ReadLine();
147 Log.WriteLine(TempStr);
148 return TempStr;
149 }
150
151 public int Read()
152 {
153 int TempInt = System.Console.Read();
154 Log.Write((char)TempInt);
155 return TempInt;
156 }
157
158 public IPAddress CmdPromptIPAddress(string prompt, string defaultvalue)
159 {
160 IPAddress address;
161 string addressStr;
162
163 while (true)
164 {
165 addressStr = MainLog.Instance.CmdPrompt(prompt, defaultvalue);
166 if (IPAddress.TryParse(addressStr, out address))
167 {
168 break;
169 }
170 else
171 {
172 MainLog.Instance.Error("Illegal address. Please re-enter.");
173 }
174 }
175
176 return address;
177 }
178
179 public int CmdPromptIPPort(string prompt, string defaultvalue)
180 {
181 int port;
182 string portStr;
183
184 while (true)
185 {
186 portStr = MainLog.Instance.CmdPrompt(prompt, defaultvalue);
187 if (int.TryParse(portStr, out port))
188 {
189 if (port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort)
190 {
191 break;
192 }
193 }
194
195 MainLog.Instance.Error("Illegal address. Please re-enter.");
196 }
197
198 return port;
199 }
200
201 // Displays a prompt and waits for the user to enter a string, then returns that string
202 // Done with no echo and suitable for passwords
203 public string PasswdPrompt(string prompt)
204 {
205 // FIXME: Needs to be better abstracted
206 Log.WriteLine(prompt);
207 this.Write(prompt);
208 ConsoleColor oldfg = System.Console.ForegroundColor;
209 System.Console.ForegroundColor = System.Console.BackgroundColor;
210 string temp = System.Console.ReadLine();
211 System.Console.ForegroundColor = oldfg;
212 return temp;
213 }
214
215 // Displays a command prompt and waits for the user to enter a string, then returns that string
216 public string CmdPrompt(string prompt)
217 {
218 this.Write(String.Format("{0}: ", prompt));
219 return this.ReadLine();
220 }
221
222 // Displays a command prompt and returns a default value if the user simply presses enter
223 public string CmdPrompt(string prompt, string defaultresponse)
224 {
225 string temp = CmdPrompt(String.Format("{0} [{1}]", prompt, defaultresponse));
226 if (temp == "")
227 {
228 return defaultresponse;
229 }
230 else
231 {
232 return temp;
233 }
234 }
235
236 // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
237 public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB)
238 {
239 bool itisdone = false;
240 string temp = CmdPrompt(prompt, defaultresponse);
241 while (itisdone == false)
242 {
243 if ((temp == OptionA) || (temp == OptionB))
244 {
245 itisdone = true;
246 }
247 else
248 {
249 Notice("Valid options are " + OptionA + " or " + OptionB);
250 temp = CmdPrompt(prompt, defaultresponse);
251 }
252 }
253 return temp;
254 }
255
256 // Runs a command with a number of parameters
257 public Object RunCmd(string Cmd, string[] cmdparams)
258 {
259 cmdparser.RunCmd(Cmd, cmdparams);
260 return null;
261 }
262
263 // Shows data about something
264 public void ShowCommands(string ShowWhat)
265 {
266 cmdparser.Show(ShowWhat);
267 }
268
269 public void MainLogPrompt()
270 {
271 string[] tempstrarray;
272 string tempstr = this.CmdPrompt(this.componentname + "# ");
273 tempstrarray = tempstr.Split(' ');
274 string cmd = tempstrarray[0];
275 Array.Reverse(tempstrarray);
276 Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1);
277 Array.Reverse(tempstrarray);
278 string[] cmdparams = (string[])tempstrarray;
279 RunCmd(cmd, cmdparams);
280 }
281 }
282}