Skip to content

Commit

Permalink
Merge pull request #674 from rapidsai/branch-22.06
Browse files Browse the repository at this point in the history
[gpuCI] Forward-merge branch-22.06 to branch-22.08 [skip gpuci]
  • Loading branch information
GPUtester authored May 24, 2022
2 parents 6be9c9c + 46eb171 commit e229c52
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 177 deletions.
139 changes: 4 additions & 135 deletions cpp/include/raft/common/detail/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,139 +15,8 @@
*/
#pragma once

#include <stdarg.h>
#pragma message(__FILE__ \
" is deprecated and will be removed in future releases." \
" Please use the <raft/core/logger.hpp> version instead.")

#define SPDLOG_HEADER_ONLY
#include <spdlog/sinks/stdout_color_sinks.h> // NOLINT
#include <spdlog/spdlog.h> // NOLINT

#include <algorithm>

#include <memory>
#include <mutex>
#include <sstream>
#include <string>

#include <raft/common/detail/callback_sink.hpp>

/**
* @defgroup logging levels used in raft
*
* @note exactly match the corresponding ones (but reverse in terms of value)
* in spdlog for wrapping purposes
*
* @{
*/
#define RAFT_LEVEL_TRACE 6
#define RAFT_LEVEL_DEBUG 5
#define RAFT_LEVEL_INFO 4
#define RAFT_LEVEL_WARN 3
#define RAFT_LEVEL_ERROR 2
#define RAFT_LEVEL_CRITICAL 1
#define RAFT_LEVEL_OFF 0
/** @} */

#if !defined(RAFT_ACTIVE_LEVEL)
#define RAFT_ACTIVE_LEVEL RAFT_LEVEL_DEBUG
#endif

namespace spdlog {
class logger;
namespace sinks {
template <class Mutex>
class CallbackSink;
using callback_sink_mt = CallbackSink<std::mutex>;
}; // namespace sinks
}; // namespace spdlog

namespace raft::detail {

/**
* @defgroup CStringFormat Expand a C-style format string
*
* @brief Expands C-style formatted string into std::string
*
* @param[in] fmt format string
* @param[in] vl respective values for each of format modifiers in the string
*
* @return the expanded `std::string`
*
* @{
*/
std::string format(const char* fmt, va_list& vl)
{
char buf[4096];
vsnprintf(buf, sizeof(buf), fmt, vl);
return std::string(buf);
}

std::string format(const char* fmt, ...)
{
va_list vl;
va_start(vl, fmt);
std::string str = format(fmt, vl);
va_end(vl);
return str;
}
/** @} */

int convert_level_to_spdlog(int level)
{
level = std::max(RAFT_LEVEL_OFF, std::min(RAFT_LEVEL_TRACE, level));
return RAFT_LEVEL_TRACE - level;
}

}; // namespace raft::detail

/**
* @defgroup loggerMacros Helper macros for dealing with logging
* @{
*/
#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_TRACE)
#define RAFT_LOG_TRACE(fmt, ...) \
do { \
std::stringstream ss; \
ss << raft::detail::format("%s:%d ", __FILE__, __LINE__); \
ss << raft::detail::format(fmt, ##__VA_ARGS__); \
raft::logger::get().log(RAFT_LEVEL_TRACE, ss.str().c_str()); \
} while (0)
#else
#define RAFT_LOG_TRACE(fmt, ...) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_DEBUG)
#define RAFT_LOG_DEBUG(fmt, ...) \
do { \
std::stringstream ss; \
ss << raft::detail::format("%s:%d ", __FILE__, __LINE__); \
ss << raft::detail::format(fmt, ##__VA_ARGS__); \
raft::logger::get().log(RAFT_LEVEL_DEBUG, ss.str().c_str()); \
} while (0)
#else
#define RAFT_LOG_DEBUG(fmt, ...) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_INFO)
#define RAFT_LOG_INFO(fmt, ...) raft::logger::get().log(RAFT_LEVEL_INFO, fmt, ##__VA_ARGS__)
#else
#define RAFT_LOG_INFO(fmt, ...) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_WARN)
#define RAFT_LOG_WARN(fmt, ...) raft::logger::get().log(RAFT_LEVEL_WARN, fmt, ##__VA_ARGS__)
#else
#define RAFT_LOG_WARN(fmt, ...) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_ERROR)
#define RAFT_LOG_ERROR(fmt, ...) raft::logger::get().log(RAFT_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#else
#define RAFT_LOG_ERROR(fmt, ...) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_CRITICAL)
#define RAFT_LOG_CRITICAL(fmt, ...) raft::logger::get().log(RAFT_LEVEL_CRITICAL, fmt, ##__VA_ARGS__)
#else
#define RAFT_LOG_CRITICAL(fmt, ...) void(0)
#endif
/** @} */
#include <raft/core/logger.hpp>
36 changes: 29 additions & 7 deletions cpp/include/raft/core/cudart_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <iomanip>
#include <iostream>
#include <mutex>
#include <unordered_map>
#include <vector>

///@todo: enable once logging has been enabled in raft
//#include "logger.hpp"
Expand Down Expand Up @@ -294,7 +294,7 @@ void print_host_vector(const char* variable_name,
if (i != 0) out << ",";
out << host_mem[i];
}
out << "];\n";
out << "];" << std::endl;
}

template <class T, class OutStream>
Expand All @@ -303,10 +303,32 @@ void print_device_vector(const char* variable_name,
size_t componentsCount,
OutStream& out)
{
T* host_mem = new T[componentsCount];
CUDA_CHECK(cudaMemcpy(host_mem, devMem, componentsCount * sizeof(T), cudaMemcpyDeviceToHost));
print_host_vector(variable_name, host_mem, componentsCount, out);
delete[] host_mem;
std::vector<T> host_mem(componentsCount);
CUDA_CHECK(
cudaMemcpy(host_mem.data(), devMem, componentsCount * sizeof(T), cudaMemcpyDeviceToHost));
print_host_vector(variable_name, host_mem.data(), componentsCount, out);
}

/**
* @brief Print an array given a device or a host pointer.
*
* @param[in] variable_name
* @param[in] ptr any pointer (device/host/managed, etc)
* @param[in] componentsCount array length
* @param out the output stream
*/
template <class T, class OutStream>
void print_vector(const char* variable_name, const T* ptr, size_t componentsCount, OutStream& out)
{
cudaPointerAttributes attr;
RAFT_CUDA_TRY(cudaPointerGetAttributes(&attr, ptr));
if (attr.hostPointer != nullptr) {
print_host_vector(variable_name, reinterpret_cast<T*>(attr.hostPointer), componentsCount, out);
} else if (attr.type == cudaMemoryTypeUnregistered) {
print_host_vector(variable_name, ptr, componentsCount, out);
} else {
print_device_vector(variable_name, ptr, componentsCount, out);
}
}
/** @} */

Expand Down Expand Up @@ -425,4 +447,4 @@ constexpr T upper_bound()

} // namespace raft

