#include "System.h"

SystemConfig    SystemCall;
// 在线程池中执行文件操作
static asio::thread_pool pool(2);  // 线程专门处理文件I/O

void SystemCallInit(void)
{
    SystemCall.ProductInfo = 0xFF;
    SystemCall.logEn = ENABLE;
    SystemCall.CanOpenStatus[0] = DISABLE;
    SystemCall.CanOpenStatus[1] = DISABLE;
}

void logAdd(const std::string& filename, const std::string& content) {
    asio::post(pool, [filename, content]() {
        std::ofstream file;

        try {
            // 以追加模式打开文件
            file.open(filename, std::ios::app | std::ios::out);
            if (!file.is_open()) {
                throw std::runtime_error("Cannot open file: " + filename);
            }
            
            // 写入内容（追加换行符）
            file << content << std::endl;
            
            if (file.fail()) {
                throw std::runtime_error("Write failed");
            }
            
            //std::cout << "Successfully appended to file: " << filename << std::endl;
        }
        catch (const std::exception& e) {
            std::cerr << "Error: " << e.what() << std::endl;
        }
    });
    
    // 注意：pool会在作用域结束时join
    // 如果需要保持运行，可以将pool作为成员变量
}

// 模拟 FreeRTOS 的延时
void vTaskDelay(TickType_t ms)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}