#include "textlogger.h"
#include "utils/datetime.h"
#include <fstream>

namespace myapp::logging {

struct text_logger::impl final
{
    std::ofstream fstream_;

    explicit impl(std::string const& filename)
    {
        fstream_.exceptions(std::ofstream::failbit | std::ofstream::badbit);
        fstream_.open(filename, std::ios_base::out | std::ios_base::app);
    }
};

static void report(std::ostream& os, std::string const& severity, std::string const& tag, std::string const& message) noexcept
{
    try 
    {
        os << utils::datetime::now().local_time() << ": " << tag << ": " << severity << " " << message << std::endl;
    }
    catch (...)
    {
        
    }
}

void text_logger::debug(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "DEBUG", tag, message);
}

void text_logger::info(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "INFO", tag, message);
}

void text_logger::notice(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "NOTICE", tag, message);
}

void text_logger::warning(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "WARNING", tag, message);
}

void text_logger::error(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "ERR", tag, message);
}

void text_logger::critical(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "CRIT", tag, message);
}

void text_logger::alert(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "ALERT", tag, message);
}

void text_logger::emergency(std::string const& tag, std::string const& message) noexcept
{
    report(impl_->fstream_, "EMERG", tag, message);
}

text_logger::text_logger(std::string const& filename)
    : impl_(std::make_unique<impl>(filename))
{
}

text_logger::~text_logger() = default;


} /* namespace myapp::logging */
