← source index

src/Log.h

48 lines  ·  1.0 K  ·  cpp
#pragma once
#include "../Include.h"
#include "../Helper.h"

enum class LogStatus : short
{
    INFO,
    WARN,
    ERROR,

    __NONE__,
    __LAST__
};

class Log
{
public:
    Log() = default;

    static Log& getInstance()
    {
        static Log log;
        return log;
    }

    void init(std::string_view, std::string_view);

    bool log(const LogStatus&, std::string_view, bool flush = true);
    bool info(const std::string& msg)  { return log(LogStatus::INFO, msg); }
    bool warn(const std::string& msg)  { return log(LogStatus::WARN, msg); }
    bool error(const std::string& msg) { return log(LogStatus::ERROR, msg); }

    ~Log();

    // Disabling move and copy
    Log(const Log&) = delete;
    Log(Log &&) = delete;
    Log& operator=(const Log&) = delete;
    Log& operator=(Log &&) = delete;

private:
    std::mutex mMtx;
    std::ofstream mLogFile;
    std::string mLogPath;
    std::string mLogFileName;

    std::string mLogStatusStr[(short) LogStatus::__LAST__ - 1] = { "INFO", "WARN", "ERROR" };
};