<commit_before>#include "vast/configuration.h"

#include "config.h"

#include <iostream>
#include <string>
#include <boost/exception/diagnostic_information.hpp>
#include "vast/exception.h"
#include "vast/fs/path.h"
#include "vast/fs/fstream.h"
#include "vast/fs/operations.h"
#include "vast/logger.h"

namespace vast {

configuration::configuration()
  : visible_("")
  , all_("")
{
  po::options_description general("general options");
  general.add_options()
    ("config,c", po::value<fs::path>(), "configuration file")
    ("directory,d", po::value<fs::path>()->default_value("vast"),
     "VAST directory")
    ("expression,e", po::value<std::string>(), "query expression")
    ("help,h", "display this help")
    ("schema,s", po::value<std::string>(), "event schema file")
    ("console-verbosity,v",
     po::value<int>()->default_value(logger::info),
     "console logging verbosity")
    ("advanced,z", "show advanced options")
    ;

  po::options_description advanced("advanced options");
  advanced.add_options()
    ("logfile-verbosity,V",
     po::value<int>()->default_value(logger::verbose),
     "log file verbosity")
    ("profile,P", po::value<unsigned>(),
     "enable getrusage profiling at a given interval (seconds)")
#ifdef USE_PERFTOOLS_CPU_PROFILER
    ("profile-cpu", "also enable Google perftools CPU profiling")
#endif
#ifdef USE_PERFTOOLS_HEAP_PROFILER
    ("profile-heap", "also enable Google perftools heap profiling")
#endif
    ("broccoli-messages", "enable broccoli debug messages")
    ("broccoli-calltrace", "enable broccoli function call tracing")
    ;

  po::options_description actor("actor options");
  actor.add_options()
    ("all-server,a", "spawn all server components")
    ("ingestor-actor,I", "spawn the ingestor locally")
    ("archive-actor,A", "spawn the archive locally")
    ("index-actor,X", "spawn the index locally")
    ("search-actor,S", "spawn the search locally")
    ("tracker-actor,T", "spawn the ID tracker locally")
    ;

  po::options_description schema("schema options");
  schema.add_options()
    ("print-schema", "print the parsed event schema")
    ;

  po::options_description tracker("ID tracker options");
  tracker.add_options()
    ("tracker.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the ID tracker")
    ("tracker.port", po::value<unsigned>()->default_value(42004),
     "TCP port of the ID tracker")
    ;

  po::options_description ingest("ingest options");
  ingest.add_options()
    ("ingest.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the broccoli source")
    ("ingest.port", po::value<unsigned>()->default_value(42000),
     "port of the broccoli source")
    ("ingest.events", po::value<std::vector<std::string>>()->multitoken(),
     "explicit list of events for broccoli to ingest")
    ("ingest.max-events-per-chunk", po::value<size_t>()->default_value(1000),
     "maximum number of events per chunk")
    ("ingest.max-segment-size", po::value<size_t>()->default_value(1),
     "maximum segment size in MB")
    ("ingest.file-type", po::value<std::string>()->default_value("bro2"),
     "file type of the file(s) to ingest")
    ("ingest.file-names", po::value<std::vector<std::string>>()->multitoken(),
     "file(s) to ingest")
    ;

  po::options_description archive("archive options");
  archive.add_options()
    ("archive.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the archive")
    ("archive.port", po::value<unsigned>()->default_value(42002),
     "port of the archive")
    ("archive.max-segments", po::value<size_t>()->default_value(500),
     "maximum number of segments to keep in memory")
    ;

  po::options_description index("index options");
  index.add_options()
    ("index.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the index")
    ("index.port", po::value<unsigned>()->default_value(42003),
     "port of the index")
    ;

  po::options_description search("search options");
  search.add_options()
    ("search.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the search")
    ("search.port", po::value<unsigned>()->default_value(42001),
     "port of the search")
    ;

  po::options_description query("query client options");
  query.add_options()
    ("query.paginate,p", po::value<unsigned>()->default_value(10),
     "number of query results per page")
    ;

  all_.add(general).add(advanced).add(actor).add(schema).add(tracker)
    .add(ingest).add(archive).add(index).add(search).add(query);

  visible_.add(general).add(actor);
}

bool configuration::load(std::string const& filename)
{
  try
  {
    if (fs::exists(filename))
    {
      fs::ifstream ifs(filename);
      po::store(po::parse_config_file(ifs, all_), config_);
    }

    init();
    return true;
  }
  catch (error::config const& e)
  {
    std::cerr << e.what() << std::endl;
  }
  catch (boost::program_options::unknown_option const& e)
  {
    std::cerr << e.what() << std::endl;
  }
  catch (boost::exception const& e)
  {
    std::cerr << boost::diagnostic_information(e);
  }

  return false;
}

bool configuration::load(int argc, char *argv[])<commit_msg>Fix error on non-existant config file.<commit_after>#include "vast/configuration.h"

#include "config.h"

#include <iostream>
#include <string>
#include <boost/exception/diagnostic_information.hpp>
#include "vast/exception.h"
#include "vast/fs/path.h"
#include "vast/fs/fstream.h"
#include "vast/fs/operations.h"
#include "vast/logger.h"

namespace vast {

configuration::configuration()
  : visible_("")
  , all_("")
{
  po::options_description general("general options");
  general.add_options()
    ("config,c", po::value<fs::path>(), "configuration file")
    ("directory,d", po::value<fs::path>()->default_value("vast"),
     "VAST directory")
    ("expression,e", po::value<std::string>(), "query expression")
    ("help,h", "display this help")
    ("schema,s", po::value<std::string>(), "event schema file")
    ("console-verbosity,v", po::value<int>()->default_value(logger::info),
     "console logging verbosity")
    ("advanced,z", "show advanced options")
    ;

  po::options_description advanced("advanced options");
  advanced.add_options()
    ("logfile-verbosity,V",
     po::value<int>()->default_value(logger::verbose),
     "log file verbosity")
    ("profile,P", po::value<unsigned>(),
     "enable getrusage profiling at a given interval (seconds)")
#ifdef USE_PERFTOOLS_CPU_PROFILER
    ("profile-cpu", "also enable Google perftools CPU profiling")
#endif
#ifdef USE_PERFTOOLS_HEAP_PROFILER
    ("profile-heap", "also enable Google perftools heap profiling")
#endif
    ("broccoli-messages", "enable broccoli debug messages")
    ("broccoli-calltrace", "enable broccoli function call tracing")
    ;

  po::options_description actor("actor options");
  actor.add_options()
    ("all-server,a", "spawn all server components")
    ("ingestor-actor,I", "spawn the ingestor locally")
    ("archive-actor,A", "spawn the archive locally")
    ("index-actor,X", "spawn the index locally")
    ("search-actor,S", "spawn the search locally")
    ("tracker-actor,T", "spawn the ID tracker locally")
    ;

  po::options_description schema("schema options");
  schema.add_options()
    ("print-schema", "print the parsed event schema")
    ;

  po::options_description tracker("ID tracker options");
  tracker.add_options()
    ("tracker.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the ID tracker")
    ("tracker.port", po::value<unsigned>()->default_value(42004),
     "TCP port of the ID tracker")
    ;

  po::options_description ingest("ingest options");
  ingest.add_options()
    ("ingest.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the broccoli source")
    ("ingest.port", po::value<unsigned>()->default_value(42000),
     "port of the broccoli source")
    ("ingest.events", po::value<std::vector<std::string>>()->multitoken(),
     "explicit list of events for broccoli to ingest")
    ("ingest.max-events-per-chunk", po::value<size_t>()->default_value(1000),
     "maximum number of events per chunk")
    ("ingest.max-segment-size", po::value<size_t>()->default_value(1),
     "maximum segment size in MB")
    ("ingest.file-type", po::value<std::string>()->default_value("bro2"),
     "file type of the file(s) to ingest")
    ("ingest.file-names", po::value<std::vector<std::string>>()->multitoken(),
     "file(s) to ingest")
    ;

  po::options_description archive("archive options");
  archive.add_options()
    ("archive.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the archive")
    ("archive.port", po::value<unsigned>()->default_value(42002),
     "port of the archive")
    ("archive.max-segments", po::value<size_t>()->default_value(500),
     "maximum number of segments to keep in memory")
    ;

  po::options_description index("index options");
  index.add_options()
    ("index.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the index")
    ("index.port", po::value<unsigned>()->default_value(42003),
     "port of the index")
    ;

  po::options_description search("search options");
  search.add_options()
    ("search.host", po::value<std::string>()->default_value("127.0.0.1"),
     "hostname or IP address of the search")
    ("search.port", po::value<unsigned>()->default_value(42001),
     "port of the search")
    ;

  po::options_description query("query client options");
  query.add_options()
    ("query.paginate,p", po::value<unsigned>()->default_value(10),
     "number of query results per page")
    ;

  all_.add(general).add(advanced).add(actor).add(schema).add(tracker)
    .add(ingest).add(archive).add(index).add(search).add(query);

  visible_.add(general).add(actor);
}

bool configuration::load(std::string const& filename)
{
  try
  {
    if (! fs::exists(filename))
      return false;

    fs::ifstream ifs(filename);
    po::store(po::parse_config_file(ifs, all_), config_);

    init();
    return true;
  }
  catch (error::config const& e)
  {
    std::cerr << e.what() << std::endl;
  }
  catch (boost::program_options::unknown_option const& e)
  {
    std::cerr << e.what() << std::endl;
  }
  catch (boost::exception const& e)
  {
    std::cerr << boost::diagnostic_information(e);
  }

  return false;
}

bool configuration::load(int argc, char *argv[])
{'fraction_non_alphanumeric': 0.1804313023825219, 'fraction_numerical': 0.01243054901591487, 'mean_word_length': 4.574803149606299, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7557190', 'n_tokens_mistral': 3541, 'n_tokens_neox': 3413, 'n_words': 730}