Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions score/os/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,32 @@ cc_library(
],
)

cc_library(
name = "syslog_impl",
srcs = [
"syslog.cpp",
"syslog_impl.cpp",
],
hdrs = [
"syslog.h",
"syslog_impl.h",
],
features = COMPILER_WARNING_FEATURES,
tags = ["FFI"],
target_compatible_with = ["@platforms//os:linux"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QNX has syslog API as well

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the idea to use slog on qnx and only syslog on linux providing the equivalent functionality on target.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to give freedom to use it also on QNX? I would add qnx target as far as code is the same

@btirunagaru btirunagaru Aug 26, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as i mentioned - the kSystem log mode on QNX is already filled by slog and its widely used on QNX and it make sense to use that.
so i would assume the choice of the users to use slog vs syslog on QNX is more of a different ask. Till the community show interest to have this dual choice. its best we treat this and compile for Linux only and enable for QNX later (until we find a genuine user for this back end)
If there is a interest and the enablement for QNX can come later as a different requirement if that's the need.

visibility = ["//visibility:public"],
deps = [
":errno",
":object_seam",
],
)

alias(
name = "syslog",
actual = "syslog_impl",
visibility = ["//visibility:public"],
)

cc_library(
name = "arpa_inet",
srcs = ["arpa_inet.cpp"],
Expand Down
13 changes: 13 additions & 0 deletions score/os/mocklib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ cc_library(
],
)

cc_library(
name = "syslog_mock",
testonly = True,
srcs = ["mock_syslog.cpp"],
hdrs = ["mock_syslog.h"],
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
deps = [
"@googletest//:gtest",
"@score_baselibs//score/os:syslog",
],
)

cc_library(
name = "pthread_mock",
testonly = True,
Expand Down
53 changes: 53 additions & 0 deletions score/os/mocklib/mock_syslog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "score/os/mocklib/mock_syslog.h"

#include <cstdarg>
#include <cstdio>
#include <string>

namespace score
{
namespace os
{

// NOLINTNEXTLINE(cert-dcl50-cpp, modernize-avoid-variadic-functions)
void MockSyslog::syslog(std::int32_t priority, const char* format, ...) const noexcept
{
// Create a va_list to hold the variable arguments
va_list args;
va_start(args, format);

// Determine required buffer size
const auto message_length = std::vsnprintf(nullptr, 0, format, args);

// Reset the va_list to be able to use it again
va_end(args);
va_start(args, format);

// Create buffer
std::string message{};
message.resize(static_cast<std::size_t>(message_length));

// Write formatted string to buffer
const auto length_including_terminator = static_cast<std::size_t>(message_length) + 1U;
std::vsnprintf(&message[0], length_including_terminator, format, args);

// Clean up the va_list
va_end(args);

MockedSyslog(priority, message);
}

} // namespace os
} // namespace score
49 changes: 49 additions & 0 deletions score/os/mocklib/mock_syslog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#ifndef SCORE_LIB_OS_MOCKLIB_MOCK_SYSLOG_H
#define SCORE_LIB_OS_MOCKLIB_MOCK_SYSLOG_H

#include "score/os/syslog.h"

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

#include <string>

namespace score
{
namespace os
{

class MockSyslog : public Syslog
{
public:
MOCK_METHOD(void,
openlog,
(const char* ident, std::int32_t option, std::int32_t facility),
(const, noexcept, override));

MOCK_METHOD(void, closelog, (), (const, noexcept, override));

// Googletest does not support variadic arguments. Therefore we pass through the formatted string so that we can
// use it with MOCK_METHOD.
MOCK_METHOD(void, MockedSyslog, (std::int32_t priority, const std::string& message), (const, noexcept));

void syslog(std::int32_t priority, const char* format, ...) const noexcept override
__attribute__((format(printf, 3, 4)));
};

} // namespace os
} // namespace score

#endif // SCORE_LIB_OS_MOCKLIB_MOCK_SYSLOG_H
29 changes: 29 additions & 0 deletions score/os/syslog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "score/os/syslog.h"

#include "score/os/syslog_impl.h"

namespace score
{
namespace os
{

/* score::cpp::pmr::make_unique takes non-const memory_resource */
score::cpp::pmr::unique_ptr<Syslog> Syslog::Default(score::cpp::pmr::memory_resource* memory_resource) noexcept
{
return score::cpp::pmr::make_unique<SyslogImpl>(memory_resource);
}

} // namespace os
} // namespace score
57 changes: 57 additions & 0 deletions score/os/syslog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#ifndef SCORE_LIB_OS_SYSLOG_H
#define SCORE_LIB_OS_SYSLOG_H

#include "score/os/errno.h"

#include "score/memory.hpp"

#include <syslog.h>

namespace score
{
namespace os
{

/// @brief Object-seam wrapper around the POSIX/glibc syslog(3) family, allowing
/// openlog/syslog/closelog to be mocked in host unit tests.
class Syslog
{
public:
static score::cpp::pmr::unique_ptr<Syslog> Default(score::cpp::pmr::memory_resource* memory_resource) noexcept;

virtual void openlog(const char* const ident,
const std::int32_t option,
const std::int32_t facility) const noexcept = 0;

virtual void syslog(const std::int32_t priority, const char* const format, ...) const noexcept
__attribute__((format(printf, 3, 4))) = 0;

virtual void closelog() const noexcept = 0;

/// @brief Construct a new Syslog object
///
Syslog() = default;

virtual ~Syslog() = default;
Syslog(const Syslog&) = delete;
Syslog& operator=(const Syslog&) = delete;
Syslog(Syslog&& other) = delete;
Syslog& operator=(Syslog&& other) = delete;
};

} // namespace os
} // namespace score

#endif // SCORE_LIB_OS_SYSLOG_H
50 changes: 50 additions & 0 deletions score/os/syslog_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "score/os/syslog_impl.h"

#include <cstdarg>

namespace score
{
namespace os
{

void SyslogImpl::openlog(const char* const ident, const std::int32_t option, const std::int32_t facility) const noexcept
{
::openlog(ident, option, facility);
}

// NOLINTNEXTLINE(cert-dcl50-cpp, modernize-avoid-variadic-functions)
void SyslogImpl::syslog(const std::int32_t priority, const char* const format, ...) const noexcept
{
// Suppressed here because POSIX method accepts va_list
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) see comment above
va_list args;
// Suppressed here because POSIX method accepts va_list
// NOLINTNEXTLINE(hicpp-no-array-decay, cppcoreguidelines-pro-type-vararg) see comment above
va_start(args, format); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay) see comment above
// Suppressed here because POSIX method accepts va_list
// NOLINTNEXTLINE(*array-decay, *pro-type-vararg,*array-to-pointer-decay) see comment above
::vsyslog(priority, format, args);
// Suppressed here because POSIX method accepts va_list
// NOLINTNEXTLINE(hicpp-no-array-decay, cppcoreguidelines-pro-type-vararg) see comment above
va_end(args); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay) see comment above
}

void SyslogImpl::closelog() const noexcept
{
::closelog();
}

} // namespace os
} // namespace score
39 changes: 39 additions & 0 deletions score/os/syslog_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#ifndef SCORE_LIB_OS_SYSLOG_IMPL_H
#define SCORE_LIB_OS_SYSLOG_IMPL_H

#include "score/os/syslog.h"

namespace score
{
namespace os
{

class SyslogImpl : public Syslog
{
public:
void openlog(const char* const ident,
const std::int32_t option,
const std::int32_t facility) const noexcept override;

void syslog(const std::int32_t priority, const char* const format, ...) const noexcept override
__attribute__((format(printf, 3, 4)));

void closelog() const noexcept override;
};

} // namespace os
} // namespace score

#endif // SCORE_LIB_OS_SYSLOG_IMPL_H
17 changes: 17 additions & 0 deletions score/os/test/linux/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test_suite(
name = "unit_tests_linux",
tests = [
":pthread_test",
":syslog_test",
":unistd_test",
],
visibility = ["@score_baselibs//score/os/test:__subpackages__"],
Expand Down Expand Up @@ -53,3 +54,19 @@ cc_test(
"@score_baselibs//score/os:pthread",
],
)

cc_test(
name = "syslog_test",
srcs = [
"syslog_test.cpp",
],
features = COMPILER_WARNING_FEATURES,
tags = [
"unit",
],
target_compatible_with = ["@platforms//os:linux"],
deps = [
"@googletest//:gtest_main",
"@score_baselibs//score/os:syslog",
],
)
Loading
Loading