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