cmake_minimum_required(VERSION 3.17)

if (DEFINED CMakeLoggerGuard)
    return()
endif()

set(CMakeLoggerGuard yes)

macro(open_log)
    cmake_policy(PUSH)
    set(CMAKE_LOGFILE ${CMAKE_CURRENT_SOURCE_DIR}/logs/cmake.log)
    if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/logs")
        file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/logs")
    endif()
    if(NOT EXISTS ${CMAKE_LOGFILE})
        file(TOUCH ${CMAKE_LOGFILE})
    endif()
    log_add_entry("**********Logging Started **********")
    cmake_policy(POP)
endmacro()

macro(log_add_entry entry_text)
    string(REGEX REPLACE "[\r\n]+" "" entry_text ${entry_text})
    string(STRIP ${entry_text} entry_text)
    string(TIMESTAMP NOW_TIME %Y/%m/%d-%H:%M:%S UTC)
    file(APPEND ${CMAKE_LOGFILE} "[${NOW_TIME}]: ${entry_text}\n")
endmacro()

macro(close_log)
    log_add_entry("**********Logging Finished **********")
endmacro()