blob: ab599b43f9d1b02bbf08916eca5bc5d9865e6e8c [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;
75 } else if (0 == memcmp(path, "syslog", 6)) {
76 openlog(tag, LOG_PID, LOG_USER);
77 syslog_radio_enable = 1;
78 } else if (0 == memcmp(path, "radio", 5)) {
79 if (tag && strlen(tag) > 0) {
80 set_service_log_tag(tag);
81 } else {
82 set_service_log_tag("MBTK");
83 }
84 syslog_radio_enable = 2;
85 } else if (path) {
86 tlog_fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0600);
87 if (tlog_fd < 0) {
88 fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
89 exit(-1);
90 }
91 }
92}
93
94/* Control the log output */
95void mbtk_log(int level, const char* format, ...)
96{
97 char buf[1024] = {0};
98 va_list ap;
99 struct timeval log_time;
100 int length = 0;
b.liu9e8584b2024-11-06 19:21:28 +0800101 int ret = 0;
liubin281ac462023-07-19 14:22:54 +0800102
b.liu07c93c82024-11-07 15:30:02 +0800103 if(!log_init) {
104 char filename[64] = {0};
105 int fd = open("/proc/self/comm", O_RDONLY);
106 if(fd > 0) {
107 if(read(fd, filename, sizeof(filename)) > 0) {
108 // Delete last '\r' / '\n' / ' '
109 char *ptr = filename + strlen(filename) - 1;
110 while(ptr >= filename && (*ptr == '\r' || *ptr == '\n' || *ptr == ' '))
111 {
112 *ptr-- = '\0';
113 }
114
115 mbtk_log_init("radio", filename);
116 } else {
117 mbtk_log_init("radio", "MBTK");
118 }
119 close(fd);
120 } else {
121 mbtk_log_init("radio", "MBTK");
122 }
123
124 //mbtk_log_init("radio", __progname);
125 }
126
liubin281ac462023-07-19 14:22:54 +0800127 va_start(ap, format);
128 length = vsnprintf(buf, sizeof(buf), format, ap);
129 if (length < 0 || 0 == length) {
l.yang4e0077b2024-11-03 18:21:32 -0800130 va_end(ap);
b.liu778645e2024-06-21 16:47:42 +0800131 return;
liubin281ac462023-07-19 14:22:54 +0800132 }
133
134 if (1 == syslog_radio_enable) {
135 syslog(level, "%s", buf);
136 } else if (2 == syslog_radio_enable) {
137 __android_log_printf(LOG_ID_RADIO, level, "%s", buf);
b.liu94baa7c2023-09-26 16:26:10 +0800138
139 if(!log_level_printed) {
140 __android_log_printf(LOG_ID_RADIO, LOG_ERR_LEVEL, "sloglevel = %d", get_service_log_level());
141 log_level_printed = TRUE;
142 }
liubin281ac462023-07-19 14:22:54 +0800143 } else if (-1 != tlog_fd) {
144 char tmp[50] = {0};
145 gettimeofday(&log_time, NULL);
146 struct tm* tm_t = localtime(&(log_time.tv_sec));
147 strftime(tmp, 50, "%F %T", tm_t);
148 snprintf(tmp + strlen(tmp), sizeof(tmp) - strlen(tmp), " %d<%d>:", (int)(log_time.tv_usec / 1000), level);
b.liu9e8584b2024-11-06 19:21:28 +0800149 ret = write(tlog_fd, tmp, strlen(tmp));
150 ret = write(tlog_fd, buf, length);
liubin281ac462023-07-19 14:22:54 +0800151 if (buf[length - 1] != '\n') {
b.liu9e8584b2024-11-06 19:21:28 +0800152 ret = write(tlog_fd, "\n", 1);
153 if(ret) {
154 // Donothing.
155 }
liubin281ac462023-07-19 14:22:54 +0800156 }
157 if (tlog_fd > 2) {
158 fsync(tlog_fd);
159 }
160 }
161
162 va_end(ap);
163}
164
165void log_hex(const char* tag, const void* data, int data_len)
166{
167 char buffer[60];
168 char str[17];
169 int size = 0;
170 uint8* ptr = (uint8*)data;
171 int i, j;
172 memset(buffer, 0x0, 60);
173 memset(str, 0x0, 17);
174 LOGI("%s,Length-%d:", tag, data_len);
175 LOGI(" 0 1 2 3 4 5 6 7 8 9 a b c d e f");
176 size += snprintf(buffer, 60, "%04x| ", 0);
177 for (i = 0; i < data_len; i++) {
178 size += snprintf(buffer + size, 60 - size, "%02x ", ptr[i]);
179 if (isprint(ptr[i])) {
180 str[i % 16] = ptr[i];
181 } else {
182 str[i % 16] = '.';
183 }
184 if ((i + 1) % 16 == 0 || i == data_len - 1) {
185 for (j = size; j < 54; j++) {
186 buffer[j] = ' ';
187 }
188 LOGI("%s| %s", buffer, str);
189
190 memset(buffer, 0x0, 60);
191 memset(str, 0x0, 17);
192 size = 0;
193 size += snprintf(buffer, 60, "%04x| ", (i + 1) / 16);
194 }
195 }
196}
197
198#define _MOPEN_RILD_SOCKET "/tmp/logd_socket"
199
200int mbtk_signal_log(char *data)
201{
202 char buff[256];
203 int size = 0;
204 int ret = 0;
liubin281ac462023-07-19 14:22:54 +0800205 static struct sockaddr_un srv_addr;
206
207 if(signal_fd < 0) {
208 if (access(_MOPEN_RILD_SOCKET, F_OK) == -1) {
209 LOGW("Service not running...");
210 return -1;
211 }
212
213 signal_fd = socket(PF_UNIX, SOCK_STREAM, 0);
214 if (signal_fd < 0) {
215 LOGE("cannot creat socket");
216 return -1;
217 }
218
219 srv_addr.sun_family = AF_UNIX;
220 strcpy(srv_addr.sun_path, _MOPEN_RILD_SOCKET);
221 ret = connect(signal_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
222 if (ret < 0) {
223 LOGE("cannot connect server, ret=%d, errno=%d", ret, errno);
224 close(signal_fd);
225 signal_fd = -1;
226 return -1;
227 }
228 }
229
230 memset(buff, 0, sizeof(buff));
231 snprintf(buff, sizeof(buff), "%s\n", data);
232 size = write(signal_fd, buff, sizeof(buff));
233 if (size < 0 || size == 0) {
234 LOGE("cannot write , ret=%d, errno=%d\n", ret, errno);
235 return 1;
236 }
237
238 // close(signal_fd);
239
240 return 0;
241}