Skip to content

Commit

Permalink
added JBOX_LOGVALUES and loguru::init_for_test
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante committed Jan 23, 2022
1 parent 0420780 commit 99e1d9e
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
78 changes: 78 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) 2022 pongasoft
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# @author Yan Pujante

# The purpose of this CMake file is to compile/test re-cmake only
# When using re-cmake, sdk.cmake needs to be included instead

cmake_minimum_required(VERSION 3.17)

project(re-cmake LANGUAGES CXX)

# Using C++17
set(CMAKE_CXX_STANDARD 17)

# Determines whether we are working on re-cmake or using it
if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
set(re-cmake_DEV_BUILD TRUE)
else()
set(re-cmake_DEV_BUILD FALSE)
endif()

if(NOT re-cmake_DEV_BUILD)
message(FATAL_ERROR "Use sdk.cmake in your project")
endif()

enable_testing()

# Using RE SDK version 4.3.0
set(RE_SDK_VERSION 4.3.0)

# Location of RE SDK: can be set when invoking cmake => cmake -D "RE_SDK_ROOT:PATH=/path/to/re_sdk"
if(APPLE)
set(RE_SDK_ROOT "/Users/Shared/ReasonStudios/JukeboxSDK_${RE_SDK_VERSION}/SDK" CACHE PATH "Location of RE SDK")
else()
set(RE_SDK_ROOT "C:/Users/Public/Documents/ReasonStudios/JukeboxSDK_${RE_SDK_VERSION}/SDK" CACHE PATH "Location of RE SDK")
endif()

# include google test
set(googletest_GIT_REPO "https://github.com/google/googletest" CACHE STRING "googletest git repository URL")
set(googletest_GIT_TAG "e2239ee6043f73722e7aa812a459f54a28552929" CACHE STRING "googletest git tag")
include(cmake/RECMakeFetchGoogleTest.cmake)
include(GoogleTest)

set(re-logging_CPP_DIR "re-logging/src/cpp/logging")

set(re-logging_SOURCES
"${re-logging_CPP_DIR}/logging.h"
"${re-logging_CPP_DIR}/loguru.cpp"
"${re-logging_CPP_DIR}/loguru.hpp"
)

add_library(re-logging STATIC "${re-logging_SOURCES}")
target_include_directories(re-logging PUBLIC "${re-logging_CPP_DIR}" "${RE_SDK_ROOT}/API")
target_compile_definitions(re-logging PUBLIC LOCAL_NATIVE_BUILD=1 DEBUG=1)

set(target_test "re-cmake_test")
set(re-cmake_CPP_TST_DIR "${CMAKE_CURRENT_LIST_DIR}/test/cpp")
set(TEST_CASE_SOURCES
"${re-cmake_CPP_TST_DIR}/re-logging/TestLogging.cpp"
)

add_executable("${target_test}" "${TEST_CASE_SOURCES}")
target_link_libraries("${target_test}" gtest_main re-logging)
#target_include_directories("${target_test}" PUBLIC "${logging_CPP_DIR}")

gtest_discover_tests("${target_test}")
61 changes: 61 additions & 0 deletions re-logging/src/cpp/logging/logging.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma once

#ifndef __Pongasoft_re_logging_h__
#define __Pongasoft_re_logging_h__

// Implementation note: when doing jbox builds, loguru.cpp cannot be compiled because it relies on includes
// (like atomic) which are not part of the RE SDK due to sandboxing, as a result we disable logging in this instance
#if LOCAL_NATIVE_BUILD && DEBUG
Expand All @@ -11,3 +16,59 @@
#endif // LOCAL_NATIVE_BUILD

#include "loguru.hpp"

#include <JukeboxTypes.h>

/**
* Implementation details
*/
namespace impl {
template<typename... Args>
inline void JBox_LogValues(const char iFile[], TJBox_Int32 iLine, char const *iMessage, Args&& ...iValues)
{
TJBox_Value values[sizeof...(iValues)] { iValues... };
JBox_TraceValues(iFile, iLine, iMessage, values, sizeof...(iValues));
}
}

#if DEBUG
/**
* Allow to write simpler code:
*
* ```cpp
* // using JBOX_TRACEVALUES
* TJBox_Value instanceIDValue = JBox_MakeNumber(JBox_GetNumber(iParams[0]));
* TJBox_Value array[1];
* array[0] = instanceIDValue;
* JBOX_TRACEVALUES("instance ID = ^0", array, 1);
*
* // using JBOX_LOGVALUES
* JBOX_LOGVALUES("instance ID = ^0", iParams[0]));
* ```
*/
#define JBOX_LOGVALUES(iMessage, ...) \
::impl::JBox_LogValues(__FILE__, __LINE__, iMessage, __VA_ARGS__)
#else
#define JBOX_LOGVALUES(iMessage, ...)
#endif

#if LOGURU_DEBUG_CHECKS
namespace loguru {
/**
* This function can be called when the device is created to make loguru output nicer (essentially replaces
* the name of the thread which is useless, by the name of the rack extension which can be useful when you
* have different REs using loguru) */
inline void init_for_re(char const *iREName = nullptr)
{
loguru::g_preamble_thread = false;
// loguru::g_preamble_prefix = iREName;
}

/**
* This function can be used from tests to replace loguru aborts with exception (which can be checked) */
void init_for_test(char const *iPrefix = nullptr);

}
#endif

#endif
13 changes: 13 additions & 0 deletions re-logging/src/cpp/logging/loguru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,19 @@ namespace loguru
"Failed to install handler for " LOGURU_FMT(s) "", s.name);
}
}

//------------------------------------------------------------------------
// init_for_test
//------------------------------------------------------------------------
void init_for_test(char const *iPrefix)
{
loguru::g_preamble_thread = false;
// loguru::g_preamble_prefix = iPrefix;
loguru::set_fatal_handler([](const loguru::Message& message) {
throw std::runtime_error(std::string(message.prefix) + message.message);
});
}

} // namespace loguru

#endif // _WIN32
Expand Down
27 changes: 27 additions & 0 deletions test/cpp/re-logging/TestLogging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2022 pongasoft
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @author Yan Pujante
*/

#include <gtest/gtest.h>
#include <logging.h>

TEST(Logging, init_for_test)
{
// init_for_test changes abort into exception that can be caught
loguru::init_for_test();
ASSERT_THROW(loguru::log_and_abort(0, "test", __FILE__, __LINE__), std::runtime_error);
}

0 comments on commit 99e1d9e

Please sign in to comment.