blob: dab429fb5a32df72b06dbdfef4955f2f4c804d61 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
b.liu778645e2024-06-21 16:47:42 +08002//#include <include/log.h>
liubin281ac462023-07-19 14:22:54 +08003#include <sys/un.h>
4#include <pthread.h>
5#include <stdarg.h>
6#include <stdlib.h>
7#include <sys/prctl.h>
8#include <time.h>
9#include <string.h>
10#include <sys/time.h>
11#include <syslog.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <errno.h>
b.liu778645e2024-06-21 16:47:42 +080016#include <unistd.h>
17#include <include/logd.h>
18#include <ctype.h>
liubin281ac462023-07-19 14:22:54 +080019
20#include "mbtk_type.h"
21#include "mbtk_log.h"
b.liu778645e2024-06-21 16:47:42 +080022#include "mbtk_str.h"
23
b.liu07c93c82024-11-07 15:30:02 +080024// extern char *__progname;
25
b.liu778645e2024-06-21 16:47:42 +080026typedef enum {
27 LOG_ID_MAIN = 0,
28 LOG_ID_RADIO = 1,
29 LOG_ID_EVENTS = 2,
30 LOG_ID_SYSTEM = 3,
31 LOG_ID_KMSG = 4,
32 LOG_ID_MAX
33} log_id_t;
liubin281ac462023-07-19 14:22:54 +080034
35#define LOG_VERBOSE 8
36
37static int tlog_fd = -1;
b.liu30d950d2023-09-25 20:37:45 +080038// Default for radio log.
39static int syslog_radio_enable = 2;
b.liu9e8584b2024-11-06 19:21:28 +080040//static FILE* logfile = NULL;
liubin281ac462023-07-19 14:22:54 +080041static int signal_fd = -1;
42
b.liu94baa7c2023-09-26 16:26:10 +080043static bool log_level_printed = FALSE;
44
b.liu07c93c82024-11-07 15:30:02 +080045static bool log_init = FALSE;
46
liubin281ac462023-07-19 14:22:54 +080047/**
48 * @brief mbtk_log_init
49 *
50 * @details 设置Log输出方式
51 * @param
52 * path:
53 * 不填参数(NULL) stdout : 命令行输
54 * "syslog":输出到syslog
55 * "radio":CatStudio
56 * 文件路径:输出到自定义文件路径
57 * tag : 自定义tag
58 *
59 * example:
60 * mbtk_log_init(NULL, "MBTK_RIL");
61 * mbtk_log_init("syslog", "MBTK_RIL");
62 * mbtk_log_init("radio", "MBTK_RIL");
63 * mbtk_log_init("/tmp/log/test.log", "MBTK_RIL");
64 */
65void mbtk_log_init(char* path, char* tag)
66{
b.liu07c93c82024-11-07 15:30:02 +080067 if(log_init) {
68 return;
69 } else {
70 log_init = TRUE;
71 }
72
liubin281ac462023-07-19 14:22:54 +080073 if (str_empty(path)) {
74 tlog_fd = STDOUT_FILENO;
hj.shao9dbd3ed2025-06-26 02:52:50 -070075 } else if (0 == memcmp(path, "radio", 5) || 0 == memcmp(path, "syslog", 6)) {
liubin281ac462023-07-19 14:22:54 +080076 if (tag && strlen(tag) > 0) {
77 set_service_log_tag(tag);
78 } else {
79 set_service_log_tag("MBTK");
80 }
81 syslog_radio_enable = 2;
82 } else if (path) {
83 tlog_fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0600);
84 if (tlog_fd < 0) {
85 fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
86 exit(-1);
87 }
88 }
89}
90
91/* Control the log output */
92void mbtk_log(int level, const char* format, ...)
93{
94 char buf[1024] = {0};
95 va_list ap;
96 struct timeval log_time;
97 int length = 0;
b.liu9e8584b2024-11-06 19:21:28 +080098 int ret = 0;
liubin281ac462023-07-19 14:22:54 +080099
b.liu07c93c82024-11-07 15:30:02 +0800100 if(!log_init) {
101 char filename[64] = {0};
102 int fd = open("/proc/self/comm", O_RDONLY);
103 if(fd > 0) {
104 if(read(fd, filename, sizeof(filename)) > 0) {
105 // Delete last '\r' / '\n' / ' '
106 char *ptr = filename + strlen(filename) - 1;
107 while(ptr >= filename && (*ptr == '\r' || *ptr == '\n' || *ptr == ' '))
108 {
109 *ptr-- = '\0';
110 }
111
112 mbtk_log_init("radio", filename);
113 } else {
114 mbtk_log_init("radio", "MBTK");
115 }
116 close(fd);
117 } else {
118 mbtk_log_init("radio", "MBTK");
119 }
120
121 //mbtk_log_init("radio", __progname);
122 }
123
liubin281ac462023-07-19 14:22:54 +0800124 va_start(ap, format);
125 length = vsnprintf(buf, sizeof(buf), format, ap);
126 if (length < 0 || 0 == length) {
l.yang4e0077b2024-11-03 18:21:32 -0800127 va_end(ap);
b.liu778645e2024-06-21 16:47:42 +0800128 return;
liubin281ac462023-07-19 14:22:54 +0800129 }
130
hj.shao9dbd3ed2025-06-26 02:52:50 -0700131 if (2 == syslog_radio_enable || 1 == syslog_radio_enable) {
liubin281ac462023-07-19 14:22:54 +0800132 __android_log_printf(LOG_ID_RADIO, level, "%s", buf);
b.liu94baa7c2023-09-26 16:26:10 +0800133
134 if(!log_level_printed) {
135 __android_log_printf(LOG_ID_RADIO, LOG_ERR_LEVEL, "sloglevel = %d", get_service_log_level());
136 log_level_printed = TRUE;
137 }
liubin281ac462023-07-19 14:22:54 +0800138 } else if (-1 != tlog_fd) {
139 char tmp[50] = {0};
140 gettimeofday(&log_time, NULL);
141 struct tm* tm_t = localtime(&(log_time.tv_sec));
142 strftime(tmp, 50, "%F %T", tm_t);
143 snprintf(tmp + strlen(tmp), sizeof(tmp) - strlen(tmp), " %d<%d>:", (int)(log_time.tv_usec / 1000), level);
b.liu9e8584b2024-11-06 19:21:28 +0800144 ret = write(tlog_fd, tmp, strlen(tmp));
145 ret = write(tlog_fd, buf, length);
liubin281ac462023-07-19 14:22:54 +0800146 if (buf[length - 1] != '\n') {
b.liu9e8584b2024-11-06 19:21:28 +0800147 ret = write(tlog_fd, "\n", 1);
148 if(ret) {
149 // Donothing.
150 }
liubin281ac462023-07-19 14:22:54 +0800151 }
152 if (tlog_fd > 2) {
153 fsync(tlog_fd);
154 }
155 }
156
157 va_end(ap);
158}
159
160void log_hex(const char* tag, const void* data, int data_len)
161{
162 char buffer[60];
163 char str[17];
164 int size = 0;
165 uint8* ptr = (uint8*)data;
166 int i, j;
167 memset(buffer, 0x0, 60);
168 memset(str, 0x0, 17);
169 LOGI("%s,Length-%d:", tag, data_len);
170 LOGI(" 0 1 2 3 4 5 6 7 8 9 a b c d e f");
171 size += snprintf(buffer, 60, "%04x| ", 0);
172 for (i = 0; i < data_len; i++) {
173 size += snprintf(buffer + size, 60 - size, "%02x ", ptr[i]);
174 if (isprint(ptr[i])) {
175 str[i % 16] = ptr[i];
176 } else {
177 str[i % 16] = '.';
178 }
179 if ((i + 1) % 16 == 0 || i == data_len - 1) {
180 for (j = size; j < 54; j++) {
181 buffer[j] = ' ';
182 }
183 LOGI("%s| %s", buffer, str);
184
185 memset(buffer, 0x0, 60);
186 memset(str, 0x0, 17);
187 size = 0;
188 size += snprintf(buffer, 60, "%04x| ", (i + 1) / 16);
189 }
190 }
191}
192
193#define _MOPEN_RILD_SOCKET "/tmp/logd_socket"
194
195int mbtk_signal_log(char *data)
196{
197 char buff[256];
198 int size = 0;
199 int ret = 0;
liubin281ac462023-07-19 14:22:54 +0800200 static struct sockaddr_un srv_addr;
201
202 if(signal_fd < 0) {
203 if (access(_MOPEN_RILD_SOCKET, F_OK) == -1) {
204 LOGW("Service not running...");
205 return -1;
206 }
207
208 signal_fd = socket(PF_UNIX, SOCK_STREAM, 0);
209 if (signal_fd < 0) {
210 LOGE("cannot creat socket");
211 return -1;
212 }
213
214 srv_addr.sun_family = AF_UNIX;
215 strcpy(srv_addr.sun_path, _MOPEN_RILD_SOCKET);
216 ret = connect(signal_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
217 if (ret < 0) {
218 LOGE("cannot connect server, ret=%d, errno=%d", ret, errno);
219 close(signal_fd);
220 signal_fd = -1;
221 return -1;
222 }
223 }
224
225 memset(buff, 0, sizeof(buff));
226 snprintf(buff, sizeof(buff), "%s\n", data);
227 size = write(signal_fd, buff, sizeof(buff));
228 if (size < 0 || size == 0) {
229 LOGE("cannot write , ret=%d, errno=%d\n", ret, errno);
230 return 1;
231 }
232
233 // close(signal_fd);
234
235 return 0;
236}