// custom_printf.h
#ifndef CUSTOM_PRINTF_H
#define CUSTOM_PRINTF_H

#include <stdio.h>
#include <stdarg.h>
#include <QtGlobal>  // ✅ 确保 QtMsgType 被正确包含

#ifdef __cplusplus
#include <list>
#include <string>
// 假设这些是你管理输出的列表
extern std::list<std::string> errorList;
extern std::list<std::string> warnList;
extern std::list<std::string> normalList;
extern std::list<std::string> debugList; 

// 开关
extern bool errorOutputEnabled;
extern bool warnOutputEnabled;
extern bool normalOutputEnabled;
extern bool debugOutputEnabled;

void redirectErrorOutput(bool enabled);
void redirectWarnOutput(bool enabled);
void redirectNormalOutput(bool enabled);
// ------------------ 重定向函数 ------------------
void redirectDebugOutput(bool enable);

// ------------------ Qt 的消息处理函数 (Qt4) ------------------
// Qt4 使用 qInstallMsgHandler(...)，签名: void myMsgHandler(QtMsgType, const char*)
void myQtMsgHandler(QtMsgType type, const char* msg);

extern pthread_mutex_t errorListMutex;
extern pthread_mutex_t warnListMutex;
extern pthread_mutex_t normalListMutex;
extern pthread_mutex_t debugListMutex;

extern "C"
{
#endif
void echo_msg_debugexy(const char *file_name, int line_no, const char *fmt, ...);
void echo_msg_warnexy(const char *file_name, int line_no, const char *fmt, ...);
void echo_msg_errexy(const char *file_name, int line_no, int rv, const char *fmt, ...);


#define echo_msg_debugexy(file_name, line_no, ...) \
    echo_msg_debugexy(file_name, line_no, __VA_ARGS__)

#define echo_msg_warnexy(file_name, line_no, ...) \
    echo_msg_warnexy(file_name, line_no, __VA_ARGS__)    

#define echo_msg_errexy(file_name, line_no, rv, ...) \
    echo_msg_errexy(file_name, line_no, rv, __VA_ARGS__)

// ======================= echo_warnX =======================
#ifdef echo_warn
#undef echo_warn
#endif
#define echo_warn(s) \
    echo_msg_warnexy(__FILE__, __LINE__, ("%s"), s)

#ifdef echo_warn1
#undef echo_warn1
#endif
#define echo_warn1(fmt, s0) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0))

#ifdef echo_warn2
#undef echo_warn2
#endif
#define echo_warn2(fmt, s0, s1) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1))

#ifdef echo_warn3
#undef echo_warn3
#endif
#define echo_warn3(fmt, s0, s1, s2) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2))

#ifdef echo_warn4
#undef echo_warn4
#endif
#define echo_warn4(fmt, s0, s1, s2, s3) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3))

#ifdef echo_warn5
#undef echo_warn5
#endif
#define echo_warn5(fmt, s0, s1, s2, s3, s4) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4))

#ifdef echo_warn6
#undef echo_warn6
#endif
#define echo_warn6(fmt, s0, s1, s2, s3, s4, s5) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5))

#ifdef echo_warn7
#undef echo_warn7
#endif
#define echo_warn7(fmt, s0, s1, s2, s3, s4, s5, s6) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5), (s6))

#ifdef echo_warn8
#undef echo_warn8
#endif
#define echo_warn8(fmt, s0, s1, s2, s3, s4, s5, s6, s7) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5), (s6), (s7))

#ifdef echo_warn9
#undef echo_warn9
#endif
#define echo_warn9(fmt, s0, s1, s2, s3, s4, s5, s6, s7, s8) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8))

#ifdef echo_warn10
#undef echo_warn10
#endif
#define echo_warn10(fmt, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9))

#ifdef echo_warn11
#undef echo_warn11
#endif
#define echo_warn11(fmt, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10))

#ifdef echo_warn12
#undef echo_warn12
#endif
#define echo_warn12(fmt, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \
    echo_msg_warnexy(__FILE__, __LINE__, (fmt), (s0), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10), (s11))

// ======================= echo_errX =======================
#ifdef echo_err
#undef echo_err
#endif
#define echo_err(s, rv) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), ("%s"), (s))

