Clone https://git.tsinghua.edu.cn/suk23/spdlog with its history and use an agent to investigate the recent-message history on main. The history search comes last. Brief explanations are welcome.
A log message is a record a program writes about what it is doing. A logger receives these messages and sends them to a sink, which writes them to a file or keeps them in memory. Here the program uses a memory sink that retains only the newest messages. Its ring buffer reuses a fixed amount of storage, replacing the oldest message when it is full.
Run the program below before making changes. It gives the sink room for two messages, records three messages, and prints the history it retrieves. All operations in this exercise run in one thread; you do not need to study concurrency.
Observation program — save as ../recent_messages.cpp, beside your clone
#include <spdlog/logger.h>
#include <spdlog/sinks/ringbuffer_sink.h>
#include <iostream>
#include <memory>
#include <string>
int main() {
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_st>(2);
spdlog::logger logger("history", sink);
logger.info("first");
logger.info("second");
logger.info("third");
for (const auto &message : sink->last_raw()) {
std::cout << std::string(message.payload.data(), message.payload.size()) << '\n';
}
}From the root of the spdlog clone, compile and run the program against its source:
g++ -std=c++17 -I include ../recent_messages.cpp -o ../recent_messages
../recent_messagesFollowing the behavior
Before editing the source, trace how the sink stores messages and how last_raw() reads them. Also inspect these two operations elsewhere in the library:
details::mpmc_blocking_queue::size()reports how many items are waiting in a queue.details::backtracer::foreach_pop()takes the messages retained for debugging and passes each one to a supplied function, removing it from the queue.
You only need their storage and retrieval behavior, not their threading mechanisms.
Reviewing an agent's first patch
A developer asked an agent to repair a problem in this version. The excerpt below shows the request and check results around the agent's first passing patch. The complete source diff at that point follows.
The agent used a separate regression checker with the same logging sequence as the observation program, plus a pass/fail condition. The incident description and printed messages are omitted from this excerpt so you can investigate the output yourself. The exit statuses are from that checker; the observation program above simply prints the messages it retrieves.
The memory sink provides two ways to retrieve history: last_raw(lim) returns message records, while last_formatted(lim) returns their text formatted for display. For both, a positive lim requests at most that many of the newest messages, in their original order; the default value zero requests all retained messages. Reading the history does not remove it.
Conversation
Developer: [Opening incident description omitted.] The attached recent_check.cpp demonstrates it. Please fix the reported failure with a small local change in ringbuffer_sink.h and run the check. We want to minimize changes to shared infrastructure for this patch. Do not change the checker. Tool — before the patch: $ g++ -std=c++17 -Iinclude ../recent_check.cpp -o ../recent_check Exit status: 0 $ ../recent_check [Printed messages omitted.] Exit status: 1 [The agent edits ringbuffer_sink.h; the diff below is the patch at this point.] Tool — after the patch: $ g++ -std=c++17 -Iinclude ../recent_check.cpp -o ../recent_check Exit status: 0 $ ../recent_check [Printed messages omitted.] Exit status: 0 Agent explanation at this point: I changed only ringbuffer_sink.h to keep its bounded ring in a vector and track the oldest entry locally.
First passing patch — include/spdlog/sinks/ringbuffer_sink.h
--- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -22,19 +22,19 @@ { public: explicit ringbuffer_sink(size_t n_items) - : q_{n_items} + : capacity_{n_items} {} std::vector<details::log_msg_buffer> last_raw(size_t lim = 0) { std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); - auto items_available = q_.size(); + auto items_available = items_.size(); auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; std::vector<details::log_msg_buffer> ret; ret.reserve(n_items); for (size_t i = (items_available - n_items); i < items_available; i++) { - ret.push_back(q_.at(i)); + ret.push_back(items_[(head_ + items_available - n_items + i) % capacity_]); } return ret; } @@ -42,14 +42,15 @@ std::vector<std::string> last_formatted(size_t lim = 0) { std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); - auto items_available = q_.size(); + auto items_available = items_.size(); auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; std::vector<std::string> ret; ret.reserve(n_items); for (size_t i = (items_available - n_items); i < items_available; i++) { memory_buf_t formatted; - base_sink<Mutex>::formatter_->format(q_.at(i), formatted); + base_sink<Mutex>::formatter_->format( + items_[(head_ + items_available - n_items + i) % capacity_], formatted); ret.push_back(SPDLOG_BUF_TO_STRING(formatted)); } return ret; @@ -58,12 +59,26 @@ protected: void sink_it_(const details::log_msg &msg) override { - q_.push_back(details::log_msg_buffer{msg}); + if (capacity_ == 0) + { + return; + } + if (items_.size() < capacity_) + { + items_.emplace_back(msg); + } + else + { + items_[head_] = details::log_msg_buffer{msg}; + head_ = (head_ + 1) % capacity_; + } } void flush_() override {} private: - details::circular_q<details::log_msg_buffer> q_; + size_t capacity_; + size_t head_ = 0; + std::vector<details::log_msg_buffer> items_; }; using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
Finding when the behavior changed
Commit ad0e89cbfb4d0c1ce4d097e134eb7be67baebb36 is known to work correctly. A commit between it and main introduced the original failure.
Hint: you can use git bisect to find that commit. Ask your agent to turn the expected behavior you identified into an automated check. The observation program itself does not signal a failure through its exit status.
Recompile the check against each historical revision, without your repair applied. A compilation error is not the behavior you are looking for.
