aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Logging/test_ringlogger.c
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-03-17 00:41:10 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2019-03-17 08:51:27 +0100
commita6f80135ef65f33da67459b56c7f659c64d6d341 (patch)
treefcfe4ec56bdcac07ad235e9db9a83a530fd0e576 /WireGuard/Shared/Logging/test_ringlogger.c
parentmacOS: Tunnel detail: Activate / Deactivate is now a button (diff)
downloadwireguard-apple-a6f80135ef65f33da67459b56c7f659c64d6d341.tar.xz
wireguard-apple-a6f80135ef65f33da67459b56c7f659c64d6d341.zip
ringlogger: support mpsc for singlefile
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuard/Shared/Logging/test_ringlogger.c')
-rw-r--r--WireGuard/Shared/Logging/test_ringlogger.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/WireGuard/Shared/Logging/test_ringlogger.c b/WireGuard/Shared/Logging/test_ringlogger.c
new file mode 100644
index 00000000..ae3f4a93
--- /dev/null
+++ b/WireGuard/Shared/Logging/test_ringlogger.c
@@ -0,0 +1,63 @@
+#include "ringlogger.h"
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <sys/wait.h>
+
+static void forkwrite(void)
+{
+ struct log *log = open_log("/tmp/test_log");
+ char c[512];
+ int i, base;
+ bool in_fork = !fork();
+
+ base = 10000 * in_fork;
+ for (i = 0; i < 1024; ++i) {
+ snprintf(c, 512, "bla bla bla %d", base + i);
+ write_msg_to_log(log, "HMM", c);
+ }
+
+
+ if (in_fork)
+ _exit(0);
+ wait(NULL);
+
+ write_log_to_file("/dev/stdout", log);
+ close_log(log);
+}
+
+static void writetext(const char *text)
+{
+ struct log *log = open_log("/tmp/test_log");
+ write_msg_to_log(log, "TXT", text);
+ close_log(log);
+}
+
+static void show_line(const char *line, uint64_t time_ns)
+{
+ printf("%" PRIu64 ": %s\n", time_ns, line);
+}
+
+static void follow(void)
+{
+ uint32_t cursor = -1;
+ struct log *log = open_log("/tmp/test_log");
+
+ for (;;) {
+ cursor = view_lines_from_cursor(log, cursor, show_line);
+ usleep(1000 * 300);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ if (!strcmp(argv[1], "fork"))
+ forkwrite();
+ else if (!strcmp(argv[1], "write"))
+ writetext(argv[2]);
+ else if (!strcmp(argv[1], "follow"))
+ follow();
+ return 0;
+}