#ifdef echo_errg
#undef echo_errg
#endif
#define echo_errg(s) \
    echo_msg_errexy(__FILE__, __LINE__, (APR_EGENERAL), ("%s"), (s))

#ifdef echo_err1
#undef echo_err1
#endif
#define echo_err1(fmt, rv, s1) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1))

#ifdef echo_err2
#undef echo_err2
#endif
#define echo_err2(fmt, rv, s1, s2) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2))

#ifdef echo_err3
#undef echo_err3
#endif
#define echo_err3(fmt, rv, s1, s2, s3) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3))

#ifdef echo_err4
#undef echo_err4
#endif
#define echo_err4(fmt, rv, s1, s2, s3, s4) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4))

#ifdef echo_err5
#undef echo_err5
#endif
#define echo_err5(fmt, rv, s1, s2, s3, s4, s5) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5))

#ifdef echo_err6
#undef echo_err6
#endif
#define echo_err6(fmt, rv, s1, s2, s3, s4, s5, s6) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6))

#ifdef echo_err7
#undef echo_err7
#endif
#define echo_err7(fmt, rv, s1, s2, s3, s4, s5, s6, s7) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7))

#ifdef echo_err8
#undef echo_err8
#endif
#define echo_err8(fmt, rv, s1, s2, s3, s4, s5, s6, s7, s8) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8))

#ifdef echo_err9
#undef echo_err9
#endif
#define echo_err9(fmt, rv, s1, s2, s3, s4, s5, s6, s7, s8, s9) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9))

#ifdef echo_err10
#undef echo_err10
#endif
#define echo_err10(fmt, rv, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10))

#ifdef echo_err11
#undef echo_err11
#endif
#define echo_err11(fmt, rv, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10), (s11))

#ifdef echo_err12
#undef echo_err12
#endif
#define echo_err12(fmt, rv, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \
    echo_msg_errexy(__FILE__, __LINE__, (rv), (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10), (s11), (s12))

// ======================= echo_msgX =======================
#ifdef echo_msg
#undef echo_msg
#endif
#define echo_msg(s) \
    echo_msg_debugexy(__FILE__, __LINE__, ("%s"), (s))

#ifdef echo_msg1
#undef echo_msg1
#endif
#define echo_msg1(fmt, s1) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1))

#ifdef echo_msg2
#undef echo_msg2
#endif
#define echo_msg2(fmt, s1, s2) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2))

#ifdef echo_msg3
#undef echo_msg3
#endif
#define echo_msg3(fmt, s1, s2, s3) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3))

#ifdef echo_msg4
#undef echo_msg4
#endif
#define echo_msg4(fmt, s1, s2, s3, s4) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4))

#ifdef echo_msg5
#undef echo_msg5
#endif
#define echo_msg5(fmt, s1, s2, s3, s4, s5) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5))

#ifdef echo_msg6
#undef echo_msg6
#endif
#define echo_msg6(fmt, s1, s2, s3, s4, s5, s6) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6))

#ifdef echo_msg7
#undef echo_msg7
#endif
#define echo_msg7(fmt, s1, s2, s3, s4, s5, s6, s7) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7))

#ifdef echo_msg8
#undef echo_msg8
#endif
#define echo_msg8(fmt, s1, s2, s3, s4, s5, s6, s7, s8) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8))

#ifdef echo_msg9
#undef echo_msg9
#endif
#define echo_msg9(fmt, s1, s2, s3, s4, s5, s6, s7, s8, s9) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9))

#ifdef echo_msg10
#undef echo_msg10
#endif
#define echo_msg10(fmt, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10))

#ifdef echo_msg11
#undef echo_msg11
#endif
#define echo_msg11(fmt, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10), (s11))

#ifdef echo_msg12
#undef echo_msg12
#endif
#define echo_msg12(fmt, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \
    echo_msg_debugexy(__FILE__, __LINE__, (fmt), (s1), (s2), (s3), (s4), (s5), (s6), (s7), (s8), (s9), (s10), (s11), (s12))

// 自定义的 printf 函数
int customPrintf(const char* format, ...);
#ifdef __cplusplus
}
#endif

// 使用宏将 printf 替换为 customPrintf
#define printf customPrintf

#endif // CUSTOM_PRINTF_H
