<commit_before>#include "logging.h"
#include "common/config.h"
#include "spdlog/sinks/null_sink.h"

std::shared_ptr<spdlog::logger> stderrLogger(
    const std::string& name,
    const std::string& pattern,
    const std::vector<std::string>& files,
    bool quiet) {
  std::vector<spdlog::sink_ptr> sinks;

  auto stderr_sink = spdlog::sinks::stderr_sink_mt::instance();

  if(!quiet)
    sinks.push_back(stderr_sink);

  for(auto&& file : files) {
    auto file_sink
        = std::make_shared<spdlog::sinks::simple_file_sink_st>(file, true);
    sinks.push_back(file_sink);
  }

  auto logger
      = std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));

  spdlog::register_logger(logger);
  logger->set_pattern(pattern);
  return logger;
}

bool
set_loglevel(spdlog::logger& logger, std::string const level) {
  if (level == "trace")
    logger.set_level(spdlog::level::trace);
  else if (level == "debug")
    logger.set_level(spdlog::level::debug);
  else if (level == "info")
    logger.set_level(spdlog::level::info);
  else if (level == "err" or level == "error")
    logger.set_level(spdlog::level::err);
  else if (level == "critical")
    logger.set_level(spdlog::level::critical);
  else if (level == "off")
    logger.set_level(spdlog::level::off);
  else {
    logger.warn("Unknown log level '{}' for logger '{}'",
                level.c_str(), logger.name().c_str());
    return false;
  }
  return true;
}

Logger checkedLog(std::string logger) {
  Logger ret = spdlog::get(logger);
  if(ret) {
    return ret;
  }
  else {
    auto null_sink = std::make_shared<spdlog::sinks::null_sink_st>();
    return std::make_shared<spdlog::logger>("null_logger", null_sink);
  }
}

void createLoggers(const marian::Config* options) {
  std::vector<std::string> generalLogs;
  std::vector<std::string> validLogs;
  if(options && options->has("log")) {
    generalLogs.push_back(options->get<std::string>("log"));
    validLogs.push_back(options->get<std::string>("log"));
  }

  if(options && options->has("valid-log")) {
    validLogs.push_back(options->get<std::string>("valid-log"));
  }

  bool quiet = options->get<bool>("quiet");
  Logger info{stderrLogger("info", "[%Y-%m-%d %T] %v", generalLogs, quiet)};
  Logger warn{stderrLogger("warn", "[%Y-%m-%d %T] [warn] %v", generalLogs, quiet)};
  Logger config{stderrLogger("config", "[%Y-%m-%d %T] [config] %v", generalLogs, quiet)};
  Logger memory{stderrLogger("memory", "[%Y-%m-%d %T] [memory] %v", generalLogs, quiet)};
  Logger data{stderrLogger("data", "[%Y-%m-%d %T] [data] %v", generalLogs, quiet)};
  Logger valid{stderrLogger("valid", "[%Y-%m-%d %T] [valid] %v", validLogs, quiet)};
  Logger translate{stderrLogger("translate", "%v", generalLogs, quiet)};
  Logger devnull{stderrLogger("devnull", "%v", {}, quiet)};
  devnull->set_level(spdlog::level::off);

  if(options && options->has("log-level")) {
    std::string loglevel = options->get<std::string>("log-level");
    if (!set_loglevel(*info, loglevel)) return;
    set_loglevel(*warn, loglevel);
    set_loglevel(*config, loglevel);
    set_loglevel(*memory, loglevel);
    set_loglevel(*data, loglevel);
    set_loglevel(*valid, loglevel);
    set_loglevel(*translate, loglevel);
  }

}<commit_msg>Fix createLoggers with no arguments<commit_after>#include "logging.h"
#include "common/config.h"
#include "spdlog/sinks/null_sink.h"

std::shared_ptr<spdlog::logger> stderrLogger(
    const std::string& name,
    const std::string& pattern,
    const std::vector<std::string>& files,
    bool quiet) {
  std::vector<spdlog::sink_ptr> sinks;

  auto stderr_sink = spdlog::sinks::stderr_sink_mt::instance();

  if(!quiet)
    sinks.push_back(stderr_sink);

  for(auto&& file : files) {
    auto file_sink
        = std::make_shared<spdlog::sinks::simple_file_sink_st>(file, true);
    sinks.push_back(file_sink);
  }

  auto logger
      = std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));

  spdlog::register_logger(logger);
  logger->set_pattern(pattern);
  return logger;
}

bool
set_loglevel(spdlog::logger& logger, std::string const level) {
  if (level == "trace")
    logger.set_level(spdlog::level::trace);
  else if (level == "debug")
    logger.set_level(spdlog::level::debug);
  else if (level == "info")
    logger.set_level(spdlog::level::info);
  else if (level == "err" or level == "error")
    logger.set_level(spdlog::level::err);
  else if (level == "critical")
    logger.set_level(spdlog::level::critical);
  else if (level == "off")
    logger.set_level(spdlog::level::off);
  else {
    logger.warn("Unknown log level '{}' for logger '{}'",
                level.c_str(), logger.name().c_str());
    return false;
  }
  return true;
}

Logger checkedLog(std::string logger) {
  Logger ret = spdlog::get(logger);
  if(ret) {
    return ret;
  }
  else {
    auto null_sink = std::make_shared<spdlog::sinks::null_sink_st>();
    return std::make_shared<spdlog::logger>("null_logger", null_sink);
  }
}

void createLoggers(const marian::Config* options) {
  std::vector<std::string> generalLogs;
  std::vector<std::string> validLogs;
  if(options && options->has("log")) {
    generalLogs.push_back(options->get<std::string>("log"));
    validLogs.push_back(options->get<std::string>("log"));
  }

  if(options && options->has("valid-log")) {
    validLogs.push_back(options->get<std::string>("valid-log"));
  }

  bool quiet = options && options->get<bool>("quiet");
  Logger info{stderrLogger("info", "[%Y-%m-%d %T] %v", generalLogs, quiet)};
  Logger warn{stderrLogger("warn", "[%Y-%m-%d %T] [warn] %v", generalLogs, quiet)};
  Logger config{stderrLogger("config", "[%Y-%m-%d %T] [config] %v", generalLogs, quiet)};
  Logger memory{stderrLogger("memory", "[%Y-%m-%d %T] [memory] %v", generalLogs, quiet)};
  Logger data{stderrLogger("data", "[%Y-%m-%d %T] [data] %v", generalLogs, quiet)};
  Logger valid{stderrLogger("valid", "[%Y-%m-%d %T] [valid] %v", validLogs, quiet)};
  Logger translate{stderrLogger("translate", "%v", generalLogs, quiet)};
  Logger devnull{stderrLogger("devnull", "%v", {}, quiet)};
  devnull->set_level(spdlog::level::off);

  if(options && options->has("log-level")) {
    std::string loglevel = options->get<std::string>("log-level");
    if (!set_loglevel(*info, loglevel)) return;
    set_loglevel(*warn, loglevel);
    set_loglevel(*config, loglevel);
    set_loglevel(*memory, loglevel);
    set_loglevel(*data, loglevel);
    set_loglevel(*valid, loglevel);
    set_loglevel(*translate, loglevel);
  }

}
{'fraction_non_alphanumeric': 0.192845079072624, 'fraction_numerical': 0.0, 'mean_word_length': 5.04828226555246, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1422709', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2331, 'n_words': 415}