2013-08-07 09:35:30 +02:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2013-08-07 09:35:30 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FatalError.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "LogV.hxx"
|
2013-08-07 09:35:30 +02:00
|
|
|
|
2020-03-13 00:31:17 +01:00
|
|
|
#include <cstdarg>
|
2020-03-27 02:44:09 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2013-08-07 09:35:30 +02:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-08-07 09:35:30 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2020-03-12 23:51:16 +01:00
|
|
|
#include <cerrno>
|
2013-08-07 09:35:30 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain fatal_error_domain("fatal_error");
|
2013-08-07 09:35:30 +02:00
|
|
|
|
2020-03-16 05:03:09 +01:00
|
|
|
[[noreturn]]
|
2013-10-30 23:41:02 +01:00
|
|
|
static void
|
|
|
|
Abort()
|
|
|
|
{
|
2020-03-27 02:44:09 +01:00
|
|
|
std::_Exit(EXIT_FAILURE);
|
2013-10-30 23:41:02 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:35:30 +02:00
|
|
|
void
|
|
|
|
FatalError(const char *msg)
|
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(fatal_error_domain, msg);
|
2013-10-30 23:41:02 +01:00
|
|
|
Abort();
|
2013-08-07 09:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FormatFatalError(const char *fmt, ...)
|
|
|
|
{
|
2020-03-13 00:31:17 +01:00
|
|
|
std::va_list ap;
|
2013-08-07 09:35:30 +02:00
|
|
|
va_start(ap, fmt);
|
2019-05-22 18:38:25 +02:00
|
|
|
LogFormatV(LogLevel::ERROR, fatal_error_domain, fmt, ap);
|
2013-08-07 09:35:30 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2013-10-30 23:41:02 +01:00
|
|
|
Abort();
|
2013-08-07 09:35:30 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2014-12-05 00:16:24 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
FatalSystemError(const char *msg, DWORD code)
|
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
nullptr, code, 0,
|
|
|
|
buffer, sizeof(buffer), nullptr);
|
|
|
|
FormatFatalError("%s: %s", msg, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-08-07 09:35:30 +02:00
|
|
|
void
|
|
|
|
FatalSystemError(const char *msg)
|
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2014-12-05 00:16:24 +01:00
|
|
|
FatalSystemError(msg, GetLastError());
|
2013-08-07 09:35:30 +02:00
|
|
|
#else
|
2020-03-27 02:44:09 +01:00
|
|
|
auto system_error = std::strerror(errno);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(fatal_error_domain, "%s: %s", msg, system_error);
|
2013-10-30 23:41:02 +01:00
|
|
|
Abort();
|
2014-12-05 00:16:24 +01:00
|
|
|
#endif
|
2013-08-07 09:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FormatFatalSystemError(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
2020-03-13 00:31:17 +01:00
|
|
|
std::va_list ap;
|
2013-08-07 09:35:30 +02:00
|
|
|
va_start(ap, fmt);
|
2020-03-27 02:44:09 +01:00
|
|
|
std::vsnprintf(buffer, sizeof(buffer), fmt, ap);
|
2013-08-07 09:35:30 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
FatalSystemError(buffer);
|
|
|
|
}
|