Skip to content

Commit

Permalink
Logging durint test improvements.
Browse files Browse the repository at this point in the history
We use a custom logger for NUTS, and this fits within the NUTS
test framework well, so that if -v is supplied we get more content.
All tests now get this by default.
  • Loading branch information
gdamore committed Apr 21, 2024
1 parent 51261d0 commit 2726170
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/sp/transport/tcp/tcp_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
// Copyright 2018 Cody Piersall <cody.piersall@gmail.com>
Expand Down Expand Up @@ -256,7 +256,6 @@ test_tcp_recv_max(void)
char *addr;

NUTS_ADDR(addr, "tcp");
NUTS_ENABLE_LOG(NNG_LOG_INFO);

NUTS_OPEN(s0);
NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_RECVTIMEO, 100));
Expand Down
35 changes: 34 additions & 1 deletion src/testing/acutest.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* <https://github.com/mity/acutest>
*
* Copyright 2013-2020 Martin Mitas
* Copyright 2019 Garrett D'Amore
* Copyright 2024 Garrett D'Amore
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -361,6 +361,7 @@ extern const struct test_ test_list_[];
int test_check_(int cond, const char* file, int line, const char* fmt, ...);
void test_case_(const char* fmt, ...);
void test_message_(const char* fmt, ...);
void test_message_color_(int, const char* fmt, ...);
void test_dump_(const char* title, const void* addr, size_t size);
void test_abort_(void) TEST_ATTRIBUTE_(noreturn);

Expand Down Expand Up @@ -779,6 +780,38 @@ test_message_(const char* fmt, ...)
}
}

/* Print a message in color, unconditionally (based on verbosity). */
void TEST_ATTRIBUTE_(format (printf, 2, 3))
test_message_color_(int color, const char* fmt, ...)
{
char buffer[TEST_MSG_MAXSIZE];
char* line_beg;
char* line_end;
va_list args;

if(test_verbose_level_ < 3)
return;

va_start(args, fmt);
vsnprintf(buffer, TEST_MSG_MAXSIZE, fmt, args);
va_end(args);
buffer[TEST_MSG_MAXSIZE-1] = '\0';

line_beg = buffer;
while(1) {
line_end = strchr(line_beg, '\n');
if(line_end == NULL)
break;
test_line_indent_(test_case_name_[0] ? 3 : 2);
test_print_in_color_(color, "%.*s\n", (int)(line_end - line_beg), line_beg);
line_beg = line_end + 1;
}
if(line_beg[0] != '\0') {
test_line_indent_(test_case_name_[0] ? 3 : 2);
test_print_in_color_(color, "%s\n", line_beg);
}
}

void
test_dump_(const char* title, const void* addr, size_t size)
{
Expand Down
15 changes: 14 additions & 1 deletion src/testing/nuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@
#ifndef NNG_TESTING_NUTS_H
#define NNG_TESTING_NUTS_H

#include <nng/nng.h>
extern void nuts_logger(
nng_log_level, nng_log_facility, const char *, const char *);

// Call nng_fini during test finalization -- this avoids leak warnings.
extern void nng_fini(void);
#define TEST_FINI nng_fini()
#define TEST_INIT \
do { \
nng_log_set_logger(nuts_logger); \
nng_log_set_level(NNG_LOG_DEBUG); \
} while (0)
#include "acutest.h"

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

// The following headers are provided for test code convenience.
#include <nng/nng.h>
#include <nng/protocol/bus0/bus.h>
#include <nng/protocol/pair0/pair.h>
#include <nng/protocol/pair1/pair.h>
Expand Down Expand Up @@ -210,6 +218,11 @@ extern const char *nuts_garbled_crt;
nng_log_set_level(level); \
} while (0)

#define NUTS_LOGGING() \
do { \
nng_log_set_logger(nuts_logger); \
nng_log_set_level(NNG_LOG_DEBUG); \
} while (0)
#ifdef __cplusplus
};
#endif
Expand Down
48 changes: 46 additions & 2 deletions src/testing/util.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -8,6 +8,7 @@
// found online at https://opensource.org/licenses/MIT.
//

#include "nng/nng.h"
#define TEST_NO_MAIN

#ifdef _WIN32
Expand Down Expand Up @@ -72,7 +73,7 @@ nuts_clock(void)
}
tv.tv_sec -= epoch;
return (
((uint64_t)(tv.tv_sec) * 1000) + (uint64_t)(tv.tv_usec / 1000));
((uint64_t) (tv.tv_sec) * 1000) + (uint64_t) (tv.tv_usec / 1000));
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -162,3 +163,46 @@ nuts_sleep(int msec)
poll(NULL, 0, msec);
#endif
}

#define NUTS_COLOR_DEFAULT_ 0
#define NUTS_COLOR_GREEN_ 1
#define NUTS_COLOR_RED_ 2
#define NUTS_COLOR_DEFAULT_INTENSIVE_ 3
#define NUTS_COLOR_GREEN_INTENSIVE_ 4
#define NUTS_COLOR_RED_INTENSIVE_ 5

void
nuts_logger(nng_log_level level, nng_log_facility fac, const char *msgid,
const char *msg)
{
(void) fac;
char *lstr;
int color;
switch (level) {
case NNG_LOG_DEBUG:
lstr = "DEBUG";
color = NUTS_COLOR_DEFAULT_;
break;
case NNG_LOG_INFO:
lstr = "INFO";
color = NUTS_COLOR_DEFAULT_;
break;
case NNG_LOG_NOTICE:
lstr = "NOTICE";
color = NUTS_COLOR_DEFAULT_INTENSIVE_;
break;
case NNG_LOG_WARN:
lstr = "WARNING";
color = NUTS_COLOR_RED_;
break;
case NNG_LOG_ERR:
lstr = "ERROR";
color = NUTS_COLOR_RED_INTENSIVE_;
break;
default:
lstr = "LEVEL UNKNOWN";
color = NUTS_COLOR_DEFAULT_;
break;
}
test_message_color_(color, "%s: %s: %s", lstr, msgid, msg);
}

0 comments on commit 2726170

Please sign in to comment.