4 #include <linux/logger.h>
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
16 #define handle_error_en(en, msg) \
17 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
19 #define BIT(nr) ((1UL) << (nr))
21 void *tstart(void *arg)
24 write(*fd, "child thread msg #1\nchild thread msg #2", 39);
28 int dump_logger(const char *device)
32 char buf[sizeof(struct logger_entry) + LOGGER_ENTRY_MAX_PAYLOAD];
33 struct logger_entry *e = (void *)buf;
36 fd = open(device, O_RDONLY | O_NONBLOCK);
42 ret = ioctl(fd, LOGGER_SET_VERSION, &version);
44 perror("ioctl(SET_VERSION)");
50 ret = read(fd, buf, sizeof(struct logger_entry) + LOGGER_ENTRY_MAX_PAYLOAD);
59 if (e->hdr_size != sizeof(struct logger_entry)) {
60 fprintf(stderr, "%d != %d\n", e->hdr_size, sizeof(struct logger_entry));
61 fprintf(stderr, "read: Invalid data\n");
66 tag_len = strlen(e->msg + 1) + 2; /* 2: priority, NUL */
67 fprintf(stdout, "[%5d.%6d] %s<%u>: %s\n", e->sec, e->nsec / 1000, &e->msg[1], e->msg[0], &e->msg[tag_len]);
73 int main(int ac, char *av[]) {
75 char *device = "/dev/log_main";
76 char *msg = "The Foo";
78 struct logger_set_tag struct_tag = {
80 .ptr = (uintptr_t)tag,
87 unsigned char prio = 4;
88 unsigned long test_mask = ~0UL;
91 static struct option long_options[] = {
92 {"priority", required_argument, 0, 'p'},
93 {"tag", required_argument, 0, 't'},
94 {"test-mask", required_argument, 0, 'm'},
95 {"device", required_argument, 0, 'd'},
99 c = getopt_long(ac, av, "p:t:m:d:D", long_options, NULL);
105 prio = (unsigned char) strtol(optarg, NULL, 10);
108 tag = strdup(optarg);
111 test_mask = (unsigned long) strtol(optarg, NULL, 16);
114 device = strdup(optarg);
125 return dump_logger(device);
128 fd = open(device, O_WRONLY);
134 if (test_mask & BIT(0)) {
135 vec[0].iov_base = &prio;
137 vec[1].iov_base = tag;
138 vec[1].iov_len = strlen(tag) + 1;
139 vec[2].iov_base = msg;
140 vec[2].iov_len = strlen(msg) + 1;
143 if (test_mask & BIT(1)) {
144 msg = "line #1\nline #2";
145 vec[2].iov_base = msg;
146 vec[2].iov_len = strlen(msg) + 1;
152 ret = ioctl(fd, LOGGER_SET_PRIO, prio);
154 perror("ioctl(SET_PRIO)");
157 ret = ioctl(fd, LOGGER_SET_TAG, &struct_tag);
159 perror("ioctl(SET_TAG)");
163 if (test_mask & BIT(2)) {
165 count = write(fd, "The Foo From STDIO\n", 19);
167 fprintf(stderr, "%d != 19\n", count);
169 write(fd, "LINE #1\nLINE #2", 15);
170 write(fd, " CONTINUED\nONCE", 15);
171 write(fd, " AGAIN\n", 7);
174 if (test_mask & BIT(3)) {
179 for (int i = 0; i < 8000; i++)
180 msg[i] = '!' + (i % 95);
185 write(fd, msg, 8000);
187 for (int i = 0; (test_mask & BIT(4)) && i < 40; i++)
188 write(fd, msg, 8000);
190 for (int i = 0; (test_mask & BIT(5)) && i < 8000; i++)
191 write(fd, &msg[i], 1);
197 if (test_mask & BIT(6)) {
201 } else if (child == 0) {
203 printf("child: %d\n", getpid());
204 write(fd, "child 1\n", 8);
206 write(fd, "child 2\n", 8);
210 write(fd, "PARENT\n", 7);
211 printf("PARENT: %d\n", getpid());
215 if (test_mask & BIT(7)) {
216 s = pthread_create(&tid, NULL, &tstart, &fd);
218 handle_error_en(s, "pthread_create");
220 write(fd, "PARENT THREAD\n", 14);
222 s = pthread_join(tid, NULL);
224 handle_error_en(s, "pthread_join");