Skip to content

Commit

Permalink
iox-eclipse-iceoryx#971 Introduce hoofs_logging and use LogDebug/LogE…
Browse files Browse the repository at this point in the history
…rror instead cout/clog/cerr in shared memory object

Signed-off-by: Christian Eltzschig <[email protected]>
  • Loading branch information
elfenpiff authored and andre-nguyen committed Apr 22, 2022
1 parent 6a3ac7d commit 0f0e054
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 19 deletions.
1 change: 1 addition & 0 deletions iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ add_library(iceoryx_hoofs
source/error_handling/error_handler.cpp
source/error_handling/error_handling.cpp
source/file_reader/file_reader.cpp
source/log/hoofs_logging.cpp
source/log/logcommon.cpp
source/log/logger.cpp
source/log/logging.cpp
Expand Down
50 changes: 50 additions & 0 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/log/hoofs_logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_LOG_HOOFS_LOGGING_HPP
#define IOX_HOOFS_LOG_HOOFS_LOGGING_HPP

/// @todo this might be needed to be public when the logger is used in templates
#include "iceoryx_hoofs/log/logging_free_function_building_block.hpp"

namespace iox
{
struct LoggingComponentHoofs
{
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr char Ctx[] = "HOOFS";
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr char Description[] = "Log context of the HOOFS component!";
};

// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LoggerHoofs = iox::log::ffbb::ComponentLogger<LoggingComponentHoofs>;

// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LogFatal = iox::log::ffbb::LogFatal<LoggingComponentHoofs>;
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LogError = iox::log::ffbb::LogError<LoggingComponentHoofs>;
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LogWarn = iox::log::ffbb::LogWarn<LoggingComponentHoofs>;
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LogInfo = iox::log::ffbb::LogInfo<LoggingComponentHoofs>;
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LogDebug = iox::log::ffbb::LogDebug<LoggingComponentHoofs>;
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr auto LogVerbose = iox::log::ffbb::LogVerbose<LoggingComponentHoofs>;

} // namespace iox

#endif
24 changes: 24 additions & 0 deletions iceoryx_hoofs/source/log/hoofs_logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/log/hoofs_logging.hpp"

namespace iox
{
constexpr char LoggingComponentHoofs::Ctx[];
constexpr char LoggingComponentHoofs::Description[];

} // namespace iox
32 changes: 13 additions & 19 deletions iceoryx_hoofs/source/posix_wrapper/shared_memory_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object.hpp"
#include "iceoryx_hoofs/cxx/attributes.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/internal/log/hoofs_logging.hpp"
#include "iceoryx_hoofs/platform/fcntl.hpp"
#include "iceoryx_hoofs/platform/unistd.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
Expand Down Expand Up @@ -66,8 +67,7 @@ SharedMemoryObject::SharedMemoryObject(const SharedMemory::Name_t& name,
.create()
.and_then([this](auto& sharedMemory) { m_sharedMemory.emplace(std::move(sharedMemory)); })
.or_else([this](auto&) {
std::cerr << "Unable to create SharedMemoryObject since we could not acquire a SharedMemory resource"
<< std::endl;
LogError() << "Unable to create SharedMemoryObject since we could not acquire a SharedMemory resource";
m_isInitialized = false;
m_errorValue = SharedMemoryObjectError::SHARED_MEMORY_CREATION_FAILED;
});
Expand All @@ -84,37 +84,30 @@ SharedMemoryObject::SharedMemoryObject(const SharedMemory::Name_t& name,
.create()
.and_then([this](auto& memoryMap) { m_memoryMap.emplace(std::move(memoryMap)); })
.or_else([this](auto) {
std::cerr << "Failed to map created shared memory into process!" << std::endl;
LogError() << "Failed to map created shared memory into process!";
m_isInitialized = false;
m_errorValue = SharedMemoryObjectError::MAPPING_SHARED_MEMORY_FAILED;
});
}

if (!m_isInitialized)
{
auto flags = std::cerr.flags();
std::cerr << "Unable to create a shared memory object with the following properties [ name = " << name
<< ", sizeInBytes = " << memorySizeInBytes
<< ", access mode = " << ACCESS_MODE_STRING[static_cast<uint64_t>(accessMode)]
<< ", open mode = " << OPEN_MODE_STRING[static_cast<uint64_t>(openMode)] << ", baseAddressHint = ";
if (baseAddressHint)
{
std::cerr << std::hex << *baseAddressHint << std::dec;
}
else
{
std::cerr << "no hint set";
}
std::cerr << ", permissions = " << std::bitset<sizeof(mode_t)>(static_cast<mode_t>(permissions)) << " ]"
<< std::endl;
std::cerr.setf(flags);
LogError() << "Unable to create a shared memory object with the following properties [ name = " << name
<< ", sizeInBytes = " << memorySizeInBytes
<< ", access mode = " << ACCESS_MODE_STRING[static_cast<uint64_t>(accessMode)]
<< ", open mode = " << OPEN_MODE_STRING[static_cast<uint64_t>(openMode)] << ", baseAddressHint = "
<< ((baseAddressHint) ? log::HexFormat(reinterpret_cast<uint64_t>(*baseAddressHint))
: log::HexFormat(static_cast<uint64_t>(0U)))
<< ((baseAddressHint) ? "" : " (no hint set)")
<< ", permissions = " << log::BinFormat(static_cast<mode_t>(permissions)) << " ]";
return;
}

m_allocator.emplace(m_memoryMap->getBaseAddress(), m_memorySizeInBytes);

if (m_isInitialized && m_sharedMemory->hasOwnership())
{
LogDebug() << "Trying to reserve " << m_memorySizeInBytes << " bytes in the shared memory [" << name << "]";
if (platform::IOX_SHM_WRITE_ZEROS_ON_CREATION)
{
// this lock is required for the case that multiple threads are creating multiple
Expand All @@ -138,6 +131,7 @@ SharedMemoryObject::SharedMemoryObject(const SharedMemory::Name_t& name,

memset(m_memoryMap->getBaseAddress(), 0, m_memorySizeInBytes);
}
LogDebug() << "Acquired " << m_memorySizeInBytes << " bytes successfully in the shared memory [" << name << "]";
}
}

Expand Down

0 comments on commit 0f0e054

Please sign in to comment.