Skip to content

Commit

Permalink
Adding CI to the project to verify build under linux, windows and macos
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaa committed Mar 9, 2024
1 parent fddb991 commit 343cc7d
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 15 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: CI

on:
push:
pull_request:
schedule:
- cron: '0 0 1 * *' # This line schedules the workflow to run at 00:00 on the first day of every month

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-latest
compiler: gcc
# - os: ubuntu-latest
# compiler: clang
# - os: windows-latest
# compiler: msvc
# - os: macos-latest
# compiler: clang

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
submodules: true

- name: Cache
uses: actions/cache@v3
with:
path: |
~/vcpkg
./build/vcpkg_installed
${{ env.HOME }}/.cache/vcpkg/archives
${{ env.XDG_CACHE_HOME }}/vcpkg/archives
${{ env.LOCALAPPDATA }}\vcpkg\archives
${{ env.APPDATA }}\vcpkg\archives
key: ${{ runner.os }}-${{ matrix.compiler }}-${{ env.BUILD_TYPE }}-${{ hashFiles('**/CMakeLists.txt') }}-${{ hashFiles('./vcpkg.json')}}
restore-keys: |
${{ runner.os }}-${{ env.BUILD_TYPE }}-
- name: Setup Cpp
uses: aminya/setup-cpp@v1
with:
compiler: ${{ matrix.compiler }}
vcvarsall: ${{ contains(matrix.os, 'windows') }}
cmake: true
ninja: true
vcpkg: true
cppcheck: false

- name: Prepare the PATH
run: |
if [[ "${{ runner.os }}" == "Windows" ]]; then
echo "$env:USERPROFILE\vcpkg" >> $GITHUB_PATH
echo "$env:USERPROFILE\ninja" >> $GITHUB_PATH
else
echo "$HOME/vcpkg" >> $GITHUB_PATH
echo "$HOME/ninja" >> $GITHUB_PATH
fi
shell: bash

- name: Install dependencies
run: |
vcpkg install
- name: Build project
shell: bash
run: |
if [ -d build ]; then
echo "Build dir exists"
ls -la build
else
mkdir build
#rm -rf build/*
fi
pushd build
cmake .. -DCMAKE_BUILD_TYPE=Release -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build .
popd
- name: Run tests
run: |
pushd build
ctest -C Release
popd
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)

project(logfault VERSION 0.3.1 LANGUAGES CXX)
project(logfault
DESCRIPTION "Simple to use, header only C++ library for application-logging on all major platforms."
HOMEPAGE_URL https://github.com/jgaa/logfault
VERSION 0.5.0
LANGUAGES CXX)

include(GNUInstallDirs)

