Skip to content

Using spdlog to output logs makes my programme stuck and unable to continue. #3365

Open
@Mq-b

Description

@Mq-b

Dump File:

Image

#include "Timer.h"

Timer::Timer() = default;

Timer::~Timer() {
    stop = true;
}

void Timer::start(){
    stop = false;
    run();
}

void Timer::addTask(const std::function<void()>& task, std::chrono::milliseconds interval, TaskType type) {
    tasks[static_cast<std::size_t>(type)] = {task, interval};
}

void Timer::run() {
    
    std::array<std::chrono::time_point<std::chrono::steady_clock>, 2> nextExecutionTimes;
    auto now = std::chrono::steady_clock::now();
    for (std::size_t i = 0; i < tasks.size(); ++i) {
        nextExecutionTimes[i] = now + tasks[i].interval;
    }

    while (!stop) {

        if (stop) break;

        auto now = std::chrono::steady_clock::now();

        auto nextTaskTime = *std::min_element(nextExecutionTimes.begin(), nextExecutionTimes.end());

        spdlog::info("Sleep...... {}", nextTaskTime - now);
        if (nextTaskTime > now) {
            std::this_thread::sleep_for(nextTaskTime - now);
        }
        spdlog::info("run.....");

        now = std::chrono::steady_clock::now();

        for (std::size_t i = 0; i < tasks.size(); ++i) {
            spdlog::info("run2.....");
            if (now >= nextExecutionTimes[i]) {
                spdlog::info("run3.....");
                tasks[i].func();

                nextExecutionTimes[i] = now + tasks[i].interval;
                spdlog::info("Task {} executed, next execution at: {}", i, formatTimePoint(nextExecutionTimes[i]));
            }
        }
    }
}

set spdlog code:

inline void setupLogging() {
    if (std::filesystem::exists("Log")) {
        std::filesystem::create_directory("Log");
    }
    auto file_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("Log/daily.log", 0, 0);
    file_sink->set_level(spdlog::level::debug);
    file_sink->set_pattern("[%Y-%m-%d %H:%M:%S] [thread %t] [%oms] [%l] %v");

    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::debug);
    console_sink->set_pattern("%^[%Y-%m-%d %H:%M:%S] [thread %t] [%oms] [%l] %v%$");

    auto logger = std::make_shared<spdlog::logger>("multi_sink", spdlog::sinks_init_list{ file_sink, console_sink });
    spdlog::register_logger(logger);

    spdlog::set_default_logger(logger);
    spdlog::flush_on(spdlog::level::debug);
}
[2025-03-25 15:57:11] [thread 7852] [3ms] [info] Sleep...... 59999999600ns
[2025-03-25 15:58:11] [thread 7852] [60002ms] [info] run.....
[2025-03-25 15:59:16] [thread 7852] [64117ms] [info] run2.....
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] run3.....
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] Reporting device status to server.
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] POST request to /report
[2025-03-25 15:59:16] [thread 7852] [591ms] [info] POST for
[2025-03-25 15:59:16] [thread 7852] [1ms] [info] POST Response code: 200
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] POST Response body: Yes
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] POST request successful.
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] Task 0 executed, next execution at: 2025-03-25 16:00:16
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] run2.....
[2025-03-25 15:59:16] [thread 7852] [0ms] [info] Sleep...... 59403475200ns
[2025-03-25 16:00:16] [thread 7852] [59405ms] [info] run.....
[2025-03-25 16:03:16] [thread 7852] [180041ms] [info] run2.....
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] run3.....
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] Reporting device status to server.
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] POST request to /report
[2025-03-25 16:03:16] [thread 7852] [239ms] [info] POST for
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] POST Response code: 200
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] POST Response body: Yes
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] POST request successful.
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] Task 0 executed, next execution at: 2025-03-25 16:04:16
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] run2.....
[2025-03-25 16:03:16] [thread 7852] [0ms] [info] Sleep...... 59756764000ns

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions