summaryrefslogtreecommitdiff
path: root/components/utilities/logtrace/log_trace.c
blob: aa2e719852538d667bd12a2f9cd87dd9208de473 (plain)
1
2
3
4
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * File      : log_trace.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2013, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 *                Bernard      the first version
 * 2013-06-26     Grissiom     refactor
 */

#include <rtthread.h>
#include <rthw.h>
#include <stdio.h>
#include "log_trace.h"

#ifdef RT_USING_FINSH
#include <finsh.h>
#else
#define FINSH_FUNCTION_EXPORT(...)
#define FINSH_FUNCTION_EXPORT_ALIAS(...)
#endif

/* log pseudo device */
static struct rt_device _log_device;

static rt_device_t _traceout_device = RT_NULL;

/* define a default lg session. The name is empty. */
static struct log_trace_session _def_session = {{"\0"}, LOG_TRACE_LEVEL_INFO};
static const struct log_trace_session *_the_sessions[LOG_TRACE_MAX_SESSION] = {&_def_session};
/* there is a default session at least */
static rt_uint16_t _the_sess_nr = 1;

rt_inline int _idname_len(log_trace_idnum_t id)
{
    /* little endian */
    if ((id & 0x000000FF) == 0)
        return 0;
    if ((id & 0x0000FF00) == 0)
        return 1;
    if ((id & 0x00FF0000) == 0)
        return 2;
    if ((id & 0xFF000000) == 0)
        return 3;
#ifndef LOG_TRACE_USE_LONGNAME
    return 4;
#else
    {
        rt_uint32_t id2 = id >> 32;
        if ((id2 & 0x000000FF) == 0)
            return 4;
        if ((id2 & 0x0000FF00) == 0)
            return 5;
        if ((id2 & 0x00FF0000) == 0)
            return 6;
        if ((id2 & 0xFF000000) == 0)
            return 7;
        return 8;
    }
#endif
}

/* lookup the session according to name.
 *
 * @param len is the length of the name
 * @return the pointer to the named session. RT_NULL when there is no such a
 * session.
 */
static struct log_trace_session* _lg_lookup_session(log_trace_idnum_t num)
{
    static const struct log_trace_session *_cache = &_def_session;
    rt_uint16_t first, last;

    if (_cache->id.num == num)
        return (struct log_trace_session *)_cache;

    first = 0;
    last  = _the_sess_nr;
    do {
        unsigned int i = (first + last)/2;

        RT_ASSERT(_the_sessions[i]);

        if (_the_sessions[i]->id.num == num)
        {
            /* there is no need to protect the _cache because write a pointer
             * is atomic. So we cannot get a invalid pointer. The worst thing
             * could happen is there is an interrupt in the read/modify/write
             * process and we wrote the old one to _cache. But it doesn't harm
             * a lot because it will be flushed in the next time. */
            _cache = _the_sessions[i];
            return (struct log_trace_session *)_the_sessions[i];
        }
        else if (_the_sessions[i]->id.num > num)
        {
            last = i;
        }
        else // _the_sessions[i]->id.num < num
        {
            first = i;
        }
    } while (first != last-1);

    return RT_NULL;
}

rt_err_t log_trace_register_session(const struct log_trace_session *session)
{
    unsigned int lvl, i;

    if (_the_sess_nr == LOG_TRACE_MAX_SESSION)
        return -RT_EFULL;

    if (session == RT_NULL)
        return RT_EOK;

    lvl = rt_hw_interrupt_disable();
    /* inserting the sessions in ascending order.
     *
     * this might take relatively long time. But since the register should only
     * happen when initialize the whole system, this should not be a matter. */
    for (i = 0; i < _the_sess_nr; i++)
    {
        if (_the_sessions[i]->id.num > session->id.num)
        {
            rt_memmove(_the_sessions+i+1, _the_sessions+i,
                       (_the_sess_nr-i)*sizeof(&_the_sessions[0]));
            _the_sessions[i] = session;
            break;
        }
        else if (_the_sessions[i]->id.num == session->id.num)
        {
            rt_kprintf("registering session 0x%p twice\n", session);
            rt_hw_interrupt_enable(lvl);
            return -RT_ERROR;
        }
    }
    if (i == _the_sess_nr)
        _the_sessions[i] = session;
    _the_sess_nr++;
    rt_hw_interrupt_enable(lvl);

    return RT_EOK;
}

