Skip to content

Commit

Permalink
Merge pull request #588 from lukepalmer/master
Browse files Browse the repository at this point in the history
Use relative source path in exceptions
  • Loading branch information
mjpt777 authored Dec 2, 2018
2 parents 728ea9f + bde538f commit 790b46b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build" FORCE)
endif(NOT CMAKE_BUILD_TYPE)

# relative file paths for use in exceptions
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__CMAKE_SOURCE_DIR__='\"${CMAKE_SOURCE_DIR}\"'")

set(AERON_THIRDPARTY_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/thirdparty")

##########################################################
Expand Down
32 changes: 19 additions & 13 deletions aeron-client/src/main/cpp/util/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@
#include <cstdint>
#include <string>
#include <stdexcept>
#include <sstream>
#include "MacroUtil.h"

// ==========================================================================================================================
// macro to create a const char* with the details of current source line in the format:
// [Class::Method : d:\projects\file.cpp : 127]
// ==========================================================================================================================

namespace aeron { namespace util {


static constexpr const char* past_prefix(const char * const prefix, const char * const filename)
{
return
*prefix == *filename ? past_prefix(prefix + 1, filename + 1) :
*filename == '/' ? filename + 1 : filename;
}

#define __SHORT_FILE__ aeron::util::past_prefix(__CMAKE_SOURCE_DIR__,__FILE__)

#ifdef _MSC_VER
#define SOURCEINFO __FUNCTION__, " : " __FILE__ " : " TOSTRING(__LINE__)
#define SOURCEINFO __FUNCTION__, __SHORT_FILE__, __LINE__
#if _MSC_VER >= 1900
#define AERON_NOEXCEPT noexcept
#else
#define AERON_NOEXCEPT throw()
#endif
#else
#define SOURCEINFO __PRETTY_FUNCTION__, " : " __FILE__ " : " TOSTRING(__LINE__)
#define SOURCEINFO __PRETTY_FUNCTION__, __SHORT_FILE__, __LINE__
#define AERON_NOEXCEPT noexcept
#endif

Expand All @@ -47,8 +53,8 @@ class SourcedException : public std::exception
std::string m_what;

public:
SourcedException(const std::string &what, const std::string& function, const std::string& where) :
m_where(function + where),
SourcedException(const std::string &what, const std::string& function, const std::string& file, const int line) :
m_where(static_cast<std::ostringstream&>(std::ostringstream() << function << " : " << file << " : " << line).str()),
m_what(what)
{
}
Expand All @@ -68,8 +74,8 @@ class SourcedException : public std::exception
class exceptionName : public aeron::util::SourcedException \
{ \
public: \
exceptionName (const std::string &what, const std::string& function, const std::string& where) \
: SourcedException (what, function, where) \
exceptionName (const std::string &what, const std::string& function, const std::string& file, const int line) \
: SourcedException (what, function, file, line) \
{} \
} \

Expand All @@ -91,8 +97,8 @@ class RegistrationException : public SourcedException

public:
RegistrationException(
std::int32_t errorCode, const std::string &what, const std::string& function, const std::string& where) :
SourcedException(what, function, where),
std::int32_t errorCode, const std::string &what, const std::string& function, const std::string& file, const int line) :
SourcedException(what, function, file, line),
m_errorCode(errorCode)
{
}
Expand Down
2 changes: 1 addition & 1 deletion aeron-client/src/test/cpp/util/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ std::string makeTempFileName()
#endif
}

void throwIllegalArgumentException()
inline void throwIllegalArgumentException()
{
throw util::IllegalArgumentException("Intentional IllegalArgumentException", SOURCEINFO);
}
Expand Down
26 changes: 25 additions & 1 deletion aeron-client/src/test/cpp/util/UtilTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
#include <util/ScopeUtils.h>
#include <util/StringUtil.h>
#include <util/BitUtil.h>
#include "TestUtils.h"

#include <gtest/gtest.h>

#include <gmock/gmock.h>

using namespace aeron::util;

Expand Down Expand Up @@ -104,3 +105,26 @@ TEST(utilTests, numberOfTrailingZeroes)
EXPECT_EQ(BitUtil::numberOfTrailingZeroes<std::uint32_t>(0xFFFF0000), 16);
EXPECT_EQ(BitUtil::numberOfTrailingZeroes<std::uint32_t>(0x00000001), 0);
}

void throwIllegalArgumentException()
{
aeron::test::throwIllegalArgumentException();
}

TEST(utilTests, sourcedException)
{
EXPECT_THROW({
try
{
aeron::test::throwIllegalArgumentException();
}
catch(const SourcedException& e)
{
// Path must be relative and not have a prefix
EXPECT_THAT(e.where(), ::testing::HasSubstr(" aeron-client/"));
// The exception should point to the code before it was inlined
EXPECT_THAT(e.where(), ::testing::HasSubstr("TestUtils.h"));
throw;
}
}, SourcedException);
}

0 comments on commit 790b46b

Please sign in to comment.