#ifndef LOG4_H
#define LOG4_H

#ifdef __cplusplus

#include <string>
#include <map>

//防止#include <log4cplus/logger.h>里的冲突
#ifdef min
#undef min
#endif

#ifdef max
#undef max
#endif
//防止#include <log4cplus/logger.h>里的冲突

#include "logger.h"
#include <set>
#include "loggingmacros.h"

#include "appender.h"

#define LOGTYPE_COM 1
#define LOGTYPE_DATA 2

struct TypedLogger {
    log4cplus::Logger logger;
    int logtype;
    TypedLogger();
    TypedLogger(const log4cplus::Logger& l, int t);
};

struct DebugSwitch {
    bool debug_open;
    std::set<std::string> targets;
    int min_level;
    std::map<int, bool> type_enable;

    DebugSwitch();
    void open();
    void close();
    void set_target(const std::string& name);
    void set_level(int level);
    void enable_type(int type);
    void disable_type(int type);
    bool match(const std::string& logger_name, int level, int logtype);
};

extern std::map<std::string, TypedLogger> logger_map;
extern DebugSwitch g_debug_switch;

extern void send_reply_to_queue(const std::string& guid, const std::string& step, const std::string& result);


//std::string get_front_type_from_subdir();


// 不带 Appender 的版本
log4cplus::Logger init_logger(const std::string& full_name,
                              const std::string& file_dir,
                              const std::string& base_file);

// 带 Appender 的版本
log4cplus::Logger init_logger(const std::string& full_name,
                              const std::string& file_dir,
                              const std::string& base_file,
                              log4cplus::SharedAppenderPtr fileAppender);

void process_log_command(const std::string& id, const std::string& level, const std::string& grade, const std::string& logtype_str);


void update_log_entries_countdown();

extern "C" {
#endif
void remove_loggers_by_terminal_id(const std::string& terminal_id_cstr);
void init_logger_process();
void init_loggers();
void init_loggers_bydevid(const std::string& dev_id);

void log_debug(const char* key, const char* msg);
void log_info(const char* key, const char* msg);
void log_warn(const char* key, const char* msg);
void log_error(const char* key, const char* msg);

void send_reply_to_queue_c(const char* guid, const char* step, const char* result);
void format_log_msg(char* buf, size_t buf_size, const char* fmt, ...);

//宏定义
#define DIY_LOG(LEVEL_FUNC, KEY, ...)                             \
    do {                                                          \
        char buf[256];                                            \
        format_log_msg(buf, sizeof(buf), __VA_ARGS__);            \
        LEVEL_FUNC(KEY, buf);                                     \
    } while (0)

#define DIY_ERRORLOG(KEY, ...) DIY_LOG(log_error, KEY, __VA_ARGS__)
#define DIY_WARNLOG(KEY, ...)  DIY_LOG(log_warn,  KEY, __VA_ARGS__)
#define DIY_INFOLOG(KEY, ...)  DIY_LOG(log_info,  KEY, __VA_ARGS__)
#define DIY_DEBUGLOG(KEY, ...) DIY_LOG(log_debug, KEY, __VA_ARGS__)


#ifdef __cplusplus
}
#endif



#endif // LOG4_H
