/**
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "logging.hh"
#include <string>
#include <mutex>

namespace Logging
{

std::shared_ptr<const Logger> Logger::getptr() const
{
  return shared_from_this();
}

void Logger::info(Logr::Priority prio, const std::string& msg) const
{
  logMessage(msg, prio, std::nullopt);
}

void Logger::logMessage(const std::string& msg, Logr::Priority prio, const std::optional<std::string>& err) const
{
  Entry entry;
  entry.level = _level;
  entry.d_priority = prio;
  ::gettimeofday(&entry.d_timestamp, nullptr);
  entry.name = _name;
  entry.message = msg;
  entry.error = err;
  auto parent = _parent;
  entry.values.insert(_values.begin(), _values.end());
  while (parent) {
    entry.values.insert(parent->_values.begin(), parent->_values.end());
    parent = parent->_parent;
  }
  _callback(entry);
}

void Logger::error(Logr::Priority prio, int err, const std::string& msg) const
{
  logMessage(msg, prio, std::string(stringerror(err)));
}

void Logger::error(Logr::Priority prio, const std::string& err, const std::string& msg) const
{
  logMessage(msg, prio, err);
}

std::shared_ptr<Logr::Logger> Logger::v(size_t level) const
{
  auto res = std::make_shared<Logger>(getptr(), _name, level + _level, _callback);
  return res;
}

std::shared_ptr<Logr::Logger> Logger::withValues(const std::map<std::string, std::string>& values) const
{
  auto res = std::make_shared<Logger>(getptr(), _name, _level, _callback);
  res->_values = values;
  return res;
}

std::shared_ptr<Logr::Logger> Logger::withName(const std::string& name) const
{
  std::shared_ptr<Logger> res;
  if (_name) {
    res = std::make_shared<Logger>(getptr(), _name.value() + "." + name, _level, _callback);
  }
  else {
    res = std::make_shared<Logger>(getptr(), name, _level, _callback);
  }
  return res;
}
std::shared_ptr<Logger> Logger::create(EntryLogger callback)
{
  return std::make_shared<Logger>(callback);
}
std::shared_ptr<Logger> Logger::create(EntryLogger callback, const std::string& name)
{
  return std::make_shared<Logger>(callback, name);
}

Logger::Logger(EntryLogger callback) :
  _callback(callback)
{
}
Logger::Logger(EntryLogger callback, std::optional<std::string> name) :
  _callback(callback), _name(std::move(name))
{
}
Logger::Logger(std::shared_ptr<const Logger> parent, std::optional<std::string> name, size_t lvl, EntryLogger callback) :
  _parent(std::move(parent)), _callback(callback), _name(std::move(name)), _level(lvl)
{
}

Logger::~Logger() = default;
};

std::shared_ptr<Logging::Logger> g_slog{nullptr};

const char* Logging::toTimestampStringMilli(const struct timeval& tval, std::array<char, 64>& buf, const std::string& format)
{
  size_t len = 0;
  if (format != "%s") {
    // strftime is not thread safe, it can access locale information
    static std::mutex mutex;
    auto lock = std::scoped_lock(mutex);
    struct tm theTime // clang-format insists on formatting it like this
      {};
    len = strftime(buf.data(), buf.size(), format.c_str(), localtime_r(&tval.tv_sec, &theTime));
  }
  if (len == 0) {
    len = snprintf(buf.data(), buf.size(), "%lld", static_cast<long long>(tval.tv_sec));
  }

  snprintf(&buf.at(len), buf.size() - len, ".%03ld", static_cast<long>(tval.tv_usec) / 1000);
  return buf.data();
}
