1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2018 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <stdio.h>
9 #include <stddef.h>
10 #include <logging/log_backend.h>
11 #include <logging/log_backend_std.h>
12 #include <logging/log_core.h>
13 #include <logging/log_msg.h>
14 #include <logging/log_output.h>
15 #include <irq.h>
16 #include <arch/posix/posix_trace.h>
17 
18 #define _STDOUT_BUF_SIZE 256
19 static char stdout_buff[_STDOUT_BUF_SIZE];
20 static int n_pend; /* Number of pending characters in buffer */
21 
preprint_char(int c)22 static void preprint_char(int c)
23 {
24 	int printnow = 0;
25 
26 	if (c == '\r') {
27 		/* Discard carriage returns */
28 		return;
29 	}
30 	if (c != '\n') {
31 		stdout_buff[n_pend++] = c;
32 		stdout_buff[n_pend] = 0;
33 	} else {
34 		printnow = 1;
35 	}
36 
37 	if (n_pend >= _STDOUT_BUF_SIZE - 1) {
38 		printnow = 1;
39 	}
40 
41 	if (printnow) {
42 		posix_print_trace("%s\n", stdout_buff);
43 		n_pend = 0;
44 		stdout_buff[0] = 0;
45 	}
46 }
47 
48 static uint8_t buf[_STDOUT_BUF_SIZE];
49 
char_out(uint8_t * data,size_t length,void * ctx)50 static int char_out(uint8_t *data, size_t length, void *ctx)
51 {
52 	for (size_t i = 0; i < length; i++) {
53 		preprint_char(data[i]);
54 	}
55 
56 	return length;
57 }
58 
59 LOG_OUTPUT_DEFINE(log_output_posix, char_out, buf, sizeof(buf));
60 
put(const struct log_backend * const backend,struct log_msg * msg)61 static void put(const struct log_backend *const backend,
62 		struct log_msg *msg)
63 {
64 	log_msg_get(msg);
65 
66 	uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
67 
68 	if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
69 		if (posix_trace_over_tty(0)) {
70 			flags |= LOG_OUTPUT_FLAG_COLORS;
71 		}
72 	}
73 
74 	if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
75 		flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
76 	}
77 
78 	log_output_msg_process(&log_output_posix, msg, flags);
79 
80 	log_msg_put(msg);
81 
82 }
83 
panic(struct log_backend const * const backend)84 static void panic(struct log_backend const *const backend)
85 {
86 	log_output_flush(&log_output_posix);
87 }
88 
dropped(const struct log_backend * const backend,uint32_t cnt)89 static void dropped(const struct log_backend *const backend, uint32_t cnt)
90 {
91 	ARG_UNUSED(backend);
92 
93 	log_output_dropped_process(&log_output_posix, cnt);
94 }
95 
sync_string(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * fmt,va_list ap)96 static void sync_string(const struct log_backend *const backend,
97 		     struct log_msg_ids src_level, uint32_t timestamp,
98 		     const char *fmt, va_list ap)
99 {
100 	uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
101 	uint32_t key;
102 
103 	if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
104 		flags |= LOG_OUTPUT_FLAG_COLORS;
105 	}
106 
107 	if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
108 		flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
109 	}
110 
111 	key = irq_lock();
112 	log_output_string(&log_output_posix, src_level,
113 			  timestamp, fmt, ap, flags);
114 	irq_unlock(key);
115 }
116 
sync_hexdump(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * metadata,const uint8_t * data,uint32_t length)117 static void sync_hexdump(const struct log_backend *const backend,
118 			 struct log_msg_ids src_level, uint32_t timestamp,
119 			 const char *metadata, const uint8_t *data, uint32_t length)
120 {
121 	uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
122 	uint32_t key;
123 
124 	if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
125 		flags |= LOG_OUTPUT_FLAG_COLORS;
126 	}
127 
128 	if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
129 		flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
130 	}
131 
132 	key = irq_lock();
133 	log_output_hexdump(&log_output_posix, src_level, timestamp,
134 			metadata, data, length, flags);
135 	irq_unlock(key);
136 }
137 
process(const struct log_backend * const backend,union log_msg2_generic * msg)138 static void process(const struct log_backend *const backend,
139 		union log_msg2_generic *msg)
140 {
141 	uint32_t flags = log_backend_std_get_flags();
142 
143 	log_output_msg2_process(&log_output_posix, &msg->log, flags);
144 }
145 
146 const struct log_backend_api log_backend_native_posix_api = {
147 	.process = IS_ENABLED(CONFIG_LOG2) ? process : NULL,
148 	.put = IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? put : NULL,
149 	.put_sync_string = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
150 			sync_string : NULL,
151 	.put_sync_hexdump = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
152 			sync_hexdump : NULL,
153 	.panic = panic,
154 	.dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
155 };
156 
157 LOG_BACKEND_DEFINE(log_backend_native_posix,
158 		   log_backend_native_posix_api,
159 		   true);
160