Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add main function wrapper to catch exceptions and avoid annoying error popup on windows #768

Merged
merged 11 commits into from
May 16, 2020
1 change: 1 addition & 0 deletions src/aliceVision/system/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Headers
set(system_files_headers
cpu.hpp
main.hpp
MemoryInfo.hpp
system.hpp
Timer.hpp
Expand Down
48 changes: 48 additions & 0 deletions src/aliceVision/system/main.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This file is part of the AliceVision project.
// Copyright (c) 2020 AliceVision contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

/**
* @file \c main() function wrapper
* Provides an implementation of \c main() that automatically catches and logs
* otherwise unhandled exceptions.
*
* To use this wrapper you need to change your source file containing \c main() as such:
* 1. Include this header
* 2. Rename \c main() to \c aliceVision_main()
*/

#include "Logger.hpp"

#include <stdexcept>

/**
* @brief Name of the application entry function, replacing \c main().
*/
int aliceVision_main(int argc, char* argv[]);

/* Implementation of the unique main() entry point.
* This method will call aliceVision_main() and, in case of any exception not
* handled there, catch those and log the error message.
* On Windows, unhandled exceptions abort the program with the cause hard to
* find out, something this main() function avoids. */
int main(int argc, char* argv[])
{
try
{
return aliceVision_main(argc, argv);
}
catch(const std::exception& e)
{
ALICEVISION_LOG_FATAL(e.what());
}
catch(...)
{
ALICEVISION_LOG_FATAL("Unknown exception");
}
return EXIT_FAILURE;
}
5 changes: 4 additions & 1 deletion src/software/convert/main_convertFloatDescriptorToUchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <aliceVision/feature/Descriptor.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/progress.hpp>
#include <boost/filesystem.hpp>
Expand All @@ -25,7 +26,7 @@ using namespace aliceVision;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main( int argc, char** argv )
int aliceVision_main( int argc, char** argv )
{
std::string verboseLevel = system::EVerboseLevel_enumToString(system::Logger::getDefaultVerboseLevel());
std::string outputFolder;
Expand Down Expand Up @@ -181,4 +182,6 @@ int main( int argc, char** argv )
}
}
ALICEVISION_LOG_INFO("Converted " << countDesc << " files .desc and copied " << countFeat << " files .feat");

return EXIT_SUCCESS;
}
3 changes: 2 additions & 1 deletion src/software/convert/main_convertLDRToHDR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aliceVision/image/io.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>

/*SFMData*/
#include <aliceVision/sfmData/SfMData.hpp>
Expand Down Expand Up @@ -100,7 +101,7 @@ inline std::istream& operator>>(std::istream& in, ECalibrationMethod& calibratio
}


