Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System error code handling and utiltity function cleanup #238

Merged
merged 5 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,6 @@ endif()
# Source directory
#-----------------------------------------------------------------------------

set(US_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/third_party ${PROJECT_BINARY_DIR}/include)

# Configure CppMicroServicesConfig.cmake for the build tree.
# The file is used in sub-directories.

Expand Down Expand Up @@ -574,7 +572,7 @@ add_subdirectory(tools/rc)
export(TARGETS ${US_RCC_EXECUTABLE_TARGET}
FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake)


add_subdirectory(util)
add_subdirectory(framework)
add_subdirectory(httpservice)
add_subdirectory(webconsole)
Expand Down
3 changes: 2 additions & 1 deletion cmake/usFunctionCreateTestBundle.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

macro(_us_create_test_bundle_helper)

add_library(${name} ${_srcs})
add_library(${name} ${_srcs} $<TARGET_OBJECTS:util>)
if(US_BUILD_SHARED_LIBS AND US_TEST_LIBRARY_EXTENSION)
set_target_properties(${name} PROPERTIES SUFFIX ${US_TEST_LIBRARY_EXTENSION})
endif()
Expand All @@ -15,6 +15,7 @@ macro(_us_create_test_bundle_helper)
set_property(TARGET ${name} PROPERTY COMPILE_FLAGS "${_compile_flags} -fPIC")
endif()

target_include_directories(${name} PRIVATE $<TARGET_PROPERTY:util,INCLUDE_DIRECTORIES>)
target_link_libraries(${name} ${${PROJECT_NAME}_TARGET} ${US_TEST_LINK_LIBRARIES} CppMicroServices)

if(_res_files OR US_TEST_LINK_LIBRARIES)
Expand Down
3 changes: 3 additions & 0 deletions cmake/usMacroCreateBundle.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ if(${PROJECT_NAME}_RESOURCES OR ${PROJECT_NAME}_BINARY_RESOURCES)
usFunctionGetResourceSource(TARGET ${${PROJECT_NAME}_TARGET} OUT ${PROJECT_NAME}_SOURCES)
endif()

list(APPEND ${PROJECT_NAME}_SOURCES $<TARGET_OBJECTS:util>)

# Create the bundle library
add_library(${PROJECT_TARGET} ${${PROJECT_NAME}_SOURCES}
${${PROJECT_NAME}_PRIVATE_HEADERS} ${${PROJECT_NAME}_PUBLIC_HEADERS})
Expand All @@ -98,6 +100,7 @@ target_include_directories(${PROJECT_TARGET}
$<INSTALL_INTERFACE:${HEADER_INSTALL_DIR}>
PRIVATE
${${PROJECT_NAME}_PRIVATE_INCLUDE_DIRS}
$<TARGET_PROPERTY:util,INCLUDE_DIRECTORIES>
)

# Compile definitions
Expand Down
11 changes: 6 additions & 5 deletions framework/src/bundle/BundleContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@

#include "cppmicroservices/Bundle.h"
#include "cppmicroservices/Framework.h"
#include "cppmicroservices/util/FileSystem.h"
#include "cppmicroservices/util/Error.h"

#include "BundleContextPrivate.h"
#include "BundlePrivate.h"
#include "BundleRegistry.h"
#include "CoreBundleContext.h"
#include "ServiceReferenceBasePrivate.h"
#include "ServiceRegistry.h"
#include "Utils.h"

