Application.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. public class Application
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. public static string iniFilePath = "";
  41. public static bool m_saveCrashDumps = false;
  42. public static string m_crashDir = "crashes";
  43. protected static OpenSimBase m_sim = null;
  44. //could move our main function into OpenSimMain and kill this class
  45. public static void Main(string[] args)
  46. {
  47. // First line
  48. AppDomain.CurrentDomain.UnhandledException +=
  49. new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  50. ArgvConfigSource configSource = new ArgvConfigSource(args);
  51. configSource.AddSwitch("Startup", "logconfig");
  52. string logConfigFile = configSource.Configs["Startup"].GetString("logconfig", String.Empty);
  53. if (logConfigFile != String.Empty)
  54. {
  55. XmlConfigurator.Configure(new System.IO.FileInfo(logConfigFile));
  56. m_log.InfoFormat("[OPENSIM MAIN]: configured log4net using \"{0}\" as configuration file",
  57. logConfigFile);
  58. }
  59. else
  60. {
  61. XmlConfigurator.Configure();
  62. m_log.Info("[OPENSIM MAIN]: configured log4net using default OpenSim.exe.config");
  63. }
  64. m_log.Info("Performing compatibility checks... ");
  65. string supported = String.Empty;
  66. if (Util.IsEnvironmentSupported(ref supported))
  67. {
  68. m_log.Info("Environment is compatible.\n");
  69. }
  70. else
  71. {
  72. m_log.Warn("Environment is unsupported (" + supported + ")\n");
  73. }
  74. Culture.SetCurrentCulture();
  75. configSource.Alias.AddAlias("On", true);
  76. configSource.Alias.AddAlias("Off", false);
  77. configSource.Alias.AddAlias("True", true);
  78. configSource.Alias.AddAlias("False", false);
  79. configSource.AddSwitch("Startup", "background");
  80. configSource.AddSwitch("Startup", "inifile");
  81. configSource.AddSwitch("Startup", "inimaster");
  82. configSource.AddSwitch("Startup", "inidirectory");
  83. configSource.AddSwitch("Startup", "gridmode");
  84. configSource.AddSwitch("Startup", "physics");
  85. configSource.AddSwitch("Startup", "gui");
  86. configSource.AddConfig("StandAlone");
  87. configSource.AddConfig("Network");
  88. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  89. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  90. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  91. if (background)
  92. {
  93. m_sim = new OpenSimBackground(configSource);
  94. m_sim.Startup();
  95. }
  96. else
  97. {
  98. m_sim = new OpenSim(configSource);
  99. m_sim.Startup();
  100. while (true)
  101. {
  102. try
  103. {
  104. MainConsole.Instance.Prompt();
  105. }
  106. catch (Exception e)
  107. {
  108. m_log.ErrorFormat("Command error: {0}", e);
  109. }
  110. }
  111. }
  112. }
  113. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  114. /// <summary>
  115. /// Global exception handler -- all unhandlet exceptions end up here :)
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  120. {
  121. if (_IsHandlingException)
  122. return;
  123. _IsHandlingException = true;
  124. // TODO: Add config option to allow users to turn off error reporting
  125. // TODO: Post error report (disabled for now)
  126. string msg = String.Empty;
  127. msg += "\r\n";
  128. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  129. msg += "\r\n";
  130. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  131. Exception ex = (Exception) e.ExceptionObject;
  132. if (ex.InnerException != null)
  133. {
  134. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  135. }
  136. msg += "\r\n";
  137. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  138. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  139. if (m_saveCrashDumps)
  140. {
  141. // Log exception to disk
  142. try
  143. {
  144. if (!Directory.Exists(m_crashDir))
  145. {
  146. Directory.CreateDirectory(m_crashDir);
  147. }
  148. string log = Util.GetUniqueFilename(ex.GetType() + ".txt");
  149. StreamWriter m_crashLog =
  150. new StreamWriter(
  151. Path.Combine(m_crashDir, log)
  152. );
  153. m_crashLog.WriteLine(msg);
  154. m_crashLog.Close();
  155. File.Copy("OpenSim.ini", Path.Combine(m_crashDir, log + "_OpenSim.ini"), true);
  156. }
  157. catch (Exception e2)
  158. {
  159. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  160. }
  161. }
  162. _IsHandlingException = false;
  163. }
  164. }
  165. }