Skip to content

Commit

Permalink
Replace loguru with spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Pisani committed Dec 12, 2021
1 parent 45edb09 commit d93e59f
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 127 deletions.
19 changes: 2 additions & 17 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ if (APPLE)
endif()

# Download External Dependencies via CPM
CPMAddPackage("gh:fmtlib/fmt#7.1.3")
CPMAddPackage("gh:fmtlib/fmt#8.0.1")
CPMAddPackage("gh:catchorg/[email protected]")
CPMAddPackage("gh:Neargye/[email protected]")
CPMAddPackage("gh:Naios/function2#4.1.0")
CPMAddPackage("gh:gabime/[email protected]")

# We're on version v0.3.0-123-gff67c1a which is not officially tagged in Github
# so we're going to use the SHA directly.
Expand Down Expand Up @@ -72,22 +73,6 @@ if (nlohmann_json_ADDED)
target_include_directories(nlohmann_json SYSTEM INTERFACE "${nlohmann_json_SOURCE_DIR}/include")
endif()

CPMAddPackage(
NAME loguru
GITHUB_REPOSITORY emilk/loguru
VERSION 2.1.0
DOWNLOAD_ONLY YES
GIT_SHALLOW ON
)

if (loguru_ADDED)
add_library(loguru "${loguru_SOURCE_DIR}/loguru.cpp")
target_compile_definitions(loguru PUBLIC "-DLOGURU_USE_FMTLIB=1")
target_link_libraries(loguru PUBLIC fmt)
#target_link_libraries(loguru PUBLIC execinfo)
target_include_directories(loguru SYSTEM PUBLIC "${loguru_SOURCE_DIR}")
endif()

CPMAddPackage(
NAME nanobench
VERSION 4.0.0
Expand Down
32 changes: 32 additions & 0 deletions scripts/yocto-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /bin/bash

ip=$1; shift
binary=${1:-otto}; shift

builddir=oe-workdir/otto-core-1.0.0+git999

sshargs="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=ERROR"

_msg() {
tput setaf 2
tput bold
echo -n "==> "
tput sgr0
tput bold
# tput setaf 2
echo "$@"
tput sgr0
}

_msg "Remounting rootfs read/write"
ssh $sshargs root@$ip mount -o remount,rw /dev/root /
_msg "Stopping OTTO core"
ssh $sshargs root@$ip /etc/init.d/otto-core.sh stop
_msg "Deploying..."
scp $sshargs $builddir/bin/$binary root@$ip:/home/root/otto/bin/$binary
_msg "Remounting rootfs readonly"
ssh $sshargs root@$ip mount -o remount,ro /dev/root /
_msg "Starting OTTO core"
ssh $sshargs root@$ip /etc/init.d/otto-core.sh start
_msg "Done!"

2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ target_link_libraries(otto_src PUBLIC fmt)
target_link_libraries(otto_src PUBLIC gamma)
target_link_libraries(otto_src PUBLIC choreograph)
target_link_libraries(otto_src PUBLIC nlohmann_json)
target_link_libraries(otto_src PUBLIC loguru)
target_link_libraries(otto_src PUBLIC spdlog)
target_link_libraries(otto_src PUBLIC nanobench)
target_link_libraries(otto_src PUBLIC magic_enum)
target_link_libraries(otto_src PUBLIC concurrentqueue)
Expand Down
2 changes: 1 addition & 1 deletion src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace otto {
});
auto stop_input = controller.set_input_handler(layers);

synth.logic->toggle_engine();
// synth.logic->toggle_engine();

