Application.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Net;
  30. using System.Reflection;
  31. using log4net;
  32. using log4net.Config;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. namespace OpenSim
  37. {
  38. public class Application
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. public static string iniFilePath = "";
  42. public static bool m_saveCrashDumps = false;
  43. public static string m_crashDir = "crashes";
  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.AddConfig("StandAlone");
  74. configSource.AddConfig("Network");
  75. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  76. bool hgrid = configSource.Configs["Startup"].GetBoolean("hypergrid", false);
  77. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  78. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  79. if (background)
  80. {
  81. OpenSimBase sim = new OpenSimBackground(configSource);
  82. sim.Startup();
  83. }
  84. else
  85. {
  86. OpenSimBase sim = null;
  87. if (hgrid)
  88. sim = new HGOpenSimNode(configSource);
  89. else
  90. sim = new OpenSim(configSource);
  91. sim.Startup();
  92. while (true)
  93. {
  94. MainConsole.Instance.Prompt();
  95. }
  96. }
  97. }
  98. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  99. /// <summary>
  100. /// Global exception handler -- all unhandlet exceptions end up here :)
  101. /// </summary>
  102. /// <param name="sender"></param>
  103. /// <param name="e"></param>
  104. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  105. {
  106. if (_IsHandlingException)
  107. return;
  108. _IsHandlingException = true;
  109. // TODO: Add config option to allow users to turn off error reporting
  110. // TODO: Post error report (disabled for now)
  111. string msg = String.Empty;
  112. msg += "\r\n";
  113. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  114. msg += "\r\n";
  115. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  116. Exception ex = (Exception)e.ExceptionObject;
  117. if (ex.InnerException != null)
  118. {
  119. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  120. }
  121. msg += "\r\n";
  122. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  123. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  124. // Log exception to disk
  125. try
  126. {
  127. if (!Directory.Exists(m_crashDir))
  128. {
  129. Directory.CreateDirectory(m_crashDir);
  130. }
  131. StreamWriter m_crashLog =
  132. new StreamWriter(
  133. Path.Combine(m_crashDir, Util.GetUniqueFilename(ex.GetType() + ".txt"))
  134. );
  135. m_crashLog.WriteLine(msg);
  136. m_crashLog.Close();
  137. }
  138. catch (Exception e2)
  139. {
  140. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  141. }
  142. _IsHandlingException=false;
  143. }
  144. }
  145. }