0

我正在尝试使用 spdlog 在 23:59 生成每日日志。日志条目发生在单独线程 (TimerThread) 上的互斥锁内。在 23:58 或 23:59,日志文件按预期停止条目,但直到凌晨 4:27-4:30 才创建新的日志文件。我的系统时区设置为 UTC。似乎启动 TimerThread 的线程也被锁定了。请参阅附件代码。我究竟做错了什么?这是我使用的 TimerThread 代码:https ://codereview.stackexchange.com/questions/127552/portable-periodic-one-shot-timer-thread-follow-up 。为了测试 TimerThread 是否对锁定做了一些有趣的事情,我有一些代码来测试带有旋转文件接收器的计时器线程,即使在 23:59 时钟时间之后它也会继续运行而没有任何问题。

#include <iostream>
#include <stdlib.h>
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <TimerThread.h>
#include <time.h>
#include <chrono>
#include <sstream>
#include <iomanip>


typedef struct data {
    int someNumber;
    std::string somestring;
}DATA;

DATA updateThis;
std::mutex lockme;
DATA testDailyLog;
DATA testCustomLog;
std::mutex dailyLogLock;
std::mutex customLogLock;

//UTC timestamp
std::string GetTimeStampUTCStr()
{
    auto time = std::chrono::system_clock::to_time_t(
        std::chrono::system_clock::now());
        std::ostringstream ss;
        ss << std::put_time(gmtime(&time), "%FT%TZ");
    return ss.str();
}

// daily log test section
void logDailyLogData(std::shared_ptr<spdlog::logger> logger)
{
    dailyLogLock.lock();
    logger->info("{}: {}", GetTimeStampUTCStr(), testDailyLog.somestring);
    dailyLogLock.unlock();
}


void updateDailyLogData(){
    int num = rand()%300;
    while(1) {
        std::this_thread::sleep_for(std::chrono::milliseconds(num));
        dailyLogLock.lock();
        testDailyLog.somestring = "some number: " + std::to_string(num);
        testDailyLog.someNumber = num;
        dailyLogLock.unlock();
    }
}

void testDialyLogger() {
    auto logger = spdlog::daily_logger_mt("dailylog","dailylog.txt", 23,59,false, 5);
    logger->set_level(spdlog::level::debug);
    logger->flush_on(spdlog::level::debug);
    TimerThread *timer = new TimerThread();
    timer->addTimer(1000, 60000, std::bind(&logDailyLogData, logger));
    updateDailyLogData();
}
// test just timer thread

void timerThreadTestFunc(std::shared_ptr<spdlog::logger> logger){
    
    logger->info("{}", GetTimeStampUTCStr());
}

void testTimerThreadOnly(){
    auto logger = spdlog::rotating_logger_mt("timerOnlyLog","timertest.txt", 55555, 5, true);
    logger->set_level(spdlog::level::debug);
    logger->flush_on(spdlog::level::debug);
    TimerThread *timer = new TimerThread();
    timer->addTimer(1000, 60000, std::bind(&timerThreadTestFunc, logger));
}


int main(int, char**) {
    std::cout << "Hello, world!\n";
    /* initialize random seed: */
    srand (time(NULL));
    std::thread dailyLogThread(testDialyLogger);
    //std::thread customLogThread(testCustomLogger);
    std::thread timerTestThread(testTimerThreadOnly);
    //std::thread dailyLogNoLockThread(testTimerThreadDailyLogWithNoLock);

    dailyLogThread.join();
    //customLogThread.join();
    timerTestThread.join();
    //dailyLogNoLockThread.join();
    return 0;
}
  • 操作系统:Ubuntu 20.04
  • 编译器 GCC 9.3.0
  • CXX_STANDARD 设置为 C++14
4

0 回答 0