From c8858f85d8366e36c588b1dcc7ef7eff40832dd0 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Jul 2014 21:58:37 +0200 Subject: util/Error: add method FormatLastError() --- src/util/Error.cxx | 26 ++++++++++++++++++++++++++ src/util/Error.hxx | 6 ++++++ 2 files changed, 32 insertions(+) (limited to 'src') diff --git a/src/util/Error.cxx b/src/util/Error.cxx index ef8282329..92b2cc5d0 100644 --- a/src/util/Error.cxx +++ b/src/util/Error.cxx @@ -145,4 +145,30 @@ Error::SetLastError(const char *prefix) SetLastError(GetLastError(), prefix); } +void +Error::FormatLastError(DWORD _code, const char *fmt, ...) +{ + char buffer[1024]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, ap); + va_end(ap); + + SetLastError(_code, buffer); +} + +void +Error::FormatLastError(const char *fmt, ...) +{ + DWORD _code = GetLastError(); + + char buffer[1024]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, ap); + va_end(ap); + + SetLastError(_code, buffer); +} + #endif diff --git a/src/util/Error.hxx b/src/util/Error.hxx index 82f01105f..ab66ae5cb 100644 --- a/src/util/Error.hxx +++ b/src/util/Error.hxx @@ -168,6 +168,12 @@ public: #ifdef WIN32 void SetLastError(DWORD _code, const char *prefix); void SetLastError(const char *prefix); + + gcc_printf(3,4) + void FormatLastError(DWORD code, const char *fmt, ...); + + gcc_printf(2,3) + void FormatLastError(const char *fmt, ...); #endif }; -- cgit v1.2.3