2020-08-24 10:35:15 +02:00
|
|
|
// The Construct
|
2019-05-29 04:18:11 +02:00
|
|
|
//
|
2020-08-24 10:35:15 +02:00
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
2019-05-29 04:18:11 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-08-25 05:26:05 +02:00
|
|
|
#include <RB_INC_SIGNAL_H
|
2019-05-29 04:18:11 +02:00
|
|
|
#include <RB_INC_MAGICK_API_H
|
|
|
|
|
|
|
|
namespace ircd::magick
|
|
|
|
{
|
2019-05-30 14:14:34 +02:00
|
|
|
struct display;
|
2019-05-30 11:44:12 +02:00
|
|
|
struct transform;
|
|
|
|
|
2020-02-02 06:52:46 +01:00
|
|
|
[[noreturn]] static void handle_exception(const ExceptionType, const char *, const char *);
|
2020-07-11 05:41:24 +02:00
|
|
|
static void handle_fatal(const ExceptionType, const char *, const char *) __attribute__((noreturn));
|
2019-07-26 22:37:40 +02:00
|
|
|
static void handle_error(const ExceptionType, const char *, const char *) noexcept;
|
|
|
|
static void handle_warning(const ExceptionType, const char *, const char *) noexcept;
|
|
|
|
static void handle_log(const ExceptionType, const char *) noexcept;
|
2019-05-30 07:27:55 +02:00
|
|
|
static void *handle_realloc(void *, size_t) noexcept;
|
|
|
|
static void *handle_malloc(size_t) noexcept;
|
|
|
|
static void handle_free(void *) noexcept;
|
2019-05-30 03:08:19 +02:00
|
|
|
static uint handle_progress(const char *, const int64_t, const uint64_t, ExceptionInfo *) noexcept;
|
2019-05-29 07:08:46 +02:00
|
|
|
|
|
|
|
template<class R, class F, class... A> static R call(F&&, A&&...);
|
|
|
|
template<class R, class F, class... A> static R callex(F&&, A&&...);
|
|
|
|
template<class F, class... A> static void callpf(F&&, A&&...);
|
|
|
|
|
2019-07-17 01:25:08 +02:00
|
|
|
extern bool call_ready;
|
|
|
|
extern ctx::dock call_dock;
|
|
|
|
extern ctx::mutex call_mutex;
|
2019-07-06 00:32:10 +02:00
|
|
|
extern conf::item<uint64_t> limit_ticks;
|
2019-06-30 02:30:59 +02:00
|
|
|
extern conf::item<uint64_t> limit_cycles;
|
2019-05-30 07:14:50 +02:00
|
|
|
extern conf::item<uint64_t> yield_threshold;
|
|
|
|
extern conf::item<uint64_t> yield_interval;
|
2019-05-29 07:08:46 +02:00
|
|
|
extern log::log log;
|
2019-05-29 04:18:11 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 14:14:34 +02:00
|
|
|
struct ircd::magick::display
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
display(const ImageInfo &, Image &);
|
2019-05-30 14:14:34 +02:00
|
|
|
display(const const_buffer &);
|
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
struct ircd::magick::transform
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
using input = std::tuple<const ImageInfo &, const Image *>;
|
2019-05-30 11:44:12 +02:00
|
|
|
using output = std::function<void (const const_buffer &)>;
|
2019-07-26 22:37:40 +02:00
|
|
|
using transformer = std::function<Image *(const input &)>;
|
2019-05-30 11:44:12 +02:00
|
|
|
|
|
|
|
transform(const const_buffer &, const output &, const transformer &);
|
|
|
|
};
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
decltype(ircd::magick::log)
|
|
|
|
ircd::magick::log
|
|
|
|
{
|
|
|
|
"magick"
|
|
|
|
};
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
decltype(ircd::magick::limit_ticks)
|
|
|
|
ircd::magick::limit_ticks
|
2019-06-30 02:14:41 +02:00
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
{ "name", "ircd.magick.limit.ticks" },
|
|
|
|
{ "default", 10000L },
|
2019-06-30 02:14:41 +02:00
|
|
|
};
|
|
|
|
|
2019-06-30 02:30:59 +02:00
|
|
|
decltype(ircd::magick::limit_cycles)
|
|
|
|
ircd::magick::limit_cycles
|
|
|
|
{
|
|
|
|
{ "name", "ircd.magick.limit.cycles" },
|
|
|
|
{ "default", 0L },
|
|
|
|
};
|
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
decltype(ircd::magick::yield_threshold)
|
|
|
|
ircd::magick::yield_threshold
|
|
|
|
{
|
|
|
|
{ "name", "ircd.magick.yield.threshold" },
|
|
|
|
{ "default", 1000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::magick::yield_interval)
|
|
|
|
ircd::magick::yield_interval
|
|
|
|
{
|
|
|
|
{ "name", "ircd.magick.yield.interval" },
|
|
|
|
{ "default", 768L },
|
|
|
|
};
|
|
|
|
|
2019-07-17 01:25:08 +02:00
|
|
|
// It is likely that we can't have two contexts enter libmagick
|
|
|
|
// simultaneously. This race is possible if the progress callback yields
|
|
|
|
// and another context starts an operation. It is highly unlikely the lib
|
|
|
|
// can handle reentrancy on the same thread. Hitting thread mutexes within
|
|
|
|
// magick will also be catastrophic to ircd::ctx.
|
|
|
|
decltype(ircd::magick::call_mutex)
|
|
|
|
ircd::magick::call_mutex;
|
|
|
|
|
|
|
|
decltype(ircd::magick::call_dock)
|
|
|
|
ircd::magick::call_dock;
|
|
|
|
|
|
|
|
decltype(ircd::magick::call_ready)
|
|
|
|
ircd::magick::call_ready;
|
|
|
|
|
2019-06-01 01:06:55 +02:00
|
|
|
decltype(ircd::magick::version_api)
|
|
|
|
ircd::magick::version_api
|
|
|
|
{
|
|
|
|
"magick", info::versions::API, MagickLibVersion, {0}, MagickLibVersionText
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::magick::version_abi)
|
|
|
|
ircd::magick::version_abi
|
|
|
|
{
|
|
|
|
"magick", info::versions::ABI, 0, {0}, []
|
|
|
|
(auto &version, const auto &buf)
|
|
|
|
{
|
|
|
|
ulong monotonic(0);
|
2019-07-26 22:37:40 +02:00
|
|
|
strlcpy(buf, GetMagickVersion(&monotonic));
|
2019-06-01 01:06:55 +02:00
|
|
|
version.monotonic = monotonic;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-25 22:54:05 +02:00
|
|
|
//
|
|
|
|
// Magick library signal handler workarounds.
|
|
|
|
//
|
|
|
|
// By default the graphicsmagick library installs signal handlers on
|
|
|
|
// supporting platforms starting in InitializeMagick() for the duration
|
|
|
|
// of the library. These handlers provide no essential functionality,
|
|
|
|
// polluting the address space for other libraries and users of our libircd,
|
|
|
|
// causing unexpected behavior.
|
|
|
|
//
|
|
|
|
// Even though the library makes a good faith attempt to not step on already-
|
|
|
|
// installed signal handlers: it falls short by not maintaining the full
|
|
|
|
// sigaction structure. It loses information for SA_SIGINFO handlers, etc.
|
|
|
|
//
|
|
|
|
// Our principal workaround involves interposing this function (thankfully
|
|
|
|
// exported by the library). Unfortunately this doesn't work in all
|
|
|
|
// environments so we retain a full list of signal numbers the libmagick
|
|
|
|
// interferes with.
|
|
|
|
|
2019-08-25 05:26:05 +02:00
|
|
|
namespace ircd::magick
|
|
|
|
{
|
|
|
|
extern const int sig_overrides[];
|
|
|
|
extern const size_t sig_overrides_num;
|
2019-08-25 22:54:05 +02:00
|
|
|
static void sig_pre(), sig_post();
|
2019-08-25 05:26:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 02:55:01 +02:00
|
|
|
/// List of signals from libmagick
|
2019-08-25 05:26:05 +02:00
|
|
|
decltype(ircd::magick::sig_overrides)
|
|
|
|
ircd::magick::sig_overrides
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
2019-09-25 02:55:01 +02:00
|
|
|
SIGABRT, SIGBUS, SIGCHLD,
|
|
|
|
SIGFPE, SIGHUP, SIGINT,
|
|
|
|
SIGQUIT, SIGTERM, SIGSEGV,
|
|
|
|
SIGXCPU, SIGXFSZ,
|
2019-08-25 05:26:05 +02:00
|
|
|
#endif HAVE_SIGNAL_H
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::magick::sig_overrides_num)
|
|
|
|
ircd::magick::sig_overrides_num
|
|
|
|
{
|
|
|
|
sizeof(sig_overrides) / sizeof(int)
|
|
|
|
};
|
|
|
|
|
2019-08-25 22:54:05 +02:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
static struct sigaction
|
|
|
|
ircd_magick_sig_vector[ircd::magick::sig_overrides_num];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::sig_pre()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
for(size_t i(0); i < sig_overrides_num; ++i)
|
|
|
|
syscall(::sigaction, sig_overrides[i], nullptr, ircd_magick_sig_vector + i);
|
|
|
|
#endif HAVE_SIGNAL_H
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::sig_post()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
for(size_t i(0); i < sig_overrides_num; ++i)
|
|
|
|
syscall(::sigaction, sig_overrides[i], ircd_magick_sig_vector + i, nullptr);
|
|
|
|
#endif HAVE_SIGNAL_H
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
InitializeMagickSignalHandlers(void)
|
|
|
|
{
|
|
|
|
ircd::log::debug
|
|
|
|
{
|
|
|
|
ircd::magick::log, "Bypassed InitializeMagickSignalHandlers()",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-29 04:18:11 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2020-08-24 10:35:15 +02:00
|
|
|
ircd::magick::init::init()
|
2019-05-29 04:18:11 +02:00
|
|
|
{
|
2019-06-01 01:06:55 +02:00
|
|
|
log::info
|
2019-05-29 04:18:11 +02:00
|
|
|
{
|
2019-06-01 01:06:55 +02:00
|
|
|
log, "Initializing Magick Library version API:%lu [%s] ABI:%lu [%s]",
|
|
|
|
long(version_api),
|
|
|
|
string_view{version_api},
|
|
|
|
long(version_abi),
|
|
|
|
string_view{version_abi},
|
2019-05-29 04:18:11 +02:00
|
|
|
};
|
|
|
|
|
2019-06-01 01:06:55 +02:00
|
|
|
if(long(version_api) != long(version_abi))
|
2019-05-29 04:18:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
2019-05-29 13:38:33 +02:00
|
|
|
log, "Magick Library version mismatch headers:%lu library:%lu",
|
2019-06-01 01:06:55 +02:00
|
|
|
long(version_api),
|
|
|
|
long(version_abi),
|
2019-05-29 04:18:11 +02:00
|
|
|
};
|
|
|
|
|
2019-08-25 22:54:05 +02:00
|
|
|
sig_pre();
|
2019-07-31 02:23:20 +02:00
|
|
|
InitializeMagick(nullptr);
|
2019-07-26 22:37:40 +02:00
|
|
|
MagickAllocFunctions(handle_free, handle_malloc, handle_realloc);
|
|
|
|
SetFatalErrorHandler(handle_fatal);
|
|
|
|
SetErrorHandler(handle_error);
|
|
|
|
SetWarningHandler(handle_warning);
|
|
|
|
SetLogMethod(handle_log);
|
|
|
|
//SetLogEventMask("all"); // Pollutes stderr :/ can't fix
|
|
|
|
SetMonitorHandler(handle_progress);
|
|
|
|
SetMagickResourceLimit(ThreadsResource, 1UL);
|
2019-08-25 22:54:05 +02:00
|
|
|
sig_post();
|
2019-08-25 05:26:05 +02:00
|
|
|
|
2019-07-17 01:25:08 +02:00
|
|
|
call_ready = true;
|
|
|
|
call_dock.notify_all();
|
|
|
|
|
2019-05-29 13:40:58 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "resource settings: pixel max:%lu:%lu height:%lu:%lu width:%lu:%lu; threads:%lu:%lu",
|
2019-07-26 22:37:40 +02:00
|
|
|
GetMagickResource(PixelsResource),
|
|
|
|
GetMagickResourceLimit(PixelsResource),
|
|
|
|
GetMagickResource(HeightResource),
|
|
|
|
GetMagickResourceLimit(HeightResource),
|
|
|
|
GetMagickResource(WidthResource),
|
|
|
|
GetMagickResourceLimit(WidthResource),
|
|
|
|
GetMagickResource(ThreadsResource),
|
|
|
|
GetMagickResourceLimit(ThreadsResource),
|
2019-05-29 13:40:58 +02:00
|
|
|
};
|
2019-05-29 04:18:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::cold]]
|
2020-08-24 10:35:15 +02:00
|
|
|
ircd::magick::init::~init()
|
|
|
|
noexcept
|
2019-05-29 04:18:11 +02:00
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
2022-06-16 23:12:11 +02:00
|
|
|
log, "Shutting down Magick Library..."
|
2019-05-29 04:18:11 +02:00
|
|
|
};
|
|
|
|
|
2019-07-17 01:25:08 +02:00
|
|
|
call_ready = false;
|
2022-06-24 04:18:05 +02:00
|
|
|
call_dock.wait([]() noexcept
|
2019-07-17 01:25:08 +02:00
|
|
|
{
|
|
|
|
return !call_mutex.locked();
|
|
|
|
});
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
DestroyMagick();
|
2019-05-29 04:18:11 +02:00
|
|
|
}
|
2019-05-29 07:08:46 +02:00
|
|
|
|
2019-05-30 15:07:39 +02:00
|
|
|
//
|
|
|
|
// thumbcrop
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::thumbcrop::thumbcrop(const const_buffer &in,
|
|
|
|
const dimensions &req,
|
|
|
|
const result_closure &out)
|
|
|
|
{
|
|
|
|
crop::offset offset;
|
|
|
|
const auto scaler{[&req, &offset]
|
|
|
|
(const auto &image)
|
|
|
|
{
|
|
|
|
const auto &img_p
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
std::get<const Image *>(image)
|
2019-05-30 15:07:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &img_x(img_p->columns);
|
|
|
|
const auto &img_y(img_p->rows);
|
2020-11-19 23:13:20 +01:00
|
|
|
const auto &[req_x_, req_y_] {req};
|
|
|
|
const auto &req_x{std::min(req_x_, img_x)};
|
|
|
|
const auto &req_y{std::min(req_y_, img_y)};
|
2019-05-30 15:07:39 +02:00
|
|
|
const bool aspect
|
|
|
|
{
|
|
|
|
req_x * img_y < req_y * img_x
|
|
|
|
};
|
|
|
|
|
|
|
|
const dimensions scaled
|
|
|
|
{
|
|
|
|
aspect? req_y * img_x / img_y : req_x,
|
|
|
|
aspect? req_y : req_x * img_y / img_x,
|
|
|
|
};
|
|
|
|
|
|
|
|
offset =
|
|
|
|
{
|
|
|
|
aspect? (scaled.first - req_x) / 2.0 : 0,
|
|
|
|
aspect? 0 : (scaled.second - req_y) / 2.0,
|
|
|
|
};
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
return callex<Image *>(ThumbnailImage, img_p, scaled.first, scaled.second);
|
2019-05-30 15:07:39 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
const auto cropper{[&req, &out, &offset]
|
|
|
|
(const const_buffer &in)
|
|
|
|
{
|
|
|
|
crop
|
|
|
|
{
|
|
|
|
in, req, offset, out
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
transform
|
|
|
|
{
|
|
|
|
in, cropper, scaler
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:25:03 +02:00
|
|
|
//
|
|
|
|
// thumbnail
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::thumbnail::thumbnail(const const_buffer &in,
|
2020-11-19 23:13:20 +01:00
|
|
|
const dimensions &req,
|
2019-05-30 11:44:12 +02:00
|
|
|
const result_closure &out)
|
2019-05-29 08:25:03 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-30 11:11:31 +02:00
|
|
|
{
|
2020-11-19 23:13:20 +01:00
|
|
|
in, out, [&req](const auto &image)
|
2019-05-30 11:44:12 +02:00
|
|
|
{
|
2020-11-19 23:07:49 +01:00
|
|
|
const auto &img_p
|
|
|
|
{
|
|
|
|
std::get<const Image *>(image)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &img_x(img_p->columns);
|
|
|
|
const auto &img_y(img_p->rows);
|
2020-11-19 23:13:20 +01:00
|
|
|
const auto &[req_x_, req_y_] {req};
|
|
|
|
const auto &req_x{std::min(req_x_, img_x)};
|
|
|
|
const auto &req_y{std::min(req_y_, img_y)};
|
2020-11-19 23:07:49 +01:00
|
|
|
const bool aspect
|
|
|
|
{
|
|
|
|
req_x * img_y < req_y * img_x
|
|
|
|
};
|
|
|
|
|
|
|
|
const dimensions scaled
|
|
|
|
{
|
|
|
|
aspect? req_y * img_x / img_y : req_x,
|
|
|
|
aspect? req_y : req_x * img_y / img_x,
|
|
|
|
};
|
|
|
|
|
|
|
|
return callex<Image *>(ThumbnailImage, img_p, scaled.first, scaled.second);
|
2019-05-30 11:44:12 +02:00
|
|
|
}
|
2019-05-30 11:11:31 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// scale
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::scale::scale(const const_buffer &in,
|
|
|
|
const dimensions &dim,
|
2019-05-30 11:44:12 +02:00
|
|
|
const result_closure &out)
|
2019-05-30 11:11:31 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-30 11:11:31 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&dim](const auto &image)
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
return callex<Image *>(ScaleImage, std::get<const Image *>(image), dim.first, dim.second);
|
2019-05-30 11:44:12 +02:00
|
|
|
}
|
2019-05-30 10:29:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// shave
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::shave::shave(const const_buffer &in,
|
|
|
|
const dimensions &dim,
|
|
|
|
const offset &off,
|
2019-05-30 11:44:12 +02:00
|
|
|
const result_closure &out)
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
const RectangleInfo geometry
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
dim.first, // width
|
|
|
|
dim.second, // height
|
|
|
|
off.first, // x
|
|
|
|
off.second, // y
|
2019-05-30 10:29:07 +02:00
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&geometry](const auto &image)
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
return callex<Image *>(ShaveImage, std::get<const Image *>(image), &geometry);
|
2019-05-30 11:44:12 +02:00
|
|
|
}
|
2019-05-30 10:29:07 +02:00
|
|
|
};
|
2019-05-30 11:44:12 +02:00
|
|
|
}
|
2019-05-30 10:29:07 +02:00
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
//
|
|
|
|
// crop
|
|
|
|
//
|
2019-05-30 10:29:07 +02:00
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
ircd::magick::crop::crop(const const_buffer &in,
|
|
|
|
const dimensions &dim,
|
|
|
|
const offset &off,
|
|
|
|
const result_closure &out)
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
const RectangleInfo geometry
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
|
|
|
dim.first, // width
|
|
|
|
dim.second, // height
|
|
|
|
off.first, // x
|
|
|
|
off.second, // y
|
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-29 08:25:03 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&geometry](const auto &image)
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
return callex<Image *>(CropImage, std::get<const Image *>(image), &geometry);
|
2019-05-30 11:44:12 +02:00
|
|
|
}
|
2019-05-29 08:25:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-30 09:51:50 +02:00
|
|
|
//
|
2019-05-30 11:44:12 +02:00
|
|
|
// transform (internal)
|
2019-05-30 09:51:50 +02:00
|
|
|
//
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
ircd::magick::transform::transform(const const_buffer &input,
|
|
|
|
const output &output,
|
|
|
|
const transformer &transformer)
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
const custom_ptr<ImageInfo> input_info
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
CloneImageInfo(nullptr),
|
|
|
|
DestroyImageInfo
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
const custom_ptr<ImageInfo> output_info
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
CloneImageInfo(nullptr),
|
|
|
|
DestroyImageInfo
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
const custom_ptr<Image> input_image
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
callex<Image *>(BlobToImage, input_info.get(), data(input), size(input)),
|
|
|
|
DestroyImage // pollock
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
const custom_ptr<Image> output_image
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
transformer({*input_info, input_image.get()}),
|
2019-07-26 22:37:40 +02:00
|
|
|
DestroyImage
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t output_size(0);
|
|
|
|
const auto output_data
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
callex<void *>(ImageToBlob, output_info.get(), output_image.get(), &output_size)
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer result
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(output_data), output_size
|
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
output(result);
|
2019-05-30 09:51:50 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 14:14:34 +02:00
|
|
|
//
|
|
|
|
// display (internal)
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::display::display(const const_buffer &input)
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
const custom_ptr<ImageInfo> input_info
|
2019-05-30 14:14:34 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
CloneImageInfo(nullptr),
|
|
|
|
DestroyImageInfo
|
2019-05-30 14:14:34 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
const custom_ptr<Image> input_image
|
2019-05-30 14:14:34 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
callex<Image *>(BlobToImage, input_info.get(), data(input), size(input)),
|
|
|
|
DestroyImage // pollock
|
2019-05-30 14:14:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
display
|
|
|
|
{
|
|
|
|
*input_info, *input_image
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
ircd::magick::display::display(const ImageInfo &info,
|
|
|
|
Image &image)
|
2019-05-30 14:14:34 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
callpf(DisplayImages, &info, &image);
|
2019-05-30 14:14:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
//
|
2019-05-30 11:44:12 +02:00
|
|
|
// util (internal)
|
2019-05-29 07:08:46 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
template<class return_t,
|
|
|
|
class function,
|
|
|
|
class... args>
|
|
|
|
return_t
|
|
|
|
ircd::magick::callex(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2019-07-17 01:25:08 +02:00
|
|
|
if(unlikely(!call_ready))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Graphics library not ready."
|
|
|
|
};
|
|
|
|
|
2019-05-30 03:08:19 +02:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
call_mutex
|
|
|
|
};
|
|
|
|
|
2019-07-26 22:37:40 +02:00
|
|
|
ExceptionInfo ei;
|
2019-05-29 07:08:46 +02:00
|
|
|
GetExceptionInfo(&ei); // initializer
|
|
|
|
const unwind destroy{[&ei]
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
DestroyExceptionInfo(&ei);
|
2019-05-29 07:08:46 +02:00
|
|
|
}};
|
|
|
|
|
2019-07-17 01:25:08 +02:00
|
|
|
assert(call_ready);
|
2019-05-29 07:08:46 +02:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
f(std::forward<args>(a)..., &ei)
|
|
|
|
};
|
|
|
|
|
2019-05-30 08:31:55 +02:00
|
|
|
const auto their_handler
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
SetErrorHandler(handle_exception)
|
2019-05-30 08:31:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const unwind reset{[&their_handler]
|
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
SetErrorHandler(their_handler);
|
2019-05-30 08:31:55 +02:00
|
|
|
}};
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
// exception comes out of here; if this is not safe we'll have to
|
|
|
|
// convey with a global or inspect ExceptionInfo manually.
|
2019-07-26 22:37:40 +02:00
|
|
|
CatchException(&ei);
|
2019-05-29 07:08:46 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class function,
|
|
|
|
class... args>
|
|
|
|
void
|
|
|
|
ircd::magick::callpf(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2021-09-05 22:14:12 +02:00
|
|
|
if(unlikely(!call<MagickPassFail>(f, std::forward<args>(a)...)))
|
2019-05-29 07:08:46 +02:00
|
|
|
throw error{};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class return_t,
|
|
|
|
class function,
|
|
|
|
class... args>
|
|
|
|
return_t
|
|
|
|
ircd::magick::call(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2019-07-17 01:25:08 +02:00
|
|
|
if(unlikely(!call_ready))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Graphics library not ready."
|
|
|
|
};
|
|
|
|
|
2019-05-30 03:08:19 +02:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
call_mutex
|
|
|
|
};
|
|
|
|
|
2019-07-17 01:25:08 +02:00
|
|
|
assert(call_ready);
|
2019-05-29 07:08:46 +02:00
|
|
|
return f(std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
//
|
|
|
|
// ircd::magick::job
|
|
|
|
//
|
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
namespace ircd::magick
|
|
|
|
{
|
2019-07-06 14:44:10 +02:00
|
|
|
static string_view loghead(const job &);
|
2019-07-06 00:32:10 +02:00
|
|
|
static void job_init(const string_view &, const int64_t &, const uint64_t &, const uint64_t &);
|
|
|
|
static void finished(job &);
|
|
|
|
static bool check_yield(job &);
|
|
|
|
static void check_cycles(job &);
|
2019-05-30 07:14:50 +02:00
|
|
|
}
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
struct ircd::magick::job::state
|
|
|
|
{
|
|
|
|
uint64_t cycles {0};
|
|
|
|
uint64_t yield {0};
|
|
|
|
char description[1024];
|
|
|
|
}
|
|
|
|
thread_local ircd::magick::job::state;
|
|
|
|
|
|
|
|
decltype(ircd::magick::job::cur) thread_local
|
|
|
|
ircd::magick::job::cur;
|
|
|
|
|
|
|
|
decltype(ircd::magick::job::tot) thread_local
|
|
|
|
ircd::magick::job::tot;
|
|
|
|
|
2019-05-29 13:55:09 +02:00
|
|
|
uint
|
2019-07-06 00:32:10 +02:00
|
|
|
ircd::magick::handle_progress(const char *const text,
|
|
|
|
const int64_t tick,
|
|
|
|
const uint64_t ticks,
|
2019-05-30 03:08:19 +02:00
|
|
|
ExceptionInfo *ei)
|
2019-05-30 08:31:55 +02:00
|
|
|
noexcept try
|
2019-05-29 13:55:09 +02:00
|
|
|
{
|
2019-06-30 02:30:59 +02:00
|
|
|
// Sample the current reference cycle count first and once. This is an
|
|
|
|
// accumulated cycle count for only this ircd::ctx and the current slice,
|
|
|
|
// (all other cycles are not accumulated here) which is non-zero by now
|
|
|
|
// and monotonically increases across jobs as well.
|
2019-07-06 00:32:10 +02:00
|
|
|
const auto cycles_sample
|
2019-06-30 02:30:59 +02:00
|
|
|
{
|
2019-07-24 01:21:22 +02:00
|
|
|
ctx::this_ctx::cycles()
|
2019-06-30 02:30:59 +02:00
|
|
|
};
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
// Detect if this is a new job. Tick is usually zero for a new job, but for
|
|
|
|
// large jobs it may start after 0. Tick always appears monotonic for a job.
|
|
|
|
// The ticks appears constant for a job, though could be the same for different
|
2019-07-05 22:48:08 +02:00
|
|
|
// jobs. We don't know of any succinct way to test for a new job, so we use all
|
|
|
|
// of the above information.
|
|
|
|
const bool new_job
|
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
tick == 0
|
|
|
|
|| tick < job::cur.tick
|
|
|
|
|| ticks != job::cur.ticks
|
2019-07-05 22:48:08 +02:00
|
|
|
};
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
// Assert general assumptions about invocations of this callback.
|
|
|
|
assert(new_job || tick >= job::cur.tick);
|
|
|
|
assert(new_job || ticks == job::cur.ticks);
|
|
|
|
|
|
|
|
// Branch after detecting this callback is unrelated to the last job.
|
2019-07-05 22:48:08 +02:00
|
|
|
if(new_job)
|
2019-05-30 07:14:50 +02:00
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
finished(job::cur);
|
|
|
|
job_init(text, tick, ticks, cycles_sample);
|
2019-05-30 07:14:50 +02:00
|
|
|
}
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
// Unconditional bookkeeping updates for this invocation. These statements
|
|
|
|
// behave properly regardless of whether this is the same or a new job.
|
|
|
|
assert(cycles_sample >= job::state.cycles);
|
|
|
|
job::cur.cycles += cycles_sample - job::state.cycles;
|
|
|
|
job::state.cycles = cycles_sample;
|
|
|
|
job::cur.tick = tick;
|
2019-06-30 02:30:59 +02:00
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
// This debug message is very noisy, even for debug mode. Developer can
|
|
|
|
// enable it at their discretion.
|
2019-05-30 03:08:19 +02:00
|
|
|
#ifdef IRCD_MAGICK_DEBUG_PROGRESS
|
2019-05-29 13:55:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-06-30 02:30:59 +02:00
|
|
|
log, "job:%lu progress %2.2lf%% (%ld/%ld) cycles:%lu :%s",
|
2019-07-06 00:32:10 +02:00
|
|
|
job::cur.id,
|
|
|
|
(job::cur.tick / double(job::cur.ticks) * 100.0),
|
|
|
|
job::cur.tick,
|
|
|
|
job::cur.ticks,
|
|
|
|
job::cur.cycles,
|
|
|
|
job::cur.text,
|
2019-05-29 13:55:09 +02:00
|
|
|
};
|
2019-05-30 03:08:19 +02:00
|
|
|
#endif
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
check_cycles(job::cur);
|
|
|
|
check_yield(job::cur);
|
2019-05-30 03:08:19 +02:00
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
return true;
|
2019-05-29 13:55:09 +02:00
|
|
|
}
|
2019-05-30 08:31:55 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
++job::cur.intrs;
|
|
|
|
job::cur.eptr = std::current_exception();
|
2019-07-26 22:37:40 +02:00
|
|
|
ThrowException(ei, MonitorError, "interrupted", e.what());
|
2019-05-30 08:31:55 +02:00
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const ctx::terminated &)
|
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
++job::cur.intrs;
|
|
|
|
job::cur.eptr = std::current_exception();
|
2019-07-26 22:37:40 +02:00
|
|
|
ThrowException(ei, MonitorError, "terminated", nullptr);
|
2019-05-30 08:31:55 +02:00
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
++job::cur.errors;
|
|
|
|
job::cur.eptr = std::current_exception();
|
2019-07-26 22:37:40 +02:00
|
|
|
ThrowLoggedException(ei, MonitorError, "error", e.what(), __FILE__, __FUNCTION__, __LINE__);
|
2019-05-30 08:31:55 +02:00
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-07-06 00:32:10 +02:00
|
|
|
++job::cur.errors;
|
|
|
|
job::cur.eptr = std::current_exception();
|
2019-07-26 22:37:40 +02:00
|
|
|
ThrowLoggedException(ei, MonitorFatalError, "unknown", nullptr, __FILE__, __FUNCTION__, __LINE__);
|
2019-05-30 08:31:55 +02:00
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 13:55:09 +02:00
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
void
|
|
|
|
ircd::magick::check_cycles(job &job)
|
|
|
|
{
|
|
|
|
const uint64_t &limit_cycles
|
|
|
|
{
|
|
|
|
magick::limit_cycles
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if job exceeded its reference cycle limit if enabled.
|
|
|
|
if(unlikely(limit_cycles && job.cycles > limit_cycles))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"job:%lu CPU cycles:%lu exceeded server limit:%lu (progress %2.2lf%% (%ld/%ld))",
|
|
|
|
job.id,
|
|
|
|
job.cycles,
|
|
|
|
limit_cycles,
|
|
|
|
(job.tick / double(job.ticks) * 100.0),
|
|
|
|
job.tick,
|
|
|
|
job.ticks,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::magick::check_yield(job &job)
|
|
|
|
{
|
|
|
|
const uint64_t &yield_threshold
|
|
|
|
{
|
|
|
|
magick::yield_threshold
|
|
|
|
};
|
|
|
|
|
|
|
|
// This job is too small to conduct any yields.
|
|
|
|
if(likely(job.ticks < yield_threshold))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const uint64_t &yield_interval
|
|
|
|
{
|
|
|
|
magick::yield_interval
|
|
|
|
};
|
|
|
|
|
|
|
|
// Haven't reached the yield interval yet.
|
|
|
|
if(likely(job.tick - job::state.yield <= yield_interval))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
job::state.yield = job.tick;
|
|
|
|
ctx::yield();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::finished(job &job)
|
|
|
|
{
|
|
|
|
// Update total state from last job
|
|
|
|
assert(job.id == job::tot.id + 1 || (job.id == job::tot.id && !job.id));
|
|
|
|
job::tot.id = job.id;
|
|
|
|
job::tot.tick += job.tick;
|
|
|
|
job::tot.ticks += job.ticks;
|
|
|
|
job::tot.cycles += job.cycles;
|
|
|
|
job::tot.yields += job.yields;
|
|
|
|
job::tot.intrs += job.intrs;
|
|
|
|
job::tot.errors += job.errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::job_init(const string_view &text,
|
|
|
|
const int64_t &tick,
|
|
|
|
const uint64_t &ticks,
|
|
|
|
const uint64_t &cycles_sample)
|
|
|
|
{
|
|
|
|
// Reset the current job structure
|
|
|
|
job::cur =
|
|
|
|
{
|
|
|
|
job::tot.id + 1, // id
|
|
|
|
tick, // tick
|
|
|
|
ticks, // ticks
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update internal state
|
|
|
|
job::state.cycles = cycles_sample;
|
|
|
|
|
|
|
|
// The description text may have this annoying empty "[]" on this
|
|
|
|
// message so we'll strip that here.
|
|
|
|
job::cur.description = strlcpy
|
|
|
|
{
|
|
|
|
job::state.description, lstrip(text, "[] ")
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "job:%lu started; ticks:%lu :%s",
|
|
|
|
job::cur.id,
|
|
|
|
job::cur.ticks,
|
|
|
|
job::cur.description,
|
|
|
|
};
|
|
|
|
|
|
|
|
// This job is too large based on the ticks measurement. This is an ad hoc
|
|
|
|
// measurement of the job size created internally by ImageMagick.
|
|
|
|
if(job::cur.ticks > uint64_t(limit_ticks))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"job:%lu computation ticks:%lu exceeds server limit:%lu :%s",
|
|
|
|
job::cur.id,
|
|
|
|
job::cur.ticks,
|
|
|
|
uint64_t(limit_ticks),
|
|
|
|
job::cur.description,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-06 14:44:10 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::magick::loghead(const job &job)
|
|
|
|
{
|
|
|
|
thread_local char buf[256];
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "job:%lu %ld/%lu [%s]",
|
|
|
|
job.id,
|
|
|
|
job.tick,
|
|
|
|
job.ticks,
|
|
|
|
job.description,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:32:10 +02:00
|
|
|
//
|
|
|
|
// (Internal) patch panels
|
|
|
|
//
|
|
|
|
|
2019-05-30 07:27:55 +02:00
|
|
|
void
|
|
|
|
ircd::magick::handle_free(void *const ptr)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ircd::magick::handle_malloc(size_t size)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return std::malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ircd::magick::handle_realloc(void *const ptr,
|
|
|
|
size_t size)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return std::realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
void
|
2019-07-26 22:37:40 +02:00
|
|
|
ircd::magick::handle_log(const ExceptionType type,
|
2019-05-29 07:08:46 +02:00
|
|
|
const char *const message)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
2019-07-06 14:44:10 +02:00
|
|
|
log, "%s (%d) %s :%s",
|
|
|
|
loghead(job::cur),
|
2019-05-30 08:31:55 +02:00
|
|
|
int(type),
|
2019-07-26 22:37:40 +02:00
|
|
|
GetLocaleExceptionMessage(type, ""),
|
2019-05-30 08:31:55 +02:00
|
|
|
message,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-26 22:37:40 +02:00
|
|
|
ircd::magick::handle_warning(const ExceptionType type,
|
2019-05-29 07:08:46 +02:00
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2019-07-06 14:44:10 +02:00
|
|
|
log, "%s (#%d) %s :%s :%s",
|
|
|
|
loghead(job::cur),
|
2019-05-30 08:31:55 +02:00
|
|
|
int(type),
|
2019-07-26 22:37:40 +02:00
|
|
|
GetLocaleExceptionMessage(type, ""),
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-26 22:37:40 +02:00
|
|
|
ircd::magick::handle_error(const ExceptionType type,
|
2019-05-29 07:08:46 +02:00
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-07-06 14:44:10 +02:00
|
|
|
log, "%s (#%d) %s :%s :%s",
|
|
|
|
loghead(job::cur),
|
2019-05-30 08:31:55 +02:00
|
|
|
int(type),
|
2019-07-26 22:37:40 +02:00
|
|
|
GetLocaleExceptionMessage(type, ""),
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-02 06:52:46 +01:00
|
|
|
void
|
2019-07-26 22:37:40 +02:00
|
|
|
ircd::magick::handle_fatal(const ExceptionType type,
|
2019-05-29 07:08:46 +02:00
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-07-06 14:44:10 +02:00
|
|
|
log, "%s (#%d) %s :%s :%s",
|
|
|
|
loghead(job::cur),
|
2019-05-30 08:31:55 +02:00
|
|
|
int(type),
|
2019-07-26 22:37:40 +02:00
|
|
|
GetLocaleExceptionMessage(type, ""),
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ircd::terminate();
|
2020-03-27 17:34:43 +01:00
|
|
|
__builtin_unreachable();
|
2019-05-29 07:08:46 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 06:52:46 +01:00
|
|
|
void
|
2019-07-26 22:37:40 +02:00
|
|
|
ircd::magick::handle_exception(const ExceptionType type,
|
2019-05-29 07:08:46 +02:00
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
const auto &message
|
2019-05-29 07:08:46 +02:00
|
|
|
{
|
2019-07-26 22:37:40 +02:00
|
|
|
GetLocaleExceptionMessage(type, "")?: "???"
|
2019-05-30 08:31:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char buf[exception::BUFSIZE];
|
|
|
|
const string_view what{fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "(#%d) %s :%s :%s",
|
|
|
|
int(type),
|
|
|
|
message,
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
|
|
|
}};
|
|
|
|
|
|
|
|
log::derror
|
|
|
|
{
|
2019-07-06 14:44:10 +02:00
|
|
|
log, "%s %s",
|
|
|
|
loghead(job::cur),
|
|
|
|
what,
|
2019-05-30 08:31:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(reason == "terminated"_sv)
|
|
|
|
throw ctx::terminated{};
|
|
|
|
|
|
|
|
if(reason == "interrupted"_sv)
|
|
|
|
throw ctx::interrupted
|
|
|
|
{
|
|
|
|
"%s", what
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"%s", what
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|