Factor out safe_main function

This commit is contained in:
Marcus Holland-Moritz 2023-07-12 14:04:19 +02:00
parent c3d79a41c4
commit f75974eb32
11 changed files with 117 additions and 55 deletions

View File

@ -426,6 +426,7 @@ list(
src/dwarfs/os_access_generic.cpp
src/dwarfs/performance_monitor.cpp
src/dwarfs/progress.cpp
src/dwarfs/safe_main.cpp
src/dwarfs/scanner.cpp
src/dwarfs/similarity.cpp
src/dwarfs/string_table.cpp

View File

@ -22,7 +22,6 @@
#pragma once
#include <exception>
#include <functional>
#include <string>
#include <system_error>
@ -98,6 +97,4 @@ void dump_exceptions();
[[noreturn]] void assertion_failed(char const* expr, std::string const& msg,
char const* file, int line);
int safe_main(std::function<int(void)> fn);
} // namespace dwarfs

View File

@ -0,0 +1,30 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs 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 3 of the License, or
* (at your option) any later version.
*
* dwarfs 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 dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <functional>
namespace dwarfs {
int safe_main(std::function<int(void)> fn);
} // namespace dwarfs

View File

@ -19,7 +19,7 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs_tool_main.h"
int SYS_MAIN(int argc, dwarfs::sys_char** argv) {

View File

@ -25,7 +25,6 @@
#include <iostream>
#include <folly/String.h>
#include <folly/experimental/symbolizer/SignalHandler.h>
#ifdef DWARFS_USE_EXCEPTION_TRACER
#include <folly/experimental/exception_tracer/ExceptionTracer.h>
@ -79,49 +78,4 @@ void assertion_failed(char const* expr, std::string const& msg,
::abort();
}
int safe_main(std::function<int(void)> fn) {
try {
#ifndef _WIN32
folly::symbolizer::installFatalSignalHandler();
#endif
#ifdef _WIN32
char const* locale = "en_US.utf8";
#else
char const* locale = "";
#endif
try {
std::locale::global(std::locale(locale));
} catch (std::exception const& e) {
std::cerr << "warning: failed to set user default locale\n";
try {
std::locale::global(std::locale::classic());
} catch (std::exception const& e) {
std::cerr << "warning: also failed to set classic locale\n";
}
}
if (!std::setlocale(LC_ALL, locale)) {
std::cerr << "warning: setlocale(LC_ALL, \"\") failed\n";
}
setup_terminal();
return fn();
} catch (system_error const& e) {
std::cerr << "ERROR: " << folly::exceptionStr(e) << " [" << e.file() << ":"
<< e.line() << "]\n";
dump_exceptions();
} catch (error const& e) {
std::cerr << "ERROR: " << folly::exceptionStr(e) << " [" << e.file() << ":"
<< e.line() << "]\n";
dump_exceptions();
} catch (std::exception const& e) {
std::cerr << "ERROR: " << folly::exceptionStr(e) << "\n";
dump_exceptions();
} catch (...) {
dump_exceptions();
}
return 1;
}
} // namespace dwarfs

80
src/dwarfs/safe_main.cpp Normal file
View File

@ -0,0 +1,80 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs 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 3 of the License, or
* (at your option) any later version.
*
* dwarfs 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 dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include <clocale>
#include <cstdlib>
#include <iostream>
#include <folly/String.h>
#include <folly/experimental/symbolizer/SignalHandler.h>
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs/terminal.h"
namespace dwarfs {
int safe_main(std::function<int(void)> fn) {
try {
#ifndef _WIN32
folly::symbolizer::installFatalSignalHandler();
#endif
#ifdef _WIN32
char const* locale = "en_US.utf8";
#else
char const* locale = "";
#endif
try {
std::locale::global(std::locale(locale));
} catch (std::exception const& e) {
std::cerr << "warning: failed to set user default locale\n";
try {
std::locale::global(std::locale::classic());
} catch (std::exception const& e) {
std::cerr << "warning: also failed to set classic locale\n";
}
}
if (!std::setlocale(LC_ALL, locale)) {
std::cerr << "warning: setlocale(LC_ALL, \"\") failed\n";
}
setup_terminal();
return fn();
} catch (system_error const& e) {
std::cerr << "ERROR: " << folly::exceptionStr(e) << " [" << e.file() << ":"
<< e.line() << "]\n";
dump_exceptions();
} catch (error const& e) {
std::cerr << "ERROR: " << folly::exceptionStr(e) << " [" << e.file() << ":"
<< e.line() << "]\n";
dump_exceptions();
} catch (std::exception const& e) {
std::cerr << "ERROR: " << folly::exceptionStr(e) << "\n";
dump_exceptions();
} catch (...) {
dump_exceptions();
}
return 1;
}
} // namespace dwarfs

View File

@ -19,7 +19,7 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs_tool_main.h"
int SYS_MAIN(int argc, dwarfs::sys_char** argv) {

View File

@ -19,7 +19,7 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs_tool_main.h"
int SYS_MAIN(int argc, dwarfs::sys_char** argv) {

View File

@ -19,7 +19,7 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs_tool_main.h"
int SYS_MAIN(int argc, dwarfs::sys_char** argv) {

View File

@ -19,7 +19,7 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs_tool_main.h"
int SYS_MAIN(int argc, dwarfs::sys_char** argv) {

View File

@ -35,7 +35,7 @@
#include <delayimp.h>
#endif
#include "dwarfs/error.h"
#include "dwarfs/safe_main.h"
#include "dwarfs/tool.h"
#include "dwarfs/util.h"
#include "dwarfs_tool_main.h"