8 #include <queued_entry.h>
10 int time_cmp(struct timespec x, int sec, int nsec)
12 if (x.tv_sec > sec) return +1;
13 if (x.tv_sec < sec) return -1;
14 if (x.tv_nsec > nsec) return +1;
15 if (x.tv_nsec < nsec) return -1;
22 char tag[] = "TEST_TAG";
23 char msg[] = "Test message with a newline \n and some special characters \e[31m in colour";
24 struct pipe_logger_entry *ple = alloca(LOG_MAX_PAYLOAD_SIZE + sizeof *ple);
25 dlogutil_entry_s *le = alloca(LOG_MAX_PAYLOAD_SIZE + sizeof *le);
26 struct timespec ts_pre, ts_post;
28 clock_gettime(CLOCK_MONOTONIC, &ts_pre);
29 create_pipe_message(ple, prio, tag, msg);
30 set_pipe_message_sent_timestamp(ple, NULL, NULL);
31 parse_pipe_message(ple, le, ple->len);
32 clock_gettime(CLOCK_MONOTONIC, &ts_post);
34 assert(le->pid == getpid());
37 if (tid != -1 && tid != 0) {
38 /* Some build environments have trouble with gettid.
39 * In those cases there's little point doing this check. */
40 assert(le->tid == tid);
43 assert(le->priority == prio);
44 assert(!strcmp(le->msg, tag));
45 assert(!strcmp(le->msg + sizeof(tag), msg));
46 assert(time_cmp(ts_pre, le->sec_sent_mono, le->nsec_sent_mono) <= 0);
47 assert(time_cmp(ts_post, le->sec_sent_mono, le->nsec_sent_mono) >= 0);
49 static const int SAFE_SIZE = ((LOG_MAX_PAYLOAD_SIZE - 1 /* prio */) / 2 /* split over tag and msg */) - 1 /* null delimiter */;
50 char random_tag[SAFE_SIZE];
51 char random_msg[SAFE_SIZE];
52 random_tag[SAFE_SIZE - 1] = random_msg[SAFE_SIZE - 1] = '\0';
56 int j = SAFE_SIZE - 1;
58 random_msg[j] = rand() & 255;
59 random_tag[j] = rand() & 255;
61 prio = (rand() % (DLOG_PRIO_MAX - DLOG_VERBOSE)) + DLOG_VERBOSE;
63 clock_gettime(CLOCK_MONOTONIC, &ts_pre);
64 create_pipe_message(ple, prio, random_tag, random_msg);
65 set_pipe_message_sent_timestamp(ple, NULL, NULL);
66 parse_pipe_message(ple, le, ple->len);
67 clock_gettime(CLOCK_MONOTONIC, &ts_post);
68 assert(le->priority == prio);
69 assert(!strcmp(le->msg, random_tag));
70 assert(!strcmp(le->msg + strlen(random_tag) + 1, random_msg));
71 assert(le->pid == getpid());
72 assert(le->tid == gettid());
73 assert(time_cmp(ts_pre, le->sec_sent_mono, le->nsec_sent_mono) <= 0);
74 assert(time_cmp(ts_post, le->sec_sent_mono, le->nsec_sent_mono) >= 0);