Merge pull request #23309 from DaanDeMeyer/log-context
basic: Add log context
This commit is contained in:
+25
-26
@@ -19,7 +19,7 @@ int main(int argc, const char *argv[]) {
|
||||
for (i = 0; i < ELEMENTSOF(items); i++) {
|
||||
LIST_INIT(item_list, &items[i]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[i]));
|
||||
LIST_PREPEND(item_list, head, &items[i]);
|
||||
assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
@@ -55,14 +55,13 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[2].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
list_item *cursor;
|
||||
LIST_FIND_HEAD(item_list, &items[0], cursor);
|
||||
list_item *cursor = LIST_FIND_HEAD(item_list, &items[0]);
|
||||
assert_se(cursor == &items[3]);
|
||||
|
||||
LIST_FIND_TAIL(item_list, &items[3], cursor);
|
||||
cursor = LIST_FIND_TAIL(item_list, &items[3]);
|
||||
assert_se(cursor == &items[0]);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[1]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[1]));
|
||||
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
@@ -73,7 +72,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[2].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]);
|
||||
assert_se(LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]) == &items[1]);
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
assert_se(items[2].item_list_next == &items[0]);
|
||||
assert_se(items[1].item_list_next == &items[2]);
|
||||
@@ -84,7 +83,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[1].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[1]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[1]));
|
||||
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
@@ -95,7 +94,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[2].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]);
|
||||
assert_se(LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]) == &items[1]);
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
assert_se(items[2].item_list_next == &items[0]);
|
||||
assert_se(items[1].item_list_next == &items[2]);
|
||||
@@ -106,7 +105,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[1].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[0]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[0]));
|
||||
|
||||
assert_se(items[2].item_list_next == NULL);
|
||||
@@ -117,7 +116,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[1].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]);
|
||||
assert_se(LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]) == &items[0]);
|
||||
assert_se(items[2].item_list_next == NULL);
|
||||
assert_se(items[1].item_list_next == &items[2]);
|
||||
assert_se(items[3].item_list_next == &items[1]);
|
||||
@@ -129,7 +128,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[0].item_list_prev == NULL);
|
||||
assert_se(head == &items[0]);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[0]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[0]));
|
||||
|
||||
assert_se(items[2].item_list_next == NULL);
|
||||
@@ -140,7 +139,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[1].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]);
|
||||
assert_se(LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]) == &items[0]);
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
assert_se(items[2].item_list_next == &items[0]);
|
||||
assert_se(items[1].item_list_next == &items[2]);
|
||||
@@ -151,7 +150,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[1].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[0]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[0]));
|
||||
|
||||
assert_se(items[2].item_list_next == NULL);
|
||||
@@ -162,7 +161,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[1].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[1]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[1]));
|
||||
|
||||
assert_se(items[2].item_list_next == NULL);
|
||||
@@ -171,18 +170,18 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[2].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[2]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[2]) == &items[2]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[2]));
|
||||
assert_se(LIST_JUST_US(item_list, head));
|
||||
|
||||
LIST_REMOVE(item_list, head, &items[3]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[3]) == &items[3]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[3]));
|
||||
|
||||
assert_se(head == NULL);
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(items); i++) {
|
||||
assert_se(LIST_JUST_US(item_list, &items[i]));
|
||||
LIST_APPEND(item_list, head, &items[i]);
|
||||
assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]);
|
||||
}
|
||||
|
||||
assert_se(!LIST_JUST_US(item_list, head));
|
||||
@@ -198,20 +197,20 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[3].item_list_prev == &items[2]);
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(items); i++)
|
||||
LIST_REMOVE(item_list, head, &items[i]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
|
||||
|
||||
assert_se(head == NULL);
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(items) / 2; i++) {
|
||||
LIST_INIT(item_list, &items[i]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[i]));
|
||||
LIST_PREPEND(item_list, head, &items[i]);
|
||||
assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
|
||||
}
|
||||
|
||||
for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) {
|
||||
LIST_INIT(item_list, &items[i]);
|
||||
assert_se(LIST_JUST_US(item_list, &items[i]));
|
||||
LIST_PREPEND(item_list, head2, &items[i]);
|
||||
assert_se(LIST_PREPEND(item_list, head2, &items[i]) == &items[i]);
|
||||
}
|
||||
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
@@ -224,7 +223,7 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[2].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_JOIN(item_list, head2, head);
|
||||
assert_se(LIST_JOIN(item_list, head2, head) == head2);
|
||||
assert_se(head == NULL);
|
||||
|
||||
assert_se(items[0].item_list_next == NULL);
|
||||
@@ -237,18 +236,18 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(items[2].item_list_prev == &items[3]);
|
||||
assert_se(items[3].item_list_prev == NULL);
|
||||
|
||||
LIST_JOIN(item_list, head, head2);
|
||||
assert_se(LIST_JOIN(item_list, head, head2) == head);
|
||||
assert_se(head2 == NULL);
|
||||
assert_se(head);
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(items); i++)
|
||||
LIST_REMOVE(item_list, head, &items[i]);
|
||||
assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
|
||||
|
||||
assert_se(head == NULL);
|
||||
|
||||
LIST_PREPEND(item_list, head, items + 0);
|
||||
LIST_PREPEND(item_list, head, items + 1);
|
||||
LIST_PREPEND(item_list, head, items + 2);
|
||||
assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0);
|
||||
assert_se(LIST_PREPEND(item_list, head, items + 1) == items + 1);
|
||||
assert_se(LIST_PREPEND(item_list, head, items + 2) == items + 2);
|
||||
|
||||
assert_se(LIST_POP(item_list, head) == items + 2);
|
||||
assert_se(LIST_POP(item_list, head) == items + 1);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "log.h"
|
||||
#include "process-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
|
||||
assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL)));
|
||||
assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL));
|
||||
@@ -69,6 +70,75 @@ static void test_log_syntax(void) {
|
||||
assert_se(log_syntax("unit", LOG_ERR, "filename", 10, SYNTHETIC_ERRNO(ENOTTY), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY);
|
||||
}
|
||||
|
||||
static void test_log_context(void) {
|
||||
{
|
||||
char **strv = STRV_MAKE("FIRST=abc", "SECOND=qrs");
|
||||
|
||||
LOG_CONTEXT_PUSH("THIRD=pfs");
|
||||
LOG_CONTEXT_PUSH("FOURTH=def");
|
||||
LOG_CONTEXT_PUSH_STRV(strv);
|
||||
LOG_CONTEXT_PUSH_STRV(strv);
|
||||
|
||||
/* Test that the log context was set up correctly. */
|
||||
assert_se(log_context_num_contexts() == 4);
|
||||
assert_se(log_context_num_fields() == 6);
|
||||
|
||||
/* Test that everything still works with modifications to the log context. */
|
||||
test_log_struct();
|
||||
test_long_lines();
|
||||
test_log_syntax();
|
||||
|
||||
{
|
||||
LOG_CONTEXT_PUSH("FIFTH=123");
|
||||
LOG_CONTEXT_PUSH_STRV(strv);
|
||||
|
||||
/* Check that our nested fields got added correctly. */
|
||||
assert_se(log_context_num_contexts() == 6);
|
||||
assert_se(log_context_num_fields() == 9);
|
||||
|
||||
/* Test that everything still works in a nested block. */
|
||||
test_log_struct();
|
||||
test_long_lines();
|
||||
test_log_syntax();
|
||||
}
|
||||
|
||||
/* Check that only the fields from the nested block got removed. */
|
||||
assert_se(log_context_num_contexts() == 4);
|
||||
assert_se(log_context_num_fields() == 6);
|
||||
}
|
||||
|
||||
assert_se(log_context_num_contexts() == 0);
|
||||
assert_se(log_context_num_fields() == 0);
|
||||
|
||||
{
|
||||
_cleanup_(log_context_freep) LogContext *ctx = NULL;
|
||||
|
||||
char **strv = STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
|
||||
assert_se(ctx = log_context_new(strv, /*owned=*/ false));
|
||||
|
||||
assert_se(log_context_num_contexts() == 1);
|
||||
assert_se(log_context_num_fields() == 2);
|
||||
|
||||
/* Test that everything still works with a manually configured log context. */
|
||||
test_log_struct();
|
||||
test_long_lines();
|
||||
test_log_syntax();
|
||||
}
|
||||
|
||||
{
|
||||
char **strv = NULL;
|
||||
|
||||
assert_se(strv = strv_new("ABC", "DEF"));
|
||||
LOG_CONTEXT_CONSUME_STRV(strv);
|
||||
|
||||
assert_se(log_context_num_contexts() == 1);
|
||||
assert_se(log_context_num_fields() == 2);
|
||||
}
|
||||
|
||||
assert_se(log_context_num_contexts() == 0);
|
||||
assert_se(log_context_num_fields() == 0);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
test_file();
|
||||
|
||||
@@ -81,6 +151,7 @@ int main(int argc, char* argv[]) {
|
||||
test_log_struct();
|
||||
test_long_lines();
|
||||
test_log_syntax();
|
||||
test_log_context();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -942,4 +942,16 @@ TEST(strv_fnmatch) {
|
||||
assert_se(pos == 1);
|
||||
}
|
||||
|
||||
TEST(strv_extend_join) {
|
||||
_cleanup_strv_free_ char **v = NULL;
|
||||
|
||||
assert_se(strv_extend_assignment(&v, "MESSAGE", "ABC") >= 0);
|
||||
assert_se(strv_extend_assignment(&v, "ABC", "QER") >= 0);
|
||||
assert_se(strv_extend_assignment(&v, "MISSING", NULL) >= 0);
|
||||
|
||||
assert_se(strv_length(v) == 2);
|
||||
assert_se(streq(v[0], "MESSAGE=ABC"));
|
||||
assert_se(streq(v[1], "ABC=QER"));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_INFO);
|
||||
|
||||
Reference in New Issue
Block a user