Skip to content

Commit

Permalink
[logging] remove spdlog
Browse files Browse the repository at this point in the history
1. add simple logger implementation without spdlog
2. update all modules to use new logger interface
  • Loading branch information
Joyjit Daw committed Dec 8, 2020
1 parent 99d49b8 commit a711fca
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 174 deletions.
10 changes: 0 additions & 10 deletions cmake/3rdparty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ if (enable_benchmarks AND NOT TARGET benchmark)
add_subdirectory(3rdparty/benchmark EXCLUDE_FROM_ALL)
endif()

if (NOT TARGET spdlog)
get_property(gw_library_type GLOBAL PROPERTY gw_library_type)
if (${gw_library_type} STREQUAL "SHARED")
set(SPDLOG_BUILD_SHARED ON CACHE BOOL "Build shared object." FORCE)
endif()
# FORCE spdlog to put out an install target, which we need
set(SPDLOG_INSTALL ON CACHE BOOL "Generate the install target." FORCE)
add_subdirectory(3rdparty/spdlog)
endif()

if (NOT TARGET spoa)
add_subdirectory(3rdparty/spoa EXCLUDE_FROM_ALL)
# Don't show warnings when compiling the 3rd party library
Expand Down
2 changes: 1 addition & 1 deletion common/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ add_library(${MODULE_NAME} ${gw_library_type}
src/logging.cpp
src/graph.cpp
)
target_link_libraries(${MODULE_NAME} PUBLIC spdlog ${CUDA_LIBRARIES})
target_link_libraries(${MODULE_NAME} PUBLIC ${CUDA_LIBRARIES})

if (gw_profiling)
find_library(NVTX_LIBRARY nvToolsExt HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64)
Expand Down
96 changes: 26 additions & 70 deletions common/base/include/claraparabricks/genomeworks/logging/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,12 @@

#include <claraparabricks/genomeworks/gw_config.hpp>

#include <cstdint>
#include <string>

/// \ingroup logging
/// \{

/// \brief DEBUG log level
#define gw_log_level_debug 0
/// \brief INFO log level
#define gw_log_level_info 1
/// \brief WARN log level
#define gw_log_level_warn 2
/// \brief ERROR log level
#define gw_log_level_error 3
/// \brief CRITICAL log level
#define gw_log_level_critical 4
/// \brief No logging
#define gw_log_level_off 5

#ifndef GW_LOG_LEVEL
#ifndef NDEBUG
/// \brief Defines the logging level used in the current module
#define GW_LOG_LEVEL gw_log_level_debug
#else // NDEBUG
/// \brief Defines the logging level used in the current module
#define GW_LOG_LEVEL gw_log_level_error
#endif // NDEBUG
#endif // GW_LOG_LEVEL

#if GW_LOG_LEVEL == gw_log_level_info
/// \brief Set log level to INFO
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
#elif GW_LOG_LEVEL == gw_log_level_debug
/// \brief Set log level to DEBUG
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#elif GW_LOG_LEVEL == gw_log_level_warn
/// \brief Set log level to WARN
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_WARN
#elif GW_LOG_LEVEL == gw_log_level_error
/// \brief Set log level to ERROR
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
#elif GW_LOG_LEVEL == gw_log_level_critical
/// \brief Set log level to CRITICAL
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_CRITICAL
#else
/// \brief Set log level to OFF
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF
#endif

// MUST come after the defines of the logging level!
#include <spdlog/spdlog.h>

