Skip to content

Commit

Permalink
Make logger work with cudf
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasr committed Nov 6, 2024
1 parent 335b685 commit 6c52a45
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ rapids_cpm_init()

add_subdirectory(rapids_logger)
if(LOGGING_COMPATIBILITY)
rapids_make_logger(rmm EXPORT_SET rmm-exports SUPPORTS_LOGGING RMM_BACKWARDS_COMPATIBILITY)
rapids_make_logger(
rmm EXPORT_SET rmm-exports VISIBILITY_MACRO "__attribute__((visibility(\"default\")))"
SUPPORTS_LOGGING RMM_BACKWARDS_COMPATIBILITY)
else()
set(_logging_support_flag)
if(SUPPORTS_LOGGING)
set(_logging_support_flag "SUPPORTS_LOGGING")
endif()
rapids_make_logger(rmm EXPORT_SET rmm-exports ${_logging_support_flag})
rapids_make_logger(rmm EXPORT_SET rmm-exports VISIBILITY_MACRO
"__attribute__((visibility(\"default\")))" ${_logging_support_flag})
endif()

include(cmake/thirdparty/get_cccl.cmake)
Expand Down
18 changes: 15 additions & 3 deletions rapids_logger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ Generate a logger implementation customized for the specified namespace.
[LOGGER_TARGET <logger-target-name>]
[LOGGER_HEADER_DIR <header-dir>]
[LOGGER_MACRO_PREFIX <macro-prefix>]
[VISIBILITY_MACRO <visibility-macro>]
[SUPPORTS_LOGGING]
[RMM_BACKWARDS_COMPATIBILITY]
[CUDF_BACKWARDS_COMPATIBILITY]
)

