memlog.c 1020 字节
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 * Change Logs:
 * Date           Author       Notes
 * 2013-06-26     Grissiom     the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <log_trace.h>

#define PIPE_SZ    2048
#define PIPE_NAME  "memlog"

static rt_pipe_t *_log_pipe = NULL;
static rt_uint8_t outbuf[1024];
void memlog_flush(void)
{
    rt_size_t readsz;
    rt_device_t console;

    console = rt_console_get_device();
    if (!console) return;

    readsz = rt_device_read((rt_device_t)&(_log_pipe->parent), 0, outbuf, sizeof(outbuf));
    if (readsz)
        rt_device_write(console, 0, outbuf, readsz);
}

int memlog_init(void)
{
    _log_pipe = rt_pipe_create(PIPE_NAME, PIPE_SZ);
    if (_log_pipe == RT_NULL)
    {
        rt_kprintf("init pipe device failed.\n");
        return -1;
    }

    log_trace_set_device(PIPE_NAME);
    rt_thread_idle_sethook(memlog_flush);
    
    return 0;
}
INIT_APP_EXPORT(memlog_init);