//// /// <summary> /// Outputs a log message indicating the current context. /// </summary> /// <param name="msg"></param> /// <param name="omitNamespace"></param> /// <param name="max"></param> /// <param name="separator"></param> /// <param name="spacer"></param> public static void LogContext(string msg, bool omitNamespace = true, int max = -1, string separator = "\n", string spacer = " ") { IList <string> contextSlice = DebugHelpers.GetContextSlice(3, omitNamespace, max); string context = string.Join(separator + spacer, contextSlice); LogHelpers.Log(msg + " at " + context); }
/// <summary> /// Formats a given message as it would appear in the log output. /// </summary> /// <param name="msg"></param> /// <param name="contextDepth">Indicates whether to also output the callstack context at the specified depth. /// No output if -1 is set.</param> /// <returns></returns> public static string FormatMessage(string msg, int contextDepth = -1) { ModHelpersMod mymod = ModHelpersMod.Instance; if (mymod == null) { contextDepth = contextDepth == -1 ? 2 : contextDepth; return("!Mod Helpers unloaded. Message called from: " + DebugHelpers.GetCurrentContext(contextDepth)); } var logHelpers = mymod.LogHelpers; string output; double nowSeconds; try { var beginning = new DateTime(1970, 1, 1, 0, 0, 0); TimeSpan nowTotalSpan = DateTime.UtcNow.Subtract(beginning); nowSeconds = nowTotalSpan.TotalSeconds - logHelpers.StartTime; } catch (Exception e) { nowSeconds = 0; output = "FORMATTING ERROR 1 (" + e.GetType().Name + ") - " + msg; } try { string nowSecondsWhole = ((int)nowSeconds).ToString("D6"); string nowSecondsDecimal = (nowSeconds - (int)nowSeconds).ToString("N2"); string now = nowSecondsWhole + "." + (nowSecondsDecimal.Length > 2 ? nowSecondsDecimal.Substring(2) : nowSecondsDecimal); string from = Main.myPlayer.ToString("D3"); string logged = Main.netMode + ":" + from + " - " + now; if (logged.Length < 26) { logged += new String(' ', 26 - logged.Length); } else { logged += " "; } output = logged + msg; } catch (Exception e) { output = "FORMATTING ERROR 2 (" + e.GetType().Name + ") - " + msg; } if (contextDepth >= 0) { output = DebugHelpers.GetCurrentContext(contextDepth) + ((output != "") ? " - " + output : ""); } return(output); }