This function produces an interface target named <logger-target-name> that, when linked to by other targets, provides them a header file that defines a logger interface for the specified namespace. The logger is generated from the provided template files and is configured to use the specified namespace and macro prefix. The generated logger is placed in the specified header directory.
Expand All @@ -76,11 +78,17 @@ When not using backwards compatibility mode, the logger implementation (which li
``LOGGER_MACRO_PREFIX``
The prefix to use for the logger macros. If not specified, the macro prefix is the uppercase version of the logger namespace.

``VISIBILITY_MACRO``
The macro to use for visibility annotations. If not specified, no visibility annotations are added to the logger interface.

``SUPPORTS_LOGGING``
If specified, the logger target is configured to support logging. If not specified, the resulting logger is entirely nonfunctional. In that case, all operations are still defined (as no-ops) so that consumers may write the same logging code regardless of whether logging is enabled or disabled. This setting is required for the logger implementation to be generated. It will be automatically turned on if `RMM_BACKWARDS_COMPATIBILITY` is specified.

``RMM_BACKWARDS_COMPATIBILITY``
If specified, the logger target is configured to support backwards compatibility with RMM. In backwards-compatibility mode, the logger implementation is entirely header-only and including it is sufficient for downstream consumers to support logging. In this mode, `SUPPORTS_LOGGING` is always true.
If specified, the logger target is configured to support backwards compatibility with RMM. In backwards-compatibility mode, the logger implementation is entirely header-only and including it is sufficient for downstream consumers to support logging. In this mode, `SUPPORTS_LOGGING` is always true. In addition, the underlying spdlog logger is exposed in public APIs when this flag is enabled (with suitable deprecation warnings).

``CUDF_BACKWARDS_COMPATIBILITY``
If specified, the underlying spdlog logger is exposed in public APIs when this flag is enabled (with suitable deprecation warnings).


Result Targets
Expand Down Expand Up @@ -115,8 +123,9 @@ Example on how to use :cmake:command:`rapids_make_logger`.
function(rapids_make_logger logger_namespace)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids_make_logger")

set(_rapids_options RMM_BACKWARDS_COMPATIBILITY SUPPORTS_LOGGING)
set(_rapids_one_value EXPORT_SET LOGGER_TARGET LOGGER_HEADER_DIR LOGGER_MACRO_PREFIX)
set(_rapids_options RMM_BACKWARDS_COMPATIBILITY CUDF_BACKWARDS_COMPATIBILITY SUPPORTS_LOGGING)
set(_rapids_one_value EXPORT_SET LOGGER_TARGET LOGGER_HEADER_DIR LOGGER_MACRO_PREFIX
VISIBILITY_MACRO)
set(_rapids_multi_value)
cmake_parse_arguments(_RAPIDS "${_rapids_options}" "${_rapids_one_value}"
"${_rapids_multi_value}" ${ARGN})
Expand Down Expand Up @@ -166,6 +175,9 @@ function(rapids_make_logger logger_namespace)
endif()
target_compile_definitions(${_RAPIDS_LOGGER_TARGET} INTERFACE RMM_BACKWARDS_COMPATIBILITY)
endif()
if(_RAPIDS_CUDF_BACKWARDS_COMPATIBILITY)
target_compile_definitions(${_RAPIDS_LOGGER_TARGET} INTERFACE CUDF_BACKWARDS_COMPATIBILITY)
endif()
if(_RAPIDS_SUPPORTS_LOGGING)
target_compile_definitions(${_RAPIDS_LOGGER_TARGET} INTERFACE SUPPORTS_LOGGING)

Expand Down
24 changes: 12 additions & 12 deletions rapids_logger/logger.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* limitations under the License.
*/

// TODO: Add RMM_EXPORT tags or equivalent

#pragma once

#ifdef RMM_BACKWARDS_COMPATIBILITY
#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
#include <spdlog/spdlog.h>
#endif

Expand Down Expand Up @@ -48,7 +46,7 @@ using sink_ptr = std::shared_ptr<sinks::sink>;

#endif

namespace @_RAPIDS_LOGGER_NAMESPACE@ {
namespace @_RAPIDS_VISIBILITY_MACRO@ @_RAPIDS_LOGGER_NAMESPACE@ {

namespace detail {

Expand All @@ -63,6 +61,8 @@ namespace detail {
inline std::string default_log_filename()
{
auto* filename = std::getenv("@_RAPIDS_LOGGER_MACRO_PREFIX@_DEBUG_LOG_FILE");
// TODO: Do we prefer rmm's default (a file rmm_log.txt) or cudf's default (a
// stderr sink)? I think the latter is better.
return (filename == nullptr) ? std::string{"@_RAPIDS_LOGGER_NAMESPACE@_log.txt"} : std::string{filename};
}

Expand Down Expand Up @@ -370,14 +370,14 @@ class logger {
log(level_enum::critical, format, std::forward<Args>(args)...);
}

#ifndef RMM_BACKWARDS_COMPATIBILITY
#ifndef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
private:
#endif
#ifdef SUPPORTS_LOGGING
std::unique_ptr<detail::impl> pImpl{}; ///< pimpl idiom
#endif

#ifdef RMM_BACKWARDS_COMPATIBILITY
#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
private:
#endif
// TODO: When we migrate to C++20 we can use std::format and format strings
Expand Down Expand Up @@ -430,7 +430,7 @@ class logger {

};

#ifdef RMM_BACKWARDS_COMPATIBILITY
#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
namespace detail {

/**
Expand All @@ -440,7 +440,7 @@ namespace detail {
*
* @return logger& The default logger
*/
inline logger& default_logger();
logger& default_logger();

/**
* @brief Get the default logger.
Expand All @@ -449,12 +449,12 @@ inline logger& default_logger();
*
* @return spdlog::logger& The default logger's underlying spdlog logger
*/
inline spdlog::logger& logger();
spdlog::logger& logger();

} // namespace detail
#endif

#ifdef RMM_BACKWARDS_COMPATIBILITY
#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
inline logger& default_logger() { return detail::default_logger(); }

[[deprecated(
Expand Down Expand Up @@ -492,7 +492,7 @@ inline logger& default_logger()
// Macros for easier logging, similar to spdlog.
// TODO: The macros below assume that we want to respect spdlog's level macros
// rather than the ones defined by this file.
#define RMM_LOGGER_CALL(logger, level, ...) (logger).log(level, __VA_ARGS__)
#define @_RAPIDS_LOGGER_MACRO_PREFIX@_LOGGER_CALL(logger, level, ...) (logger).log(level, __VA_ARGS__)

#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
#define @_RAPIDS_LOGGER_MACRO_PREFIX@_LOG_TRACE(...) \
Expand Down Expand Up @@ -536,6 +536,6 @@ inline logger& default_logger()

} // namespace @_RAPIDS_LOGGER_NAMESPACE@

#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
#ifdef RMM_BACKWARDS_COMPATIBILITY
#include "logger_impl/logger_impl.hpp"
#endif
35 changes: 30 additions & 5 deletions rapids_logger/logger_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,29 @@
#include <spdlog/spdlog.h>

#include <memory>
#include <sstream>


namespace @_RAPIDS_LOGGER_NAMESPACE@ {

namespace detail {

level_enum string_to_level(std::string_view const env_lvl_str)
{
if (env_lvl_str == "TRACE") return level_enum::trace;
if (env_lvl_str == "DEBUG") return level_enum::debug;
if (env_lvl_str == "INFO") return level_enum::info;
if (env_lvl_str == "WARN") return level_enum::warn;
if (env_lvl_str == "ERROR") return level_enum::error;
if (env_lvl_str == "CRITICAL") return level_enum::critical;
if (env_lvl_str == "OFF") return level_enum::off;
std::ostringstream os{};
os << "Invalid logging level: " << env_lvl_str;
throw std::invalid_argument(os.str());
}



/**
* @brief The real implementation of the logger using spdlog with a basic file sink.
*/
Expand All @@ -52,7 +70,14 @@ struct impl {
)}
{
underlying.set_pattern("[%6t][%H:%M:%S:%f][%-6l] %v");
flush_on(level_enum::warn);
auto const env_logging_level = std::getenv("@_RAPIDS_LOGGER_MACRO_PREFIX@_DEFAULT_LOGGING_LEVEL");
if (env_logging_level != nullptr) {
set_level(string_to_level(env_logging_level));
}
auto const env_flush_level = std::getenv("@_RAPIDS_LOGGER_MACRO_PREFIX@_DEFAULT_FLUSH_LEVEL");
if (env_flush_level != nullptr) {
flush_on(string_to_level(env_flush_level));
}
}

/**
Expand Down Expand Up @@ -156,7 +181,7 @@ BACKWARDS_COMPAT_INLINE logger::logger(std::string name, std::string filename)

BACKWARDS_COMPAT_INLINE logger::~logger() = default;
BACKWARDS_COMPAT_INLINE logger::logger(logger&&) = default;
#ifdef RMM_BACKWARDS_COMPATIBILITY
#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
BACKWARDS_COMPAT_INLINE class logger& logger::operator=(logger&&) = default;
#else
BACKWARDS_COMPAT_INLINE logger& logger::operator=(logger&&) = default;
Expand All @@ -173,10 +198,10 @@ BACKWARDS_COMPAT_INLINE void logger::add_sink(spdlog::sink_ptr sink) { pImpl->ad
BACKWARDS_COMPAT_INLINE void logger::remove_sink(spdlog::sink_ptr sink) { pImpl->remove_sink(sink); }
BACKWARDS_COMPAT_INLINE level_enum logger::level() const { return pImpl->level(); }

#ifdef RMM_BACKWARDS_COMPATIBILITY
#ifdef @_RAPIDS_LOGGER_MACRO_PREFIX@_BACKWARDS_COMPATIBILITY
namespace detail {

inline class logger& default_logger()
class logger& default_logger()
{
static class logger logger_ = [] {
class logger logger_ {
Expand All @@ -194,7 +219,7 @@ inline class logger& default_logger()
return logger_;
}

inline class spdlog::logger& logger() { return default_logger().pImpl->underlying; }
class spdlog::logger& logger() { return default_logger().pImpl->underlying; }

} // namespace detail
#endif
Expand Down

0 comments on commit 6c52a45

Please sign in to comment.