Skip to content

Commit

Permalink
Fix linking against spdlog (#5125)
Browse files Browse the repository at this point in the history
  • Loading branch information
theartful authored Dec 8, 2020
1 parent 3ae4f47 commit 9b8d856
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 64 deletions.
11 changes: 0 additions & 11 deletions cmake/Findspdlog.cmake

This file was deleted.

17 changes: 13 additions & 4 deletions src/shogun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,27 @@ IF (NOT HAVE_STD_VARIANT)
ENDIF()

# spdlog
FIND_PACKAGE(spdlog CONFIG)
FIND_PACKAGE(spdlog 1.3.1 CONFIG)
IF (NOT spdlog_FOUND)
include(external/spdlog)
SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM
$<BUILD_INTERFACE:${spdlog_INCLUDE_DIR}>
$<INSTALL_INTERFACE:include/shogun/third_party>
)
ELSE()
SET(USE_EXTERNAL_SPDLOG 1)
target_link_libraries(shogun PRIVATE spdlog::spdlog)
get_target_property(
spdlog_INCLUDE_DIR spdlog::spdlog INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(
spdlog_COMPILE_DEFINITIONS spdlog::spdlog INTERFACE_COMPILE_DEFINITIONS)

target_include_directories(libshogun PRIVATE ${spdlog_INCLUDE_DIR})
if (spdlog_COMPILE_DEFINITIONS)
target_compile_definitions(libshogun PRIVATE ${spdlog_COMPILE_DEFINITIONS})
endif()

target_link_libraries(shogun PUBLIC spdlog::spdlog)
if (EXISTS shogun-static)
target_link_libraries(shogun-static PRIVATE spdlog::spdlog)
target_link_libraries(shogun-static PUBLIC spdlog::spdlog)
endif()
ENDIF()

Expand Down
30 changes: 12 additions & 18 deletions src/shogun/base/progress.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ namespace shogun
// progress_bar_space > 0,
// "Not enough terminal space to show the progress bar!");

char str[1000];
float64_t runtime = Time::get_curtime();

if (difference > 0.0)
Expand Down Expand Up @@ -243,17 +242,15 @@ namespace shogun

if (estimate > 120)
{
snprintf(
str, sizeof(str),
" {:1.1f} minutes remaining {:1.1f} minutes total\r");
io::print(str, estimate / 60, total_estimate / 60);
io::print(
" {:1.1f} minutes remaining {:1.1f} minutes total\r",
estimate / 60, total_estimate / 60);
}
else
{
snprintf(
str, sizeof(str),
" {:1.1f} seconds remaining {:1.1f} seconds total\r");
io::print(str, estimate, total_estimate);
io::print(
" {:1.1f} seconds remaining {:1.1f} seconds total\r",
estimate, total_estimate);
}
}

Expand Down Expand Up @@ -292,7 +289,6 @@ namespace shogun
// progress_bar_space > 0,
// "Not enough terminal space to show the progress bar!");

char str[1000];
float64_t runtime = Time::get_curtime();

if (difference > 0.0)
Expand Down Expand Up @@ -338,17 +334,15 @@ namespace shogun

