public static void Write(string text, int showStackFromThisFrame = 0, string prefix = "<>Warning: ") { if (AOpt.Warnings.IsDisabled(text)) { return; } if (!AOpt.Warnings.Verbose) { var t = Api.GetTickCount64(); if (t - s_warningTime < 1000) { return; } s_warningTime = t; } string s = text ?? ""; if (showStackFromThisFrame >= 0) { var x = new StackTrace(showStackFromThisFrame + 1, true); var st = x.ToString(); var rn = st.Ends('\n') ? "" : "\r\n"; s = $"{prefix}{s} <fold><\a>\r\n{st}{rn}</\a></fold>"; } else { s = prefix + s; } AOutput.Write(s); }
static void _Print(object text, string cp, int cln, string cmn) { string s = AOutput.ObjectToString_(text); string prefix = null; if (s.Starts("<>")) { prefix = "<>"; s = s.Substring(2); } AOutput.Write($"{prefix}Debug: {cmn} ({APath.GetFileName(cp)}:{cln}): {s}"); }
internal static void OnHostHandledException(UnhandledExceptionEventArgs e) { var k = s_instance; if (k != null) { k.OnUnhandledException(e); } else { AOutput.Write(e.ExceptionObject); } }
public static void PrintFunc([CallerMemberName] string name = null) => AOutput.Write(name);
static void _Print2(object o) { AOutput.Write(o?.ToString()); }
/// <summary> /// Runs a console program, waits until its process ends, and gets its output text. /// This overload writes text lines to the output in real time. /// </summary> /// <param name="exe"> /// Path or name of an .exe or .bat file. Can be: /// - Full path. Examples: <c>@"C:\folder\x.exe"</c>, <c>AFolders.System + "x.exe"</c>, <c>@"%AFolders.System%\x.exe"</c>. /// - Filename, like <c>"x.exe"</c>. This function calls <see cref="AFile.SearchPath"/>. /// - Path relative to <see cref="AFolders.ThisApp"/>. Examples: <c>"x.exe"</c>, <c>@"subfolder\x.exe"</c>, <c>@".\subfolder\x.exe"</c>, <c>@"..\folder\x.exe"</c>. /// /// Supports environment variables, like <c>@"%TMP%\x.bat"</c>. See <see cref="APath.ExpandEnvVar"/>. /// </param> /// <param name="args">null or command line arguments.</param> /// <param name="curDir"> /// Initial current directory of the new process. /// - If null, uses <c>Directory.GetCurrentDirectory()</c>. /// - Else if "", calls <c>APath.GetDirectoryPath(exe)</c>. /// - Else calls <see cref="APath.ExpandEnvVar"/>. /// </param> /// <param name="encoding"> /// Console's text encoding. /// If null (default), uses the default console text encoding (API <msdn>GetOEMCP</msdn>); it is not Unicode. Programs that display Unicode text use <see cref="Encoding.UTF8"/>. /// </param> /// <returns>The process exit code. Usually a non-0 value means error.</returns> /// <exception cref="AuException">Failed, for example file not found.</exception> /// <remarks> /// The console window is hidden. The text that would be displayed in it is redirected to this function. /// /// Console programs have two output text streams - standard output and standard error. This function gets both. /// Alternatively use <see cref="Process.Start"/>. It gets the output and error streams separately, and some lines may be received in incorrect order in time. /// </remarks> /// <example> /// <code><![CDATA[ /// string v = "example"; /// int r1 = AExec.RunConsole(@"Q:\Test\console1.exe", $@"/an ""{v}"" /etc"); /// /// int r2 = AExec.RunConsole(s => AOutput.Write(s), @"Q:\Test\console2.exe"); /// /// int r3 = AExec.RunConsole(out var text, @"Q:\Test\console3.exe", encoding: Encoding.UTF8); /// AOutput.Write(text); /// ]]></code> /// </example> public static unsafe int RunConsole(string exe, string args = null, string curDir = null, Encoding encoding = null) { return(_RunConsole(s => AOutput.Write(s), null, exe, args, curDir, encoding)); }
/// <summary> /// Writes exception info to the output. /// Override this function to intercept unhandled exceptions. Call the base function if want to see exception info as usually. /// </summary> /// <param name="e"></param> protected virtual void OnUnhandledException(UnhandledExceptionEventArgs e) => AOutput.Write(e.ExceptionObject);