using System; using System.Globalization; using System.IO; using Decal.Adapter; namespace Mag.Shared { static class Debug { static string _debugLogPath; static string _errorLogPath; static string _pluginName; public static void Init(string debugLogPath, string errorLogPath, string pluginName) { _debugLogPath = debugLogPath; _errorLogPath = errorLogPath; _pluginName = pluginName; } /// /// This will write to the debug.txt /// public static void LogDebug(string message) { try { if (String.IsNullOrEmpty(_debugLogPath)) return; FileInfo fileInfo = new FileInfo(_debugLogPath); // Limit the file to 1MB bool append = !(fileInfo.Exists && fileInfo.Length > 1048576); using (StreamWriter writer = new StreamWriter(fileInfo.FullName, append)) { writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "," + message); writer.Close(); } } catch { // Eat the exception, yumm. } } /// /// This will only write the exception to the errors.txt file if DebugEnabled is true. /// public static void LogException(Exception ex, string note = null) { try { //if (!Settings.SettingsManager.Misc.DebuggingEnabled.Value) // return; if (note != null) MyClasses.VCS_Connector.SendChatTextCategorized("Errors", "<{" + _pluginName + "}>: " + "Exception caught: " + ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.StackTrace + Environment.NewLine + "Note: " + note, 5); else MyClasses.VCS_Connector.SendChatTextCategorized("Errors", "<{" + _pluginName + "}>: " + "Exception caught: " + ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.StackTrace, 5); if (String.IsNullOrEmpty(_errorLogPath)) return; FileInfo fileInfo = new FileInfo(_errorLogPath); // Limit the file to 1MB bool append = !(fileInfo.Exists && fileInfo.Length > 1048576); using (StreamWriter writer = new StreamWriter(fileInfo.FullName, append)) { writer.WriteLine("============================================================================"); writer.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture)); writer.WriteLine(ex); if (note != null) writer.WriteLine("Note: " + note); writer.WriteLine("============================================================================"); writer.WriteLine(""); writer.Close(); } } catch { // Eat the exception, yumm. } } public static void LogText(string text) { try { //if (!Settings.SettingsManager.Misc.DebuggingEnabled.Value) // return; MyClasses.VCS_Connector.SendChatTextCategorized("CommandLine", "<{" + _pluginName + "}>: " + "Log Text: " + text, 5); if (String.IsNullOrEmpty(_errorLogPath)) return; FileInfo fileInfo = new FileInfo(_errorLogPath); // Limit the file to 1MB bool append = !(fileInfo.Exists && fileInfo.Length > 1048576); using (StreamWriter writer = new StreamWriter(fileInfo.FullName, append)) { writer.WriteLine(DateTime.Now + ": " + text); writer.Close(); } } catch (Exception ex) { LogException(ex); } } /// /// This will only write the message to the chat if DebugEnabled is true. /// /// /// /// public static void WriteToChat(string message, int color = 5, int target = 1) { try { //if (!Settings.SettingsManager.Misc.DebuggingEnabled.Value) // return; MyClasses.VCS_Connector.SendChatTextCategorized("CommandLine", "<{" + _pluginName + "}>: " + message, color, target); } catch (Exception ex) { LogException(ex); } } } }