1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 21:35:45 +02:00

logging: get Task names for log messages

When a Task worker thread is executing a Task, the thread name is less
useful than the Task description.

Use the Task description instead of the thread name if the thread has
no BeesThread name and the thread is currently executing a task.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell 2018-01-20 00:54:13 -05:00 committed by Zygo Blaxell
parent fef7aed8fa
commit 5063a635fc

View File

@ -10,6 +10,7 @@
#include <iostream>
#include <memory>
#include <sstream>
// PRIx64
#include <inttypes.h>
@ -136,16 +137,33 @@ BeesNote::set_name(const string &name)
string
BeesNote::get_name()
{
if (tl_name.empty()) {
char buf[100];
memset(buf, '\0', sizeof(buf));
pthread_getname_np(pthread_self(), buf, sizeof(buf));
buf[sizeof(buf) - 1] = '\0';
tl_name = buf;
if (tl_name.empty()) {
tl_name = "bees";
}
if (!tl_name.empty()) {
return tl_name;
}
// Try a Task name. If there is one, return it, but do not
// remember it. Each output message may be a different Task.
// The current task is thread_local so we don't need to worry
// about it being destroyed under us.
auto current_task = Task::current_task();
if (current_task) {
ostringstream oss;
oss << current_task;
return oss.str();
}
// OK try the pthread name next.
char buf[100];
memset(buf, '\0', sizeof(buf));
pthread_getname_np(pthread_self(), buf, sizeof(buf));
buf[sizeof(buf) - 1] = '\0';
tl_name = buf;
// Give up and use a generic name.
if (tl_name.empty()) {
tl_name = "bees";
}
return tl_name;
}