aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
authorMelanie2012-11-22 13:37:27 +0000
committerMelanie2012-11-22 13:37:27 +0000
commit3c1a58c67a9162480054674ae8c83f5b501576db (patch)
tree7864eb3dd4aea66bd41b49a43a810c6c623c2485 /OpenSim/Framework
parentMerge branch 'avination' into careminster (diff)
parentFactor out command script code. (diff)
downloadopensim-SC_OLD-3c1a58c67a9162480054674ae8c83f5b501576db.zip
opensim-SC_OLD-3c1a58c67a9162480054674ae8c83f5b501576db.tar.gz
opensim-SC_OLD-3c1a58c67a9162480054674ae8c83f5b501576db.tar.bz2
opensim-SC_OLD-3c1a58c67a9162480054674ae8c83f5b501576db.tar.xz
Merge branch 'master' into careminster
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r--OpenSim/Framework/Console/CommandConsole.cs2
-rwxr-xr-xOpenSim/Framework/Console/ConsoleBase.cs10
-rw-r--r--OpenSim/Framework/Console/MockConsole.cs7
-rw-r--r--OpenSim/Framework/ICommandConsole.cs5
-rw-r--r--OpenSim/Framework/IConsole.cs2
-rw-r--r--OpenSim/Framework/Servers/BaseOpenSimServer.cs369
-rw-r--r--OpenSim/Framework/Servers/ServerBase.cs510
7 files changed, 561 insertions, 344 deletions
diff --git a/OpenSim/Framework/Console/CommandConsole.cs b/OpenSim/Framework/Console/CommandConsole.cs
index bd23d1c..d1e29b4 100644
--- a/OpenSim/Framework/Console/CommandConsole.cs
+++ b/OpenSim/Framework/Console/CommandConsole.cs
@@ -711,7 +711,7 @@ namespace OpenSim.Framework.Console
711 /// </summary> 711 /// </summary>
712 public void Prompt() 712 public void Prompt()
713 { 713 {
714 string line = ReadLine(m_defaultPrompt + "# ", true, true); 714 string line = ReadLine(DefaultPrompt + "# ", true, true);
715 715
716 if (line != String.Empty) 716 if (line != String.Empty)
717 Output("Invalid command"); 717 Output("Invalid command");
diff --git a/OpenSim/Framework/Console/ConsoleBase.cs b/OpenSim/Framework/Console/ConsoleBase.cs
index 4b375d9..2d8e723 100755
--- a/OpenSim/Framework/Console/ConsoleBase.cs
+++ b/OpenSim/Framework/Console/ConsoleBase.cs
@@ -43,15 +43,7 @@ namespace OpenSim.Framework.Console
43 43
44 public object ConsoleScene { get; set; } 44 public object ConsoleScene { get; set; }
45 45
46 /// <summary> 46 public string DefaultPrompt { get; set; }
47 /// The default prompt text.
48 /// </summary>
49 public string DefaultPrompt
50 {
51 set { m_defaultPrompt = value; }
52 get { return m_defaultPrompt; }
53 }
54 protected string m_defaultPrompt;
55 47
56 public ConsoleBase(string defaultPrompt) 48 public ConsoleBase(string defaultPrompt)
57 { 49 {
diff --git a/OpenSim/Framework/Console/MockConsole.cs b/OpenSim/Framework/Console/MockConsole.cs
index b489f93..8ba58e4 100644
--- a/OpenSim/Framework/Console/MockConsole.cs
+++ b/OpenSim/Framework/Console/MockConsole.cs
@@ -46,13 +46,18 @@ namespace OpenSim.Framework.Console
46 46
47 public ICommands Commands { get { return m_commands; } } 47 public ICommands Commands { get { return m_commands; } }
48 48
49 public string DefaultPrompt { get; set; }
50
49 public void Prompt() {} 51 public void Prompt() {}
50 52
51 public void RunCommand(string cmd) {} 53 public void RunCommand(string cmd) {}
52 54
53 public string ReadLine(string p, bool isCommand, bool e) { return ""; } 55 public string ReadLine(string p, bool isCommand, bool e) { return ""; }
54 56
55 public object ConsoleScene { get { return null; } } 57 public object ConsoleScene {
58 get { return null; }
59 set {}
60 }
56 61
57 public void Output(string text, string level) {} 62 public void Output(string text, string level) {}
58 public void Output(string text) {} 63 public void Output(string text) {}
diff --git a/OpenSim/Framework/ICommandConsole.cs b/OpenSim/Framework/ICommandConsole.cs
index 8cd20da..a6573f8 100644
--- a/OpenSim/Framework/ICommandConsole.cs
+++ b/OpenSim/Framework/ICommandConsole.cs
@@ -83,6 +83,11 @@ namespace OpenSim.Framework
83 ICommands Commands { get; } 83 ICommands Commands { get; }
84 84
85 /// <summary> 85 /// <summary>
86 /// The default prompt text.
87 /// </summary>
88 string DefaultPrompt { get; set; }
89
90 /// <summary>
86 /// Display a command prompt on the console and wait for user input 91 /// Display a command prompt on the console and wait for user input
87 /// </summary> 92 /// </summary>
88 void Prompt(); 93 void Prompt();
diff --git a/OpenSim/Framework/IConsole.cs b/OpenSim/Framework/IConsole.cs
index 33024b2..79560d8 100644
--- a/OpenSim/Framework/IConsole.cs
+++ b/OpenSim/Framework/IConsole.cs
@@ -32,7 +32,7 @@ namespace OpenSim.Framework
32{ 32{
33 public interface IConsole 33 public interface IConsole
34 { 34 {
35 object ConsoleScene { get; } 35 object ConsoleScene { get; set; }
36 36
37 void Output(string text, string level); 37 void Output(string text, string level);
38 void Output(string text); 38 void Output(string text);
diff --git a/OpenSim/Framework/Servers/BaseOpenSimServer.cs b/OpenSim/Framework/Servers/BaseOpenSimServer.cs
index e6ca380..2c21800 100644
--- a/OpenSim/Framework/Servers/BaseOpenSimServer.cs
+++ b/OpenSim/Framework/Servers/BaseOpenSimServer.cs
@@ -61,22 +61,6 @@ namespace OpenSim.Framework.Servers
61 /// server. 61 /// server.
62 /// </summary> 62 /// </summary>
63 private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000); 63 private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000);
64
65 protected CommandConsole m_console;
66 protected OpenSimAppender m_consoleAppender;
67 protected IAppender m_logFileAppender = null;
68
69 /// <summary>
70 /// Record the initial startup directory for info purposes
71 /// </summary>
72 protected string m_startupDirectory = Environment.CurrentDirectory;
73
74 /// <summary>
75 /// Server version information. Usually VersionInfo + information about git commit, operating system, etc.
76 /// </summary>
77 protected string m_version;
78
79 protected string m_pidFile = String.Empty;
80 64
81 /// <summary> 65 /// <summary>
82 /// Random uuid for private data 66 /// Random uuid for private data
@@ -91,27 +75,11 @@ namespace OpenSim.Framework.Servers
91 75
92 public BaseOpenSimServer() : base() 76 public BaseOpenSimServer() : base()
93 { 77 {
94 m_version = VersionInfo.Version;
95
96 // Random uuid for private data 78 // Random uuid for private data
97 m_osSecret = UUID.Random().ToString(); 79 m_osSecret = UUID.Random().ToString();
98 80
99 m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics); 81 m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
100 m_periodicDiagnosticsTimer.Enabled = true; 82 m_periodicDiagnosticsTimer.Enabled = true;
101
102 // This thread will go on to become the console listening thread
103 Thread.CurrentThread.Name = "ConsoleThread";
104
105 ILoggerRepository repository = LogManager.GetRepository();
106 IAppender[] appenders = repository.GetAppenders();
107
108 foreach (IAppender appender in appenders)
109 {
110 if (appender.Name == "LogFileAppender")
111 {
112 m_logFileAppender = appender;
113 }
114 }
115 } 83 }
116 84
117 /// <summary> 85 /// <summary>
@@ -119,77 +87,40 @@ namespace OpenSim.Framework.Servers
119 /// </summary> 87 /// </summary>
120 protected virtual void StartupSpecific() 88 protected virtual void StartupSpecific()
121 { 89 {
122 if (m_console != null) 90 if (m_console == null)
123 { 91 return;
124 ILoggerRepository repository = LogManager.GetRepository(); 92
125 IAppender[] appenders = repository.GetAppenders(); 93 RegisterCommonCommands();
126 94
127 foreach (IAppender appender in appenders) 95 m_console.Commands.AddCommand("General", false, "quit",
128 { 96 "quit",
129 if (appender.Name == "Console") 97 "Quit the application", HandleQuit);
130 { 98
131 m_consoleAppender = (OpenSimAppender)appender; 99 m_console.Commands.AddCommand("General", false, "shutdown",
132 break; 100 "shutdown",
133 } 101 "Quit the application", HandleQuit);
134 } 102
135 103 m_console.Commands.AddCommand("General", false, "show threads",
136 if (null == m_consoleAppender) 104 "show threads",
137 { 105 "Show thread status", HandleShow);
138 Notice("No appender named Console found (see the log4net config file for this executable)!"); 106
139 } 107 m_console.Commands.AddCommand("General", false, "show version",
140 else 108 "show version",
141 { 109 "Show server version", HandleShow);
142 m_consoleAppender.Console = m_console; 110
143 111 m_console.Commands.AddCommand("General", false, "threads abort",
144 // If there is no threshold set then the threshold is effectively everything. 112 "threads abort <thread-id>",
145 if (null == m_consoleAppender.Threshold) 113 "Abort a managed thread. Use \"show threads\" to find possible threads.", HandleThreadsAbort);
146 m_consoleAppender.Threshold = Level.All; 114
147 115 m_console.Commands.AddCommand("General", false, "threads show",
148 Notice(String.Format("Console log level is {0}", m_consoleAppender.Threshold)); 116 "threads show",
149 } 117 "Show thread status. Synonym for \"show threads\"",
150 118 (string module, string[] args) => Notice(GetThreadsReport()));
151 m_console.Commands.AddCommand("General", false, "quit", 119
152 "quit", 120 m_console.Commands.AddCommand("General", false, "force gc",
153 "Quit the application", HandleQuit); 121 "force gc",
154 122 "Manually invoke runtime garbage collection. For debugging purposes",
155 m_console.Commands.AddCommand("General", false, "shutdown", 123 HandleForceGc);
156 "shutdown",
157 "Quit the application", HandleQuit);
158
159 m_console.Commands.AddCommand("General", false, "set log level",
160 "set log level <level>",
161 "Set the console logging level", HandleLogLevel);
162
163 m_console.Commands.AddCommand("General", false, "show info",
164 "show info",
165 "Show general information about the server", HandleShow);
166
167 m_console.Commands.AddCommand("General", false, "show threads",
168 "show threads",
169 "Show thread status", HandleShow);
170
171 m_console.Commands.AddCommand("General", false, "show uptime",
172 "show uptime",
173 "Show server uptime", HandleShow);
174
175 m_console.Commands.AddCommand("General", false, "show version",
176 "show version",
177 "Show server version", HandleShow);
178
179 m_console.Commands.AddCommand("General", false, "threads abort",
180 "threads abort <thread-id>",
181 "Abort a managed thread. Use \"show threads\" to find possible threads.", HandleThreadsAbort);
182
183 m_console.Commands.AddCommand("General", false, "threads show",
184 "threads show",
185 "Show thread status. Synonym for \"show threads\"",
186 (string module, string[] args) => Notice(GetThreadsReport()));
187
188 m_console.Commands.AddCommand("General", false, "force gc",
189 "force gc",
190 "Manually invoke runtime garbage collection. For debugging purposes",
191 HandleForceGc);
192 }
193 } 124 }
194 125
195 private void HandleForceGc(string module, string[] args) 126 private void HandleForceGc(string module, string[] args)
@@ -281,8 +212,6 @@ namespace OpenSim.Framework.Servers
281 public virtual void Startup() 212 public virtual void Startup()
282 { 213 {
283 m_log.Info("[STARTUP]: Beginning startup processing"); 214 m_log.Info("[STARTUP]: Beginning startup processing");
284
285 EnhanceVersionInformation();
286 215
287 m_log.Info("[STARTUP]: Careminster version: " + m_version + Environment.NewLine); 216 m_log.Info("[STARTUP]: Careminster version: " + m_version + Environment.NewLine);
288 // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and 217 // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
@@ -319,56 +248,10 @@ namespace OpenSim.Framework.Servers
319 Shutdown(); 248 Shutdown();
320 } 249 }
321 250
322 private void HandleLogLevel(string module, string[] cmd) 251 public override void HandleShow(string module, string[] cmd)
323 {
324 if (null == m_consoleAppender)
325 {
326 Notice("No appender named Console found (see the log4net config file for this executable)!");
327 return;
328 }
329
330 if (cmd.Length > 3)
331 {
332 string rawLevel = cmd[3];
333
334 ILoggerRepository repository = LogManager.GetRepository();
335 Level consoleLevel = repository.LevelMap[rawLevel];
336
337 if (consoleLevel != null)
338 m_consoleAppender.Threshold = consoleLevel;
339 else
340 Notice(
341 String.Format(
342 "{0} is not a valid logging level. Valid logging levels are ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF",
343 rawLevel));
344 }
345
346 Notice(String.Format("Console log level is {0}", m_consoleAppender.Threshold));
347 }
348
349 /// <summary>
350 /// Show help information
351 /// </summary>
352 /// <param name="helpArgs"></param>
353 protected virtual void ShowHelp(string[] helpArgs)
354 { 252 {
355 Notice(""); 253 base.HandleShow(module, cmd);
356
357 if (helpArgs.Length == 0)
358 {
359 Notice("set log level [level] - change the console logging level only. For example, off or debug.");
360 Notice("show info - show server information (e.g. startup path).");
361 Notice("show threads - list tracked threads");
362 Notice("show uptime - show server startup time and uptime.");
363 Notice("show version - show server version.");
364 Notice("");
365 254
366 return;
367 }
368 }
369
370 public virtual void HandleShow(string module, string[] cmd)
371 {
372 List<string> args = new List<string>(cmd); 255 List<string> args = new List<string>(cmd);
373 256
374 args.RemoveAt(0); 257 args.RemoveAt(0);
@@ -377,18 +260,10 @@ namespace OpenSim.Framework.Servers
377 260
378 switch (showParams[0]) 261 switch (showParams[0])
379 { 262 {
380 case "info":
381 ShowInfo();
382 break;
383
384 case "threads": 263 case "threads":
385 Notice(GetThreadsReport()); 264 Notice(GetThreadsReport());
386 break; 265 break;
387 266
388 case "uptime":
389 Notice(GetUptimeReport());
390 break;
391
392 case "version": 267 case "version":
393 Notice(GetVersionText()); 268 Notice(GetVersionText());
394 break; 269 break;
@@ -414,160 +289,7 @@ namespace OpenSim.Framework.Servers
414 MainConsole.Instance.OutputFormat("Aborted thread with id {0}", threadId); 289 MainConsole.Instance.OutputFormat("Aborted thread with id {0}", threadId);
415 else 290 else
416 MainConsole.Instance.OutputFormat("ERROR - Thread with id {0} not found in managed threads", threadId); 291 MainConsole.Instance.OutputFormat("ERROR - Thread with id {0} not found in managed threads", threadId);
417 } 292 }
418
419 protected void ShowInfo()
420 {
421 Notice(GetVersionText());
422 Notice("Startup directory: " + m_startupDirectory);
423 if (null != m_consoleAppender)
424 Notice(String.Format("Console log level: {0}", m_consoleAppender.Threshold));
425 }
426
427 protected string GetVersionText()
428 {
429 return String.Format("Version: {0} (interface version {1})", m_version, VersionInfo.MajorInterfaceVersion);
430 }
431
432 /// <summary>
433 /// Console output is only possible if a console has been established.
434 /// That is something that cannot be determined within this class. So
435 /// all attempts to use the console MUST be verified.
436 /// </summary>
437 /// <param name="msg"></param>
438 protected void Notice(string msg)
439 {
440 if (m_console != null)
441 {
442 m_console.Output(msg);
443 }
444 }
445
446 /// <summary>
447 /// Console output is only possible if a console has been established.
448 /// That is something that cannot be determined within this class. So
449 /// all attempts to use the console MUST be verified.
450 /// </summary>
451 /// <param name="format"></param>
452 /// <param name="components"></param>
453 protected void Notice(string format, params string[] components)
454 {
455 if (m_console != null)
456 m_console.OutputFormat(format, components);
457 }
458
459 /// <summary>
460 /// Enhance the version string with extra information if it's available.
461 /// </summary>
462 protected void EnhanceVersionInformation()
463 {
464 string buildVersion = string.Empty;
465
466 // The subversion information is deprecated and will be removed at a later date
467 // Add subversion revision information if available
468 // Try file "svn_revision" in the current directory first, then the .svn info.
469 // This allows to make the revision available in simulators not running from the source tree.
470 // FIXME: Making an assumption about the directory we're currently in - we do this all over the place
471 // elsewhere as well
472 string gitDir = "../.git/";
473 string gitRefPointerPath = gitDir + "HEAD";
474
475 string svnRevisionFileName = "svn_revision";
476 string svnFileName = ".svn/entries";
477 string manualVersionFileName = ".version";
478 string inputLine;
479 int strcmp;
480
481 if (File.Exists(manualVersionFileName))
482 {
483 using (StreamReader CommitFile = File.OpenText(manualVersionFileName))
484 buildVersion = CommitFile.ReadLine();
485
486 m_version += buildVersion ?? "";
487 }
488 else if (File.Exists(gitRefPointerPath))
489 {
490// m_log.DebugFormat("[OPENSIM]: Found {0}", gitRefPointerPath);
491
492 string rawPointer = "";
493
494 using (StreamReader pointerFile = File.OpenText(gitRefPointerPath))
495 rawPointer = pointerFile.ReadLine();
496
497// m_log.DebugFormat("[OPENSIM]: rawPointer [{0}]", rawPointer);
498
499 Match m = Regex.Match(rawPointer, "^ref: (.+)$");
500
501 if (m.Success)
502 {
503// m_log.DebugFormat("[OPENSIM]: Matched [{0}]", m.Groups[1].Value);
504
505 string gitRef = m.Groups[1].Value;
506 string gitRefPath = gitDir + gitRef;
507 if (File.Exists(gitRefPath))
508 {
509// m_log.DebugFormat("[OPENSIM]: Found gitRefPath [{0}]", gitRefPath);
510
511 using (StreamReader refFile = File.OpenText(gitRefPath))
512 {
513 string gitHash = refFile.ReadLine();
514 m_version += gitHash.Substring(0, 7);
515 }
516 }
517 }
518 }
519 else
520 {
521 // Remove the else logic when subversion mirror is no longer used
522 if (File.Exists(svnRevisionFileName))
523 {
524 StreamReader RevisionFile = File.OpenText(svnRevisionFileName);
525 buildVersion = RevisionFile.ReadLine();
526 buildVersion.Trim();
527 RevisionFile.Close();
528 }
529
530 if (string.IsNullOrEmpty(buildVersion) && File.Exists(svnFileName))
531 {
532 StreamReader EntriesFile = File.OpenText(svnFileName);
533 inputLine = EntriesFile.ReadLine();
534 while (inputLine != null)
535 {
536 // using the dir svn revision at the top of entries file
537 strcmp = String.Compare(inputLine, "dir");
538 if (strcmp == 0)
539 {
540 buildVersion = EntriesFile.ReadLine();
541 break;
542 }
543 else
544 {
545 inputLine = EntriesFile.ReadLine();
546 }
547 }
548 EntriesFile.Close();
549 }
550
551 m_version += string.IsNullOrEmpty(buildVersion) ? " " : ("." + buildVersion + " ").Substring(0, 6);
552 }
553 }
554
555 protected void CreatePIDFile(string path)
556 {
557 try
558 {
559 string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
560 FileStream fs = File.Create(path);
561
562 Byte[] buf = Encoding.ASCII.GetBytes(pidstring);
563 fs.Write(buf, 0, buf.Length);
564 fs.Close();
565 m_pidFile = path;
566 }
567 catch (Exception)
568 {
569 }
570 }
571 293
572 public string osSecret { 294 public string osSecret {
573 // Secret uuid for the simulator 295 // Secret uuid for the simulator
@@ -586,20 +308,5 @@ namespace OpenSim.Framework.Servers
586 return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version); 308 return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
587 } 309 }
588 } 310 }
589
590 protected void RemovePIDFile()
591 {
592 if (m_pidFile != String.Empty)
593 {
594 try
595 {
596 File.Delete(m_pidFile);
597 m_pidFile = String.Empty;
598 }
599 catch (Exception)
600 {
601 }
602 }
603 }
604 } 311 }
605} 312} \ No newline at end of file
diff --git a/OpenSim/Framework/Servers/ServerBase.cs b/OpenSim/Framework/Servers/ServerBase.cs
index d19234b..c182a3a 100644
--- a/OpenSim/Framework/Servers/ServerBase.cs
+++ b/OpenSim/Framework/Servers/ServerBase.cs
@@ -26,20 +26,392 @@
26 */ 26 */
27 27
28using System; 28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
29using System.Text; 32using System.Text;
33using System.Text.RegularExpressions;
34using log4net;
35using log4net.Appender;
36using log4net.Core;
37using log4net.Repository;
38using Nini.Config;
39using OpenSim.Framework.Console;
30 40
31namespace OpenSim.Framework.Servers 41namespace OpenSim.Framework.Servers
32{ 42{
33 public class ServerBase 43 public class ServerBase
34 { 44 {
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46
47 public IConfigSource Config { get; protected set; }
48
35 /// <summary> 49 /// <summary>
36 /// Time at which this server was started 50 /// Console to be used for any command line output. Can be null, in which case there should be no output.
37 /// </summary> 51 /// </summary>
52 protected ICommandConsole m_console;
53
54 protected OpenSimAppender m_consoleAppender;
55 protected FileAppender m_logFileAppender;
56
38 protected DateTime m_startuptime; 57 protected DateTime m_startuptime;
58 protected string m_startupDirectory = Environment.CurrentDirectory;
59
60 protected string m_pidFile = String.Empty;
61
62 /// <summary>
63 /// Server version information. Usually VersionInfo + information about git commit, operating system, etc.
64 /// </summary>
65 protected string m_version;
39 66
40 public ServerBase() 67 public ServerBase()
41 { 68 {
42 m_startuptime = DateTime.Now; 69 m_startuptime = DateTime.Now;
70 m_version = VersionInfo.Version;
71 EnhanceVersionInformation();
72 }
73
74 protected void CreatePIDFile(string path)
75 {
76 try
77 {
78 string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
79
80 using (FileStream fs = File.Create(path))
81 {
82 Byte[] buf = Encoding.ASCII.GetBytes(pidstring);
83 fs.Write(buf, 0, buf.Length);
84 }
85
86 m_pidFile = path;
87
88 m_log.InfoFormat("[SERVER BASE]: Created pid file {0}", m_pidFile);
89 }
90 catch (Exception e)
91 {
92 m_log.Warn(string.Format("[SERVER BASE]: Could not create PID file at {0} ", path), e);
93 }
94 }
95
96 protected void RemovePIDFile()
97 {
98 if (m_pidFile != String.Empty)
99 {
100 try
101 {
102 File.Delete(m_pidFile);
103 }
104 catch (Exception e)
105 {
106 m_log.Error(string.Format("[SERVER BASE]: Error whilst removing {0} ", m_pidFile), e);
107 }
108
109 m_pidFile = String.Empty;
110 }
111 }
112
113 public void RegisterCommonAppenders(IConfig startupConfig)
114 {
115 ILoggerRepository repository = LogManager.GetRepository();
116 IAppender[] appenders = repository.GetAppenders();
117
118 foreach (IAppender appender in appenders)
119 {
120 if (appender.Name == "Console")
121 {
122 m_consoleAppender = (OpenSimAppender)appender;
123 }
124 else if (appender.Name == "LogFileAppender")
125 {
126 m_logFileAppender = (FileAppender)appender;
127 }
128 }
129
130 if (null == m_consoleAppender)
131 {
132 Notice("No appender named Console found (see the log4net config file for this executable)!");
133 }
134 else
135 {
136 // FIXME: This should be done through an interface rather than casting.
137 m_consoleAppender.Console = (ConsoleBase)m_console;
138
139 // If there is no threshold set then the threshold is effectively everything.
140 if (null == m_consoleAppender.Threshold)
141 m_consoleAppender.Threshold = Level.All;
142
143 Notice(String.Format("Console log level is {0}", m_consoleAppender.Threshold));
144 }
145
146 if (m_logFileAppender != null && startupConfig != null)
147 {
148 string cfgFileName = startupConfig.GetString("LogFile", null);
149 if (cfgFileName != null)
150 {
151 m_logFileAppender.File = cfgFileName;
152 m_logFileAppender.ActivateOptions();
153 }
154
155 m_log.InfoFormat("[SERVER BASE]: Logging started to file {0}", m_logFileAppender.File);
156 }
157 }
158
159 /// <summary>
160 /// Register common commands once m_console has been set if it is going to be set
161 /// </summary>
162 public void RegisterCommonCommands()
163 {
164 if (m_console == null)
165 return;
166
167 m_console.Commands.AddCommand(
168 "General", false, "show info", "show info", "Show general information about the server", HandleShow);
169
170 m_console.Commands.AddCommand(
171 "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow);
172
173 m_console.Commands.AddCommand(
174 "General", false, "get log level", "get log level", "Get the current console logging level",
175 (mod, cmd) => ShowLogLevel());
176
177 m_console.Commands.AddCommand(
178 "General", false, "set log level", "set log level <level>",
179 "Set the console logging level for this session.", HandleSetLogLevel);
180
181 m_console.Commands.AddCommand(
182 "General", false, "config set",
183 "config set <section> <key> <value>",
184 "Set a config option. In most cases this is not useful since changed parameters are not dynamically reloaded. Neither do changed parameters persist - you will have to change a config file manually and restart.", HandleConfig);
185
186 m_console.Commands.AddCommand(
187 "General", false, "config get",
188 "config get [<section>] [<key>]",
189 "Synonym for config show",
190 HandleConfig);
191
192 m_console.Commands.AddCommand(
193 "General", false, "config show",
194 "config show [<section>] [<key>]",
195 "Show config information",
196 "If neither section nor field are specified, then the whole current configuration is printed." + Environment.NewLine
197 + "If a section is given but not a field, then all fields in that section are printed.",
198 HandleConfig);
199
200 m_console.Commands.AddCommand(
201 "General", false, "config save",
202 "config save <path>",
203 "Save current configuration to a file at the given path", HandleConfig);
204
205 m_console.Commands.AddCommand(
206 "General", false, "command-script",
207 "command-script <script>",
208 "Run a command script from file", HandleScript);
209 }
210
211 public virtual void HandleShow(string module, string[] cmd)
212 {
213 List<string> args = new List<string>(cmd);
214
215 args.RemoveAt(0);
216
217 string[] showParams = args.ToArray();
218
219 switch (showParams[0])
220 {
221 case "info":
222 ShowInfo();
223 break;
224
225 case "uptime":
226 Notice(GetUptimeReport());
227 break;
228 }
229 }
230
231 /// <summary>
232 /// Change and load configuration file data.
233 /// </summary>
234 /// <param name="module"></param>
235 /// <param name="cmd"></param>
236 private void HandleConfig(string module, string[] cmd)
237 {
238 List<string> args = new List<string>(cmd);
239 args.RemoveAt(0);
240 string[] cmdparams = args.ToArray();
241
242 if (cmdparams.Length > 0)
243 {
244 string firstParam = cmdparams[0].ToLower();
245
246 switch (firstParam)
247 {
248 case "set":
249 if (cmdparams.Length < 4)
250 {
251 Notice("Syntax: config set <section> <key> <value>");
252 Notice("Example: config set ScriptEngine.DotNetEngine NumberOfScriptThreads 5");
253 }
254 else
255 {
256 IConfig c;
257 IConfigSource source = new IniConfigSource();
258 c = source.AddConfig(cmdparams[1]);
259 if (c != null)
260 {
261 string _value = String.Join(" ", cmdparams, 3, cmdparams.Length - 3);
262 c.Set(cmdparams[2], _value);
263 Config.Merge(source);
264
265 Notice("In section [{0}], set {1} = {2}", c.Name, cmdparams[2], _value);
266 }
267 }
268 break;
269
270 case "get":
271 case "show":
272 if (cmdparams.Length == 1)
273 {
274 foreach (IConfig config in Config.Configs)
275 {
276 Notice("[{0}]", config.Name);
277 string[] keys = config.GetKeys();
278 foreach (string key in keys)
279 Notice(" {0} = {1}", key, config.GetString(key));
280 }
281 }
282 else if (cmdparams.Length == 2 || cmdparams.Length == 3)
283 {
284 IConfig config = Config.Configs[cmdparams[1]];
285 if (config == null)
286 {
287 Notice("Section \"{0}\" does not exist.",cmdparams[1]);
288 break;
289 }
290 else
291 {
292 if (cmdparams.Length == 2)
293 {
294 Notice("[{0}]", config.Name);
295 foreach (string key in config.GetKeys())
296 Notice(" {0} = {1}", key, config.GetString(key));
297 }
298 else
299 {
300 Notice(
301 "config get {0} {1} : {2}",
302 cmdparams[1], cmdparams[2], config.GetString(cmdparams[2]));
303 }
304 }
305 }
306 else
307 {
308 Notice("Syntax: config {0} [<section>] [<key>]", firstParam);
309 Notice("Example: config {0} ScriptEngine.DotNetEngine NumberOfScriptThreads", firstParam);
310 }
311
312 break;
313
314 case "save":
315 if (cmdparams.Length < 2)
316 {
317 Notice("Syntax: config save <path>");
318 return;
319 }
320
321 string path = cmdparams[1];
322 Notice("Saving configuration file: {0}", path);
323
324 if (Config is IniConfigSource)
325 {
326 IniConfigSource iniCon = (IniConfigSource)Config;
327 iniCon.Save(path);
328 }
329 else if (Config is XmlConfigSource)
330 {
331 XmlConfigSource xmlCon = (XmlConfigSource)Config;
332 xmlCon.Save(path);
333 }
334
335 break;
336 }
337 }
338 }
339
340 private void HandleSetLogLevel(string module, string[] cmd)
341 {
342 if (cmd.Length != 4)
343 {
344 Notice("Usage: set log level <level>");
345 return;
346 }
347
348 if (null == m_consoleAppender)
349 {
350 Notice("No appender named Console found (see the log4net config file for this executable)!");
351 return;
352 }
353
354 string rawLevel = cmd[3];
355
356 ILoggerRepository repository = LogManager.GetRepository();
357 Level consoleLevel = repository.LevelMap[rawLevel];
358
359 if (consoleLevel != null)
360 m_consoleAppender.Threshold = consoleLevel;
361 else
362 Notice(
363 "{0} is not a valid logging level. Valid logging levels are ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF",
364 rawLevel);
365
366 ShowLogLevel();
367 }
368
369 private void ShowLogLevel()
370 {
371 Notice("Console log level is {0}", m_consoleAppender.Threshold);
372 }
373
374 protected virtual void HandleScript(string module, string[] parms)
375 {
376 if (parms.Length != 2)
377 {
378 Notice("Usage: command-script <path-to-script");
379 return;
380 }
381
382 RunCommandScript(parms[1]);
383 }
384
385 /// <summary>
386 /// Run an optional startup list of commands
387 /// </summary>
388 /// <param name="fileName"></param>
389 protected void RunCommandScript(string fileName)
390 {
391 if (m_console == null)
392 return;
393
394 if (File.Exists(fileName))
395 {
396 m_log.Info("[SERVER BASE]: Running " + fileName);
397
398 using (StreamReader readFile = File.OpenText(fileName))
399 {
400 string currentCommand;
401 while ((currentCommand = readFile.ReadLine()) != null)
402 {
403 currentCommand = currentCommand.Trim();
404 if (!(currentCommand == ""
405 || currentCommand.StartsWith(";")
406 || currentCommand.StartsWith("//")
407 || currentCommand.StartsWith("#")))
408 {
409 m_log.Info("[SERVER BASE]: Running '" + currentCommand + "'");
410 m_console.RunCommand(currentCommand);
411 }
412 }
413 }
414 }
43 } 415 }
44 416
45 /// <summary> 417 /// <summary>
@@ -54,5 +426,141 @@ namespace OpenSim.Framework.Servers
54 426
55 return sb.ToString(); 427 return sb.ToString();
56 } 428 }
429
430 protected void ShowInfo()
431 {
432 Notice(GetVersionText());
433 Notice("Startup directory: " + m_startupDirectory);
434 if (null != m_consoleAppender)
435 Notice(String.Format("Console log level: {0}", m_consoleAppender.Threshold));
436 }
437
438 /// <summary>
439 /// Enhance the version string with extra information if it's available.
440 /// </summary>
441 protected void EnhanceVersionInformation()
442 {
443 string buildVersion = string.Empty;
444
445 // The subversion information is deprecated and will be removed at a later date
446 // Add subversion revision information if available
447 // Try file "svn_revision" in the current directory first, then the .svn info.
448 // This allows to make the revision available in simulators not running from the source tree.
449 // FIXME: Making an assumption about the directory we're currently in - we do this all over the place
450 // elsewhere as well
451 string gitDir = "../.git/";
452 string gitRefPointerPath = gitDir + "HEAD";
453
454 string svnRevisionFileName = "svn_revision";
455 string svnFileName = ".svn/entries";
456 string manualVersionFileName = ".version";
457 string inputLine;
458 int strcmp;
459
460 if (File.Exists(manualVersionFileName))
461 {
462 using (StreamReader CommitFile = File.OpenText(manualVersionFileName))
463 buildVersion = CommitFile.ReadLine();
464
465 m_version += buildVersion ?? "";
466 }
467 else if (File.Exists(gitRefPointerPath))
468 {
469// m_log.DebugFormat("[SERVER BASE]: Found {0}", gitRefPointerPath);
470
471 string rawPointer = "";
472
473 using (StreamReader pointerFile = File.OpenText(gitRefPointerPath))
474 rawPointer = pointerFile.ReadLine();
475
476// m_log.DebugFormat("[SERVER BASE]: rawPointer [{0}]", rawPointer);
477
478 Match m = Regex.Match(rawPointer, "^ref: (.+)$");
479
480 if (m.Success)
481 {
482// m_log.DebugFormat("[SERVER BASE]: Matched [{0}]", m.Groups[1].Value);
483
484 string gitRef = m.Groups[1].Value;
485 string gitRefPath = gitDir + gitRef;
486 if (File.Exists(gitRefPath))
487 {
488// m_log.DebugFormat("[SERVER BASE]: Found gitRefPath [{0}]", gitRefPath);
489
490 using (StreamReader refFile = File.OpenText(gitRefPath))
491 {
492 string gitHash = refFile.ReadLine();
493 m_version += gitHash.Substring(0, 7);
494 }
495 }
496 }
497 }
498 else
499 {
500 // Remove the else logic when subversion mirror is no longer used
501 if (File.Exists(svnRevisionFileName))
502 {
503 StreamReader RevisionFile = File.OpenText(svnRevisionFileName);
504 buildVersion = RevisionFile.ReadLine();
505 buildVersion.Trim();
506 RevisionFile.Close();
507 }
508
509 if (string.IsNullOrEmpty(buildVersion) && File.Exists(svnFileName))
510 {
511 StreamReader EntriesFile = File.OpenText(svnFileName);
512 inputLine = EntriesFile.ReadLine();
513 while (inputLine != null)
514 {
515 // using the dir svn revision at the top of entries file
516 strcmp = String.Compare(inputLine, "dir");
517 if (strcmp == 0)
518 {
519 buildVersion = EntriesFile.ReadLine();
520 break;
521 }
522 else
523 {
524 inputLine = EntriesFile.ReadLine();
525 }
526 }
527 EntriesFile.Close();
528 }
529
530 m_version += string.IsNullOrEmpty(buildVersion) ? " " : ("." + buildVersion + " ").Substring(0, 6);
531 }
532 }
533
534 protected string GetVersionText()
535 {
536 return String.Format("Version: {0} (interface version {1})", m_version, VersionInfo.MajorInterfaceVersion);
537 }
538
539 /// <summary>
540 /// Console output is only possible if a console has been established.
541 /// That is something that cannot be determined within this class. So
542 /// all attempts to use the console MUST be verified.
543 /// </summary>
544 /// <param name="msg"></param>
545 protected void Notice(string msg)
546 {
547 if (m_console != null)
548 {
549 m_console.Output(msg);
550 }
551 }
552
553 /// <summary>
554 /// Console output is only possible if a console has been established.
555 /// That is something that cannot be determined within this class. So
556 /// all attempts to use the console MUST be verified.
557 /// </summary>
558 /// <param name="format"></param>
559 /// <param name="components"></param>
560 protected void Notice(string format, params object[] components)
561 {
562 if (m_console != null)
563 m_console.OutputFormat(format, components);
564 }
57 } 565 }
58} \ No newline at end of file 566} \ No newline at end of file