Skip to content

Commit 48910cd

Browse files
committed
Remove union in favour of void *
1 parent 34387eb commit 48910cd

File tree

5 files changed

+60
-49
lines changed

5 files changed

+60
-49
lines changed

src/libmongoc/src/mongoc/mongoc-structured-log-command.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
static void
2222
_mongoc_log_structured_append_command_data (
23-
mongoc_structured_log_entry_t *entry)
23+
void *structured_log_data, bson_t *structured_message /* OUT */)
2424
{
25-
_mongoc_structured_log_command_t *log_command = entry->command;
25+
_mongoc_structured_log_command_t *log_command =
26+
(_mongoc_structured_log_command_t *) structured_log_data;
2627

27-
BCON_APPEND (entry->structured_message,
28+
BCON_APPEND (structured_message,
2829
"commandName",
2930
BCON_UTF8 (log_command->command_name),
3031
"requestId",
@@ -41,18 +42,22 @@ _mongoc_log_structured_append_command_data (
4142

4243
static void
4344
mongoc_log_structured_build_command_started_message (
44-
mongoc_structured_log_entry_t *entry)
45+
mongoc_structured_log_component_t component,
46+
void *structured_log_data,
47+
bson_t *structured_message /* OUT */)
4548
{
4649
char *cmd_json;
47-
_mongoc_structured_log_command_t *log_command = entry->command;
50+
_mongoc_structured_log_command_t *log_command =
51+
(_mongoc_structured_log_command_t *) structured_log_data;
4852

49-
BSON_ASSERT (entry->component == MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND);
53+
BSON_ASSERT (component == MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND);
5054

5155
cmd_json = bson_as_canonical_extended_json (log_command->command, NULL);
5256

53-
_mongoc_log_structured_append_command_data (entry);
57+
_mongoc_log_structured_append_command_data (structured_log_data,
58+
structured_message);
5459

55-
BCON_APPEND (entry->structured_message,
60+
BCON_APPEND (structured_message,
5661
"databaseName",
5762
BCON_UTF8 (log_command->db_name),
5863
"command",
@@ -63,18 +68,22 @@ mongoc_log_structured_build_command_started_message (
6368

6469
static void
6570
mongoc_log_structured_build_command_succeeded_message (
66-
mongoc_structured_log_entry_t *entry)
71+
mongoc_structured_log_component_t component,
72+
void *structured_log_data,
73+
bson_t *structured_message /* OUT */)
6774
{
6875
char *reply_json;
69-
_mongoc_structured_log_command_t *log_command = entry->command;
76+
_mongoc_structured_log_command_t *log_command =
77+
(_mongoc_structured_log_command_t *) structured_log_data;
7078

71-
BSON_ASSERT (entry->component == MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND);
79+
BSON_ASSERT (component == MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND);
7280

7381
reply_json = bson_as_canonical_extended_json (log_command->reply, NULL);
7482

75-
_mongoc_log_structured_append_command_data (entry);
83+
_mongoc_log_structured_append_command_data (structured_log_data,
84+
structured_message);
7685

77-
BCON_APPEND (entry->structured_message,
86+
BCON_APPEND (structured_message,
7887
"duration",
7988
BCON_INT64 (log_command->duration),
8089
"reply",
@@ -85,21 +94,25 @@ mongoc_log_structured_build_command_succeeded_message (
8594

8695
static void
8796
mongoc_log_structured_build_command_failed_message (
88-
mongoc_structured_log_entry_t *entry)
97+
mongoc_structured_log_component_t component,
98+
void *structured_log_data,
99+
bson_t *structured_message /* OUT */)
89100
{
90101
char *reply_json;
91-
_mongoc_structured_log_command_t *log_command = entry->command;
102+
_mongoc_structured_log_command_t *log_command =
103+
(_mongoc_structured_log_command_t *) structured_log_data;
92104

93-
BSON_ASSERT (entry->component == MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND);
105+
BSON_ASSERT (component == MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND);
94106

95107
reply_json = bson_as_canonical_extended_json (log_command->reply, NULL);
96108

97-
_mongoc_log_structured_append_command_data (entry);
109+
_mongoc_log_structured_append_command_data (structured_log_data,
110+
structured_message);
98111

99-
BCON_APPEND (entry->structured_message, "reply", BCON_UTF8 (reply_json));
112+
BCON_APPEND (structured_message, "reply", BCON_UTF8 (reply_json));
100113

101114
if (log_command->error) {
102-
BCON_APPEND (entry->structured_message,
115+
BCON_APPEND (structured_message,
103116
"failure",
104117
BCON_UTF8 (log_command->error->message));
105118
}

src/libmongoc/src/mongoc/mongoc-structured-log-connection.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ mongoc_structured_log_connection_client_created (void)
2424
"Client created",
2525
NULL,
2626
NULL);
27-
2827
}

src/libmongoc/src/mongoc/mongoc-structured-log-private.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
#define MONGOC_STRUCTURED_LOG_DEFAULT_LEVEL MONGOC_STRUCTURED_LOG_LEVEL_WARNING;
2727

2828
typedef void (*mongoc_structured_log_build_message_t) (
29-
mongoc_structured_log_entry_t *entry);
29+
mongoc_structured_log_component_t component,
30+
void *structured_log_data,
31+
bson_t *structured_message /* OUT */);
3032

3133
struct _mongoc_structured_log_entry_t {
3234
mongoc_structured_log_level_t level;
3335
mongoc_structured_log_component_t component;
3436
const char *message;
3537
bson_t *structured_message;
3638
mongoc_structured_log_build_message_t build_message_func;
37-
union {
38-
_mongoc_structured_log_command_t *command;
39-
};
39+
void *structured_log_data;
4040
};
4141

4242
void

src/libmongoc/src/mongoc/mongoc-structured-log.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ mongoc_structured_log_entry_get_message (mongoc_structured_log_entry_t *entry)
5353
BCON_NEW ("message", BCON_UTF8 (entry->message));
5454

5555
if (entry->build_message_func) {
56-
entry->build_message_func (entry);
56+
entry->build_message_func (entry->component,
57+
entry->structured_log_data,
58+
entry->structured_message);
5759
}
5860
}
5961

src/libmongoc/tests/test-mongoc-structured-log.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ test_plain_log_entry ()
6262
{
6363
struct structured_log_state old_state;
6464
struct log_assumption assumption = {
65-
.expected_entry =
66-
{
67-
.level = MONGOC_STRUCTURED_LOG_LEVEL_WARNING,
68-
.component = MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND,
69-
.message = "Plain log entry",
70-
.structured_message =
71-
BCON_NEW ("message", BCON_UTF8 ("Plain log entry")),
72-
},
73-
.expected_calls = 1,
74-
.calls = 0,
65+
{
66+
MONGOC_STRUCTURED_LOG_LEVEL_WARNING,
67+
MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND,
68+
"Plain log entry",
69+
BCON_NEW ("message", BCON_UTF8 ("Plain log entry")),
70+
},
71+
1,
72+
0,
7573
};
7674

7775
save_state (&old_state);
@@ -90,28 +88,27 @@ test_plain_log_entry ()
9088
}
9189

9290
void
93-
_test_append_extra_data (mongoc_structured_log_entry_t *entry)
91+
_test_append_extra_data (mongoc_structured_log_component_t component, void *structured_log_data, bson_t *structured_message /* OUT */)
9492
{
95-
BCON_APPEND (entry->structured_message, "extra", BCON_INT32 (1));
93+
BCON_APPEND (structured_message, "extra", BCON_INT32 (1));
9694
}
9795

9896
void
9997
test_log_entry_with_extra_data ()
10098
{
10199
struct structured_log_state old_state;
102100
struct log_assumption assumption = {
103-
.expected_entry =
104-
{
105-
.level = MONGOC_STRUCTURED_LOG_LEVEL_WARNING,
106-
.component = MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND,
107-
.message = "Plain log entry",
108-
.structured_message = BCON_NEW ("message",
109-
BCON_UTF8 ("Plain log entry"),
110-
"extra",
111-
BCON_INT32 (1)),
112-
},
113-
.expected_calls = 1,
114-
.calls = 0,
101+
{
102+
MONGOC_STRUCTURED_LOG_LEVEL_WARNING,
103+
MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND,
104+
"Plain log entry",
105+
BCON_NEW ("message",
106+
BCON_UTF8 ("Plain log entry"),
107+
"extra",
108+
BCON_INT32 (1)),
109+
},
110+
1,
111+
0,
115112
};
116113

117114
save_state (&old_state);

0 commit comments

Comments
 (0)