Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "cmake"]
path = lib/cmake
url = https://github.com/OliverBenz/cmake_files.git
url = git@github.com:OliverBenz/cmake_files.git
72 changes: 65 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)

project(Logger VERSION 1.0.0 LANGUAGES CXX)

project(LoggingProject)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(LOGGER_IS_TOP_LEVEL OFF)
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(LOGGER_IS_TOP_LEVEL ON)
endif()

# Normalize install prefix for Unix-like systems (prefer /opt).
if(UNIX AND NOT WIN32)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR CMAKE_INSTALL_PREFIX MATCHES "^/var/empty")
set(CMAKE_INSTALL_PREFIX "/opt" CACHE PATH "Install path prefix" FORCE)
endif()
endif()

# Project Options
option(LOGGER_BUILD_TESTS "Build Logger unit tests" ${LOGGER_IS_TOP_LEVEL})
option(LOGGER_BUILD_EXAMPLES "Build Logger examples" ${LOGGER_IS_TOP_LEVEL})
option(LOGGER_ENABLE_INSTALL "Enable install/export targets for Logger" ${LOGGER_IS_TOP_LEVEL})

option(BUILD_TESTS "Create the unit tests for the project." True)
option(BUILD_EXAMPLES "Create examples for the project." True)
# Dependencies Options
set(FETCHCONTENT_UPDATES_DISCONNECTED ON CACHE BOOL "Use previously downloaded FetchContent sources" FORCE)
set(INSTALL_GTEST OFF CACHE BOOL "Do not install googletest" FORCE)
set(INSTALL_GMOCK OFF CACHE BOOL "Do not install googlemock" FORCE)

# Installation
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

# Setup project settings
add_subdirectory(lib)

project_settings_configure_install_layout(
LOGGER_INSTALL_ARCH
LOGGER_INSTALL_CONFIG
LOGGER_INSTALL_BASE
PROJECT_NAME ${PROJECT_NAME}
INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}
)
add_subdirectory(src)
if(BUILD_TESTS)

if(LOGGER_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
if(BUILD_EXAMPLES)
if(LOGGER_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
endif()

configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/LoggerConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/LoggerConfig.cmake
INSTALL_DESTINATION ${LOGGER_INSTALL_BASE}/lib/cmake
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/LoggerConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)

export(
EXPORT LoggerTargets
FILE ${CMAKE_CURRENT_BINARY_DIR}/LoggerTargets.cmake
NAMESPACE Logger::
)

if(LOGGER_ENABLE_INSTALL)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/LoggerConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/LoggerConfigVersion.cmake
DESTINATION ${LOGGER_INSTALL_BASE}/lib/cmake
)
endif()
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# Template
# Simple Logger
Just a simple logger.
Check examples for how to use.

### Documentation
- [General Documentation](docs/Documentation.md)
- [Installation](docs/Installation.md)
3 changes: 3 additions & 0 deletions cmake/LoggerConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/LoggerTargets.cmake")
2 changes: 2 additions & 0 deletions docs/Documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Gotta do this.
Just check the examples for how to use.
54 changes: 54 additions & 0 deletions docs/Installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Logger installation & integration

## CMake Options
- `LOGGER_BUILD_TESTS` (default: `ON` when top level) – build unit tests.
- `LOGGER_BUILD_EXAMPLES` (default: `ON` when top level) – build example apps.
- `LOGGER_ENABLE_INSTALL` (default: `ON` when top level) – emit install/export targets.

## Build locally
```bash
cmake -S . -B build -DLOGGER_BUILD_TESTS=ON -DLOGGER_BUILD_EXAMPLES=ON
cmake --build build -j
ctest --test-dir build --output-on-failure
```

## Use as a git submodule
1) `git submodule add <repo-url> external/logger`
2) In your root `CMakeLists.txt`:
```cmake
set(LOGGER_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(LOGGER_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(external/logger)
target_link_libraries(<your-target> PRIVATE Logger::Logger)
```
- Choose this when you want an auditable, locked revision in your tree.

## Use via FetchContent
```cmake
include(FetchContent)
FetchContent_Declare(
Logger
GIT_REPOSITORY <repo-url>
GIT_TAG <commit-or-tag>
)
set(LOGGER_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(LOGGER_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(Logger)
target_link_libraries(<your-target> PRIVATE Logger::Logger)
```
- Prefer this when you want simple, on-demand fetching without VCS plumbing.
- Pin `GIT_TAG` to a commit or release; mirror internally if external fetches are blocked.

## Use an installed package
```bash
cmake --build build --target install --prefix <install-prefix>
```
```cmake
find_package(Logger CONFIG REQUIRED)
target_link_libraries(<your-target> PRIVATE Logger::Logger)
```

## Integration notes
- Public include path is `Logger/` (e.g., `#include <Logger/Logger.hpp>`).
- The library is C++20 and exports an ALIAS target `Logger::Logger`.
- Default output dirs: `${binary_dir}/out/bin` and `${binary_dir}/out/lib`.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)

add_subdirectory(standard)
add_subdirectory(globalConfig)
4 changes: 2 additions & 2 deletions examples/globalConfig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)

set(targetName Example.GlobalConfig)

Expand All @@ -9,7 +9,7 @@ add_executable(${targetName}
)

# Link to required libraries
target_link_libraries(${targetName} PUBLIC Logging)
target_link_libraries(${targetName} PUBLIC Logger::Logger)
set_target_properties(${targetName} PROPERTIES FOLDER "${ideFolderExamples}")

