Skip to content

Commit

Permalink
Hide symbols, use fPIC and add define for hiding source loc
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernthedev committed Jan 29, 2024
1 parent 553bbe0 commit eb87fa3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 36 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
# compile options used
add_compile_options(-frtti -fexceptions)
add_compile_options(-O3)
add_compile_options(-fPIE -fPIC -fvisibility=hidden)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# compile definitions used
Expand Down
6 changes: 6 additions & 0 deletions shared/_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
#warning Removing quest modloader dependency
#endif

#define PAPER_EXPORT __attribute__((visibility("default")))
#ifdef __cplusplus
#define PAPER_FUNC extern "C" __attribute__((visibility("default")))
#else
#define PAPER_FUNC MODLOADER_EXPORT
#endif
25 changes: 18 additions & 7 deletions shared/internal_logger.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <utility>
#include "_config.h"
#include "queue/blockingconcurrentqueue.h"
#include "queue/concurrentqueue.h"

Expand Down Expand Up @@ -39,16 +40,26 @@ enum class LogLevel : uint8_t;

using TimePoint = std::chrono::system_clock::time_point;

struct LogData {
struct PAPER_EXPORT LogData {
LogData(LogData const&) = delete;
LogData(LogData&&) = default;



#ifdef PAPER_DISABLE_SOURCE_LOC
explicit LogData() : loc(Paper::sl::no_source()){};

LogData(std::string str, std::thread::id threadId, std::string_view tag, Paper::sl, LogLevel level, TimePoint logTime)
: str(std::move(str)), threadId(threadId), tag(tag), loc(Paper::sl::no_source()), level(level), logTime(logTime) {
}
#else
explicit LogData() : loc(Paper::sl::current()){};

LogData(std::string str, std::thread::id threadId, std::string_view tag, sl const& loc, LogLevel level,
LogData(std::string str, std::thread::id threadId, std::string_view tag, Paper::sl const& loc, LogLevel level,
TimePoint logTime)
: str(std::move(str)), threadId(threadId), tag(tag), loc(loc), level(level), logTime(logTime) {}

#endif

~LogData() = default;

// constexpr ThreadData(fmt::string_view const &str, fmt::format_args const &args, std::thread::id const
Expand Down Expand Up @@ -84,11 +95,11 @@ using ThreadData [[deprecated("Use Paper::Logdata instead!")]] = LogData;
namespace Internal {
void LogThread();

void Queue(LogData&& threadData) noexcept;
void Queue(LogData&& threadData, moodycamel::ProducerToken const& token) noexcept;
moodycamel::ProducerToken MakeProducerToken() noexcept;
PAPER_EXPORT void Queue(LogData&& threadData) noexcept;
PAPER_EXPORT void Queue(LogData&& threadData, moodycamel::ProducerToken const& token) noexcept;
PAPER_EXPORT moodycamel::ProducerToken MakeProducerToken() noexcept;

// [[deprecated("Do not call! Kept for legacy purposes")]]
extern moodycamel::BlockingConcurrentQueue<LogData> logQueue;
extern PAPER_EXPORT moodycamel::BlockingConcurrentQueue<LogData> logQueue;
} // namespace Internal
} // namespace Paper
28 changes: 15 additions & 13 deletions shared/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ static constexpr const std::string_view GLOBAL_TAG = "GLOBAL";

// TODO: Inherit when NDK fixes bug
// https://github.com/android/ndk/issues/1677
template <typename Char, typename... TArgs> struct BasicFmtStrSrcLoc {
template <typename Char, typename... TArgs>
struct BasicFmtStrSrcLoc {
using ParentType = fmt::basic_format_string<Char, TArgs...>;
ParentType parentType;

Expand Down Expand Up @@ -106,7 +107,7 @@ template <typename... Args> using FmtStrSrcLoc = BasicFmtStrSrcLoc<char, fmt::ty
/// This does not give the origianl string without the initial fmt run
using LogSink = std::function<void(LogData const&, std::string_view fmtMessage, std::string_view originalString)>;

struct LoggerConfig {
struct PAPER_EXPORT LoggerConfig {
LoggerConfig() = default;

char lineEnd = '\n';
Expand Down Expand Up @@ -162,36 +163,36 @@ inline void fmtThrowErrorTag(FmtStrSrcLoc<TArgs...> const& str, std::string_view
throw Exception(fmt::format("{} {}", tag, exceptionMsg));
}

void Backtrace(std::string_view tag, uint16_t frameCount);
PAPER_EXPORT void Backtrace(std::string_view tag, uint16_t frameCount);

inline auto Backtrace(uint16_t frameCount) {
return Backtrace(GLOBAL_TAG, frameCount);
}

std::filesystem::path getLogDirectoryPathGlobal();
PAPER_EXPORT std::filesystem::path getLogDirectoryPathGlobal();

void Init(std::string_view logPath);
void Init(std::string_view logPath, LoggerConfig const& config);
bool IsInited();
PAPER_EXPORT void Init(std::string_view logPath);
PAPER_EXPORT void Init(std::string_view logPath, LoggerConfig const& config);
PAPER_EXPORT bool IsInited();

void RegisterFileContextId(std::string_view contextId, std::string_view logPath);
PAPER_EXPORT void RegisterFileContextId(std::string_view contextId, std::string_view logPath);

inline auto RegisterFileContextId(std::string_view contextId) {
return Logger::RegisterFileContextId(contextId, contextId);
}

void UnregisterFileContextId(std::string_view contextId);
PAPER_EXPORT void UnregisterFileContextId(std::string_view contextId);

void WaitForFlush();
PAPER_EXPORT void WaitForFlush();
/**
* @brief Returns a mutable reference to the global configuration.
* NOTE THAT MODIFYING THIS MAY NOT BE UPDATED ON THE CURRENT FLUSH DUE TO RACE CONDITIONS!
*
* @return LoggerConfig& The mutable reference to the global configuration.
**/
LoggerConfig& GlobalConfig();
PAPER_EXPORT LoggerConfig& GlobalConfig();

void AddLogSink(LogSink const& sink);
PAPER_EXPORT void AddLogSink(LogSink const& sink);
}; // namespace Logger

namespace Logger {
Expand Down Expand Up @@ -255,7 +256,8 @@ template <typename Str> struct BaseLoggerContext {
}
};

template <std::size_t sz> struct ConstLoggerContext : public BaseLoggerContext<char[sz]> {
template <std::size_t sz>
struct ConstLoggerContext : public BaseLoggerContext<char[sz]> {
constexpr ConstLoggerContext(char const (&s)[sz]) : BaseLoggerContext<char[sz]>() {
std::copy(s, s + sz, BaseLoggerContext<char[sz]>::tag);
}
Expand Down
3 changes: 3 additions & 0 deletions shared/source_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ struct source_location {
return columnOffset;
}

constexpr static source_location no_source() noexcept {
return source_location("no_source_loc", "no_source_loc", -1, -1);
}
private:
constexpr source_location(char const* fileName, char const* functionName, const uint_least32_t lineNumber,
const uint_least32_t columnOffset) noexcept
Expand Down
32 changes: 16 additions & 16 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#endif

// extern defines
EARLY_INIT_ATTRIBUTE moodycamel::BlockingConcurrentQueue<Paper::LogData> Paper::Internal::logQueue;
EARLY_INIT_ATTRIBUTE PAPER_EXPORT moodycamel::BlockingConcurrentQueue<Paper::LogData> Paper::Internal::logQueue;

#pragma region internals

Expand Down Expand Up @@ -208,12 +208,12 @@ inline void writeLog(Paper::LogData const& threadData, std::tm const& time, std:

#pragma region LoggerImpl

void Paper::Logger::Init(std::string_view logPath) {
PAPER_EXPORT void Paper::Logger::Init(std::string_view logPath) {
LoggerConfig config{};
return Init(logPath, config);
}

void Paper::Logger::Init(std::string_view logPath, LoggerConfig const& config) {
PAPER_EXPORT void Paper::Logger::Init(std::string_view logPath, LoggerConfig const& config) {
if (inited) {
return;
// throw std::runtime_error("Already started the logger thread!");
Expand All @@ -233,26 +233,26 @@ void Paper::Logger::Init(std::string_view logPath, LoggerConfig const& config) {
inited = true;
}

bool Paper::Logger::IsInited() {
PAPER_EXPORT bool Paper::Logger::IsInited() {
return inited;
}

#pragma endregion

#pragma region Internal

void Paper::Internal::Queue(Paper::LogData&& threadData) noexcept {
PAPER_EXPORT void Paper::Internal::Queue(Paper::LogData&& threadData) noexcept {
Internal::logQueue.enqueue(std::forward<Paper::LogData>(threadData));
}

void Paper::Internal::Queue(Paper::LogData&& threadData, moodycamel::ProducerToken const& token) noexcept {
PAPER_EXPORT void Paper::Internal::Queue(Paper::LogData&& threadData, moodycamel::ProducerToken const& token) noexcept {
Internal::logQueue.enqueue(token, std::forward<Paper::LogData>(threadData));
}
moodycamel::ProducerToken Paper::Internal::MakeProducerToken() noexcept {
PAPER_EXPORT moodycamel::ProducerToken Paper::Internal::MakeProducerToken() noexcept {
return moodycamel::ProducerToken(logQueue);
}

void Paper::Internal::LogThread() {
PAPER_EXPORT void Paper::Internal::LogThread() {
try {
moodycamel::ConsumerToken token(Paper::Internal::logQueue);

Expand Down Expand Up @@ -404,16 +404,16 @@ void Paper::Internal::LogThread() {
logInternal(Paper::LogLevel::INF, "Finished log thread");
}

void Paper::Logger::WaitForFlush() {
PAPER_EXPORT void Paper::Logger::WaitForFlush() {
flushSemaphore.acquire();
}

std::filesystem::path Paper::Logger::getLogDirectoryPathGlobal() {
PAPER_EXPORT std::filesystem::path Paper::Logger::getLogDirectoryPathGlobal() {
return globalLogPath;
}

// TODO: Lock?
void Paper::Logger::RegisterFileContextId(std::string_view contextId, std::string_view logPath) {
PAPER_EXPORT void Paper::Logger::RegisterFileContextId(std::string_view contextId, std::string_view logPath) {

auto filePath = getLogDirectoryPathGlobal() / logPath;
filePath.replace_extension(".log");
Expand All @@ -429,15 +429,15 @@ void Paper::Logger::RegisterFileContextId(std::string_view contextId, std::strin
registeredFileContexts.try_emplace(contextId.data(), std::move(f));
}

void Paper::Logger::UnregisterFileContextId(std::string_view contextId) {
PAPER_EXPORT void Paper::Logger::UnregisterFileContextId(std::string_view contextId) {
registeredFileContexts.erase(contextId.data());
}

void Paper::Logger::AddLogSink(LogSink const& sink) {
PAPER_EXPORT void Paper::Logger::AddLogSink(LogSink const& sink) {
sinks.emplace_back(sink);
}

Paper::LoggerConfig& Paper::Logger::GlobalConfig() {
PAPER_EXPORT Paper::LoggerConfig& Paper::Logger::GlobalConfig() {
return globalLoggerConfig;
}

Expand Down Expand Up @@ -472,7 +472,7 @@ size_t captureBacktrace(void** buffer, uint16_t max) {
}
} // namespace

void Paper::Logger::Backtrace(std::string_view const tag, uint16_t frameCount) {
PAPER_EXPORT void Paper::Logger::Backtrace(std::string_view const tag, uint16_t frameCount) {
void* buffer[frameCount + 1];
captureBacktrace(buffer, frameCount + 1);
fmtLogTag<LogLevel::DBG>("Printing backtrace with: {} max lines:", tag, frameCount);
Expand Down Expand Up @@ -503,7 +503,7 @@ void Paper::Logger::Backtrace(std::string_view const tag, uint16_t frameCount) {
#else

#warning No unwind found, compiling stub backtrace function
void Paper::Logger::Backtrace(std::string_view const tag, uint16_t frameCount) {}
PAPER_EXPORT void Paper::Logger::Backtrace(std::string_view const tag, uint16_t frameCount) {}

#endif

Expand Down

0 comments on commit eb87fa3

Please sign in to comment.