Skip to content

Commit ef76ac2

Browse files
author
Jan VL
committed
refactor(c-api): add logging infrastructure and explicit MQTT error reporting
1 parent deec61f commit ef76ac2

File tree

6 files changed

+69
-33
lines changed

6 files changed

+69
-33
lines changed

include/sparkplug/edge_node.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// include/sparkplug/edge_node.hpp
21
#pragma once
32

43
#include "detail/compat.hpp"
4+
#include "logging.hpp"
55
#include "mqtt_handle.hpp"
66
#include "payload_builder.hpp"
77
#include "sparkplug_b.pb.h"
@@ -129,13 +129,9 @@ class EdgeNode {
129129
username{}; ///< MQTT username for authentication (optional)
130130
std::optional<std::string>
131131
password{}; ///< MQTT password for authentication (optional)
132-
std::optional<CommandCallback>
133-
command_callback{}; ///< Optional callback for NCMD messages (subscribed before
134-
///< NBIRTH)
135-
std::optional<std::string>
136-
primary_host_id{}; ///< Optional Primary Host Application ID. If set, EdgeNode
137-
///< will subscribe to STATE/{primary_host_id} and wait for
138-
///< {"online":true} before publishing NBIRTH/DBIRTH
132+
std::optional<CommandCallback> command_callback{};
133+
std::optional<std::string> primary_host_id{};
134+
std::optional<LogCallback> log_callback{};
139135
};
140136

141137
/**
@@ -179,6 +175,8 @@ class EdgeNode {
179175
*/
180176
void set_tls(std::optional<TlsOptions> tls);
181177

178+
void set_log_callback(std::optional<LogCallback> callback);
179+
182180
/**
183181
* @brief Connects to the MQTT broker and establishes a Sparkplug B session.
184182
*
@@ -424,6 +422,8 @@ class EdgeNode {
424422
std::string_view target_device_id,
425423
PayloadBuilder& payload);
426424

425+
void log(LogLevel level, std::string_view message) const noexcept;
426+
427427
private:
428428
/**
429429
* @brief Tracks state for an individual device attached to this edge node.

include/sparkplug/host_application.hpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// include/sparkplug/host_application.hpp
21
#pragma once
32

43
#include "detail/compat.hpp"
4+
#include "logging.hpp"
55
#include "mqtt_handle.hpp"
66
#include "payload_builder.hpp"
77
#include "sparkplug_b.pb.h"
@@ -19,21 +19,6 @@
1919

2020
namespace sparkplug {
2121

22-
/**
23-
* @brief Log severity levels for library diagnostics.
24-
*/
25-
enum class LogLevel {
26-
DEBUG = 0, ///< Detailed debugging information
27-
INFO = 1, ///< Informational messages
28-
WARN = 2, ///< Warning messages (potential issues)
29-
ERROR = 3 ///< Error messages (serious problems)
30-
};
31-
32-
/**
33-
* @brief Callback function type for receiving log messages from the library.
34-
*/
35-
using LogCallback = std::function<void(LogLevel, std::string_view)>;
36-
3722
/**
3823
* @brief Callback function type for receiving Sparkplug B messages.
3924
*

include/sparkplug/logging.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <string_view>
5+
6+
namespace sparkplug {
7+
8+
enum class LogLevel { DEBUG = 0, INFO = 1, WARN = 2, ERROR = 3 };
9+
10+
using LogCallback = std::function<void(LogLevel, std::string_view)>;
11+
12+
} // namespace sparkplug

include/sparkplug/sparkplug_c.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,10 @@ int sparkplug_publisher_set_tls(sparkplug_publisher_t* pub,
210210
const char* private_key_password,
211211
int enable_server_cert_auth);
212212

213-
/**
214-
* @brief Connects the publisher to the MQTT broker.
215-
*
216-
* @param pub Publisher handle
217-
* @return 0 on success, -1 on failure
218-
*/
213+
void sparkplug_publisher_set_log_callback(sparkplug_publisher_t* pub,
214+
sparkplug_log_callback_t callback,
215+
void* user_data);
216+
219217
int sparkplug_publisher_connect(sparkplug_publisher_t* pub);
220218

221219
/**

src/c_bindings.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// src/c_bindings.cpp
21
#include "sparkplug/edge_node.hpp"
32
#include "sparkplug/host_application.hpp"
43
#include "sparkplug/payload_builder.hpp"
@@ -197,10 +196,35 @@ int sparkplug_publisher_set_tls(sparkplug_publisher_t* pub,
197196
return 0;
198197
}
199198

199+
void sparkplug_publisher_set_log_callback(sparkplug_publisher_t* pub,
200+
sparkplug_log_callback_t callback,
201+
void* user_data) {
202+
if (!pub) {
203+
return;
204+
}
205+
206+
if (callback) {
207+
auto cpp_callback = [callback, user_data](sparkplug::LogLevel level,
208+
std::string_view message) {
209+
callback(static_cast<int>(level), message.data(), message.size(), user_data);
210+
};
211+
pub->impl.set_log_callback(std::move(cpp_callback));
212+
} else {
213+
pub->impl.set_log_callback(std::nullopt);
214+
}
215+
}
216+
200217
int sparkplug_publisher_connect(sparkplug_publisher_t* pub) {
201218
if (!pub)
202219
return -1;
203-
return pub->impl.connect().has_value() ? 0 : -1;
220+
221+
auto result = pub->impl.connect();
222+
if (!result.has_value()) {
223+
auto error_msg = std::format("EdgeNode MQTT connection failed: {}", result.error());
224+
pub->impl.log(sparkplug::LogLevel::ERROR, error_msg);
225+
return -1;
226+
}
227+
return 0;
204228
}
205229

206230
int sparkplug_publisher_disconnect(sparkplug_publisher_t* pub) {
@@ -869,7 +893,13 @@ int sparkplug_host_application_connect(sparkplug_host_application_t* host) {
869893
}
870894

871895
auto result = host->impl.connect();
872-
return result.has_value() ? 0 : -1;
896+
if (!result.has_value()) {
897+
auto error_msg =
898+
std::format("HostApplication MQTT connection failed: {}", result.error());
899+
host->impl.log(sparkplug::LogLevel::ERROR, error_msg);
900+
return -1;
901+
}
902+
return 0;
873903
}
874904

875905
int sparkplug_host_application_disconnect(sparkplug_host_application_t* host) {

src/edge_node.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ void EdgeNode::set_tls(std::optional<TlsOptions> tls) {
188188
config_.tls = std::move(tls);
189189
}
190190

191+
void EdgeNode::set_log_callback(std::optional<LogCallback> callback) {
192+
std::lock_guard<std::mutex> lock(mutex_);
193+
config_.log_callback = std::move(callback);
194+
}
195+
191196
stdx::expected<void, std::string> EdgeNode::connect() {
192197
std::lock_guard<std::mutex> lock(mutex_);
193198

@@ -870,4 +875,10 @@ EdgeNode::publish_device_command(std::string_view target_edge_node_id,
870875
return publish_message(client, topic_str, payload_data, qos, false);
871876
}
872877

878+
void EdgeNode::log(LogLevel level, std::string_view message) const noexcept {
879+
if (config_.log_callback) {
880+
config_.log_callback.value()(level, message);
881+
}
882+
}
883+
873884
} // namespace sparkplug

0 commit comments

Comments
 (0)