#include <memory>
#include <stdio.h>
Expand Down Expand Up @@ -253,7 +254,7 @@ struct ServiceHolder
// the BundlePrivate or CoreBundleContext objects.
if (!b.expired())
{
DIAG_LOG(*b.lock()->coreCtx->sink) << "UngetService threw an exception. " << GetLastExceptionStr();
DIAG_LOG(*b.lock()->coreCtx->sink) << "UngetService threw an exception. " << util::GetLastExceptionStr();
}
// don't throw exceptions from the destructor. For an explanation, see:
// https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md
Expand Down Expand Up @@ -467,11 +468,11 @@ std::string BundleContext::GetDataFile(const std::string &filename) const
std::string dataRoot = b->bundleDir;
if (!dataRoot.empty())
{
if (!fs::Exists(dataRoot))
if (!util::Exists(dataRoot))
{
fs::MakePath(dataRoot);
util::MakePath(dataRoot);
}
return dataRoot + DIR_SEP + filename;
return dataRoot + util::DIR_SEP + filename;
}
return std::string();
}
Expand Down
27 changes: 15 additions & 12 deletions framework/src/bundle/BundlePrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "cppmicroservices/FrameworkEvent.h"
#include "cppmicroservices/ServiceRegistration.h"

#include "cppmicroservices/util/String.h"
#include "cppmicroservices/util/FileSystem.h"
#include "cppmicroservices/util/Error.h"

#include "BundleArchive.h"
#include "BundleContextPrivate.h"
#include "BundleResourceContainer.h"
Expand All @@ -39,7 +43,6 @@
#include "CoreBundleContext.h"
#include "Fragment.h"
#include "ServiceReferenceBasePrivate.h"
#include "Utils.h" // cppmicroservices::ToString()

#include <algorithm>
#include <cassert>
Expand All @@ -61,7 +64,7 @@ void BundlePrivate::Stop(uint32_t options)
auto l = coreCtx->resolver.Lock();
if (IsFragment())
{
throw std::runtime_error("Bundle#" + cppmicroservices::ToString(id) + ", can not stop a fragment");
throw std::runtime_error("Bundle#" + util::ToString(id) + ", can not stop a fragment");
}

// 1:
Expand Down Expand Up @@ -135,7 +138,7 @@ std::exception_ptr BundlePrivate::Stop1()
catch (...)
{
res = std::make_exception_ptr(
std::runtime_error("Bundle#" + cppmicroservices::ToString(id) + ", BundleActivator::Stop() failed: " + GetLastExceptionStr()));
std::runtime_error("Bundle#" + util::ToString(id) + ", BundleActivator::Stop() failed: " + util::GetLastExceptionStr()));
}

// if stop was aborted (uninstall or timeout), make sure
Expand Down Expand Up @@ -229,7 +232,7 @@ void BundlePrivate::WaitOnOperation(WaitConditionType& wc, LockType& lock, const
op = "update";
break;
}
throw std::runtime_error(src + " called during " + op + " of Bundle#" + cppmicroservices::ToString(id));
throw std::runtime_error(src + " called during " + op + " of Bundle#" + util::ToString(id));
}
}

Expand Down Expand Up @@ -396,7 +399,7 @@ void BundlePrivate::FinalizeActivation(LockType& l)
// This happens if call start from inside the BundleActivator.stop
// method.
// Don't allow it.
throw std::runtime_error("Bundle#" + cppmicroservices::ToString(id) +
throw std::runtime_error("Bundle#" + util::ToString(id) +
", start called from BundleActivator::Stop");
case Bundle::STATE_UNINSTALLED:
throw std::logic_error("Bundle is in UNINSTALLED state");
Expand Down Expand Up @@ -488,7 +491,7 @@ void BundlePrivate::Uninstall()
{
try
{
if (fs::Exists(bundleDir)) fs::RemoveDirectoryRecursive(bundleDir);
if (util::Exists(bundleDir)) util::RemoveDirectoryRecursive(bundleDir);
}
catch (...)
{
Expand Down Expand Up @@ -625,7 +628,7 @@ std::exception_ptr BundlePrivate::Start0()
CreateActivatorHook createActivatorHook = nullptr;

void* libHandle = nullptr;
if ((lib.GetFilePath() == BundleUtils::GetExecutablePath()))
if ((lib.GetFilePath() == util::GetExecutablePath()))
{
libHandle = BundleUtils::GetExecutableHandle();
}
Expand Down Expand Up @@ -675,7 +678,7 @@ std::exception_ptr BundlePrivate::Start0()
}
catch (...)
{
res = std::make_exception_ptr(std::runtime_error("Bundle#" + cppmicroservices::ToString(id) + " start failed: " + GetLastExceptionStr()));
res = std::make_exception_ptr(std::runtime_error("Bundle#" + util::ToString(id) + " start failed: " + util::GetLastExceptionStr()));
}
}

Expand Down Expand Up @@ -720,7 +723,7 @@ std::exception_ptr BundlePrivate::Start0()
}
if (!cause.empty())
{
res = std::make_exception_ptr("Bundle#" + cppmicroservices::ToString(id) + " start failed: " + cause);
res = std::make_exception_ptr("Bundle#" + util::ToString(id) + " start failed: " + cause);
}
}

