Skip to content

Commit c8ffee2

Browse files
committed
Add a thread id to messages
Automatically prepend messages with a thread id. Makes debugging easier.
1 parent b68dd9b commit c8ffee2

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/pal/pal.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,23 @@ namespace snmalloc
168168
Pal::error(msg.get_message());
169169
}
170170

171+
static size_t get_tid()
172+
{
173+
static inline thread_local size_t tid {0};
174+
static inline std::atomic<size_t> tid_source {1};
175+
176+
if (tid == 0)
177+
{
178+
tid = tid_source++;
179+
}
180+
return tid;
181+
}
182+
171183
template<size_t BufferSize, typename... Args>
172184
inline void message(Args... args)
173185
{
174186
MessageBuilder<BufferSize> msg{std::forward<Args>(args)...};
175-
Pal::message(msg.get_message());
187+
MessageBuilder<BufferSize> msg_tid{"{}: {}", get_tid(), msg.get_message()};
188+
Pal::message(msg_tid.get_message());
176189
}
177190
} // namespace snmalloc

0 commit comments

Comments
 (0)