diff options
Diffstat (limited to 'Common/OpenSim.Framework.Console/LogBase.cs')
-rw-r--r-- | Common/OpenSim.Framework.Console/LogBase.cs | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/Common/OpenSim.Framework.Console/LogBase.cs b/Common/OpenSim.Framework.Console/LogBase.cs new file mode 100644 index 0000000..b625b75 --- /dev/null +++ b/Common/OpenSim.Framework.Console/LogBase.cs | |||
@@ -0,0 +1,230 @@ | |||
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 | System.Console.ForegroundColor = color; | ||
129 | System.Console.WriteLine(format, args); | ||
130 | System.Console.ResetColor(); | ||
131 | } | ||
132 | return; | ||
133 | } | ||
134 | |||
135 | public string ReadLine() | ||
136 | { | ||
137 | string TempStr = System.Console.ReadLine(); | ||
138 | Log.WriteLine(TempStr); | ||
139 | return TempStr; | ||
140 | } | ||
141 | |||
142 | public int Read() | ||
143 | { | ||
144 | int TempInt = System.Console.Read(); | ||
145 | Log.Write((char)TempInt); | ||
146 | return TempInt; | ||
147 | } | ||
148 | |||
149 | // Displays a prompt and waits for the user to enter a string, then returns that string | ||
150 | // Done with no echo and suitable for passwords | ||
151 | public string PasswdPrompt(string prompt) | ||
152 | { | ||
153 | // FIXME: Needs to be better abstracted | ||
154 | Log.WriteLine(prompt); | ||
155 | this.Write(prompt); | ||
156 | ConsoleColor oldfg = System.Console.ForegroundColor; | ||
157 | System.Console.ForegroundColor = System.Console.BackgroundColor; | ||
158 | string temp = System.Console.ReadLine(); | ||
159 | System.Console.ForegroundColor = oldfg; | ||
160 | return temp; | ||
161 | } | ||
162 | |||
163 | // Displays a command prompt and waits for the user to enter a string, then returns that string | ||
164 | public string CmdPrompt(string prompt) | ||
165 | { | ||
166 | this.Write(String.Format("{0}: ", prompt)); | ||
167 | return this.ReadLine(); | ||
168 | } | ||
169 | |||
170 | // Displays a command prompt and returns a default value if the user simply presses enter | ||
171 | public string CmdPrompt(string prompt, string defaultresponse) | ||
172 | { | ||
173 | string temp = CmdPrompt(String.Format( "{0} [{1}]", prompt, defaultresponse )); | ||
174 | if (temp == "") | ||
175 | { | ||
176 | return defaultresponse; | ||
177 | } | ||
178 | else | ||
179 | { | ||
180 | return temp; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | // Displays a command prompt and returns a default value, user may only enter 1 of 2 options | ||
185 | public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB) | ||
186 | { | ||
187 | bool itisdone = false; | ||
188 | string temp = CmdPrompt(prompt, defaultresponse); | ||
189 | while (itisdone == false) | ||
190 | { | ||
191 | if ((temp == OptionA) || (temp == OptionB)) | ||
192 | { | ||
193 | itisdone = true; | ||
194 | } | ||
195 | else | ||
196 | { | ||
197 | Notice("Valid options are " + OptionA + " or " + OptionB); | ||
198 | temp = CmdPrompt(prompt, defaultresponse); | ||
199 | } | ||
200 | } | ||
201 | return temp; | ||
202 | } | ||
203 | |||
204 | // Runs a command with a number of parameters | ||
205 | public Object RunCmd(string Cmd, string[] cmdparams) | ||
206 | { | ||
207 | cmdparser.RunCmd(Cmd, cmdparams); | ||
208 | return null; | ||
209 | } | ||
210 | |||
211 | // Shows data about something | ||
212 | public void ShowCommands(string ShowWhat) | ||
213 | { | ||
214 | cmdparser.Show(ShowWhat); | ||
215 | } | ||
216 | |||
217 | public void MainLogPrompt() | ||
218 | { | ||
219 | string[] tempstrarray; | ||
220 | string tempstr = this.CmdPrompt(this.componentname + "# "); | ||
221 | tempstrarray = tempstr.Split(' '); | ||
222 | string cmd = tempstrarray[0]; | ||
223 | Array.Reverse(tempstrarray); | ||
224 | Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1); | ||
225 | Array.Reverse(tempstrarray); | ||
226 | string[] cmdparams = (string[])tempstrarray; | ||
227 | RunCmd(cmd, cmdparams); | ||
228 | } | ||
229 | } | ||
230 | } | ||