1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/syslog/syslog.h"
4
5#include "mos/platform/platform.h"
6#include "mos/tasks/task_types.h"
7#include "proto/syslog.pb.h"
8
9#include <mos/compiler.h>
10#include <mos/mos_global.h>
11#include <mos_stdio.h>
12#include <pb_encode.h>
13
14static spinlock_t global_syslog_lock = SPINLOCK_INIT;
15
16long do_syslog(loglevel_t level, thread_t *thread, const char *file, const char *func, int line, debug_info_entry *feat, const char *fmt, ...)
17{
18 pb_syslog_message msg = {
19 .timestamp = platform_get_timestamp(),
20 .cpu_id = platform_current_cpu_id(),
21 };
22
23 msg.info.level = (syslog_level) level;
24 msg.info.featid = feat ? feat->id : 0;
25 msg.info.source_location.line = line;
26 strncpy(dest: msg.info.source_location.filename, src: file, n: sizeof(msg.info.source_location.filename));
27 strncpy(dest: msg.info.source_location.function, src: func, n: sizeof(msg.info.source_location.function));
28
29 if (thread)
30 {
31 msg.thread.tid = thread->tid;
32 msg.process.pid = thread->owner->pid;
33 strncpy(dest: msg.thread.name, src: thread->name, n: sizeof(msg.thread.name));
34 strncpy(dest: msg.process.name, src: thread->owner->name, n: sizeof(msg.process.name));
35 }
36
37 va_list args;
38 va_start(args, fmt);
39 vsnprintf(buf: msg.message, size: sizeof(msg.message), format: fmt, args);
40 va_end(args);
41
42 spinlock_acquire(&global_syslog_lock);
43
44 if (level != MOS_LOG_UNSET)
45 {
46 lprintk(loglevel: level, format: "\r\n");
47 if (feat)
48 lprintk(loglevel: level, format: "%-10s | ", feat->name);
49
50#if MOS_CONFIG(MOS_PRINTK_WITH_TIMESTAMP)
51 lprintk(level, "%-16llu | ", platform_get_timestamp());
52#endif
53
54#if MOS_CONFIG(MOS_PRINTK_WITH_DATETIME)
55 lprintk(level, "%s | ", (const char *) platform_get_datetime_str());
56#endif
57
58#if MOS_CONFIG(MOS_PRINTK_WITH_CPU_ID)
59 lprintk(level, "cpu %2d | ", msg.cpu_id);
60#endif
61
62#if MOS_CONFIG(MOS_PRINTK_WITH_FILENAME)
63 lprintk(loglevel: level, format: "%-15s | ", msg.info.source_location.filename);
64#endif
65
66#if MOS_CONFIG(MOS_PRINTK_WITH_THREAD_ID)
67 lprintk(level, "%pt\t| ", ((void *) thread));
68#endif
69 }
70
71 lprintk(loglevel: level, format: "%s", msg.message);
72
73 spinlock_release(&global_syslog_lock);
74
75 return 0;
76}
77