set(LOGFAULT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

message(STATUS "Using ${CMAKE_CXX_COMPILER}")

macro(SET_CPP_STANDARD target)
Expand All @@ -19,8 +26,9 @@ include_directories(
option(LOGFAULT_BUILD_TESTS "Build tests" ON)

if(LOGFAULT_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
find_package(GTest REQUIRED)
enable_testing()
add_subdirectory(tests)
endif()

add_library(${PROJECT_NAME} INTERFACE)
Expand Down
8 changes: 7 additions & 1 deletion include/logfault/logfault.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,16 @@ Home: https://github.com/jgaa/logfault
}
# define LOGFAULT_THREAD_NAME logfault_get_thread_name_()
#
# elif defined(LOGFAULT_USE_TID_AS_NAME)
# elif defined(LOGFAULT_USE_TID_AS_NAME) && defined(__linux__)
# include <unistd.h>
# include <sys/syscall.h>
# define LOGFAULT_THREAD_NAME syscall(__NR_gettid)
# elif defined(LOGFAULT_USE_TID_AS_NAME) && defined(__unix__)
# include <pthread.h>
# define LOGFAULT_THREAD_NAME pthread_self()
# elif defined(LOGFAULT_USE_TID_AS_NAME) && defined(WIN32)
# include <windows.h>
# define LOGFAULT_THREAD_NAME GetCurrentThreadId()
# else
# define LOGFAULT_THREAD_NAME std::this_thread::get_id()
# endif
Expand Down
66 changes: 55 additions & 11 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,61 @@

option(WITH_QT "Enable QT" OFF)

if (WITH_QT)
add_definitions(-DLOGFAULT_USE_QT_LOG=1)
find_package(Qt6 6.4 REQUIRED COMPONENTS Test)
qt_standard_project_setup(REQUIRES 6.5)
qt_add_executable(general_tests general_tests.cpp)

else()
add_executable(general_tests general_tests.cpp)
target_link_libraries(general_tests
${DEFAULT_LIBRARIES}
# if (WITH_QT)
# add_definitions(-DLOGFAULT_USE_QT_LOG=1)
# find_package(Qt6 6.4 REQUIRED COMPONENTS Test)
# qt_standard_project_setup(REQUIRES 6.5)
# qt_add_executable(general_tests general_tests.cpp)

# else()
# add_executable(general_tests general_tests.cpp)
# target_link_libraries(general_tests
# ${DEFAULT_LIBRARIES}
# )
# endif()

####### default_to_clog

add_executable(default_to_clog
default_to_clog.cpp
)

set_property(TARGET default_to_clog PROPERTY CXX_STANDARD 11)

target_include_directories(default_to_clog
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}
$<BUILD_INTERFACE:${LOGFAULT_ROOT}/include
$<BUILD_INTERFACE:${GTEST_INCLUDE_DIRS}
)

target_link_libraries(default_to_clog
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)

add_test(NAME default_to_clog COMMAND default_to_clog)

####### default_to_clog_with_tid

add_executable(default_to_clog_with_tid
default_to_clog.cpp
)

target_compile_definitions(default_to_clog_with_tid PRIVATE LOGFAULT_USE_TID_AS_NAME=1)

set_property(TARGET default_to_clog_with_tid PROPERTY CXX_STANDARD 11)

target_include_directories(default_to_clog_with_tid
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}
$<BUILD_INTERFACE:${LOGFAULT_ROOT}/include
$<BUILD_INTERFACE:${GTEST_INCLUDE_DIRS}
)
endif()

target_link_libraries(default_to_clog_with_tid
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)

add_test(NAME default_to_clog_with_tid COMMAND default_to_clog_with_tid)
111 changes: 111 additions & 0 deletions tests/default_to_clog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <regex>

#include "logfault/logfault.h"
#include "gtest/gtest.h"

using namespace std;
using namespace logfault;

namespace {
struct ClogRedirector {
ClogRedirector(string& out, ostream& ios = std::clog)
: out_{out}, ios_{ios} {

orig_ = ios.rdbuf();
ios_.rdbuf(buffer_.rdbuf());
}

~ClogRedirector() {
out_ = buffer_.str();
ios_.rdbuf(orig_);
}

private:
ostream& ios_;
decltype(std::clog.rdbuf()) orig_{};
stringstream buffer_;
string& out_;
};
} // namespace

TEST(Logfault, HelloTrace) {

string output;
{
ClogRedirector redir{output};
LFLOG_TRACE << "Test log";
}

regex pattern{R"(.* TRACE .* Test log.*)"};
EXPECT_TRUE(regex_search(output, pattern));
}

TEST(Logfault, HelloDebug) {

string output;
{
ClogRedirector redir{output};
LFLOG_DEBUG << "Test log";
}

regex pattern{R"(.* DEBUGGING .* Test log.*)"};
EXPECT_TRUE(regex_search(output, pattern));
}

TEST(Logfault, HelloInfo) {

string output;
{
ClogRedirector redir{output};
LFLOG_INFO << "Test log";
}

regex pattern{R"(.* INFO .* Test log.*)"};
EXPECT_TRUE(regex_search(output, pattern));
}

TEST(Logfault, HelloWarn) {

string output;
{
ClogRedirector redir{output};
LFLOG_WARN << "Test log";
}

regex pattern{R"(.* WARNING .* Test log.*)"};
EXPECT_TRUE(regex_search(output, pattern));
}

TEST(Logfault, HelloError) {

string output;
{
ClogRedirector redir{output};
LFLOG_ERROR << "Test log";
}

regex pattern{R"(.* ERROR .* Test log.*)"};
EXPECT_TRUE(regex_search(output, pattern));
}

#if defined(__linux__) && defined(LOGFAULT_USE_TID_AS_NAME)
TEST(Logfault, HelloTid) {

string output;
{
ClogRedirector redir{output};
LFLOG_INFO << "Test log";
}

regex pattern{R"(.* INFO [0-9]{1,8} Test log.*)"};
EXPECT_TRUE(regex_search(output, pattern));
}
#endif

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);

logfault::LogManager::Instance().AddHandler(
make_unique<logfault::StreamHandler>(clog, logfault::LogLevel::TRACE));
return RUN_ALL_TESTS();
}
2 changes: 2 additions & 0 deletions tests/general_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

// General tests for manual testing during development

#include <iostream>

#define LOGFAULT_ENABLE_ALL
Expand Down

0 comments on commit 343cc7d

Please sign in to comment.