diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..631303f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +# Не включать эти папки в GitHub source archive (tar.gz/zip) +vcpkg-overlay/ export-ignore +.github/ export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0d4d703 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,98 @@ +name: CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + linux: + runs-on: ubuntu-latest + strategy: + matrix: + std: [11, 17] + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - run: git submodule update --init --recursive + - name: Configure + run: cmake -S . -B build -DLOG_IT_CPP_BUILD_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} + - name: Build + run: cmake --build build + - name: Install + run: cmake --install build --prefix install + - name: Test + run: ctest --test-dir build + - name: Configure consumer project + run: cmake -S tests/install_consumer -B build-consumer -DCMAKE_PREFIX_PATH=${{ github.workspace }}/install -DCMAKE_CXX_STANDARD=${{ matrix.std }} + - name: Build consumer project + run: cmake --build build-consumer + + windows: + runs-on: windows-latest + strategy: + matrix: + std: [11, 17] + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - run: git submodule update --init --recursive + - name: Configure + run: cmake -S . -B build -DLOG_IT_CPP_BUILD_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} + - name: Build + run: cmake --build build --config Release + - name: Install + run: cmake --install build --prefix install --config Release + - name: Test + run: ctest --test-dir build -C Release + - name: Configure consumer project + run: cmake -S tests/install_consumer -B build-consumer -DCMAKE_PREFIX_PATH="${{ github.workspace }}/install" -DCMAKE_CXX_STANDARD=${{ matrix.std }} + - name: Build consumer project + run: cmake --build build-consumer --config Release + + macos: + runs-on: macos-latest + strategy: + matrix: + std: [11, 17] + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - run: git submodule update --init --recursive + - name: Configure + run: cmake -S . -B build -DLOG_IT_CPP_BUILD_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} + - name: Build + run: cmake --build build + - name: Install + run: cmake --install build --prefix install + - name: Test + run: ctest --test-dir build + - name: Configure consumer project + run: cmake -S tests/install_consumer -B build-consumer -DCMAKE_PREFIX_PATH=${{ github.workspace }}/install -DCMAKE_CXX_STANDARD=${{ matrix.std }} + - name: Build consumer project + run: cmake --build build-consumer + + vcpkg-install: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - run: git submodule update --init --recursive + - name: Install vcpkg + run: | + git clone https://github.com/microsoft/vcpkg.git + ./vcpkg/bootstrap-vcpkg.sh -disableMetrics + - name: Validate port + run: | + ./vcpkg/vcpkg install log-it-cpp \ + --overlay-ports=vcpkg-overlay/ports \ + --overlay-ports=libs/time-shield-cpp/vcpkg-overlay/ports + - name: Configure consumer project + run: cmake -B build -S tests/install_consumer -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake + - name: Build consumer project + run: cmake --build build diff --git a/CMakeLists.txt b/CMakeLists.txt index 40c5412..ed53776 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,71 +1,70 @@ -# When generating a Code::Blocks project with MinGW, you can use: -# cmake -G "CodeBlocks - MinGW Makefiles" -S . -B build-cb cmake_minimum_required(VERSION 3.18) -project(time_shield LANGUAGES CXX) +project(log-it-cpp VERSION 1.0.0 LANGUAGES CXX) -if(MINGW OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(COMMON_WARN_FLAGS -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow) -endif() - -if(MSVC) - set(COMMON_WARN_FLAGS /W4 /wd4996) -endif() +option(LOG_IT_CPP_BUILD_TESTS "Build log-it-cpp tests" ${PROJECT_IS_TOP_LEVEL}) +option(LOG_IT_CPP_BUILD_EXAMPLES "Build log-it-cpp examples" OFF) if(NOT DEFINED CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 11) endif() set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Absolute path to the project root (where CMakeLists.txt resides) -get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE) +# Dependency: TimeShield +find_package(TimeShield 1.0.3 QUIET CONFIG) +if(NOT TimeShield_FOUND) + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/libs/time-shield-cpp/CMakeLists.txt") + add_subdirectory(libs/time-shield-cpp) + else() + message(FATAL_ERROR "TimeShield not found. Please install it or add as submodule.") + endif() +endif() -# Define the LOGIT_BASE_PATH macro -add_compile_definitions(LOGIT_BASE_PATH="${PROJECT_ROOT}") +add_library(log-it-cpp INTERFACE) -# Include and library paths -set(PROJECT_INCLUDE_DIRS - ${CMAKE_CURRENT_SOURCE_DIR}/include/logit_cpp - ${CMAKE_CURRENT_SOURCE_DIR}/libs/fmt/include - ${CMAKE_CURRENT_SOURCE_DIR}/libs/time-shield-cpp/include/time_shield_cpp -) +add_compile_definitions(LOGIT_BASE_PATH="$") -# Header files from include/ -file(GLOB_RECURSE PROJECT_HEADERS - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - include/*.hpp +target_include_directories(log-it-cpp INTERFACE + $ + $ ) -# Create a virtual target for headers so IDEs show them -add_custom_target(project_headers SOURCES ${PROJECT_HEADERS}) +target_link_libraries(log-it-cpp INTERFACE time_shield::time_shield) -# Locate all .cpp files in examples/ -file(GLOB EXAMPLES_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} examples/*.cpp) +if(LOG_IT_CPP_BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() -# Create a separate target for each example -foreach(example_src ${EXAMPLES_SOURCES}) - get_filename_component(example_name ${example_src} NAME_WE) +if(LOG_IT_CPP_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() - add_executable(${example_name} ${example_src}) +include(CMakePackageConfigHelpers) - target_include_directories(${example_name} PRIVATE ${PROJECT_INCLUDE_DIRS}) - - if(COMMON_WARN_FLAGS) - target_compile_options(${example_name} PRIVATE ${COMMON_WARN_FLAGS}) - endif() +install(DIRECTORY include/ DESTINATION include) - target_link_directories(${example_name} PRIVATE ${PROJECT_LIBRARY_DIRS}) - target_compile_definitions(${example_name} PRIVATE ${PROJECT_DEFINES}) - target_link_libraries(${example_name} PRIVATE ${PROJECT_LIBS}) - - # Add headers as SOURCES so IDEs display them - set_source_files_properties(${PROJECT_HEADERS} PROPERTIES HEADER_FILE_ONLY ON) - target_sources(${example_name} PRIVATE ${PROJECT_HEADERS}) - - foreach(dll ${DLL_FILES}) - add_custom_command(TARGET ${example_name} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${dll}" - "$" - ) - endforeach() -endforeach() +install(TARGETS log-it-cpp EXPORT log-it-cppTargets) + +install(EXPORT log-it-cppTargets + FILE log-it-cppTargets.cmake + NAMESPACE log-it-cpp:: + DESTINATION lib/cmake/log-it-cpp +) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/log-it-cppConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion +) + +configure_package_config_file( + cmake/log-it-cppConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/log-it-cppConfig.cmake" + INSTALL_DESTINATION lib/cmake/log-it-cpp +) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/log-it-cppConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/log-it-cppConfigVersion.cmake" + DESTINATION lib/cmake/log-it-cpp +) diff --git a/cmake/log-it-cppConfig.cmake.in b/cmake/log-it-cppConfig.cmake.in new file mode 100644 index 0000000..b4190ca --- /dev/null +++ b/cmake/log-it-cppConfig.cmake.in @@ -0,0 +1,6 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) +find_dependency(TimeShield) + +include("${CMAKE_CURRENT_LIST_DIR}/log-it-cppTargets.cmake") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..89facfe --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,6 @@ +file(GLOB EXAMPLES_SOURCES *.cpp) +foreach(example_src ${EXAMPLES_SOURCES}) + get_filename_component(example_name ${example_src} NAME_WE) + add_executable(${example_name} ${example_src}) + target_link_libraries(${example_name} PRIVATE log-it-cpp) +endforeach() diff --git a/include/logit_cpp/logit/utils/path_utils.hpp b/include/logit_cpp/logit/utils/path_utils.hpp index 8a9afc3..5d1881d 100644 --- a/include/logit_cpp/logit/utils/path_utils.hpp +++ b/include/logit_cpp/logit/utils/path_utils.hpp @@ -19,8 +19,17 @@ #include #include #include +#elif defined(__APPLE__) +// For macOS systems +#include +#include +#include +#include +#include +#include +#include #else -// For POSIX systems +// For other POSIX systems #include #include #include @@ -95,6 +104,24 @@ namespace logit { // Convert from std::wstring (UTF-16) to std::string (UTF-8) std::wstring_convert> converter; return converter.to_bytes(exe_path); +# elif defined(__APPLE__) + uint32_t size = 0; + _NSGetExecutablePath(nullptr, &size); + std::vector path(size); + if (_NSGetExecutablePath(path.data(), &size) != 0) { + throw std::runtime_error("Failed to get executable path."); + } + std::string exe_path(path.data()); + char resolved[PATH_MAX]; + if (realpath(exe_path.c_str(), resolved) == nullptr) { + throw std::runtime_error("Failed to resolve executable path."); + } + exe_path = resolved; + size_t pos = exe_path.find_last_of("\\/"); + if (pos != std::string::npos) { + exe_path = exe_path.substr(0, pos); + } + return exe_path; # else char result[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..fe9a0a6 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,7 @@ +file(GLOB TEST_SOURCES *.cpp) +foreach(test_src ${TEST_SOURCES}) + get_filename_component(test_name ${test_src} NAME_WE) + add_executable(${test_name} ${test_src}) + target_link_libraries(${test_name} PRIVATE log-it-cpp) + add_test(NAME ${test_name} COMMAND ${test_name}) +endforeach() diff --git a/tests/file_logger_test.cpp b/tests/file_logger_test.cpp new file mode 100644 index 0000000..15b222f --- /dev/null +++ b/tests/file_logger_test.cpp @@ -0,0 +1,16 @@ +#define LOGIT_FILE_LOGGER_PATH "." +#include +#include +#include + +int main() { + LOGIT_ADD_FILE_LOGGER_DEFAULT(); + const std::string message = "test log message"; + LOGIT_INFO(message); + LOGIT_WAIT(); + const std::string log_path = LOGIT_GET_LAST_FILE_PATH(0); + std::ifstream in(log_path); + std::string line; + std::getline(in, line); + return line.find(message) != std::string::npos ? 0 : 1; +} diff --git a/tests/install_consumer/CMakeLists.txt b/tests/install_consumer/CMakeLists.txt new file mode 100644 index 0000000..a27006f --- /dev/null +++ b/tests/install_consumer/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.18) +project(install_consumer LANGUAGES CXX) + +find_package(log-it-cpp CONFIG REQUIRED) + +add_executable(install_consumer main.cpp) +target_link_libraries(install_consumer PRIVATE log-it-cpp::log-it-cpp) diff --git a/tests/install_consumer/main.cpp b/tests/install_consumer/main.cpp new file mode 100644 index 0000000..b604ffa --- /dev/null +++ b/tests/install_consumer/main.cpp @@ -0,0 +1,9 @@ +#define LOGIT_SHORT_NAME +#include + +int main() { + LOGIT_ADD_CONSOLE_DEFAULT(); + LOG_INFO("consumer works"); + LOGIT_WAIT(); + return 0; +} diff --git a/vcpkg-overlay/ports/log-it-cpp/portfile.cmake b/vcpkg-overlay/ports/log-it-cpp/portfile.cmake new file mode 100644 index 0000000..a6e80af --- /dev/null +++ b/vcpkg-overlay/ports/log-it-cpp/portfile.cmake @@ -0,0 +1,24 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO NewYaroslav/log-it-cpp + REF a5ef55f060f720063bbbb5c9dfdb57086d618e96 + SHA512 1a5a418fcae653ab9731068a21c17a56577f98162d2c292418f60d2722ef3075c83f065c4b6115979dd1f38dd54d50f2ecb635ac0049cafd3cfc2a33aa708fff + HEAD_REF main +) + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" + OPTIONS -DLOG_IT_CPP_BUILD_TESTS=OFF +) + +vcpkg_cmake_install() + +vcpkg_cmake_config_fixup(PACKAGE_NAME log-it-cpp CONFIG_PATH lib/cmake/log-it-cpp) + +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") + +file(REMOVE_RECURSE + "${CURRENT_PACKAGES_DIR}/debug/include" + "${CURRENT_PACKAGES_DIR}/debug/lib" + "${CURRENT_PACKAGES_DIR}/debug/share/${PORT}" +) diff --git a/vcpkg-overlay/ports/log-it-cpp/vcpkg.json b/vcpkg-overlay/ports/log-it-cpp/vcpkg.json new file mode 100644 index 0000000..a38ab21 --- /dev/null +++ b/vcpkg-overlay/ports/log-it-cpp/vcpkg.json @@ -0,0 +1,11 @@ +{ + "name": "log-it-cpp", + "version-string": "1.0.0", + "description": "LogIt++ logging library", + "homepage": "https://github.com/NewYaroslav/log-it-cpp", + "dependencies": [ + "vcpkg-cmake", + "vcpkg-cmake-config", + "time-shield-cpp" + ] +}