struct log_trace_session* log_trace_session_find(const char *name)
{
    union log_trace_id *idp;

    RT_ASSERT(name);
    idp = (union log_trace_id*)name;
    return _lg_lookup_session(idp->num);
}

void log_trace_set_level(rt_uint8_t level)
{
    _def_session.lvl = level;
}
FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_level, log_level, set the filter level of log trace);

void log_trace_session_set_level(struct log_trace_session *sess, rt_uint8_t level)
{
    RT_ASSERT(sess);
    sess->lvl = level;
}

/* parse the level info in fmt
 *
 * @param flen the length of the format.
 * @param lvlp the pointer to level. It will store the level in the memory the
 *        lvlp points to. The default value is LOG_TRACE_LEVEL_DEFAULT.
 * @return the number of char it scaned.
 */
static rt_size_t _lg_parse_lvl(const char *fmt, rt_size_t flen, int *lvlp)
{
    RT_ASSERT(fmt);
    RT_ASSERT(lvlp);

    /* setup default value */
    *lvlp = LOG_TRACE_LEVEL_DEFAULT;

    if (flen < 3)
    {
        return 0;
    }

    if (fmt[0] == '<' && fmt[2] == '>')
    {
        *lvlp = fmt[1] - '0';
        return 3;
    }
    return 0;
}

/* parse the header in fmt
 *
 * @param flen the length of the format.
 * @param sessp the pointer of pointer to the session. It will store the
 *        session pointer in the memory the sessp points to. When failed to
 *        find the session, it will be setted to the default session.
 * @return the number of char it scaned, i.e., the length of header.
 */
static rt_size_t _lg_parse_session(
        const char *fmt, rt_size_t flen, struct log_trace_session **sessp)
{
    unsigned int i;
    struct log_trace_session *tmpsess;
    union log_trace_id id;

    RT_ASSERT(fmt);
    RT_ASSERT(sessp);

    /* setup default value */
    *sessp = &_def_session;

    /* no name space left */
    if (flen < sizeof(id) + 2)
        return 0;

    if (fmt[0] != '[')
        return 0;

    id.num = 0;
    /* skip '[' and convert the string to id number. */
    for (i = 1; fmt[i] != ']'; i++)
    {
        if (i - 1 == sizeof(id))
            return 0;
        id.name[i-1] = fmt[i];
    }
    tmpsess = _lg_lookup_session(id.num);
    if (tmpsess != RT_NULL)
    {
        *sessp = tmpsess;
        /* only count the header length when we found the session. So
         * the wrong [name] will be printed out. */
        return i + 1;
    }

    return 0;
}

void __logtrace_vfmtout(const struct log_trace_session *session,
                        const char *fmt,
                        va_list argptr)
{
    /* 1 for ']' */
    static char _trace_buf[1+LOG_TRACE_BUFSZ];
    char *ptr;
    rt_size_t length;

    RT_ASSERT(session);
    RT_ASSERT(fmt);

    rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get());
    if (_traceout_device != RT_NULL)
    {
        rt_device_write(_traceout_device, -1, _trace_buf, 11);
        rt_device_write(_traceout_device, -1,
                session->id.name, _idname_len(session->id.num));
    }

    _trace_buf[0] = ']';
    ptr = &_trace_buf[1];
    length = rt_vsnprintf(ptr, LOG_TRACE_BUFSZ, fmt, argptr);

    if (length >= LOG_TRACE_BUFSZ)
        length = LOG_TRACE_BUFSZ - 1;

    if (_traceout_device != RT_NULL)
    {
        rt_device_write(_traceout_device, -1, _trace_buf, length + 1);
    }
}

