Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : #include "SolidSyslogEventSd.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : : #include <stdint.h>
10 : :
11 : : #include "SolidSyslogError.h"
12 : : #include "SolidSyslogErrorCategory.h"
13 : : #include "SolidSyslogSdElement.h"
14 : : #include "SolidSyslogSdValue.h"
15 : : #include "SolidSyslogStructuredDataDefinition.h"
16 : :
17 : : static void EventSd_Format(struct SolidSyslogStructuredData* base, struct SolidSyslogSdElement* element);
18 : : static inline const struct SolidSyslogEventSd* EventSd_SelfFromBase(const struct SolidSyslogStructuredData* base);
19 : : static inline bool EventSd_HasSourceName(const struct SolidSyslogErrorEvent* event);
20 : : static inline void EventSd_Detail(struct SolidSyslogSdValue* value, const struct SolidSyslogErrorEvent* event);
21 : : static inline void EventSd_Int32(struct SolidSyslogSdValue* value, int32_t number);
22 : :
23 : 458 : struct SolidSyslogStructuredData* SolidSyslogEventSd_Initialise(
24 : : struct SolidSyslogEventSd* self,
25 : : const struct SolidSyslogErrorEvent* event
26 : : )
27 : : {
28 : 458 : self->Base.Format = EventSd_Format;
29 : 458 : self->Event = event;
30 : 458 : return &self->Base;
31 : : }
32 : :
33 : : /* cppcheck-suppress constParameterCallback -- the signature is the StructuredData vtable's Format. */
34 : 458 : static void EventSd_Format(struct SolidSyslogStructuredData* base, struct SolidSyslogSdElement* element)
35 : : {
36 : : /* The element's SD-ID is solidsyslog@67478: a private name under the IANA
37 : : * private enterprise number assigned to Cozens Software Solutions Limited. */
38 : : static const char name[] = "solidsyslog";
39 : 458 : const uint32_t enterpriseNumber = 67478U;
40 : 458 : const struct SolidSyslogErrorEvent* event = EventSd_SelfFromBase(base)->Event;
41 : :
42 : 458 : SolidSyslogSdElement_Begin(element, name, enterpriseNumber);
43 [ + + ]: 458 : if (EventSd_HasSourceName(event))
44 : : {
45 : 454 : SolidSyslogSdValue_String(SolidSyslogSdElement_Param(element, "source"), event->Source->Name);
46 : : }
47 : 458 : SolidSyslogSdValue_String(
48 : : SolidSyslogSdElement_Param(element, "category"),
49 : 458 : SolidSyslogErrorCategory_Name(event->Category)
50 : : );
51 : 458 : EventSd_Detail(SolidSyslogSdElement_Param(element, "detail"), event);
52 [ + + ]: 458 : if (event->HasNativeCode)
53 : : {
54 : 15 : EventSd_Int32(SolidSyslogSdElement_Param(element, "native"), event->NativeCode);
55 : : }
56 : 458 : SolidSyslogSdElement_End(element);
57 : 458 : }
58 : :
59 : 458 : static inline const struct SolidSyslogEventSd* EventSd_SelfFromBase(const struct SolidSyslogStructuredData* base)
60 : : {
61 : 458 : return (const struct SolidSyslogEventSd*) base;
62 : : }
63 : :
64 : : /* An integrator's own SolidSyslog_Error call may carry no source, or one
65 : : * with no name. */
66 : 458 : static inline bool EventSd_HasSourceName(const struct SolidSyslogErrorEvent* event)
67 : : {
68 [ + + + + ]: 458 : return (event->Source != NULL) && (event->Source->Name != NULL);
69 : : }
70 : :
71 : 458 : static inline void EventSd_Detail(struct SolidSyslogSdValue* value, const struct SolidSyslogErrorEvent* event)
72 : : {
73 : 458 : const char* name = SolidSyslogErrorEvent_DetailName(event);
74 : :
75 [ + + ]: 458 : if (name != NULL)
76 : : {
77 : 403 : SolidSyslogSdValue_String(value, name);
78 : : }
79 : : else
80 : : {
81 : 55 : EventSd_Int32(value, event->Detail);
82 : : }
83 : 458 : }
84 : :
85 : : /* SdValue writes unsigned numbers only, so a negative one is its sign and
86 : : * then its magnitude, taken in unsigned arithmetic so that INT32_MIN has one. */
87 : 70 : static inline void EventSd_Int32(struct SolidSyslogSdValue* value, int32_t number)
88 : : {
89 : 70 : uint32_t magnitude = (uint32_t) number;
90 : :
91 [ + + ]: 70 : if (number < 0)
92 : : {
93 : 4 : SolidSyslogSdValue_String(value, "-");
94 : 4 : magnitude = 0U - magnitude;
95 : : }
96 : :
97 : 70 : SolidSyslogSdValue_Uint32(value, magnitude);
98 : 70 : }
|