diff options
author | Justin Clark-Casey (justincc) | 2012-11-22 04:45:53 +0000 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2012-11-22 04:45:53 +0000 |
commit | 45d1e6ab09309e21f96979548b5d1b4904800f65 (patch) | |
tree | 7100fb7c02636a06e9c7de8d0c644de5f118c5e1 /OpenSim/Framework/Servers/ServerBase.cs | |
parent | Factor out common registration of "show uptime" command (diff) | |
download | opensim-SC_OLD-45d1e6ab09309e21f96979548b5d1b4904800f65.zip opensim-SC_OLD-45d1e6ab09309e21f96979548b5d1b4904800f65.tar.gz opensim-SC_OLD-45d1e6ab09309e21f96979548b5d1b4904800f65.tar.bz2 opensim-SC_OLD-45d1e6ab09309e21f96979548b5d1b4904800f65.tar.xz |
Make "show info" command available across all servers
This helpfully lists version information, startup location and console log level
Diffstat (limited to 'OpenSim/Framework/Servers/ServerBase.cs')
-rw-r--r-- | OpenSim/Framework/Servers/ServerBase.cs | 184 |
1 files changed, 182 insertions, 2 deletions
diff --git a/OpenSim/Framework/Servers/ServerBase.cs b/OpenSim/Framework/Servers/ServerBase.cs index 0835aad..129b5fa 100644 --- a/OpenSim/Framework/Servers/ServerBase.cs +++ b/OpenSim/Framework/Servers/ServerBase.cs | |||
@@ -27,26 +27,90 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.IO; | ||
31 | using System.Reflection; | ||
30 | using System.Text; | 32 | using System.Text; |
33 | using System.Text.RegularExpressions; | ||
34 | using log4net; | ||
35 | using log4net.Appender; | ||
36 | using log4net.Core; | ||
37 | using log4net.Repository; | ||
38 | using Nini.Config; | ||
31 | using OpenSim.Framework.Console; | 39 | using OpenSim.Framework.Console; |
32 | 40 | ||
33 | namespace OpenSim.Framework.Servers | 41 | namespace OpenSim.Framework.Servers |
34 | { | 42 | { |
35 | public class ServerBase | 43 | public class ServerBase |
36 | { | 44 | { |
45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
46 | |||
37 | /// <summary> | 47 | /// <summary> |
38 | /// Console to be used for any command line output. Can be null, in which case there should be no output. | 48 | /// Console to be used for any command line output. Can be null, in which case there should be no output. |
39 | /// </summary> | 49 | /// </summary> |
40 | protected ICommandConsole m_console; | 50 | protected ICommandConsole m_console; |
41 | 51 | ||
52 | protected OpenSimAppender m_consoleAppender; | ||
53 | protected FileAppender m_logFileAppender; | ||
54 | |||
55 | protected DateTime m_startuptime; | ||
56 | protected string m_startupDirectory = Environment.CurrentDirectory; | ||
57 | |||
42 | /// <summary> | 58 | /// <summary> |
43 | /// Time at which this server was started | 59 | /// Server version information. Usually VersionInfo + information about git commit, operating system, etc. |
44 | /// </summary> | 60 | /// </summary> |
45 | protected DateTime m_startuptime; | 61 | protected string m_version; |
46 | 62 | ||
47 | public ServerBase() | 63 | public ServerBase() |
48 | { | 64 | { |
49 | m_startuptime = DateTime.Now; | 65 | m_startuptime = DateTime.Now; |
66 | m_version = VersionInfo.Version; | ||
67 | EnhanceVersionInformation(); | ||
68 | } | ||
69 | |||
70 | public void RegisterCommonAppenders(IConfig startupConfig) | ||
71 | { | ||
72 | ILoggerRepository repository = LogManager.GetRepository(); | ||
73 | IAppender[] appenders = repository.GetAppenders(); | ||
74 | |||
75 | foreach (IAppender appender in appenders) | ||
76 | { | ||
77 | if (appender.Name == "Console") | ||
78 | { | ||
79 | m_consoleAppender = (OpenSimAppender)appender; | ||
80 | } | ||
81 | else if (appender.Name == "LogFileAppender") | ||
82 | { | ||
83 | m_logFileAppender = (FileAppender)appender; | ||
84 | } | ||
85 | } | ||
86 | |||
87 | if (null == m_consoleAppender) | ||
88 | { | ||
89 | Notice("No appender named Console found (see the log4net config file for this executable)!"); | ||
90 | } | ||
91 | else | ||
92 | { | ||
93 | // FIXME: This should be done through an interface rather than casting. | ||
94 | m_consoleAppender.Console = (ConsoleBase)m_console; | ||
95 | |||
96 | // If there is no threshold set then the threshold is effectively everything. | ||
97 | if (null == m_consoleAppender.Threshold) | ||
98 | m_consoleAppender.Threshold = Level.All; | ||
99 | |||
100 | Notice(String.Format("Console log level is {0}", m_consoleAppender.Threshold)); | ||
101 | } | ||
102 | |||
103 | if (m_logFileAppender != null && startupConfig != null) | ||
104 | { | ||
105 | string cfgFileName = startupConfig.GetString("LogFile", null); | ||
106 | if (cfgFileName != null) | ||
107 | { | ||
108 | m_logFileAppender.File = cfgFileName; | ||
109 | m_logFileAppender.ActivateOptions(); | ||
110 | } | ||
111 | |||
112 | m_log.InfoFormat("[LOGGING]: Logging started to file {0}", m_logFileAppender.File); | ||
113 | } | ||
50 | } | 114 | } |
51 | 115 | ||
52 | /// <summary> | 116 | /// <summary> |
@@ -58,6 +122,9 @@ namespace OpenSim.Framework.Servers | |||
58 | return; | 122 | return; |
59 | 123 | ||
60 | m_console.Commands.AddCommand( | 124 | m_console.Commands.AddCommand( |
125 | "General", false, "show info", "show info", "Show general information about the server", HandleShow); | ||
126 | |||
127 | m_console.Commands.AddCommand( | ||
61 | "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow); | 128 | "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow); |
62 | } | 129 | } |
63 | 130 | ||
@@ -71,6 +138,10 @@ namespace OpenSim.Framework.Servers | |||
71 | 138 | ||
72 | switch (showParams[0]) | 139 | switch (showParams[0]) |
73 | { | 140 | { |
141 | case "info": | ||
142 | ShowInfo(); | ||
143 | break; | ||
144 | |||
74 | case "uptime": | 145 | case "uptime": |
75 | Notice(GetUptimeReport()); | 146 | Notice(GetUptimeReport()); |
76 | break; | 147 | break; |
@@ -90,6 +161,115 @@ namespace OpenSim.Framework.Servers | |||
90 | return sb.ToString(); | 161 | return sb.ToString(); |
91 | } | 162 | } |
92 | 163 | ||
164 | protected void ShowInfo() | ||
165 | { | ||
166 | Notice(GetVersionText()); | ||
167 | Notice("Startup directory: " + m_startupDirectory); | ||
168 | if (null != m_consoleAppender) | ||
169 | Notice(String.Format("Console log level: {0}", m_consoleAppender.Threshold)); | ||
170 | } | ||
171 | |||
172 | /// <summary> | ||
173 | /// Enhance the version string with extra information if it's available. | ||
174 | /// </summary> | ||
175 | protected void EnhanceVersionInformation() | ||
176 | { | ||
177 | string buildVersion = string.Empty; | ||
178 | |||
179 | // The subversion information is deprecated and will be removed at a later date | ||
180 | // Add subversion revision information if available | ||
181 | // Try file "svn_revision" in the current directory first, then the .svn info. | ||
182 | // This allows to make the revision available in simulators not running from the source tree. | ||
183 | // FIXME: Making an assumption about the directory we're currently in - we do this all over the place | ||
184 | // elsewhere as well | ||
185 | string gitDir = "../.git/"; | ||
186 | string gitRefPointerPath = gitDir + "HEAD"; | ||
187 | |||
188 | string svnRevisionFileName = "svn_revision"; | ||
189 | string svnFileName = ".svn/entries"; | ||
190 | string manualVersionFileName = ".version"; | ||
191 | string inputLine; | ||
192 | int strcmp; | ||
193 | |||
194 | if (File.Exists(manualVersionFileName)) | ||
195 | { | ||
196 | using (StreamReader CommitFile = File.OpenText(manualVersionFileName)) | ||
197 | buildVersion = CommitFile.ReadLine(); | ||
198 | |||
199 | m_version += buildVersion ?? ""; | ||
200 | } | ||
201 | else if (File.Exists(gitRefPointerPath)) | ||
202 | { | ||
203 | // m_log.DebugFormat("[OPENSIM]: Found {0}", gitRefPointerPath); | ||
204 | |||
205 | string rawPointer = ""; | ||
206 | |||
207 | using (StreamReader pointerFile = File.OpenText(gitRefPointerPath)) | ||
208 | rawPointer = pointerFile.ReadLine(); | ||
209 | |||
210 | // m_log.DebugFormat("[OPENSIM]: rawPointer [{0}]", rawPointer); | ||
211 | |||
212 | Match m = Regex.Match(rawPointer, "^ref: (.+)$"); | ||
213 | |||
214 | if (m.Success) | ||
215 | { | ||
216 | // m_log.DebugFormat("[OPENSIM]: Matched [{0}]", m.Groups[1].Value); | ||
217 | |||
218 | string gitRef = m.Groups[1].Value; | ||
219 | string gitRefPath = gitDir + gitRef; | ||
220 | if (File.Exists(gitRefPath)) | ||
221 | { | ||
222 | // m_log.DebugFormat("[OPENSIM]: Found gitRefPath [{0}]", gitRefPath); | ||
223 | |||
224 | using (StreamReader refFile = File.OpenText(gitRefPath)) | ||
225 | { | ||
226 | string gitHash = refFile.ReadLine(); | ||
227 | m_version += gitHash.Substring(0, 7); | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | else | ||
233 | { | ||
234 | // Remove the else logic when subversion mirror is no longer used | ||
235 | if (File.Exists(svnRevisionFileName)) | ||
236 | { | ||
237 | StreamReader RevisionFile = File.OpenText(svnRevisionFileName); | ||
238 | buildVersion = RevisionFile.ReadLine(); | ||
239 | buildVersion.Trim(); | ||
240 | RevisionFile.Close(); | ||
241 | } | ||
242 | |||
243 | if (string.IsNullOrEmpty(buildVersion) && File.Exists(svnFileName)) | ||
244 | { | ||
245 | StreamReader EntriesFile = File.OpenText(svnFileName); | ||
246 | inputLine = EntriesFile.ReadLine(); | ||
247 | while (inputLine != null) | ||
248 | { | ||
249 | // using the dir svn revision at the top of entries file | ||
250 | strcmp = String.Compare(inputLine, "dir"); | ||
251 | if (strcmp == 0) | ||
252 | { | ||
253 | buildVersion = EntriesFile.ReadLine(); | ||
254 | break; | ||
255 | } | ||
256 | else | ||
257 | { | ||
258 | inputLine = EntriesFile.ReadLine(); | ||
259 | } | ||
260 | } | ||
261 | EntriesFile.Close(); | ||
262 | } | ||
263 | |||
264 | m_version += string.IsNullOrEmpty(buildVersion) ? " " : ("." + buildVersion + " ").Substring(0, 6); | ||
265 | } | ||
266 | } | ||
267 | |||
268 | protected string GetVersionText() | ||
269 | { | ||
270 | return String.Format("Version: {0} (interface version {1})", m_version, VersionInfo.MajorInterfaceVersion); | ||
271 | } | ||
272 | |||
93 | /// <summary> | 273 | /// <summary> |
94 | /// Console output is only possible if a console has been established. | 274 | /// Console output is only possible if a console has been established. |
95 | /// That is something that cannot be determined within this class. So | 275 | /// That is something that cannot be determined within this class. So |