From 5063a635fcb7d84ef88d7499e9e1559d6087fe7f Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sat, 20 Jan 2018 00:54:13 -0500 Subject: [PATCH] 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 --- src/bees.cc | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/bees.cc b/src/bees.cc index 1c4c165..8056121 100644 --- a/src/bees.cc +++ b/src/bees.cc @@ -10,6 +10,7 @@ #include #include +#include // PRIx64 #include @@ -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; }