Application.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 OpenSim 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 += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  49. XmlConfigurator.Configure();
  50. Console.Write("Performing compatibility checks... ");
  51. string supported = String.Empty;
  52. if (Util.IsEnvironmentSupported(ref supported))
  53. {
  54. Console.WriteLine(" Environment is compatible.\n");
  55. }
  56. else
  57. {
  58. Console.WriteLine(" Environment is unsupported (" + supported + ")\n");
  59. }
  60. Culture.SetCurrentCulture();
  61. ArgvConfigSource configSource = new ArgvConfigSource(args);
  62. configSource.Alias.AddAlias("On", true);
  63. configSource.Alias.AddAlias("Off", false);
  64. configSource.Alias.AddAlias("True", true);
  65. configSource.Alias.AddAlias("False", false);
  66. configSource.AddSwitch("Startup", "background");
  67. configSource.AddSwitch("Startup", "inifile");
  68. configSource.AddSwitch("Startup", "inimaster");
  69. configSource.AddSwitch("Startup", "gridmode");
  70. configSource.AddSwitch("Startup", "physics");
  71. configSource.AddSwitch("Startup", "useexecutepath");
  72. configSource.AddSwitch("Startup", "hypergrid");
  73. configSource.AddSwitch("Startup", "gui");
  74. configSource.AddConfig("StandAlone");
  75. configSource.AddConfig("Network");
  76. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  77. bool hgrid = configSource.Configs["Startup"].GetBoolean("hypergrid", false);
  78. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  79. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  80. if (background)
  81. {
  82. m_sim = new OpenSimBackground(configSource);
  83. m_sim.Startup();
  84. }
  85. else
  86. {
  87. m_sim = null;
  88. if (hgrid)
  89. m_sim = new HGOpenSimNode(configSource);
  90. else
  91. m_sim = new OpenSim(configSource);
  92. m_sim.Startup();
  93. while (true)
  94. {
  95. try
  96. {
  97. MainConsole.Instance.Prompt();
  98. }
  99. catch (Exception e)
  100. {
  101. Console.WriteLine("Caught exception from command: {0}", e);
  102. }
  103. }
  104. }
  105. }
  106. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  107. /// <summary>
  108. /// Global exception handler -- all unhandlet exceptions end up here :)
  109. /// </summary>
  110. /// <param name="sender"></param>
  111. /// <param name="e"></param>
  112. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  113. {
  114. if (_IsHandlingException)
  115. return;
  116. _IsHandlingException = true;
  117. // TODO: Add config option to allow users to turn off error reporting
  118. // TODO: Post error report (disabled for now)
  119. string msg = String.Empty;
  120. msg += "\r\n";
  121. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  122. msg += "\r\n";
  123. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  124. Exception ex = (Exception)e.ExceptionObject;
  125. if (ex.InnerException != null)
  126. {
  127. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  128. }
  129. msg += "\r\n";
  130. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  131. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  132. // Log exception to disk
  133. try
  134. {
  135. if (!Directory.Exists(m_crashDir))
  136. {
  137. Directory.CreateDirectory(m_crashDir);
  138. }
  139. string log = Util.GetUniqueFilename(ex.GetType() + ".txt");
  140. StreamWriter m_crashLog =
  141. new StreamWriter(
  142. Path.Combine(m_crashDir, log)
  143. );
  144. m_crashLog.WriteLine(msg);
  145. m_crashLog.Close();
  146. File.Copy("OpenSim.ini", Path.Combine(m_crashDir, log + "_OpenSim.ini"), true);
  147. }
  148. catch (Exception e2)
  149. {
  150. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  151. }
  152. _IsHandlingException=false;
  153. }
  154. }
  155. }