int main(int argc, char * argv[])
int aliceVision_main(int argc, char * argv[])
{
std::string verboseLevel = system::EVerboseLevel_enumToString(system::Logger::getDefaultVerboseLevel());
std::string sfmInputDataFilename = "";
Expand Down
3 changes: 2 additions & 1 deletion src/software/convert/main_convertRAW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aliceVision/image/io.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand All @@ -25,7 +26,7 @@ using namespace aliceVision;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main(int argc, char** argv)
int aliceVision_main(int argc, char** argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/convert/main_convertSfMFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/config.hpp>

#include <boost/program_options.hpp>
Expand All @@ -31,7 +32,7 @@ namespace po = boost::program_options;
namespace fs = boost::filesystem;

// convert from a SfMData format to another
int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportAnimatedCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/system/Timer.hpp>
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/sfmDataIO/AlembicExporter.hpp>
Expand All @@ -30,7 +31,7 @@ using namespace aliceVision;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main(int argc, char** argv)
int aliceVision_main(int argc, char** argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportCameraFrustums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <aliceVision/system/Timer.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand All @@ -26,7 +27,7 @@ namespace po = boost::program_options;
namespace fs = boost::filesystem;

/// Export camera frustrums as a triangle PLY file
int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportColoredPointCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/config.hpp>

#include <boost/program_options.hpp>
Expand All @@ -27,7 +28,7 @@ using namespace aliceVision;
namespace po = boost::program_options;

// Convert from a SfMData format to another
int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportKeypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <aliceVision/sfm/pipeline/regionsIO.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/image/all.hpp>

#include <dependencies/vectorGraphics/svgDrawer.hpp>
Expand Down Expand Up @@ -41,7 +42,7 @@ using namespace svg;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main(int argc, char ** argv)
int aliceVision_main(int argc, char ** argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportMVE2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aliceVision/sfmData/SfMData.hpp>
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/image/all.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -240,7 +241,7 @@ bool exportToMVE2Format(
return bOk;
}

int main(int argc, char *argv[])
int aliceVision_main(int argc, char *argv[])
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportMVSTexturing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aliceVision/sfmData/SfMData.hpp>
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/image/all.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand All @@ -27,7 +28,7 @@ using namespace aliceVision::sfmData;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportMatches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <aliceVision/feature/svgVisualization.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>

#include <dependencies/vectorGraphics/svgDrawer.hpp>

Expand Down Expand Up @@ -71,7 +72,7 @@ void hslToRgb(float h, float s, float l,
}
}

int main(int argc, char ** argv)
int aliceVision_main(int argc, char ** argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportMatlab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/image/all.hpp>
#include <aliceVision/image/convertion.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -137,7 +138,7 @@ bool exportToMatlab(
return true;
}

int main(int argc, char *argv[])
int aliceVision_main(int argc, char *argv[])
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportMeshlab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/numeric/numeric.hpp>
#include <aliceVision/image/all.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand All @@ -29,7 +30,7 @@ using namespace aliceVision::sfmData;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportMeshroomMaya.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <aliceVision/sfmData/SfMData.hpp>
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/image/all.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand All @@ -27,7 +28,7 @@ namespace po = boost::program_options;
namespace fs = boost::filesystem;
namespace oiio = OIIO;

int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportPMVS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aliceVision/sfmData/SfMData.hpp>
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/image/all.hpp>
#include <aliceVision/system/main.hpp>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -304,7 +305,7 @@ bool exportToBundlerFormat(
return true;
}

int main(int argc, char *argv[])
int aliceVision_main(int argc, char *argv[])
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/export/main_exportTracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <aliceVision/image/all.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/system/main.hpp>

#include <software/utils/sfmHelper/sfmIOHelper.hpp>

Expand All @@ -41,7 +42,7 @@ using namespace svg;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

int main(int argc, char ** argv)
int aliceVision_main(int argc, char ** argv)
{
// command-line parameters

Expand Down
3 changes: 2 additions & 1 deletion src/software/pipeline/main_cameraCalibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <aliceVision/calibration/exportData.hpp>
#include <aliceVision/system/Timer.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/system/cmdline.hpp>
#include <aliceVision/config.hpp>

Expand Down Expand Up @@ -48,7 +49,7 @@
namespace bfs = boost::filesystem;
namespace po = boost::program_options;

int main(int argc, char** argv)
int aliceVision_main(int argc, char** argv)
{
// Command line arguments
bfs::path inputPath;
Expand Down
3 changes: 2 additions & 1 deletion src/software/pipeline/main_cameraInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <aliceVision/sfmDataIO/viewIO.hpp>
#include <aliceVision/sensorDB/parseDatabase.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/system/cmdline.hpp>

#include <boost/program_options.hpp>
Expand Down Expand Up @@ -167,7 +168,7 @@ inline std::istream& operator>>(std::istream& in, EGroupCameraFallback& s)
* @brief Create the description of an input image dataset for AliceVision toolsuite
* - Export a SfMData file with View & Intrinsic data
*/
int main(int argc, char **argv)
int aliceVision_main(int argc, char **argv)
{
// command-line parameters

Expand Down
5 changes: 4 additions & 1 deletion src/software/pipeline/main_cameraLocalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <aliceVision/sfmDataIO/sfmDataIO.hpp>
#include <aliceVision/robustEstimation/estimators.hpp>
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/main.hpp>
#include <aliceVision/system/cmdline.hpp>

#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -60,7 +61,7 @@ std::string myToString(std::size_t i, std::size_t zeroPadding)
return ss.str();
}

int main(int argc, char** argv)
int aliceVision_main(int argc, char** argv)
{
/// the calibration file
std::string calibFile;
Expand Down Expand Up @@ -519,4 +520,6 @@ int main(int argc, char** argv)
ALICEVISION_COUT("Mean time for localization: " << bacc::mean(stats) << " [ms]");
ALICEVISION_COUT("Max time for localization: " << bacc::max(stats) << " [ms]");
ALICEVISION_COUT("Min time for localization: " << bacc::min(stats) << " [ms]");

return EXIT_SUCCESS;
}
Loading