2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2018-03-08 17:34:55 +01:00
|
|
|
#include <boost/system/system_error.hpp>
|
|
|
|
|
2018-01-11 06:32:32 +01:00
|
|
|
[[noreturn]] static void
|
|
|
|
ircd_terminate_handler()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::aborting()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::set_terminate(&ircd_terminate_handler);
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:34:55 +01:00
|
|
|
std::string
|
|
|
|
ircd::string(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return string(e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::string(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return ircd::string(128, [&ec]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return string(buf, ec);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::string
|
|
|
|
ircd::string(const std::system_error &e)
|
|
|
|
{
|
|
|
|
return string(e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::string(const std::error_code &ec)
|
|
|
|
{
|
|
|
|
return ircd::string(128, [&ec]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return string(buf, ec);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:34:55 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return string(buf, e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const boost::system::error_code &ec)
|
|
|
|
{
|
2018-09-19 09:02:47 +02:00
|
|
|
return string(buf, make_system_error(ec));
|
2018-03-08 18:21:16 +01:00
|
|
|
}
|
2018-03-08 17:34:55 +01:00
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const std::system_error &e)
|
|
|
|
{
|
|
|
|
return string(buf, e.code());
|
2018-03-08 17:34:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const std::error_code &ec)
|
2018-03-08 17:34:55 +01:00
|
|
|
{
|
2018-03-08 18:21:16 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s: %s", ec.category().name(), ec.message()
|
|
|
|
};
|
2018-03-08 17:34:55 +01:00
|
|
|
}
|
|
|
|
|
2018-11-09 06:04:07 +01:00
|
|
|
bool
|
2018-11-09 09:29:31 +01:00
|
|
|
ircd::is(const std::error_code &ec,
|
|
|
|
const std::errc &errc)
|
|
|
|
noexcept
|
2018-11-09 06:04:07 +01:00
|
|
|
{
|
2018-11-09 09:29:31 +01:00
|
|
|
return system_category(ec) && ec.value() == int(errc);
|
2018-11-09 06:04:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const std::error_code &ec)
|
2018-11-09 09:29:31 +01:00
|
|
|
noexcept
|
2018-11-09 06:04:07 +01:00
|
|
|
{
|
|
|
|
return system_category(ec.category());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const std::error_category &ec)
|
2018-11-09 09:29:31 +01:00
|
|
|
noexcept
|
2018-11-09 06:04:07 +01:00
|
|
|
{
|
|
|
|
return ec == std::system_category() ||
|
|
|
|
ec == std::generic_category() ||
|
|
|
|
ec == boost::system::system_category();
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::system::system_error &e)
|
2018-01-11 06:32:32 +01:00
|
|
|
{
|
2018-03-08 18:21:16 +01:00
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(e.code()), e.what()
|
|
|
|
};
|
2018-01-11 06:32:32 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
2018-11-12 00:52:48 +01:00
|
|
|
make_error_code(ec), ec.message()
|
2018-03-08 18:21:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-09 03:07:22 +01:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const std::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(ec)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const std::errc &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(ec)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::system_error
|
2018-01-11 06:32:32 +01:00
|
|
|
ircd::make_system_error(const int &code)
|
|
|
|
{
|
2018-03-08 18:21:16 +01:00
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(code)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
e.code().value(), e.code().category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
ec.value(), ec.category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:23:59 +02:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const std::system_error &e)
|
|
|
|
{
|
|
|
|
return e.code();
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const int &code)
|
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
code, std::system_category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const std::error_code &ec)
|
|
|
|
{
|
|
|
|
return ec;
|
2018-01-11 06:32:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-09 02:10:56 +01:00
|
|
|
//
|
|
|
|
// exception
|
|
|
|
//
|
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
ssize_t
|
2017-03-18 01:37:40 +01:00
|
|
|
ircd::exception::generate(const char *const &fmt,
|
|
|
|
const va_rtti &ap)
|
2016-07-26 04:06:31 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2017-03-18 01:37:40 +01:00
|
|
|
return fmt::vsnprintf(buf, sizeof(buf), fmt, ap);
|
2016-08-15 04:03:25 +02:00
|
|
|
}
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
ssize_t
|
2017-03-18 01:37:40 +01:00
|
|
|
ircd::exception::generate(const char *const &name,
|
|
|
|
const char *const &fmt,
|
|
|
|
const va_rtti &ap)
|
2016-08-15 04:03:25 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2016-07-26 04:06:31 +02:00
|
|
|
size_t size(0);
|
|
|
|
const bool empty(!fmt || !fmt[0] || fmt[0] == ' ');
|
2016-11-29 16:23:38 +01:00
|
|
|
size = strlcat(buf, name, sizeof(buf));
|
|
|
|
size = strlcat(buf, empty? "." : ": ", sizeof(buf));
|
2016-07-26 04:06:31 +02:00
|
|
|
if(size < sizeof(buf))
|
2017-03-18 01:37:40 +01:00
|
|
|
size += fmt::vsnprintf(buf + size, sizeof(buf) - size, fmt, ap);
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
return size;
|
2016-07-26 04:06:31 +02:00
|
|
|
}
|
2017-11-26 01:20:42 +01:00
|
|
|
|
2018-11-09 02:10:56 +01:00
|
|
|
//
|
|
|
|
// assertion
|
|
|
|
//
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::assertion::assertion()
|
2018-01-27 06:49:41 +01:00
|
|
|
noexcept(RB_DEBUG_LEVEL)
|
2018-11-05 02:18:00 +01:00
|
|
|
:assertion
|
|
|
|
{
|
|
|
|
"without exception"_sv
|
|
|
|
}
|
2018-01-08 22:26:29 +01:00
|
|
|
{
|
2018-10-16 07:29:39 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::assertion::assertion(const string_view &msg)
|
2018-10-16 07:29:39 +02:00
|
|
|
noexcept(RB_DEBUG_LEVEL)
|
|
|
|
{
|
2018-08-16 21:41:19 +02:00
|
|
|
log::critical
|
|
|
|
{
|
2018-10-16 07:29:39 +02:00
|
|
|
"IRCd Assertion :%s", msg
|
2018-08-16 21:41:19 +02:00
|
|
|
};
|
|
|
|
|
2018-10-16 07:29:39 +02:00
|
|
|
if(std::uncaught_exceptions())
|
2018-11-05 02:18:00 +01:00
|
|
|
assertion
|
|
|
|
{
|
|
|
|
std::current_exception()
|
|
|
|
};
|
2018-10-16 07:29:39 +02:00
|
|
|
|
2018-08-16 21:41:19 +02:00
|
|
|
assert(0);
|
2018-10-16 07:29:39 +02:00
|
|
|
throw assertive
|
|
|
|
{
|
|
|
|
"IRCd Assertion :%s", msg
|
|
|
|
};
|
2018-01-08 22:26:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::assertion::assertion(std::exception_ptr eptr)
|
2018-01-27 06:49:41 +01:00
|
|
|
noexcept(RB_DEBUG_LEVEL) try
|
2018-01-08 22:26:29 +01:00
|
|
|
{
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-11-05 02:18:00 +01:00
|
|
|
assertion{e};
|
2018-01-08 22:26:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::assertion::assertion(const std::exception &e)
|
2018-01-27 06:49:41 +01:00
|
|
|
noexcept(RB_DEBUG_LEVEL)
|
2018-01-08 22:26:29 +01:00
|
|
|
{
|
|
|
|
#ifdef RB_DEBUG
|
2018-11-05 02:18:00 +01:00
|
|
|
terminate{e};
|
2018-01-08 22:26:29 +01:00
|
|
|
#else
|
2018-08-16 21:41:19 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"IRCd Assertion %s", e.what()
|
|
|
|
};
|
|
|
|
|
2018-01-08 22:26:29 +01:00
|
|
|
throw e;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-09 02:10:56 +01:00
|
|
|
//
|
|
|
|
// terminate
|
|
|
|
//
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::terminate::terminate()
|
2017-11-26 01:20:42 +01:00
|
|
|
noexcept
|
2018-11-05 02:18:00 +01:00
|
|
|
:terminate
|
|
|
|
{
|
|
|
|
std::current_exception()
|
|
|
|
}
|
2017-11-26 01:20:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::terminate::terminate(std::exception_ptr eptr)
|
2017-11-26 01:20:42 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(eptr) try
|
|
|
|
{
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-11-05 02:18:00 +01:00
|
|
|
terminate{e};
|
2017-11-26 01:20:42 +01:00
|
|
|
}
|
|
|
|
|
2018-08-16 21:41:19 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"IRCd Terminate without exception"
|
|
|
|
};
|
|
|
|
|
2018-11-02 06:25:10 +01:00
|
|
|
fputs("\nIRCd Terminate without exception\n", stderr);
|
|
|
|
|
|
|
|
::fflush(stdout);
|
|
|
|
::fflush(stderr);
|
2017-11-26 01:20:42 +01:00
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
2018-11-05 02:18:00 +01:00
|
|
|
ircd::terminate::terminate(const std::exception &e)
|
2017-11-26 01:20:42 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-08-16 21:41:19 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"IRCd Terminated: %s", e.what()
|
|
|
|
};
|
|
|
|
|
2018-11-02 06:25:10 +01:00
|
|
|
fprintf(stderr, "\nIRCd Terminated: %s\n", e.what());
|
|
|
|
::fflush(stderr);
|
2018-11-05 02:18:00 +01:00
|
|
|
::fflush(stdout);
|
2018-11-02 06:25:10 +01:00
|
|
|
std::terminate();
|
2017-11-26 01:20:42 +01:00
|
|
|
}
|