auto stop_graphics = graphics.show([&](skia::Canvas& ctx) {
ledman.process(layers);
Expand Down
6 changes: 2 additions & 4 deletions src/app/layers/navigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ namespace otto {
{
if (shift_held) {
return shift_binds_;
} else {
return binds_;
}
return binds_;
}

const std::unordered_map<Key, ScreenWithHandlerPtr>& NavKeyMap::current_binds() const
{
if (shift_held) {
return shift_binds_;
} else {
return binds_;
}
return binds_;
}

void NavKeyMap::bind_nav_key(Key key, ScreenWithHandlerPtr scrn, bool shift_held)
Expand Down
32 changes: 32 additions & 0 deletions src/lib/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,36 @@ namespace otto {
IScreen* screen = nullptr;
IInputLayer* input = nullptr;
};

template<std::derived_from<IScreen> Screen, typename... Args>
requires(std::is_constructible_v<Screen, itc::Context&, Args...>) ScreenWithHandler
make_with_internal_handler(itc::Context& ctx, Args&&... args)
{
struct Handler final : IInputLayer, itc::Sender<KeyPress, KeyRelease, EncoderEvent> {
using Sender::Sender;

void handle(KeyPress e) noexcept override
{
this->send(e);
}
void handle(KeyRelease e) noexcept override
{
this->send(e);
}
void handle(EncoderEvent e) noexcept override
{
this->send(e);
}
[[nodiscard]] KeySet key_mask() const noexcept override
{
return KeySet::make_with_all();
}
};

return {
std::make_unique<Screen>(ctx, std::forward<Args>(args)...),
std::make_unique<Handler>(ctx),
};
}

} // namespace otto
7 changes: 7 additions & 0 deletions src/lib/itc/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
#include "executor.hpp"

namespace otto::itc {
using DomainId = std::uintptr_t;

/// Provide access to an executor. Inherit from this virtually
struct IDomain {
IDomain() noexcept = default;
virtual ~IDomain() noexcept = default;
virtual IExecutor& executor() = 0;

DomainId domain_id()
{
return reinterpret_cast<DomainId>(&executor());
}
};

namespace detail {
Expand Down
16 changes: 16 additions & 0 deletions src/lib/itc/state/producer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include "lib/util/local_vector.hpp"

#include "lib/itc/domain.hpp"

#include "../services/provider.hpp"
#include "state.hpp"

Expand Down Expand Up @@ -70,6 +74,18 @@ namespace otto::itc {
}
}

/// Calls Executor::sync for the executor of each consumer
void sync()
{
util::local_set<IExecutor*, 8> executors;
for (Consumer<State>* c : Provider<state_service<State>>::accessors()) {
executors.insert(c->exec_);
}
for (auto* e : executors) {
e->sync();
}
}

virtual void on_state_change(const State&) {}

private:
Expand Down
32 changes: 11 additions & 21 deletions src/lib/logging.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
#include "logging.hpp"

#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#include "lib/util/exception.hpp"

namespace otto::logging {

using namespace std::literals;

void init(int argc, char* argv[], bool enable_console, const char* logFilePath)
void init(const char* logFilePath)
{
// std::string def_path = Application::current().data_dir / "log.txt";
if (logFilePath == nullptr) {
// logFilePath = def_path.c_str();
}
auto async_logger = spdlog::create_async<spdlog::sinks::stdout_color_sink_mt>("async_logger");

if (!enable_console) {
loguru::g_stderr_verbosity = loguru::Verbosity_OFF;
if (logFilePath != nullptr) {
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilePath, true);
async_logger->sinks().push_back(file_sink);
}

char exc[] = "otto";
char* eptr[] = {exc, nullptr};
if (argc == 0) {
argc = 1;
argv = eptr;
}

loguru::init(argc, argv);
if (logFilePath) loguru::add_file(logFilePath, loguru::Append, loguru::Verbosity_MAX);

loguru::set_fatal_handler([](const loguru::Message& message) {
if (message.prefix != "Signal: "sv) throw util::exception(std::string(message.prefix) + message.message);
});
spdlog::set_default_logger(async_logger);
spdlog::set_error_handler([](const std::string& message) { throw util::exception("{}", message); });

LOGI("Logging initialized");
}

void set_thread_name(const std::string& name)
{
pthread_setname_np(pthread_self(), name.c_str());
loguru::set_thread_name(name.c_str());
}
} // namespace otto::logging
104 changes: 27 additions & 77 deletions src/lib/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,66 @@

#include <fmt/format.h>
#include <fmt/ostream.h>

#define LOGURU_USE_FMTLIB 1
#include <loguru.hpp>
#include <spdlog/spdlog.h>

#include "lib/util/concepts.hpp"
#include "lib/util/macros.hpp"

namespace otto::logging {
void init(int argc = 0, char** argv = nullptr, bool enable_console = true, const char* logFilePath = nullptr);
void init(const char* logFilePath = nullptr);

/// Set how the current thread appears in the log
void set_thread_name(const std::string& name);
} // namespace otto::logging

/// Shorthand to the loguru macro LOG_F(INFO, ...)
#define LOGI(...) VLOG_F(loguru::Verbosity_INFO, __VA_ARGS__)
#define LOGI(...) spdlog::info(__VA_ARGS__)

/// Shorthand to the loguru macro LOG_F(WARNING, ...)
#define LOGW(...) VLOG_F(loguru::Verbosity_WARNING, __VA_ARGS__)
#define LOGW(...) spdlog::warn(__VA_ARGS__)

/// Shorthand to the loguru macro LOG_F(ERROR, ...)
#define LOGE(...) VLOG_F(loguru::Verbosity_ERROR, __VA_ARGS__)
#define LOGE(...) spdlog::error(__VA_ARGS__)

/// Shorthand to the loguru macro LOG_F(FATAL, ...)
#define LOGF(...) VLOG_F(loguru::Verbosity_FATAL, __VA_ARGS__)
#define LOGF(...) spdlog::critical(__VA_ARGS__)

/// Shorthand to the loguru macro DLOG_F(INFO, ...)
#define DLOGI(...) DVLOG_F(loguru::Verbosity_INFO, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_F(WARNING, ...)
#define DLOGW(...) DVLOG_F(loguru::Verbosity_WARNING, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_F(ERROR, ...)
#define DLOGE(...) DVLOG_F(loguru::Verbosity_ERROR, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_F(FATAL, ...)
#define DLOGF(...) DVLOG_F(loguru::Verbosity_FATAL, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_IF_F(INFO, ...)
#define LOGI_IF(...) VLOG_IF_F(loguru::Verbosity_INFO, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_IF_F(WARNING, ...)
#define LOGW_IF(...) VLOG_IF_F(loguru::Verbosity_WARNING, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_IF_F(ERROR, ...)
#define LOGE_IF(...) VLOG_IF_F(loguru::Verbosity_ERROR, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_IF_F(FATAL, ...)
#define LOGF_IF(...) VLOG_IF_F(loguru::Verbosity_FATAL, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_IF_F(INFO, ...)
#define DLOGI_IF(...) DVLOG_IF_F(loguru::Verbosity_INFO, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_IF_F(WARNING, ...)
#define DLOGW_IF(...) DVLOG_IF_F(loguru::Verbosity_WARNING, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_IF_F(ERROR, ...)
#define DLOGE_IF(...) DVLOG_IF_F(loguru::Verbosity_ERROR, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_IF_F(FATAL, ...)
#define DLOGF_IF(...) DVLOG_IF_F(loguru::Verbosity_FATAL, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_SCOPE_F(INFO, ...)
#define LOGI_SCOPE(...) VLOG_SCOPE_F(loguru::Verbosity_INFO, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_SCOPE_F(WARNING, ...)
#define LOGW_SCOPE(...) VLOG_SCOPE_F(loguru::Verbosity_WARNING, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_SCOPE_F(ERROR, ...)
#define LOGE_SCOPE(...) VLOG_SCOPE_F(loguru::Verbosity_ERROR, __VA_ARGS__)

/// Shorthand to the loguru macro LOG_SCOPE_F(FATAL, ...)
#define LOGF_SCOPE(...) VLOG_SCOPE_F(loguru::Verbosity_FATAL, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_SCOPE_F(INFO, ...)
#define DLOGI_SCOPE(...) DVLOG_SCOPE_F(loguru::Verbosity_INFO, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_SCOPE_F(WARNING, ...)
#define DLOGW_SCOPE(...) DVLOG_SCOPE_F(loguru::Verbosity_WARNING, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_SCOPE_F(ERROR, ...)
#define DLOGE_SCOPE(...) DVLOG_SCOPE_F(loguru::Verbosity_ERROR, __VA_ARGS__)

/// Shorthand to the loguru macro DLOG_SCOPE_F(FATAL, ...)
#define DLOGF_SCOPE(...) DVLOG_SCOPE_F(loguru::Verbosity_FATAL, __VA_ARGS__)
#define DLOGI(...) spdlog::debug(__VA_ARGS__)

namespace otto::detail {
template<typename... Args>
inline void handle_assert(const char* file, int line_number, const char* expression, bool assertion) noexcept
{
if (assertion) return;
LOGF("Assertion failed at {}:{}: {}", file, line_number, expression);
}

template<typename... Args>
inline void handle_assert(const char* file,
int line_number,
const char* expression,
bool assertion,
fmt::format_string<Args...> fs,
Args&&... args) noexcept
{
if (assertion) return;
if constexpr (sizeof...(args) > 0) {
LOGF("Assertion failed at {}:{}: {} {}", file, line_number, expression, fmt::format(FWD(args)...));
} else {
LOGF("Assertion failed at {}:{}: {}", file, line_number, expression);
}
LOGF("Assertion failed at {}:{}: {} {}", file, line_number, expression, fmt::format(fs, FWD(args)...));
}

template<typename... Args>
inline void handle_unreachable(const char* file, int line_number) noexcept
{
LOGF("Unreachable code reached at {}:{}", file, line_number);
}

template<typename... Args>
inline void handle_unreachable(const char* file, int line_number, Args&&... args) noexcept
inline void handle_unreachable(const char* file,
int line_number,
fmt::format_string<Args...> fs,
Args&&... args) noexcept
{
if constexpr (sizeof...(args) > 0) {
LOGF("Unreachable code reached at {}:{}: {}", file, line_number, fmt::format(FWD(args)...));
} else {
LOGF("Unreachable code reached at {}:{}", file, line_number);
}
LOGF("Unreachable code reached at {}:{}: {}", file, line_number, fmt::format(fs, FWD(args)...));
}
} // namespace otto::detail

Expand Down
Loading

0 comments on commit d93e59f

Please sign in to comment.