From f055a122305cf306b022bd17de35ab999ba002d1 Mon Sep 17 00:00:00 2001 From: Jiakun Yan Date: Wed, 20 Dec 2023 11:23:06 -0600 Subject: [PATCH] Let LCT Logger also report hostname --- lct/api/lct.h | 5 +++++ lct/lct.cpp | 11 +++++++---- lct/log/logger.cpp | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lct/api/lct.h b/lct/api/lct.h index ea6b95bb..d62712b0 100644 --- a/lct/api/lct.h +++ b/lct/api/lct.h @@ -19,9 +19,14 @@ LCT_API void LCT_init(); LCT_API void LCT_fina(); // rank +extern int LCT_rank; LCT_API void LCT_set_rank(int rank); LCT_API int LCT_get_rank(); +// hostname +#define LCT_HOSTNAME_MAX_LENGTH 128 +extern char LCT_hostname[LCT_HOSTNAME_MAX_LENGTH]; + // time typedef uint64_t LCT_time_t; LCT_API LCT_time_t LCT_now(); diff --git a/lct/lct.cpp b/lct/lct.cpp index f470398e..e19a091b 100644 --- a/lct/lct.cpp +++ b/lct/lct.cpp @@ -3,6 +3,8 @@ #include LCT_API LCT_log_ctx_t LCT_log_ctx_default = nullptr; +LCT_API int LCT_rank = -1; +LCT_API char LCT_hostname[LCT_HOSTNAME_MAX_LENGTH] = "uninitialized"; namespace lct { @@ -14,6 +16,9 @@ void init() // init has been called return; + // initialize hostname + gethostname(LCT_hostname, LCT_HOSTNAME_MAX_LENGTH); + // initialize LCT_log_ctx_default const char* const log_levels[] = { "error", "warn", "diag", "info", "debug", "trace", @@ -49,8 +54,6 @@ void LCT_init() { lct::init(); } void LCT_fina() { lct::fina(); } -int LCTI_rank = -1; - -void LCT_set_rank(int rank) { LCTI_rank = rank; } +void LCT_set_rank(int rank) { LCT_rank = rank; } -int LCT_get_rank() { return LCTI_rank; } \ No newline at end of file +int LCT_get_rank() { return LCT_rank; } \ No newline at end of file diff --git a/lct/log/logger.cpp b/lct/log/logger.cpp index 71ca702f..dea10360 100644 --- a/lct/log/logger.cpp +++ b/lct/log/logger.cpp @@ -91,10 +91,10 @@ struct ctx_t { // do nothing. if (blacklist != nullptr && strstr(blacklist, log_tag) != nullptr) return; // print the log - size = snprintf(buf, sizeof(buf), "%d:%d:%d:%s:%s:%d<%s:%s:%s> ", - LCT_get_rank(), getpid(), LCT_get_thread_id(), file, func, - line, ctx_name.c_str(), log_levels[log_level].c_str(), - log_tag); + size = snprintf(buf, sizeof(buf), "%d:%s:%d:%d:%s:%s:%d<%s:%s:%s> ", + LCT_get_rank(), LCT_hostname, getpid(), LCT_get_thread_id(), + file, func, line, ctx_name.c_str(), + log_levels[log_level].c_str(), log_tag); vsnprintf(buf + size, sizeof(buf) - size, format, vargs);