From bde60cc92e0966177a1aeb03dce3183a92b7a38c Mon Sep 17 00:00:00 2001 From: Jak Daniels Date: Mon, 22 Sep 2014 16:54:12 +0100 Subject: [PATCH] Add persistent command history in console Signed-off-by: BlueWall --- OpenSim/Framework/Console/LocalConsole.cs | 58 ++++++++++++++++++++++- OpenSim/Region/Application/OpenSim.cs | 2 +- OpenSim/Server/Base/ServicesServerBase.cs | 2 +- bin/OpenSim.ini.example | 13 +++++ bin/OpenSimDefaults.ini | 10 ++++ bin/Robust.HG.ini.example | 10 ++++ bin/Robust.ini.example | 10 ++++ 7 files changed, 101 insertions(+), 4 deletions(-) diff --git a/OpenSim/Framework/Console/LocalConsole.cs b/OpenSim/Framework/Console/LocalConsole.cs index 260a86fb9a..28293c0dd0 100644 --- a/OpenSim/Framework/Console/LocalConsole.cs +++ b/OpenSim/Framework/Console/LocalConsole.cs @@ -32,6 +32,8 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading; +using System.IO; +using Nini.Config; using log4net; namespace OpenSim.Framework.Console @@ -41,7 +43,9 @@ namespace OpenSim.Framework.Console /// public class LocalConsole : CommandConsole { -// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private string m_historyPath; + private bool m_historyEnable; // private readonly object m_syncRoot = new object(); private const string LOGLEVEL_NONE = "(none)"; @@ -79,8 +83,54 @@ namespace OpenSim.Framework.Console return Colors[(Math.Abs(input.ToUpper().GetHashCode()) % Colors.Length)]; } - public LocalConsole(string defaultPrompt) : base(defaultPrompt) + public LocalConsole(string defaultPrompt, IConfig startupConfig = null) : base(defaultPrompt) { + + if (startupConfig == null) return; + + m_historyEnable = startupConfig.GetBoolean("ConsoleHistoryFileEnabled", false); + if (!m_historyEnable) + { + m_log.Info("[LOCAL CONSOLE]: Persistent command line history from file is Disabled"); + return; + } + + string m_historyFile = startupConfig.GetString("ConsoleHistoryFile", "OpenSimConsoleHistory.txt"); + int m_historySize = startupConfig.GetInt("ConsoleHistoryFileLines", 100); + m_historyPath = Path.GetFullPath(Path.Combine(Util.configDir(), m_historyFile)); + m_log.InfoFormat("[LOCAL CONSOLE]: Persistent command line history is Enabled, up to {0} lines from file {1}", m_historySize, m_historyPath); + + if (File.Exists(m_historyPath)) + { + using (StreamReader history_file = new StreamReader(m_historyPath)) + { + string line; + while ((line = history_file.ReadLine()) != null) + { + m_history.Add(line); + } + } + + if (m_history.Count > m_historySize) + { + while (m_history.Count > m_historySize) + m_history.RemoveAt(0); + + using (StreamWriter history_file = new StreamWriter(m_historyPath)) + { + foreach (string line in m_history) + { + history_file.WriteLine(line); + } + } + } + m_log.InfoFormat("[LOCAL CONSOLE]: Read {0} lines of command line history from file {1}", m_history.Count, m_historyPath); + } + else + { + m_log.InfoFormat("[LOCAL CONSOLE]: Creating new empty command line history file {0}", m_historyPath); + File.Create(m_historyPath).Dispose(); + } } private void AddToHistory(string text) @@ -89,6 +139,10 @@ namespace OpenSim.Framework.Console m_history.RemoveAt(0); m_history.Add(text); + if (m_historyEnable) + { + File.AppendAllText(m_historyPath, text + Environment.NewLine); + } } /// diff --git a/OpenSim/Region/Application/OpenSim.cs b/OpenSim/Region/Application/OpenSim.cs index e7f68c2162..d5ea3ef404 100644 --- a/OpenSim/Region/Application/OpenSim.cs +++ b/OpenSim/Region/Application/OpenSim.cs @@ -155,7 +155,7 @@ namespace OpenSim ((RemoteConsole)m_console).ReadConfig(Config); break; default: - m_console = new LocalConsole("Region"); + m_console = new LocalConsole("Region", Config.Configs["Startup"]); break; } } diff --git a/OpenSim/Server/Base/ServicesServerBase.cs b/OpenSim/Server/Base/ServicesServerBase.cs index 8352ee2620..86a25518c5 100644 --- a/OpenSim/Server/Base/ServicesServerBase.cs +++ b/OpenSim/Server/Base/ServicesServerBase.cs @@ -158,7 +158,7 @@ namespace OpenSim.Server.Base } else { - MainConsole.Instance = new LocalConsole(prompt); + MainConsole.Instance = new LocalConsole(prompt, startupConfig); } m_console = MainConsole.Instance; diff --git a/bin/OpenSim.ini.example b/bin/OpenSim.ini.example index 8c1a2c6673..573180abec 100644 --- a/bin/OpenSim.ini.example +++ b/bin/OpenSim.ini.example @@ -51,6 +51,19 @@ ;; \\ - substitute \ ; ConsolePrompt = "Region (\R) " + ;# {ConsoleHistoryFileEnabled} {} {Save console commands to a history file?} {true false} true + ;; Console commands can be saved to a file, so the command history persists after a restart. (default is false) + ; ConsoleHistoryFileEnabled = true + + ;# {ConsoleHistoryFile} {} {Filename in which to save history} {} OpenSimConsoleHistory.txt + ;; The history file can be just a filename (relative to OpenSim's bin/ directory + ;; or it can be a full path to somewhere else. (default is OpenSimConsoleHistory.txt in bin/) + ; ConsoleHistoryFile = "OpenSimConsoleHistory.txt" + + ;# {ConsoleHistoryFileLines} {} {How many lines of history to save?} {} 100 + ;; How many lines of command history should we keep? (default is 100) + ; ConsoleHistoryFileLines = 100 + ;# {save_crashes} {} {Save crashes to disk?} {true false} false ;; Set this to true if you want to log crashes to disk ;; this can be useful when submitting bug reports. diff --git a/bin/OpenSimDefaults.ini b/bin/OpenSimDefaults.ini index 5361e8d100..c6250e5710 100644 --- a/bin/OpenSimDefaults.ini +++ b/bin/OpenSimDefaults.ini @@ -9,6 +9,16 @@ ; \\ - substtitue \ ConsolePrompt = "Region (\R) " + ; Console commands can be saved to a file, so the command history persists after a restart. (default is true) + ConsoleHistoryFileEnabled = true + + ; The history file can be just a filename (relative to OpenSim's bin/ directory + ; or it can be a full path to somewhere else. (default is OpenSimConsoleHistory.txt in bin/) + ConsoleHistoryFile = "OpenSimConsoleHistory.txt" + + ; How many lines of command history should we keep? (default is 100) + ConsoleHistoryFileLines = 100 + ; Set this to true if you want to log crashes to disk ; this can be useful when submitting bug reports. ; However, this will only log crashes within OpenSimulator that cause the entire program to exit diff --git a/bin/Robust.HG.ini.example b/bin/Robust.HG.ini.example index edcbec3a74..ef6f080a4f 100644 --- a/bin/Robust.HG.ini.example +++ b/bin/Robust.HG.ini.example @@ -37,6 +37,16 @@ ; The Robust.exe process must have R/W access to the location ConfigDirectory = "." + ; Console commands can be saved to a file, so the command history persists after a restart. (default is true) + ConsoleHistoryFileEnabled = true + + ; The history file can be just a filename (relative to OpenSim's bin/ directory + ; or it can be a full path to somewhere else. (default is OpenSimConsoleHistory.txt in bin/) + ConsoleHistoryFile = "RobustConsoleHistory.txt" + + ; How many lines of command history should we keep? (default is 100) + ConsoleHistoryFileLines = 100 + [ServiceList] AssetServiceConnector = "8003/OpenSim.Server.Handlers.dll:AssetServiceConnector" diff --git a/bin/Robust.ini.example b/bin/Robust.ini.example index 6686c3f7a2..ca0b699dad 100644 --- a/bin/Robust.ini.example +++ b/bin/Robust.ini.example @@ -28,7 +28,17 @@ ; Set path to directory for modular ini files... ; The Robust.exe process must have R/W access to the location ConfigDirectory = "." + + ; Console commands can be saved to a file, so the command history persists after a restart. (default is true) + ConsoleHistoryFileEnabled = true + ; The history file can be just a filename (relative to OpenSim's bin/ directory + ; or it can be a full path to somewhere else. (default is OpenSimConsoleHistory.txt in bin/) + ConsoleHistoryFile = "RobustConsoleHistory.txt" + + ; How many lines of command history should we keep? (default is 100) + ConsoleHistoryFileLines = 100 + [ServiceList] AssetServiceConnector = "8003/OpenSim.Server.Handlers.dll:AssetServiceConnector" InventoryInConnector = "8003/OpenSim.Server.Handlers.dll:XInventoryInConnector"