Commit be752a28 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Set logging fully up befor entering try-catch loop to avoid crash from out-of-scope log file

(cherry picked from commit c677bb20)
parent 8917795f
Loading
Loading
Loading
Loading
+85 −87
Original line number Diff line number Diff line
@@ -55,18 +55,9 @@ int main(int argc, char** argv) {

    int return_code = 0;

    try {

    // Register the default set of units with this executable:
    allpix::register_units();

        // If no arguments are provided, print the help:
        bool print_help = false;
        if(argc == 1) {
            print_help = true;
            return_code = 1;
        }

    // Add stream and set default logging level
    Log::addStream(std::cout);

@@ -74,19 +65,26 @@ int main(int argc, char** argv) {
    std::signal(SIGQUIT, interrupt_handler);
    std::signal(SIGINT, interrupt_handler);

    // If no arguments are provided, print the help:
    bool print_help = false;
    if(argc == 1) {
        print_help = true;
        return_code = 1;
    }

    std::string file_prefix;
    std::string init_file_prefix;
    std::string log_file_name;

    std::string conf_file_name;
        allpix::LogLevel log_level = allpix::LogLevel::NONE;

    for(int i = 1; i < argc; i++) {
        if(strcmp(argv[i], "-h") == 0) {
            print_help = true;
        } else if(strcmp(argv[i], "-v") == 0 && (i + 1 < argc)) {
            try {
                    log_level = Log::getLevelFromString(std::string(argv[++i]));
                auto log_level = Log::getLevelFromString(std::string(argv[++i]));
                Log::setReportingLevel(log_level);
            } catch(std::invalid_argument& e) {
                LOG(ERROR) << "Invalid verbosity level \"" << std::string(argv[i]) << "\", ignoring overwrite";
                return_code = 1;
@@ -142,35 +140,35 @@ int main(int argc, char** argv) {
        return return_code;
    }

    // NOTE: this stream should be available for the duration of the logging
    std::ofstream log_file;
    if(!log_file_name.empty()) {
        log_file.open(log_file_name, std::ios_base::out | std::ios_base::trunc);
        if(!log_file.good()) {
            LOG(FATAL) << "Cannot write to provided log file! Check if permissions are sufficient.";
            Log::finish();
            return 1;
        }
        Log::addStream(log_file);
    }

    try {
        std::ifstream file(conf_file_name);
        allpix::ConfigReader reader(file, conf_file_name);
        allpix::Configuration config = reader.getHeaderConfiguration();

        auto log_level = Log::getReportingLevel();
        if(log_level == allpix::LogLevel::NONE) {
            auto log_level_string = config.get<std::string>("log_level", "INFO");
            std::transform(log_level_string.begin(), log_level_string.end(), log_level_string.begin(), ::toupper);
            try {
                log_level = Log::getLevelFromString(log_level_string);
                Log::setReportingLevel(log_level);
            } catch(std::invalid_argument& e) {
                LOG(ERROR) << "Log level \"" << log_level_string
                           << "\" specified in the configuration is invalid, defaulting to INFO instead";
                log_level = allpix::LogLevel::INFO;
            }
        }

        // Set log level:
        Log::setReportingLevel(log_level);

        // NOTE: this stream should be available for the duration of the logging
        std::ofstream log_file;
        if(!log_file_name.empty()) {
            log_file.open(log_file_name, std::ios_base::out | std::ios_base::trunc);
            if(!log_file.good()) {
                LOG(FATAL) << "Cannot write to provided log file! Check if permissions are sufficient.";
                Log::finish();
                return 1;
                Log::setReportingLevel(allpix::LogLevel::INFO);
            }
            Log::addStream(log_file);
        }

        LOG(STATUS) << "Welcome to the Mesh Converter Tool of Allpix^2 " << ALLPIX_PROJECT_VERSION;
@@ -193,7 +191,7 @@ int main(int argc, char** argv) {
        const auto units = config.get<std::string>("observable_units");
        // Test if this unit is valid:
        try {
            auto unit = Units::get(units);
            (void)Units::get(units);
        } catch(std::invalid_argument& e) {
            throw allpix::InvalidValueError(config, "observable_units", e.what());
        }