void log_trace(const char *fmt, ...)
{
    va_list args;
    int level;
    struct log_trace_session *session;

    RT_ASSERT(fmt);

    fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
    fmt += _lg_parse_session(fmt, strlen(fmt), &session);

    /* filter by level */
    if (level > session->lvl)
        return;

    va_start(args, fmt);
    __logtrace_vfmtout(session, fmt, args);
    va_end(args);
}
FINSH_FUNCTION_EXPORT(log_trace, log trace);

void log_session(const struct log_trace_session *session, const char *fmt, ...)
{
    va_list args;
    int level;

    RT_ASSERT(session);
    RT_ASSERT(fmt);

    fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
    if (level > session->lvl)
        return;

    va_start(args, fmt);
    __logtrace_vfmtout(session, fmt, args);
    va_end(args);
}

void log_trace_flush(void)
{
    rt_device_control(_traceout_device, LOG_TRACE_CTRL_FLUSH, RT_NULL);
}
FINSH_FUNCTION_EXPORT_ALIAS(log_trace_flush, log_flush, flush log on the buffer);

/* RT-Thread common device interface */
static rt_size_t _log_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    char c;
    int level;
    rt_size_t head_len;
    const char *ptr = buffer;
    struct log_trace_session *session;

    head_len = _lg_parse_lvl(ptr, size, &level);
    head_len += _lg_parse_session(ptr+head_len, size-head_len, &session);

    /* filter by level */
    if (level > session->lvl)
        return size;

    if (_traceout_device != RT_NULL)
    {
        c = '[';
        rt_device_write(_traceout_device, -1, &c, 1);
        rt_device_write(_traceout_device, -1, session->id.name, _idname_len(session->id.num));
        c = ']';
        rt_device_write(_traceout_device, -1, &c, 1);
        rt_device_write(_traceout_device, -1, ((char*)buffer)+head_len, size - head_len);
    }

    return size;
}

static rt_err_t _log_control(rt_device_t dev, int cmd, void *arg)
{
    if (_traceout_device == RT_NULL) return -RT_ERROR;

    return rt_device_control(_traceout_device, cmd, arg);
}

int log_trace_init(void)
{
    rt_memset(&_log_device, 0x00, sizeof(_log_device));

    _log_device.type = RT_Device_Class_Char;
    _log_device.init    = RT_NULL;
    _log_device.open    = RT_NULL;
    _log_device.close   = RT_NULL;
    _log_device.read    = RT_NULL;
    _log_device.write   = _log_write;
    _log_device.control = _log_control;

    /* no indication and complete callback */
    _log_device.rx_indicate = RT_NULL;
    _log_device.tx_complete = RT_NULL;

    rt_device_register(&_log_device, "log", RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);

	/* set console as default device */
	_traceout_device = rt_console_get_device();

    return 0;
}
INIT_DEVICE_EXPORT(log_trace_init);

rt_device_t log_trace_get_device(void)
{
    return _traceout_device;
}

rt_err_t log_trace_set_device(const char *device_name)
{
    struct rt_device *output_device;

    /* find out output device */
    output_device = rt_device_find(device_name);
    if (output_device != RT_NULL)
    {
        rt_err_t result;

        /* open device */
        result = rt_device_open(output_device, RT_DEVICE_FLAG_STREAM | RT_DEVICE_OFLAG_RDWR);
        if (result != RT_EOK)
        {
            rt_kprintf("Open trace device failed.\n");
            return -RT_ERROR;
        }
    }

    /* set trace out device */
    if (_traceout_device != RT_NULL)
        rt_device_close(_traceout_device);
    _traceout_device = output_device;

    return RT_EOK;
}
FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_device, log_device, set device of log trace);