Merge pull request #155 from vibe-d/log_exception

Implement logException.
merged-on-behalf-of: Leonid Kramer <l-kramer@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2019-05-25 23:29:51 +02:00 committed by GitHub
commit 94ed25d831
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -148,6 +148,30 @@ void logFatal(string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy
} }
} }
/** Logs an exception, including a debug stack trace.
*/
void logException(LogLevel level = LogLevel.error, string file = __FILE__,
int line = __LINE__)(Throwable exception, string error_description)
@safe nothrow {
log!(level, file, line)("%s: %s", error_description, exception.msg);
try logDebug!(file, line)("Full exception: %s", () @trusted { return exception.toString(); } ());
catch (Exception e) logDebug("Failed to print full exception: %s", e.msg);
}
///
unittest {
void test() nothrow
{
try {
throw new Exception("Something failed!");
} catch (Exception e) {
logException(e, "Failed to carry out some operation");
}
}
}
/// Specifies the log level for a particular log message. /// Specifies the log level for a particular log message.
enum LogLevel { enum LogLevel {
trace, /// Developer information for locating events when no useful stack traces are available trace, /// Developer information for locating events when no useful stack traces are available