Skip to content
Open
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
37 changes: 28 additions & 9 deletions LogMonitor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,52 @@ cmake_minimum_required(VERSION 3.15)
# Define the project
project(LogMonitor)

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_BUILD_TYPE Release)
set(VCPKG_TARGET_TRIPLET x64-windows)
set(VCPKG_TARGET_TRIPLET x64-windows-static)

# Use vcpkg if available
if (DEFINED ENV{VCPKG_ROOT})
if(DEFINED ENV{VCPKG_ROOT})
set(VCPKG_ROOT $ENV{VCPKG_ROOT})
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Toolchain file for vcpkg" FORCE)

# Set Boost paths
set(BOOST_ROOT "${VCPKG_ROOT}/installed/x64-windows" CACHE PATH "Boost installation root")
set(Boost_INCLUDE_DIR "${VCPKG_ROOT}/installed/x64-windows/include" CACHE PATH "Boost include directory")
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file" FORCE)
set(VCPKG_TARGET_TRIPLET "x64-windows-static" CACHE STRING "Vcpkg target triplet")
endif()

# Enforce static MSVC runtime (/MT or /MTd)
if(MSVC)
foreach(flag_var CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endforeach()
endif()

# Set Windows SDK version if available
if (DEFINED ENV{SDKVersion})
set(CMAKE_SYSTEM_VERSION $ENV{SDKVersion})
endif()

# Enable Unicode globally
add_definitions(-DUNICODE -D_UNICODE)

# Enable warnings
if (MSVC)
add_compile_options(/W4)
else()
add_compile_options(-Wall -Wextra -pedantic)
endif()

# Enable testing framework
enable_testing()

# Enable Unicode globally
add_definitions(-DUNICODE -D_UNICODE)
# Find dependencies
find_package(nlohmann_json CONFIG REQUIRED)

# Include subdirectories for main and test executables
add_subdirectory(src) # Add main executable's CMake
Expand Down
8 changes: 4 additions & 4 deletions LogMonitor/LogMonitorTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)

project(LogMonitorTests)

find_package(Boost REQUIRED COMPONENTS json)
find_package(nlohmann_json CONFIG REQUIRED)

# Automatically gather all test source files
file(GLOB_RECURSE TEST_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.cpp")
Expand All @@ -22,7 +22,7 @@ target_compile_definitions(LogMonitorTests PRIVATE LOGMONITORTESTS_EXPORTS)
target_include_directories(LogMonitorTests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR} # Includes Utility.h and pch.h
${CMAKE_CURRENT_SOURCE_DIR}/../src
${Boost_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/../src/LogMonitor
)

# Set Windows-specific linker flags
Expand All @@ -33,8 +33,8 @@ set_target_properties(LogMonitorTests PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)

# Link LogMonitor and Boost.JSON
target_link_libraries(LogMonitorTests PRIVATE LogMonitor Boost::json)
# Link LogMonitor and Nlohmann JSON
target_link_libraries(LogMonitorTests PRIVATE LogMonitorLib nlohmann_json::nlohmann_json)

# Enable testing
enable_testing()
Expand Down
3 changes: 3 additions & 0 deletions LogMonitor/LogMonitorTests/UtilityTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace UtilityTests
std::wstring expect = L"say, \\\"hello\\\"";
Utility::SanitizeJson(str);
Assert::IsTrue(str == expect, L"should escape \"");

str = L"\"hello\"";
expect = L"\\\"hello\\\"";
Utility::SanitizeJson(str);
Expand All @@ -69,6 +70,7 @@ namespace UtilityTests
expect = L"hello\\r\\nworld";
Utility::SanitizeJson(str);
Assert::IsTrue(str == expect, L"should escape \r and \n");

str = L"\r\nHello\r\n";
expect = L"\\r\\nHello\\r\\n";
Utility::SanitizeJson(str);
Expand All @@ -78,6 +80,7 @@ namespace UtilityTests
expect = L"\\\\Driver\\\\XX\\\\";
Utility::SanitizeJson(str);
Assert::IsTrue(str == expect, L"should escape \\");

str = L"C:\\Drive\\XX";
expect = L"C:\\\\Drive\\\\XX";
Utility::SanitizeJson(str);
Expand Down
1 change: 1 addition & 0 deletions LogMonitor/LogMonitorTests/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <direct.h >
#include <io.h>
#include <fcntl.h>
#include <nlohmann/json.hpp>
#include "../src/LogMonitor/Utility.h"
#include "../src/LogMonitor/Parser/ConfigFileParser.h"
#include "../src/LogMonitor/Parser/LoggerSettings.h"
Expand Down
39 changes: 31 additions & 8 deletions LogMonitor/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,45 @@ cmake_minimum_required(VERSION 3.15)

project(LogMonitor)

find_package(Boost REQUIRED COMPONENTS json)
find_package(nlohmann_json CONFIG REQUIRED)

# Gather source files
file(GLOB_RECURSE SourceFiles RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.cpp")
file(GLOB_RECURSE HeaderFiles RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.h")

# Define LogMonitor as a library
add_library(LogMonitor ${SourceFiles})
# Define LogMonitorLib as a static library
add_library(LogMonitorLib STATIC ${SourceFiles})
Comment on lines 8 to +12
Copy link

Copilot AI Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same set of source files is used to build both the static library (LogMonitorLib) and the executable (LogMonitor), potentially leading to duplicate compilation. Consider separating the core sources for the library from those defining the executable's entry point to avoid symbol duplication and reduce build overhead.

Copilot uses AI. Check for mistakes.

# Set the output name of the static library to "LogMonitor.lib"
set_target_properties(LogMonitorLib PROPERTIES
OUTPUT_NAME "LogMonitor"
)

# Define LogMonitor as an executable
add_executable(LogMonitor ${SourceFiles})

# Add precompiled headers (PCH)
target_precompile_headers(LogMonitor PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/LogMonitor/pch.h)

# Include directories for LogMonitor
# Include directories for both targets
target_include_directories(LogMonitorLib PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/LogMonitor
${CMAKE_CURRENT_SOURCE_DIR}/LogMonitor/FileMonitor
)

target_include_directories(LogMonitor PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/LogMonitor
${CMAKE_CURRENT_SOURCE_DIR}/LogMonitor/FileMonitor
${Boost_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/LogMonitor/FileMonitor
)

# Link Boost JSON to LogMonitor
target_link_libraries(LogMonitor PRIVATE Boost::json)
# Link dependencies
target_link_libraries(LogMonitorLib PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(LogMonitor PRIVATE LogMonitorLib nlohmann_json::nlohmann_json)

# Set output path
set_target_properties(LogMonitor PROPERTIES
OUTPUT_NAME "LogMonitor"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)
Loading