Skip to content

Commit

Permalink
Let LCT Logger also report hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
JiakunYan committed Dec 20, 2023
1 parent 6bacb6f commit f055a12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lct/api/lct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 7 additions & 4 deletions lct/lct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <unistd.h>

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
{
Expand All @@ -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",
Expand Down Expand Up @@ -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; }
int LCT_get_rank() { return LCT_rank; }
8 changes: 4 additions & 4 deletions lct/log/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f055a12

Please sign in to comment.