#ifndef DEBUG_LOGGER_H
#define DEBUG_LOGGER_H
#include <iostream>
#include <fstream>
#include <mutex>
#include <thread>
#include <chrono>
#include <cstdarg>
#include <cstdio>
#include <string>
#include <sstream>
#ifdef _WIN32
#include <windows.h>
#elif defined(__APPLE__) || defined(__linux__)
#include <pthread.h>
#include <unistd.h>
#endif
class DebugLogger {
public:
static DebugLogger& getInstance();
void init(bool enable_debug = false, const char* log_file = nullptr);
void debug(const char* file, int line, const char* func, const char* format, ...);
bool isEnabled() const { return enabled_; }
void cleanup();
private:
DebugLogger() = default;
~DebugLogger();
std::mutex mutex_;
bool enabled_ = false;
std::ofstream file_stream_;
bool use_file_ = false;
std::string getCurrentTimestamp();
std::string getThreadId();
std::string extractFileName(const char* full_path);
};
#define CTP_DEBUG_INIT(enable, file) DebugLogger::getInstance().init(enable, file)
#define CTP_DEBUG_CLEANUP() DebugLogger::getInstance().cleanup()
#define CTP_DEBUG(format, ...) do { \
if (DebugLogger::getInstance().isEnabled()) { \
DebugLogger::getInstance().debug(__FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__); \
} \
} while(0)
typedef struct {
int enable_debug; const char* log_file_path; int max_file_size_mb; int max_backup_files; } CtpLogConfig;
#ifdef __cplusplus
extern "C" {
#endif
void CTP_InitializeDebugLogging(const CtpLogConfig* config);
void CTP_CleanupDebugLogging();
#ifdef __cplusplus
}
#endif
#endif