summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGrissiom <[email protected]>2013-10-23 01:48:47 +0800
committerGrissiom <[email protected]>2013-10-23 01:48:47 +0800
commita391e0a89307258b54e2599368963ce95fa7e7d3 (patch)
treedb2cd3e4f7fbf74a11216829ffb0fe9501d030cd /examples
parent0feefc03b6008301a19a430427390cc9c44decf8 (diff)
log_trace: add a in-mem-log example
After `log_trace_init()`, call `memlog_init`. It then turn the logtrace into a "in-memory" logger which will buffer all the log in memory. It also set a hook in idle to flush all the log into console. One may create an other thread to flush the logs but idle might be the simplest place to go.
Diffstat (limited to 'examples')
-rw-r--r--examples/log_trace/memlog.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/log_trace/memlog.c b/examples/log_trace/memlog.c
new file mode 100644
index 0000000000..a94e77fe7e
--- /dev/null
+++ b/examples/log_trace/memlog.c
@@ -0,0 +1,52 @@
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <log_trace.h>
+
+#define PIPE_SZ 2048
+#define PIPE_NAME "lgpipe"
+
+static rt_uint8_t pipebuf[PIPE_SZ];
+static struct rt_pipe_device pipedev;
+
+static rt_uint8_t outbuf[1024];
+void memlog_flush(void)
+{
+ rt_size_t remainsz, readsz;
+ rt_device_t console;
+
+ console = rt_console_get_device();
+
+ if (!console)
+ return;
+
+ rt_device_control((rt_device_t)&pipedev, PIPE_CTRL_GET_SPACE, &remainsz);
+ if (remainsz == 0)
+ {
+ rt_kprintf("logtrace pipe "PIPE_NAME" is full, some log may lost\n");
+ }
+
+ readsz = rt_device_read((rt_device_t)&pipedev, 0, outbuf, sizeof(outbuf));
+ if (readsz)
+ rt_device_write(console, 0, outbuf, readsz);
+}
+
+void memlog_init(void)
+{
+ rt_err_t res;
+
+ /* make sure the RT_PIPE_FLAG_BLOCK_RD is not set. The Idle should not be
+ * blocked. RT_PIPE_FLAG_FORCE_WR will let the pipe discard some old data
+ * when pipe is full. */
+ res = rt_pipe_init(&pipedev, PIPE_NAME, RT_PIPE_FLAG_FORCE_WR,
+ pipebuf, sizeof(pipebuf));
+ if (res != RT_EOK)
+ {
+ rt_kprintf("init pipe device failed: %d\n", res);
+ return;
+ }
+
+ log_trace_set_device(PIPE_NAME);
+
+ rt_thread_idle_sethook(memlog_flush);
+}
+