namespace claraparabricks
{

Expand All @@ -101,64 +58,63 @@ namespace genomeworks

namespace logging
{
/// \ingroup logging
/// Logging status type
enum class LoggingStatus

/// GenomeWorks Logging levels.
enum LogLevel
{
success = 0, ///< Success
cannot_open_file, ///< Initialization could not open the output file requested
cannot_open_stdout ///< Stdout could not be opened for logging
CRITICAL = 0,
ERROR,
WARN,
INFO,
DEBUG
};

/// \ingroup logging
/// Init Initialize the logging
/// \param filename if specified, the path/name of the file into which logging should be placed.
/// The default is stdout
/// \return success or error status
LoggingStatus Init(const char* filename = nullptr);
/// Initialize logger across GenomeWorks.
/// \param [in] level LogLevel for logger.
/// \param [in] filename File to redirect log messages to.
void create_logger(LogLevel level, const std::string& filename = "");

/// \ingroup logging
/// SetHeader Adjust the header/preface for each log message
/// \param logTime if true, the detailed time will be prepended to each message.
/// \param logLocation if true, the file and line location logging will be prepended to each message.
/// \return success or error status
LoggingStatus SetHeader(bool logTime, bool logLocation);
/// Log messages to logger.
/// \param [in] level LogLevel for message.
/// \param [in] file Filename for originating message.
/// \param [in] line Line number for originating message.
/// \param [in] msg Content of log message.
void log(LogLevel level, const std::string& file, int32_t line, const std::string& msg);

/// \ingroup logging
/// \def GW_LOG_DEBUG
/// \brief Log at debug level
///
/// parameters as per https://github.com/gabime/spdlog/blob/v1.x/README.md
#define GW_LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__)
#define GW_LOG_DEBUG(msg) claraparabricks::genomeworks::logging::log(claraparabricks::genomeworks::logging::LogLevel::DEBUG, __FILE__, __LINE__, msg)

/// \ingroup logging
/// \def GW_LOG_INFO
/// \brief Log at info level
///
/// parameters as per https://github.com/gabime/spdlog/blob/v1.x/README.md
#define GW_LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__)
#define GW_LOG_INFO(msg) claraparabricks::genomeworks::logging::log(claraparabricks::genomeworks::logging::LogLevel::INFO, __FILE__, __LINE__, msg)

/// \ingroup logging
/// \def GW_LOG_WARN
/// \brief Log at warning level
///
/// parameters as per https://github.com/gabime/spdlog/blob/v1.x/README.md
#define GW_LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__)
#define GW_LOG_WARN(msg) claraparabricks::genomeworks::logging::log(claraparabricks::genomeworks::logging::LogLevel::WARN, __FILE__, __LINE__, msg)

/// \ingroup logging
/// \def GW_LOG_ERROR
/// \brief Log at error level
///
/// parameters as per https://github.com/gabime/spdlog/blob/v1.x/README.md
#define GW_LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__)
#define GW_LOG_ERROR(msg) claraparabricks::genomeworks::logging::log(claraparabricks::genomeworks::logging::LogLevel::ERROR, __FILE__, __LINE__, msg)

/// \ingroup logging
/// \def GW_LOG_CRITICAL
/// \brief Log at fatal/critical error level (does NOT exit)
///
/// parameters as per https://github.com/gabime/spdlog/blob/v1.x/README.md
#define GW_LOG_CRITICAL(...) SPDLOG_CRITICAL(__VA_ARGS__)

#define GW_LOG_CRITICAL(msg) claraparabricks::genomeworks::logging::log(claraparabricks::genomeworks::logging::LogLevel::CRITICAL, __FILE__, __LINE__, msg)
} // namespace logging

} // namespace genomeworks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class CachingDeviceAllocator
{
if (!memory_resource_)
{
GW_LOG_ERROR("{}\n", "ERROR:: Trying to allocate memory from an default-constructed CachingDeviceAllocator. Please assign a non-default-constructed CachingDeviceAllocator before performing any memory operations.");
GW_LOG_ERROR("ERROR:: Trying to allocate memory from an default-constructed CachingDeviceAllocator. Please assign a non-default-constructed CachingDeviceAllocator before performing any memory operations.");
assert(false);
std::abort();
}
Expand All @@ -291,7 +291,7 @@ class CachingDeviceAllocator
static_cast<void>(n);
if (!memory_resource_)
{
GW_LOG_ERROR("{}\n", "ERROR:: Trying to deallocate memory from an default-constructed CachingDeviceAllocator. Please assign a non-default-constructed CachingDeviceAllocator before performing any memory operations.");
GW_LOG_ERROR("ERROR:: Trying to deallocate memory from an default-constructed CachingDeviceAllocator. Please assign a non-default-constructed CachingDeviceAllocator before performing any memory operations.");
assert(false);
std::abort();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ inline void gpu_assert(cudaError_t code, const char* file, int line)
std::string(cudaGetErrorString(code)) +
" " + std::string(file) +
" " + std::to_string(line);
GW_LOG_ERROR("{}\n", err);
GW_LOG_ERROR(err.c_str());
// In Debug mode, this assert will cause a debugger trap
// which is beneficial when debugging errors.
assert(false);
Expand Down
97 changes: 50 additions & 47 deletions common/base/src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

#include <claraparabricks/genomeworks/logging/logging.hpp>

#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_sinks.h>
#include <iostream>
#include <fstream>
#include <memory>

namespace claraparabricks
{
Expand All @@ -27,66 +28,68 @@ namespace genomeworks

namespace logging
{
static std::shared_ptr<spdlog::logger> logger = nullptr;
static std::unique_ptr<std::ostream> out_stream_ = nullptr;
static std::unique_ptr<std::ofstream> out_file_ = nullptr;

LoggingStatus Init(const char* filename)
static LogLevel level_ = LogLevel::ERROR;

void check_logger()
{
// for now, first call wins:
if (logger != nullptr)
return LoggingStatus::success;
if (out_stream_ == nullptr)
{
std::cerr << "Automatically initializing logger..." << std::endl;
create_logger(LogLevel::ERROR);
}
}

std::string log_level_str(LogLevel level)
{
std::string prefix;
switch (level)
{
case CRITICAL: prefix = "CRITICAL"; break;
case ERROR: prefix = "ERROR"; break;
case WARN: prefix = "WARN"; break;
case INFO: prefix = "INFO"; break;
case DEBUG: prefix = "DEBUG"; break;
default: throw std::runtime_error("Unknown Log Level passed.\n");
}
return prefix;
}

if (filename != nullptr)
void create_logger(LogLevel level, const std::string& filename)
{
if (out_stream_ == nullptr)
{
try
std::streambuf* buffer = nullptr;
level_ = level;
if (filename != "")
{
logger = spdlog::basic_logger_mt("GWLogger", filename);
out_file_ = std::make_unique<std::ofstream>(filename);
buffer = out_file_->rdbuf();
}
catch (const spdlog::spdlog_ex& ex)
else
{
return LoggingStatus::cannot_open_file;
buffer = std::cerr.rdbuf();
}
out_stream_ = std::make_unique<std::ostream>(buffer);
*out_stream_ << "Initialized GenomeWorks logger..." << std::endl;
}
else
{
try
{
logger = spdlog::stderr_logger_mt("GWLogger");
}
catch (const spdlog::spdlog_ex& ex)
{
return LoggingStatus::cannot_open_stdout;
}
*out_stream_ << "Logger already initialized with log level " << log_level_str(level_) << std::endl;
}

spdlog::set_default_logger(logger);

#ifdef _DEBUG
SetHeader(true, true);
#else
SetHeader(false, false);
#endif

spdlog::flush_every(std::chrono::seconds(1));

return LoggingStatus::success;
}

LoggingStatus SetHeader(bool logTime, bool logLocation)
void log(LogLevel level, const std::string& file, int32_t line, const std::string& msg)
{
std::string pattern = "";

if (logTime)
pattern = pattern + "[%H:%M:%S %z]";

if (logLocation)
pattern = pattern + "[%@]";

pattern = pattern + "%v";

spdlog::set_pattern(pattern);

return LoggingStatus::success;
check_logger();
if (level <= level_)
{
std::string prefix = log_level_str(level);
*out_stream_ << "[" << prefix << " " << file << ":" << line << "] " << msg << std::endl;
}
}

} // namespace logging

} // namespace genomeworks
Expand Down
8 changes: 4 additions & 4 deletions cudaaligner/src/aligner_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,27 @@ StatusType AlignerGlobal::add_alignment(const char* query, int32_t query_length,
{
if (query_length < 0 || target_length < 0)
{
GW_LOG_DEBUG("{} {}", "Negative target or query length is not allowed.");
GW_LOG_DEBUG("Negative target or query length is not allowed.");
return StatusType::generic_error;
}

int32_t const max_alignment_length = std::max(max_query_length_, max_target_length_);
int32_t const num_alignments = get_size(alignments_);
if (num_alignments >= max_alignments_)
{
GW_LOG_DEBUG("{} {}", "Exceeded maximum number of alignments allowed : ", max_alignments_);
GW_LOG_DEBUG("Exceeded maximum number of alignments allowed : " + max_alignments_);
return StatusType::exceeded_max_alignments;
}

if (query_length > max_query_length_)
{
GW_LOG_DEBUG("{} {}", "Exceeded maximum length of query allowed : ", max_query_length_);
GW_LOG_DEBUG("Exceeded maximum length of query allowed : " + max_query_length_);
return StatusType::exceeded_max_length;
}

if (target_length > max_target_length_)
{
GW_LOG_DEBUG("{} {}", "Exceeded maximum length of target allowed : ", max_target_length_);
GW_LOG_DEBUG("Exceeded maximum length of target allowed : " + max_target_length_);
return StatusType::exceeded_max_length;
}

Expand Down
2 changes: 1 addition & 1 deletion cudaaligner/src/aligner_global_ukkonen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ StatusType AlignerGlobalUkkonen::add_alignment(const char* query, int32_t query_
int32_t const allocated_max_length_difference = this->get_max_target_length() * max_target_query_length_difference;
if (std::abs(query_length - target_length) > allocated_max_length_difference)
{
GW_LOG_DEBUG("{} {}", "Exceeded maximum length difference b/w target and query allowed : ", allocated_max_length_difference);
GW_LOG_DEBUG("Exceeded maximum length difference b/w target and query allowed : " + allocated_max_length_difference);
return StatusType::exceeded_max_alignment_difference;
}

Expand Down
4 changes: 1 addition & 3 deletions cudaaligner/src/cudaaligner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ namespace cudaaligner

StatusType Init()
{
if (logging::LoggingStatus::success != logging::Init())
return StatusType::generic_error;

create_logger(claraparabricks::genomeworks::logging::LogLevel::WARN);
return StatusType::success;
}
} // namespace cudaaligner
Expand Down
4 changes: 1 addition & 3 deletions cudaextender/src/cudaextender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ namespace cudaextender

StatusType Init()
{
if (logging::LoggingStatus::success != logging::Init())
return StatusType::generic_error;

create_logger(claraparabricks::genomeworks::logging::LogLevel::WARN);
return StatusType::success;
}
} // namespace cudaextender
Expand Down
Loading

0 comments on commit a711fca

Please sign in to comment.