From bd8c2c6e90dca2fb0bf7d2a10fdfd426a9ee119a Mon Sep 17 00:00:00 2001 From: Geod24 Date: Fri, 24 Jul 2020 15:51:40 +0900 Subject: [PATCH] Use logException consistently and use logDiagnostic Many places around were re-inventing logException with varying level of success. In addition, the full error is now printed as a diagnostic instead of debug. This is more in line with the description of `LogLevel.diagnostic` ("Extended user information (e.g. for more detailed error information)"), as opposed to the one of `LogLevel.debug_` ("Developer information useful for algorithm debugging"). --- source/vibe/core/core.d | 9 +++------ source/vibe/core/log.d | 4 ++-- source/vibe/core/net.d | 3 +-- source/vibe/core/task.d | 16 ++++++---------- source/vibe/core/taskpool.d | 3 +-- tests/0-tcpproxy.d | 3 +-- 6 files changed, 14 insertions(+), 24 deletions(-) diff --git a/source/vibe/core/core.d b/source/vibe/core/core.d index 8d48ed5..8f70c05 100644 --- a/source/vibe/core/core.d +++ b/source/vibe/core/core.d @@ -102,9 +102,8 @@ int runApplication(string[]* args_out = null) version (VibeDebugCatchAll) { try { status = runEventLoop(); - } catch( Throwable th ){ - logError("Unhandled exception in event loop: %s", th.msg); - logDiagnostic("Full exception: %s", th.toString().sanitize()); + } catch (Throwable th) { + th.logException("Unhandled exception in event loop"); return 1; } } else { @@ -908,9 +907,7 @@ Timer setTimer(Duration timeout, void delegate() callback, bool periodic = false return setTimer(timeout, () @trusted nothrow { try callback(); catch (Exception e) { - logWarn("Timer callback failed: %s", e.msg); - scope (failure) assert(false); - logDebug("Full error: %s", e.toString().sanitize); + e.logException!(LogLevel.warn)("Timer callback failed"); } }, periodic); } diff --git a/source/vibe/core/log.d b/source/vibe/core/log.d index d5376a4..78e44fc 100644 --- a/source/vibe/core/log.d +++ b/source/vibe/core/log.d @@ -211,9 +211,9 @@ void logException(LogLevel level = LogLevel.error)(Throwable exception, string file = __FILE__, int line = __LINE__) @safe nothrow { doLog(level, mod, func, file, line, "%s: %s", error_description, exception.msg); - try doLog(LogLevel.debug_, mod, func, file, line, + try doLog(LogLevel.diagnostic, mod, func, file, line, "Full exception: %s", () @trusted { return exception.toString(); } ()); - catch (Exception e) logDebug("Failed to print full exception: %s", e.msg); + catch (Exception e) logDiagnostic("Failed to print full exception: %s", e.msg); } /// diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index 8295a0f..5a87bda 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -143,9 +143,8 @@ TCPListener listenTCP(ushort port, void delegate(TCPConnection) connection_callb return listenTCP(port, (conn) @trusted nothrow { try connection_callback(conn); catch (Exception e) { - logError("Handling of connection failed: %s", e.msg); + e.logException("Handling of connection failed"); conn.close(); - logDebug("Full error: %s", e); } }, address, options); } diff --git a/source/vibe/core/task.d b/source/vibe/core/task.d index b112651..ef113e7 100644 --- a/source/vibe/core/task.d +++ b/source/vibe/core/task.d @@ -399,8 +399,8 @@ final package class TaskFiber : Fiber { debug (VibeTaskLog) logTrace("putting fiber to sleep waiting for new task..."); Fiber.yield(); } catch (Exception e) { - logWarn("CoreTaskFiber was resumed with exception but without active task!"); - logDiagnostic("Full error: %s", e.toString().sanitize()); + e.logException!(LogLevel.warn)( + "CoreTaskFiber was resumed with exception but without active task"); } if (m_shutdown) return; } @@ -430,9 +430,7 @@ final package class TaskFiber : Fiber { logDebug("Task exited while an interrupt was in flight."); } catch (Exception e) { debug if (ms_taskEventCallback) ms_taskEventCallback(TaskEvent.fail, handle); - import std.encoding; - logCritical("Task terminated with uncaught exception: %s", e.msg); - logDebug("Full error: %s", e.toString().sanitize()); + e.logException!(LogLevel.critical)("Task terminated with uncaught exception"); } debug assert(Thread.getThis() is m_thread, "Fiber moved?"); @@ -472,9 +470,8 @@ final package class TaskFiber : Fiber { // make the fiber available for the next task recycleFiber(this); } - } catch(UncaughtException th) { - logCritical("CoreTaskFiber was terminated unexpectedly: %s", th.msg); - logDiagnostic("Full error: %s", th.toString().sanitize()); + } catch (UncaughtException th) { + th.logException("CoreTaskFiber was terminated unexpectedly"); } catch (Throwable th) { import std.stdio : stderr, writeln; import core.stdc.stdlib : abort; @@ -1016,8 +1013,7 @@ package struct TaskScheduler { assert(th, "Fiber returned exception object that is not a Throwable!?"); assert(() @trusted nothrow { return t.fiber.state; } () == Fiber.State.TERM); - logError("Task terminated with unhandled exception: %s", th.msg); - logDebug("Full error: %s", () @trusted { return th.toString().sanitize; } ()); + th.logException("Task terminated with unhandled exception"); // always pass Errors on if (auto err = cast(Error)th) throw err; diff --git a/source/vibe/core/taskpool.d b/source/vibe/core/taskpool.d index f862ab5..ebf3816 100644 --- a/source/vibe/core/taskpool.d +++ b/source/vibe/core/taskpool.d @@ -349,8 +349,7 @@ private final class WorkerThread : Thread { handleWorkerTasks(); logDebug("Worker thread exit."); } catch (Throwable th) { - logFatal("Worker thread terminated due to uncaught error: %s", th.msg); - logDebug("Full error: %s", th.toString().sanitize()); + th.logException!(LogLevel.fatal)("Worker thread terminated due to uncaught error"); abort(); } } diff --git a/tests/0-tcpproxy.d b/tests/0-tcpproxy.d index 65668ec..dda1507 100644 --- a/tests/0-tcpproxy.d +++ b/tests/0-tcpproxy.d @@ -112,8 +112,7 @@ int main() runTask({ try runTest(); catch (Throwable th) { - logError("Test failed: %s", th.msg); - logDiagnostic("Full error: %s", th); + th.logException("Test failed"); ret = 1; } finally exitEventLoop(true); });