From 2f80b3573b90791c70354279e8b5553092bd5588 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 14 Aug 2021 21:28:06 +0200 Subject: [PATCH] statustime: Add signal handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because why not? 😊 --- statustime.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/statustime.cpp b/statustime.cpp index c2e7e89..0153273 100755 --- a/statustime.cpp +++ b/statustime.cpp @@ -4,11 +4,30 @@ #include #include +#include #include #include #include #include +void signal_handler(int signum) +{ + switch (signum) + { + case SIGTERM: + case SIGINT: + { + std::cout << "\nTime script terminated.\n"; + std::exit(signum); // NOLINT(concurrency-mt-unsafe) + break; + } + default: + { + break; + } + } +} + int main() { using fmt::format; @@ -20,6 +39,9 @@ int main() // Set explicitly, because LC_TIME is en_DK.UTF-8. std::locale::global(std::locale("de_DE.UTF-8")); + std::signal(SIGINT, signal_handler); + std::signal(SIGTERM, signal_handler); + try { while (true)