Expand Down Expand Up @@ -861,7 +864,7 @@ BundlePrivate::BundlePrivate(
}
catch (...)
{
throw std::runtime_error(std::string("Parsing of manifest.json for bundle ") + symbolicName + " at " + location + " failed: " + GetLastExceptionStr());
throw std::runtime_error(std::string("Parsing of manifest.json for bundle ") + symbolicName + " at " + location + " failed: " + util::GetLastExceptionStr());
}
}
}
Expand All @@ -881,7 +884,7 @@ BundlePrivate::BundlePrivate(
}
catch (...)
{
errMsg = std::string("The version identifier is invalid: ") + GetLastExceptionStr();
errMsg = std::string("The version identifier is invalid: ") + util::GetLastExceptionStr();
}

if (!errMsg.empty())
Expand All @@ -905,7 +908,7 @@ BundlePrivate::BundlePrivate(
auto snbl = coreCtx->bundleRegistry.GetBundles(symbolicName, version);
if (!snbl.empty())
{
throw std::invalid_argument("Bundle#" + cppmicroservices::ToString(id) +
throw std::invalid_argument("Bundle#" + util::ToString(id) +
", a bundle with same symbolic name and version " +
"is already installed (" + symbolicName + ", " +
version.ToString() + ")");
Expand Down
10 changes: 6 additions & 4 deletions framework/src/bundle/BundleRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#include "cppmicroservices/FrameworkEvent.h"
#include "cppmicroservices/GetBundleContext.h"

#include "cppmicroservices/util/Error.h"
#include "cppmicroservices/util/String.h"

#include "BundleContextPrivate.h"
#include "BundlePrivate.h"
#include "BundleResourceContainer.h"
#include "BundleStorage.h"
#include "CoreBundleContext.h"
#include "FrameworkPrivate.h"
#include "Utils.h" // cppmicroservices::ToString()

#include <cassert>
#include <map>
Expand Down Expand Up @@ -148,7 +150,7 @@ std::vector<Bundle> BundleRegistry::Install0(
{
ba->Purge();
}
throw std::runtime_error("Failed to install bundle library at " + location + ": " + GetLastExceptionStr());
throw std::runtime_error("Failed to install bundle library at " + location + ": " + util::GetLastExceptionStr());
}
}

Expand Down Expand Up @@ -255,9 +257,9 @@ void BundleRegistry::Load()
{
ba->SetAutostartSetting(-1); // Do not start on launch
std::cerr << "Failed to load bundle "
<< cppmicroservices::ToString(ba->GetBundleId())
<< util::ToString(ba->GetBundleId())
<< " (" + ba->GetBundleLocation() + ") uninstalled it!"
<< " (execption: " << GetExceptionStr(std::current_exception()) << ")"
<< " (execption: " << util::GetExceptionStr(std::current_exception()) << ")"
<< std::endl;
}
}
Expand Down
4 changes: 2 additions & 2 deletions framework/src/bundle/BundleResourceContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "cppmicroservices/BundleResource.h"

#include "Utils.h"
#include "cppmicroservices/util/FileSystem.h"

#include <cassert>
#include <climits>
Expand All @@ -38,7 +38,7 @@ BundleResourceContainer::BundleResourceContainer(const std::string& location)
: m_Location(location)
, m_ZipArchive()
{
if (!fs::Exists(location))
if (!util::Exists(location))
{
throw std::runtime_error("Location does not exist");
}
Expand Down
5 changes: 3 additions & 2 deletions framework/src/bundle/BundleThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/FrameworkEvent.h"

#include "cppmicroservices/util/String.h"

#include "BundlePrivate.h"
#include "CoreBundleContext.h"
#include "Utils.h" // cppmicroservices::ToString()

#include <future>

Expand Down Expand Up @@ -252,7 +253,7 @@ std::exception_ptr BundleThread::StartAndWait(BundlePrivate* b, int operation, U
b->ResetBundleThread();

return std::make_exception_ptr(std::runtime_error(
"Bundle#" + cppmicroservices::ToString(b->id) + " " +
"Bundle#" + util::ToString(b->id) + " " +
opType + " failed with reason: " + reason
));
}
Expand Down
45 changes: 3 additions & 42 deletions framework/src/bundle/BundleUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@
#include "BundleContextPrivate.h"
#include "BundlePrivate.h"
#include "CoreBundleContext.h"
#include "Utils.h"

#ifdef US_PLATFORM_WINDOWS

#include "cppmicroservices/util/Error.h"

#include <windows.h>

#define RTLD_LAZY 0 // unused

const char* dlerror(void)
{
static std::string errStr;
errStr = cppmicroservices::GetLastWin32ErrorStr();
errStr = cppmicroservices::util::GetLastWin32ErrorStr();
return errStr.c_str();
}

Expand Down Expand Up @@ -71,9 +72,6 @@ void* dlsym(void *handle, const char *symbol)

#endif

#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif

namespace cppmicroservices {

Expand All @@ -92,43 +90,6 @@ void* GetExecutableHandle()
return dlopen(0, RTLD_LAZY);;
}

std::string GetExecutablePath()
{
uint32_t bufsize = MAXPATHLEN;
std::unique_ptr<char[]> buf(new char[bufsize]);

#ifdef US_PLATFORM_WINDOWS
if (GetModuleFileName(nullptr, buf.get(), bufsize) == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
DIAG_LOG(*GetFrameworkLogSink()) << "GetModuleFileName failed" << GetLastWin32ErrorStr();
buf[0] = '\0';
}
#elif defined(US_PLATFORM_APPLE)
int status = _NSGetExecutablePath(buf.get(), &bufsize);
if (status == -1)
{
buf.reset(new char[bufsize]);
status = _NSGetExecutablePath(buf.get(), &bufsize);
}
if (status != 0)
{
DIAG_LOG(*GetFrameworkLogSink()) << "_NSGetExecutablePath() failed";
}
// the returned path may not be an absolute path
#elif defined(US_PLATFORM_LINUX)
ssize_t len = ::readlink("/proc/self/exe", buf.get(), bufsize);
if (len == -1 || len == bufsize)
{
len = 0;
}
buf[len] = '\0';
#else
// 'dlsym' does not work with symbol name 'main'
DIAG_LOG(*GetFrameworkLogSink()) << "GetExecutablePath failed";
#endif
return buf.get();
}

void* GetSymbol(void* libHandle, const char* symbol)
{
void* addr = libHandle ? dlsym(libHandle, symbol) : nullptr;
Expand Down
5 changes: 1 addition & 4 deletions framework/src/bundle/BundleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ namespace BundleUtils
{
// returns the handle to the current executable
void* GetExecutableHandle();

// returns the file path of the current executable
std::string GetExecutablePath();


// returns the address of the symbol in library libHandle
void* GetSymbol(void* libHandle, const char* symbol);
}
Expand Down
Loading