#endif
#endif
31 changes: 16 additions & 15 deletions cpp/include/raft/core/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

namespace raft {

Expand Down Expand Up @@ -126,20 +127,20 @@ struct logic_error : public raft::exception {
* Macro to append error message to first argument.
* This should only be called in contexts where it is OK to throw exceptions!
*/
#define SET_ERROR_MSG(msg, location_prefix, fmt, ...) \
do { \
int size1 = std::snprintf(nullptr, 0, "%s", location_prefix); \
int size2 = std::snprintf(nullptr, 0, "file=%s line=%d: ", __FILE__, __LINE__); \
int size3 = std::snprintf(nullptr, 0, fmt, ##__VA_ARGS__); \
if (size1 < 0 || size2 < 0 || size3 < 0) \
throw raft::exception("Error in snprintf, cannot handle raft exception."); \
auto size = size1 + size2 + size3 + 1; /* +1 for final '\0' */ \
auto buf = std::make_unique<char[]>(size_t(size)); \
std::snprintf(buf.get(), size1 + 1 /* +1 for '\0' */, "%s", location_prefix); \
std::snprintf( \
buf.get() + size1, size2 + 1 /* +1 for '\0' */, "file=%s line=%d: ", __FILE__, __LINE__); \
std::snprintf(buf.get() + size1 + size2, size3 + 1 /* +1 for '\0' */, fmt, ##__VA_ARGS__); \
msg += std::string(buf.get(), buf.get() + size - 1); /* -1 to remove final '\0' */ \
#define SET_ERROR_MSG(msg, location_prefix, fmt, ...) \
do { \
int size1 = std::snprintf(nullptr, 0, "%s", location_prefix); \
int size2 = std::snprintf(nullptr, 0, "file=%s line=%d: ", __FILE__, __LINE__); \
int size3 = std::snprintf(nullptr, 0, fmt, ##__VA_ARGS__); \
if (size1 < 0 || size2 < 0 || size3 < 0) \
throw raft::exception("Error in snprintf, cannot handle raft exception."); \
auto size = size1 + size2 + size3 + 1; /* +1 for final '\0' */ \
std::vector<char> buf(size); \
std::snprintf(buf.data(), size1 + 1 /* +1 for '\0' */, "%s", location_prefix); \
std::snprintf( \
buf.data() + size1, size2 + 1 /* +1 for '\0' */, "file=%s line=%d: ", __FILE__, __LINE__); \
std::snprintf(buf.data() + size1 + size2, size3 + 1 /* +1 for '\0' */, fmt, ##__VA_ARGS__); \
msg += std::string(buf.data(), buf.data() + size - 1); /* -1 to remove final '\0' */ \
} while (0)

/**
Expand Down Expand Up @@ -173,4 +174,4 @@ struct logic_error : public raft::exception {
throw raft::logic_error(msg); \
} while (0)

#endif
#endif
47 changes: 33 additions & 14 deletions cpp/include/raft/core/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#define SPDLOG_HEADER_ONLY
#include <raft/common/detail/callback_sink.hpp>
#include <raft/cudart_utils.h>
#include <spdlog/sinks/stdout_color_sinks.h> // NOLINT
#include <spdlog/spdlog.h> // NOLINT

Expand All @@ -50,14 +51,16 @@
/** @} */

#if !defined(RAFT_ACTIVE_LEVEL)
#define RAFT_ACTIVE_LEVEL RAFT_LEVEL_DEBUG
#define RAFT_ACTIVE_LEVEL RAFT_LEVEL_INFO
#endif

namespace raft {

static const std::string RAFT_NAME = "raft";
static const std::string default_log_pattern("[%L] [%H:%M:%S.%f] %v");

namespace detail {

/**
* @defgroup CStringFormat Expand a C-style format string
*
Expand All @@ -70,14 +73,16 @@ static const std::string default_log_pattern("[%L] [%H:%M:%S.%f] %v");
*
* @{
*/
std::string format(const char* fmt, va_list& vl)
inline std::string format(const char* fmt, va_list& vl)
{
char buf[4096];
vsnprintf(buf, sizeof(buf), fmt, vl);
return std::string(buf);
int length = std::vsnprintf(nullptr, 0, fmt, vl);
assert(length >= 0);
std::vector<char> buf(length + 1);
std::vsnprintf(buf.data(), length + 1, fmt, vl);
return std::string(buf.data());
}

std::string format(const char* fmt, ...)
inline std::string format(const char* fmt, ...)
{
va_list vl;
va_start(vl, fmt);
Expand All @@ -87,12 +92,14 @@ std::string format(const char* fmt, ...)
}
/** @} */

int convert_level_to_spdlog(int level)
inline int convert_level_to_spdlog(int level)
{
level = std::max(RAFT_LEVEL_OFF, std::min(RAFT_LEVEL_TRACE, level));
return RAFT_LEVEL_TRACE - level;
}

} // namespace detail

/**
* @brief The main Logging class for raft library.
*
Expand All @@ -112,7 +119,7 @@ class logger {
cur_pattern()
{
set_pattern(default_log_pattern);
set_level(RAFT_LEVEL_INFO);
set_level(RAFT_ACTIVE_LEVEL);
}
/**
* @brief Singleton method to get the underlying logger object
Expand Down Expand Up @@ -140,7 +147,7 @@ class logger {
*/
void set_level(int level)
{
level = convert_level_to_spdlog(level);
level = raft::detail::convert_level_to_spdlog(level);
spdlogger->set_level(static_cast<spdlog::level::level_enum>(level));
}

Expand Down Expand Up @@ -179,7 +186,7 @@ class logger {
*/
bool should_log_for(int level) const
{
level = convert_level_to_spdlog(level);
level = raft::detail::convert_level_to_spdlog(level);
auto level_e = static_cast<spdlog::level::level_enum>(level);
return spdlogger->should_log(level_e);
}
Expand Down Expand Up @@ -209,13 +216,13 @@ class logger {
*/
void log(int level, const char* fmt, ...)
{
level = convert_level_to_spdlog(level);
level = raft::detail::convert_level_to_spdlog(level);
auto level_e = static_cast<spdlog::level::level_enum>(level);
// explicit check to make sure that we only expand messages when required
if (spdlogger->should_log(level_e)) {
va_list vl;
va_start(vl, fmt);
auto msg = format(fmt, vl);
auto msg = raft::detail::format(fmt, vl);
va_end(vl);
spdlogger->log(level_e, msg);
}
Expand Down Expand Up @@ -256,12 +263,24 @@ class logger {
#define RAFT_LOG_TRACE(fmt, ...) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_TRACE)
#define RAFT_LOG_TRACE_VEC(ptr, len) \
do { \
std::stringstream ss; \
ss << raft::detail::format("%s:%d ", __FILE__, __LINE__); \
print_vector(#ptr, ptr, len, ss); \
raft::logger::get(RAFT_NAME).log(RAFT_LEVEL_TRACE, ss.str().c_str()); \
} while (0)
#else
#define RAFT_LOG_TRACE_VEC(ptr, len) void(0)
#endif

#if (RAFT_ACTIVE_LEVEL >= RAFT_LEVEL_DEBUG)
#define RAFT_LOG_DEBUG(fmt, ...) \
do { \
std::stringstream ss; \
ss << raft::format("%s:%d ", __FILE__, __LINE__); \
ss << raft::format(fmt, ##__VA_ARGS__); \
ss << raft::detail::format("%s:%d ", __FILE__, __LINE__); \
ss << raft::detail::format(fmt, ##__VA_ARGS__); \
raft::logger::get(RAFT_NAME).log(RAFT_LEVEL_DEBUG, ss.str().c_str()); \
} while (0)
#else
Expand Down
Loading

0 comments on commit e229c52

Please sign in to comment.