#include <log4cplus/log4cplus.h>

#include <iostream>

#include "net_server.h"

#define COLOR_WARN   "\033[33m" // 黄色
#define COLOR_RESET  "\033[0m"  // 重置颜色
#define RESET   "\033[0m"
#define RED     "\033[31m"
#define GREEN   "\033[32m"
#define YELLOW  "\033[33m"
#define BLUE    "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN    "\033[36m"
#define WHITE   "\033[37m"

#define LOG_WARN(logger, format, ...) \
    { \
        std::ostringstream oss; \
        oss << COLOR_WARN << format << COLOR_RESET; \
        LOG4CPLUS_WARN_FMT(logger, oss.str().c_str(), ##__VA_ARGS__); \
    }

class CDemoLogger
{
public:
    log4cplus::Initializer initializer;

public:
    void inita()
    {
        // 第1步：创建ConsoleAppender(实例化一个appender对象)
        log4cplus::SharedAppenderPtr appender(new log4cplus::ConsoleAppender());
        // 第2步：设置Appender的名称和输出格式（SimpleLayout）
        appender->setName(LOG4CPLUS_TEXT2("console"));
        // 第3步：实例化一个layout对象，将layout对象绑定到appender对象
        log4cplus::tstring pattern =
            LOG4CPLUS_TEXT("%D{%m/%d/%y %H:%M:%S,%Q} [%t] %-5p %c - %m [%l]%n");
        appender->setLayout(std::unique_ptr<log4cplus::Layout>(
            new log4cplus::PatternLayout(pattern)));
        // 第4步：实例化一个封装了日志输出的Logger对象，并设置其日志输出等级阈值
        log4cplus::Logger logger =
            log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("test"));
        // 第5步：将appender对象绑定到logger对象
        logger.addAppender(appender);
        // 第6步：设置日志log的优先级
        logger.setLogLevel(log4cplus::INFO_LOG_LEVEL);
        // 使用宏将日志输出
        LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("Hello world"));
        LOG4CPLUS_WARN(logger, LOG4CPLUS_TEXT("Hello world"));
        LOG4CPLUS_DEBUG(logger, LOG4CPLUS_TEXT("Hello world"));
        LOG4CPLUS_ERROR(logger, LOG4CPLUS_TEXT("Hello world"));
        LOG4CPLUS_FATAL(logger, LOG4CPLUS_TEXT("Hello world"));
    }
    void initb()
    {
        log4cplus::initialize();
        log4cplus::PropertyConfigurator::doConfigure(
            LOG4CPLUS_TEXT("D:/Code/nettrans/log4config.properties"));

        log4cplus::Logger logger = log4cplus::Logger::getRoot();

        // log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("logConsole"));
        LOG4CPLUS_INFO_FMT(logger, LOG4CPLUS_TEXT("Hello world"));
        std::string data("中文测试2");
        std::string data3(u8"中文测试3");
        std::cout << data << std::endl;
        std::cout << data3 << std::endl;
        LOG_WARN(logger, u8"Hello world 中文测试5 %d", 3434);
        LOG4CPLUS_WARN_FMT(logger, u8"Hello world 中文测试5 %d", 3434);
        LOG4CPLUS_WARN_FMT(logger, "Hello world 中文测试6 %d", 3434);
        LOG4CPLUS_WARN_FMT(logger, data.c_str());
        LOG4CPLUS_DEBUG(logger, LOG4CPLUS_TEXT("Hello world"));
        LOG4CPLUS_ERROR(logger, LOG4CPLUS_TEXT("Hello world"));
        LOG4CPLUS_FATAL(logger, LOG4CPLUS_TEXT("Hello world"));
    }
};

int main()
{
    // CNetServer server;
    // server.run();
    // std::cout << "Done" << std::endl;

    #ifdef _WIN32
        system("chcp 65001");
    #endif

    CDemoLogger log;
    // log.inita();
    log.initb();

    std::cin.get();
    return 0;
}