if (estimate > 120)
{
snprintf(
str, sizeof(str),
" {:1.1f} minutes remaining {:1.1f} minutes total\r");
io::print(str, estimate / 60, total_estimate / 60);
io::print(
" {:1.1f} minutes remaining {:1.1f} minutes total\r",
estimate / 60, total_estimate / 60);
}
else
{
snprintf(
str, sizeof(str),
" {:1.1f} seconds remaining {:1.1f} seconds total\r");
io::print(str, estimate, total_estimate);
io::print(
" {:1.1f} seconds remaining {:1.1f} seconds total\r",
estimate, total_estimate);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/shogun/io/SGIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
#include <spdlog/sinks/base_sink.h>
#include <spdlog/sinks/null_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/details/fmt_helper.h>
#include <spdlog/version.h>
#if ((SPDLOG_VER_MAJOR == 1) && (SPDLOG_VER_MINOR >= 6))
#include <spdlog/pattern_formatter.h>
#else
#include <spdlog/details/pattern_formatter.h>
#endif

using namespace shogun;
using namespace shogun::io;
Expand Down
45 changes: 25 additions & 20 deletions src/shogun/io/SGIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define __SGIO_H__

#include <shogun/base/ShogunEnv.h>
#include <shogun/io/fmt/fmt.h>
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>
#include <shogun/lib/exception/ShogunException.h>
Expand All @@ -25,6 +24,8 @@
#include <unistd.h>
#endif

#include <spdlog/fmt/fmt.h>

namespace spdlog
{
class logger;
Expand Down Expand Up @@ -233,7 +234,7 @@ namespace shogun
template <typename... Args>
void message(
EMessageType prio, const SourceLocation& loc,
const char* format, const Args&... args) const;
const char* format, Args&&... args) const;

/**
* Return a C string from the substring
Expand Down Expand Up @@ -301,32 +302,35 @@ namespace shogun
template <typename... Args>
void SGIO::message(
EMessageType prio, const SourceLocation& loc, const char* format,
const Args&... args) const
Args&&... args) const
{
if (should_log(prio))
{
fmt::memory_buffer msg;
fmt::format_to(msg, format, args...);
fmt::format_to(msg, format, std::forward<Args>(args)...);
message_(prio, loc, fmt::string_view(msg.data(), msg.size()));
}
}

template <typename... Args>
static inline void print(const char* format, const Args&... args)
static inline void print(const char* format, Args&&... args)
{
env()->io()->message(MSG_MESSAGEONLY, {}, format, args...);
env()->io()->message(
MSG_MESSAGEONLY, {}, format, std::forward<Args>(args)...);
}

template <typename... Args>
static inline void info(const char* format, const Args&... args)
static inline void info(const char* format, Args&&... args)
{
env()->io()->message(MSG_INFO, {}, format, args...);
env()->io()->message(
MSG_INFO, {}, format, std::forward<Args>(args)...);
}

template <typename... Args>
static inline void warn(const char* format, const Args&... args)
static inline void warn(const char* format, Args&&... args)
{
env()->io()->message(MSG_WARN, {}, format, args...);
env()->io()->message(
MSG_WARN, {}, format, std::forward<Args>(args)...);
}

static inline void progress_done()
Expand All @@ -341,8 +345,8 @@ namespace shogun

#ifndef SWIG
template <typename ExceptionType = ShogunException, typename... Args>
static inline void error(
const io::SourceLocation& loc, const char* format, const Args&... args)
[[noreturn]] static inline void
error(const io::SourceLocation& loc, const char* format, Args&&... args)
{
// help clang static analyzer to identify custom assertation functions
#ifdef __clang_analyzer__
Expand All @@ -353,47 +357,48 @@ namespace shogun
"ExceptionType must be nothrow copy constructible");

fmt::memory_buffer msg;
fmt::format_to(msg, format, args...);
fmt::format_to(msg, format, std::forward<Args>(args)...);
msg.push_back('\0');
env()->io()->message(io::MSG_ERROR, loc, msg.data());
throw ExceptionType(msg.data());
#endif /* __clang_analyzer__ */
}

template <typename ExceptionType = ShogunException, typename... Args>
static inline void error(const char* format, const Args&... args)
[[noreturn]] static inline void error(const char* format, Args&&... args)
{
error<ExceptionType>(io::SourceLocation{}, format, args...);
error<ExceptionType>(
io::SourceLocation{}, format, std::forward<Args>(args)...);
}

template <
typename ExceptionType = ShogunException, typename Condition,
typename... Args>
static inline void
require(const Condition& condition, const char* format, const Args&... args)
require(const Condition& condition, const char* format, Args&&... args)
{
if (SG_UNLIKELY(!condition))
{
error<ExceptionType>(format, args...);
error<ExceptionType>(format, std::forward<Args>(args)...);
}
}

/** print error message 'not implemented' */
static inline void not_implemented(const io::SourceLocation& loc = {})
[[noreturn]] static inline void
not_implemented(const io::SourceLocation& loc = {})
{
error<ShogunException>(loc, "Sorry, not yet implemented.");
}

/** print error message 'Only available with GPL parts.' */
static inline void gpl_only(const io::SourceLocation& loc = {})
[[noreturn]] static inline void gpl_only(const io::SourceLocation& loc = {})
{
error<ShogunException>(
loc, "This feature is only "
"available if Shogun is built "
"with GPL codes.");
}


static inline void unstable(const io::SourceLocation& loc = {})
{
env()->io()->message(
Expand Down
10 changes: 0 additions & 10 deletions src/shogun/io/fmt/fmt.h

This file was deleted.

1 change: 0 additions & 1 deletion src/shogun/lib/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
/* does the compiler support abi::__cxa_demangle */
#cmakedefine HAVE_CXA_DEMANGLE 1

#cmakedefine USE_EXTERNAL_SPDLOG 1
#cmakedefine DEBUG_BUILD 1

/* parameter backend */
Expand Down

0 comments on commit 9b8d856

Please sign in to comment.