Application.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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. using System;
  28. using System.IO;
  29. using System.Reflection;
  30. using log4net;
  31. using log4net.Config;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. namespace OpenSim
  36. {
  37. /// <summary>
  38. /// Starting class for the OpenSimulator Region
  39. /// </summary>
  40. public class Application
  41. {
  42. /// <summary>
  43. /// Text Console Logger
  44. /// </summary>
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Path to the main ini Configuration file
  48. /// </summary>
  49. public static string iniFilePath = "";
  50. /// <summary>
  51. /// Save Crashes in the bin/crashes folder. Configurable with m_crashDir
  52. /// </summary>
  53. public static bool m_saveCrashDumps = false;
  54. /// <summary>
  55. /// Directory to save crash reports to. Relative to bin/
  56. /// </summary>
  57. public static string m_crashDir = "crashes";
  58. /// <summary>
  59. /// Instance of the OpenSim class. This could be OpenSim or OpenSimBackground depending on the configuration
  60. /// </summary>
  61. protected static OpenSimBase m_sim = null;
  62. //could move our main function into OpenSimMain and kill this class
  63. public static void Main(string[] args)
  64. {
  65. // First line, hook the appdomain to the crash reporter
  66. AppDomain.CurrentDomain.UnhandledException +=
  67. new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  68. // Add the arguments supplied when running the application to the configuration
  69. ArgvConfigSource configSource = new ArgvConfigSource(args);
  70. // Configure Log4Net
  71. configSource.AddSwitch("Startup", "logconfig");
  72. string logConfigFile = configSource.Configs["Startup"].GetString("logconfig", String.Empty);
  73. if (logConfigFile != String.Empty)
  74. {
  75. XmlConfigurator.Configure(new System.IO.FileInfo(logConfigFile));
  76. m_log.InfoFormat("[OPENSIM MAIN]: configured log4net using \"{0}\" as configuration file",
  77. logConfigFile);
  78. }
  79. else
  80. {
  81. XmlConfigurator.Configure();
  82. m_log.Info("[OPENSIM MAIN]: configured log4net using default OpenSim.exe.config");
  83. }
  84. // Increase the number of IOCP threads available. Mono defaults to a tragically low number
  85. int workerThreads, iocpThreads;
  86. System.Threading.ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
  87. m_log.InfoFormat("[OPENSIM MAIN]: Runtime gave us {0} worker threads and {1} IOCP threads", workerThreads, iocpThreads);
  88. if (workerThreads < 500 || iocpThreads < 1000)
  89. {
  90. workerThreads = 500;
  91. iocpThreads = 1000;
  92. m_log.Info("[OPENSIM MAIN]: Bumping up to 500 worker threads and 1000 IOCP threads");
  93. System.Threading.ThreadPool.SetMaxThreads(workerThreads, iocpThreads);
  94. }
  95. // Check if the system is compatible with OpenSimulator.
  96. // Ensures that the minimum system requirements are met
  97. m_log.Info("Performing compatibility checks... ");
  98. string supported = String.Empty;
  99. if (Util.IsEnvironmentSupported(ref supported))
  100. {
  101. m_log.Info("Environment is compatible.\n");
  102. }
  103. else
  104. {
  105. m_log.Warn("Environment is unsupported (" + supported + ")\n");
  106. }
  107. // Configure nIni aliases and localles
  108. Culture.SetCurrentCulture();
  109. configSource.Alias.AddAlias("On", true);
  110. configSource.Alias.AddAlias("Off", false);
  111. configSource.Alias.AddAlias("True", true);
  112. configSource.Alias.AddAlias("False", false);
  113. configSource.AddSwitch("Startup", "background");
  114. configSource.AddSwitch("Startup", "inifile");
  115. configSource.AddSwitch("Startup", "inimaster");
  116. configSource.AddSwitch("Startup", "inidirectory");
  117. configSource.AddSwitch("Startup", "gridmode");
  118. configSource.AddSwitch("Startup", "physics");
  119. configSource.AddSwitch("Startup", "gui");
  120. configSource.AddSwitch("Startup", "console");
  121. configSource.AddConfig("StandAlone");
  122. configSource.AddConfig("Network");
  123. // Check if we're running in the background or not
  124. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  125. // Check if we're saving crashes
  126. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  127. // load Crash directory config
  128. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  129. if (background)
  130. {
  131. m_sim = new OpenSimBackground(configSource);
  132. m_sim.Startup();
  133. }
  134. else
  135. {
  136. m_sim = new OpenSim(configSource);
  137. m_sim.Startup();
  138. while (true)
  139. {
  140. try
  141. {
  142. // Block thread here for input
  143. MainConsole.Instance.Prompt();
  144. }
  145. catch (Exception e)
  146. {
  147. m_log.ErrorFormat("Command error: {0}", e);
  148. }
  149. }
  150. }
  151. }
  152. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  153. /// <summary>
  154. /// Global exception handler -- all unhandlet exceptions end up here :)
  155. /// </summary>
  156. /// <param name="sender"></param>
  157. /// <param name="e"></param>
  158. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  159. {
  160. if (_IsHandlingException)
  161. {
  162. return;
  163. }
  164. _IsHandlingException = true;
  165. // TODO: Add config option to allow users to turn off error reporting
  166. // TODO: Post error report (disabled for now)
  167. string msg = String.Empty;
  168. msg += "\r\n";
  169. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  170. msg += "\r\n";
  171. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  172. Exception ex = (Exception) e.ExceptionObject;
  173. if (ex.InnerException != null)
  174. {
  175. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  176. }
  177. msg += "\r\n";
  178. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  179. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  180. if (m_saveCrashDumps)
  181. {
  182. // Log exception to disk
  183. try
  184. {
  185. if (!Directory.Exists(m_crashDir))
  186. {
  187. Directory.CreateDirectory(m_crashDir);
  188. }
  189. string log = Util.GetUniqueFilename(ex.GetType() + ".txt");
  190. using (StreamWriter m_crashLog = new StreamWriter(Path.Combine(m_crashDir, log)))
  191. {
  192. m_crashLog.WriteLine(msg);
  193. }
  194. File.Copy("OpenSim.ini", Path.Combine(m_crashDir, log + "_OpenSim.ini"), true);
  195. }
  196. catch (Exception e2)
  197. {
  198. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  199. }
  200. }
  201. _IsHandlingException = false;
  202. }
  203. }
  204. }