Application.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // Check if the system is compatible with OpenSimulator.
  85. // Ensures that the minimum system requirements are met
  86. m_log.Info("Performing compatibility checks... ");
  87. string supported = String.Empty;
  88. if (Util.IsEnvironmentSupported(ref supported))
  89. {
  90. m_log.Info("Environment is compatible.\n");
  91. }
  92. else
  93. {
  94. m_log.Warn("Environment is unsupported (" + supported + ")\n");
  95. }
  96. // Configure nIni aliases and localles
  97. Culture.SetCurrentCulture();
  98. configSource.Alias.AddAlias("On", true);
  99. configSource.Alias.AddAlias("Off", false);
  100. configSource.Alias.AddAlias("True", true);
  101. configSource.Alias.AddAlias("False", false);
  102. configSource.AddSwitch("Startup", "background");
  103. configSource.AddSwitch("Startup", "inifile");
  104. configSource.AddSwitch("Startup", "inimaster");
  105. configSource.AddSwitch("Startup", "inidirectory");
  106. configSource.AddSwitch("Startup", "gridmode");
  107. configSource.AddSwitch("Startup", "physics");
  108. configSource.AddSwitch("Startup", "gui");
  109. configSource.AddConfig("StandAlone");
  110. configSource.AddConfig("Network");
  111. // Check if we're running in the background or not
  112. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  113. // Check if we're saving crashes
  114. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  115. // load Crash directory config
  116. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  117. if (background)
  118. {
  119. m_sim = new OpenSimBackground(configSource);
  120. m_sim.Startup();
  121. }
  122. else
  123. {
  124. m_sim = new OpenSim(configSource);
  125. m_sim.Startup();
  126. while (true)
  127. {
  128. try
  129. {
  130. // Block thread here for input
  131. MainConsole.Instance.Prompt();
  132. }
  133. catch (Exception e)
  134. {
  135. m_log.ErrorFormat("Command error: {0}", e);
  136. }
  137. }
  138. }
  139. }
  140. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  141. /// <summary>
  142. /// Global exception handler -- all unhandlet exceptions end up here :)
  143. /// </summary>
  144. /// <param name="sender"></param>
  145. /// <param name="e"></param>
  146. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  147. {
  148. if (_IsHandlingException)
  149. {
  150. return;
  151. }
  152. _IsHandlingException = true;
  153. // TODO: Add config option to allow users to turn off error reporting
  154. // TODO: Post error report (disabled for now)
  155. string msg = String.Empty;
  156. msg += "\r\n";
  157. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  158. msg += "\r\n";
  159. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  160. Exception ex = (Exception) e.ExceptionObject;
  161. if (ex.InnerException != null)
  162. {
  163. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  164. }
  165. msg += "\r\n";
  166. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  167. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  168. if (m_saveCrashDumps)
  169. {
  170. // Log exception to disk
  171. try
  172. {
  173. if (!Directory.Exists(m_crashDir))
  174. {
  175. Directory.CreateDirectory(m_crashDir);
  176. }
  177. string log = Util.GetUniqueFilename(ex.GetType() + ".txt");
  178. StreamWriter m_crashLog =
  179. new StreamWriter(
  180. Path.Combine(m_crashDir, log)
  181. );
  182. m_crashLog.WriteLine(msg);
  183. m_crashLog.Close();
  184. File.Copy("OpenSim.ini", Path.Combine(m_crashDir, log + "_OpenSim.ini"), true);
  185. }
  186. catch (Exception e2)
  187. {
  188. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  189. }
  190. }
  191. _IsHandlingException = false;
  192. }
  193. }
  194. }