# Setup project settings
Expand Down
26 changes: 13 additions & 13 deletions examples/globalConfig/LogModule.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#include "LogModule.hpp"

#include "LogConfig.hpp"
#include "LogOutputFile.hpp"
#include "LogOutputConsole.hpp"
#include "Logger/LogConfig.hpp"
#include "Logger/LogOutputConsole.hpp"
#include "Logger/LogOutputFile.hpp"

Logging::LogConfig config;

//! Enable logging of any entries to an output file + console(for debug builds).
static void InitializeLogger() {
config.SetLogEnabled(true);
config.SetMinLogLevel(Logging::LogLevel::Any);
config.SetLogEnabled(true);
config.SetMinLogLevel(Logging::LogLevel::Any);

auto logFile = std::make_shared<Logging::LogOutputFile>("Logfile.txt");
config.AddLogOutput(logFile);
config.AddLogOutput(logFile);

#ifdef _DEBUG
config.AddLogOutput(std::make_shared<Logging::LogOutputConsole>());
config.AddLogOutput(std::make_shared<Logging::LogOutputConsole>());
#endif
}

Logging::Logger Logger() {
static bool initialized = false;
if (!initialized) {
InitializeLogger();
initialized = true;
}
static bool initialized = false;
if (!initialized) {
InitializeLogger();
initialized = true;
}

return Logging::Logger(config);
return Logging::Logger(config);
}
2 changes: 1 addition & 1 deletion examples/globalConfig/LogModule.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "Logger.hpp"
#include "Logger/Logger.hpp"

//! Returns the logger instance based on the set up configuration.
Logging::Logger Logger();
34 changes: 17 additions & 17 deletions examples/globalConfig/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define VERSION_STRING "Version: " STR(LOGVERSION_MAJOR) "." STR(LOGVERSION_MINOR) "." STR(LOGVERSION_PATCH)
#define VERSION_STRING "Version: " STR(LOGVERSION_MAJOR) "." STR(LOGVERSION_MINOR) "." STR(LOGVERSION_PATCH)

//! Example for logging.
class Calculator {
public:
Calculator(const int a, const int b) : m_a(a), m_b(b) {
Logger().Log(Logging::LogLevel::Debug, "Calculator constructor called.");
}
Calculator(const int a, const int b) : m_a(a), m_b(b) {
Logger().Log(Logging::LogLevel::Debug, "Calculator constructor called.");
}

int Add() const {
Logger().Log(Logging::LogLevel::Debug, "Add function called.");
return m_a + m_b;
}
int Add() const {
Logger().Log(Logging::LogLevel::Debug, "Add function called.");
return m_a + m_b;
}

private:
int m_a;
int m_b;
int m_a;
int m_b;
};

int main() {
auto logger = Logger();
logger.Log(Logging::LogLevel::Info, "Starting application.");
logger.Log(Logging::LogLevel::Info, VERSION_STRING);
logger.Log(Logging::LogLevel::Info, "Author: Oliver Benz");
logger.Flush();
auto logger = Logger();
logger.Log(Logging::LogLevel::Info, "Starting application.");
logger.Log(Logging::LogLevel::Info, VERSION_STRING);
logger.Log(Logging::LogLevel::Info, "Author: Oliver Benz");
logger.Flush();

Calculator calc(9, 10);
calc.Add();
Calculator calc(9, 10);
calc.Add();
}
4 changes: 2 additions & 2 deletions examples/standard/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)

set(targetName Example.Standard)

Expand All @@ -8,7 +8,7 @@ add_executable(${targetName}
)

# Link to required libraries
target_link_libraries(${targetName} PUBLIC Logging)
target_link_libraries(${targetName} PUBLIC Logger::Logger)
set_target_properties(${targetName} PROPERTIES FOLDER "${ideFolderExamples}")

# Setup project settings
Expand Down
17 changes: 9 additions & 8 deletions examples/standard/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "Logger/LogLevel.hpp"
#include "Logger/LogOutputConsole.hpp"
#include "Logger/LogOutputFile.hpp"
#include "Logger/LogOutputMock.hpp"
#include "Logger/Logger.hpp"

#include <iostream>
#include "Logger.hpp"
#include "LogLevel.hpp"
#include "LogOutputConsole.hpp"
#include "LogOutputMock.hpp"
#include "LogOutputFile.hpp"

int main(){
int main() {
std::cout << "Version: " << LOGVERSION_MAJOR << "." << LOGVERSION_MINOR << "." << LOGVERSION_PATCH << std::endl;

static Logging::LogConfig config;
auto mock = std::make_shared<Logging::LogOutputMock>();
auto logFile = std::make_shared<Logging::LogOutputFile>("Filename.txt");
Expand All @@ -24,7 +25,7 @@ int main(){
}

std::cout << "\nMockData:\n";
for (const auto& entry : mock->m_logEntries){
for (const auto& entry: mock->m_logEntries) {
std::cout << LevelToText(entry.m_level) + " " + entry.m_text + "\n";
}

Expand Down
5 changes: 2 additions & 3 deletions lib/FetchLibraries.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)

include(FetchContent)

# GTest
# Version 1.13.0
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG b796f7d44681514f58a683a3a71ff17c94edb0c1
GIT_TAG v1.17.0
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # For Windows: Prevent overriding the parent project's compiler/linker settings

Expand Down
2 changes: 1 addition & 1 deletion lib/cmake
Loading
Loading