|
1 | | -// src/c_bindings.cpp |
2 | 1 | #include "sparkplug/edge_node.hpp" |
3 | 2 | #include "sparkplug/host_application.hpp" |
4 | 3 | #include "sparkplug/payload_builder.hpp" |
@@ -197,10 +196,35 @@ int sparkplug_publisher_set_tls(sparkplug_publisher_t* pub, |
197 | 196 | return 0; |
198 | 197 | } |
199 | 198 |
|
| 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 | + |
200 | 217 | int sparkplug_publisher_connect(sparkplug_publisher_t* pub) { |
201 | 218 | if (!pub) |
202 | 219 | 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; |
204 | 228 | } |
205 | 229 |
|
206 | 230 | int sparkplug_publisher_disconnect(sparkplug_publisher_t* pub) { |
@@ -869,7 +893,13 @@ int sparkplug_host_application_connect(sparkplug_host_application_t* host) { |
869 | 893 | } |
870 | 894 |
|
871 | 895 | 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; |
873 | 903 | } |
874 | 904 |
|
875 | 905 | int sparkplug_host_application_disconnect(sparkplug_host_application_t* host) { |
|
0 commit comments