Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions gloo/common/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,39 @@
#include "gloo/common/logging.h"

#include <algorithm>
#include <cstring>
#include <numeric>

namespace gloo {

// Initialize log level from environment variable, and return static value at
// each inquiry.
inline LogLevel logLevel() {
// Global log level. Initialized once.
static LogLevel log_level = LogLevel::UNSET;
if (log_level != LogLevel::UNSET) {
return log_level;
}

const char* level = getenv("GLOO_LOG_LEVEL");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the info logs are currently very important for debugging production jobs. Can we also check LOGLEVEL and use that value if GLOO_LOG_LEVEL isn't set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what and where is LOGLEVEL? I don't have enough context.

// Defaults to WARN.
if (level == nullptr) {
log_level = LogLevel::WARN;
return log_level;
}

if (std::strcmp(level, "DEBUG") == 0) {
log_level = LogLevel::DEBUG;
} else if (std::strcmp(level, "INFO") == 0) {
log_level = LogLevel::INFO;
} else if (std::strcmp(level, "WARN") == 0) {
log_level = LogLevel::WARN;
} else {
log_level = LogLevel::ERROR;
}
return log_level;
}

EnforceNotMet::EnforceNotMet(
const char* file,
const int line,
Expand Down
41 changes: 37 additions & 4 deletions gloo/common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,47 @@

namespace gloo {

enum LogLevel {
ERROR,
WARN,
INFO,
DEBUG,
UNSET,
};

inline LogLevel logLevel();

#define GLOO_LOG_MSG(level, ...) \
std::cerr << ::gloo::MakeString( \
"[", __FILE__, ":", __LINE__, "] ", level, " ", __VA_ARGS__, "\n")

#define GLOO_INFO(...) GLOO_LOG_MSG("INFO", __VA_ARGS__)
#define GLOO_ERROR(...) GLOO_LOG_MSG("ERROR", __VA_ARGS__)
#define GLOO_WARN(...) GLOO_LOG_MSG("WARN", __VA_ARGS__)
#define GLOO_DEBUG(...) // GLOO_LOG_MSG("DEBUG", __VA_ARGS__)
#define GLOO_ERROR(...) \
do { \
if (logLevel() >= LogLevel::ERROR) { \
GLOO_LOG_MSG("ERROR", __VA_ARGS__); \
} \
} while (0)

#define GLOO_WARN(...) \
do { \
if (logLevel() >= LogLevel::WARN) { \
GLOO_LOG_MSG("WARN", __VA_ARGS__); \
} \
} while (0)

#define GLOO_INFO(...) \
do { \
if (logLevel() >= LogLevel::INFO) { \
GLOO_LOG_MSG("INFO", __VA_ARGS__); \
} \
} while (0)

#define GLOO_DEBUG(...) \
do { \
if (logLevel() >= LogLevel::DEBUG) { \
GLOO_LOG_MSG("DEBUG", __VA_ARGS__); \
} \
} while (0)

class EnforceNotMet : public std::exception {
public:
Expand Down
Loading