blob: 11ec8fd134e47d2178a89f2345d0a2b8b9a1416e [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
24typedef enum {
25 LOG_ID_MAIN = 0,
26 LOG_ID_RADIO = 1,
27 LOG_ID_EVENTS = 2,
28 LOG_ID_SYSTEM = 3,
29 LOG_ID_KMSG = 4,
30 LOG_ID_MAX
31} log_id_t;
liubin281ac462023-07-19 14:22:54 +080032
33#define LOG_VERBOSE 8
34
35static int tlog_fd = -1;
b.liu30d950d2023-09-25 20:37:45 +080036// Default for radio log.
37static int syslog_radio_enable = 2;
liubin281ac462023-07-19 14:22:54 +080038static FILE* logfile = NULL;
39static int signal_fd = -1;
40
b.liu94baa7c2023-09-26 16:26:10 +080041static bool log_level_printed = FALSE;
42
liubin281ac462023-07-19 14:22:54 +080043/**
44 * @brief mbtk_log_init
45 *
46 * @details 设置Log输出方式
47 * @param
48 * path:
49 * 不填参数(NULL) stdout : 命令行输
50 * "syslog":输出到syslog
51 * "radio":CatStudio
52 * 文件路径:输出到自定义文件路径
53 * tag : 自定义tag
54 *
55 * example:
56 * mbtk_log_init(NULL, "MBTK_RIL");
57 * mbtk_log_init("syslog", "MBTK_RIL");
58 * mbtk_log_init("radio", "MBTK_RIL");
59 * mbtk_log_init("/tmp/log/test.log", "MBTK_RIL");
60 */
61void mbtk_log_init(char* path, char* tag)
62{
63 if (str_empty(path)) {
64 tlog_fd = STDOUT_FILENO;
65 } else if (0 == memcmp(path, "syslog", 6)) {
66 openlog(tag, LOG_PID, LOG_USER);
67 syslog_radio_enable = 1;
68 } else if (0 == memcmp(path, "radio", 5)) {
69 if (tag && strlen(tag) > 0) {
70 set_service_log_tag(tag);
71 } else {
72 set_service_log_tag("MBTK");
73 }
74 syslog_radio_enable = 2;
75 } else if (path) {
76 tlog_fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0600);
77 if (tlog_fd < 0) {
78 fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
79 exit(-1);
80 }
81 }
82}
83
84/* Control the log output */
85void mbtk_log(int level, const char* format, ...)
86{
87 char buf[1024] = {0};
88 va_list ap;
89 struct timeval log_time;
90 int length = 0;
91
92 va_start(ap, format);
93 length = vsnprintf(buf, sizeof(buf), format, ap);
94 if (length < 0 || 0 == length) {
b.liu778645e2024-06-21 16:47:42 +080095 return;
liubin281ac462023-07-19 14:22:54 +080096 }
97
98 if (1 == syslog_radio_enable) {
99 syslog(level, "%s", buf);
100 } else if (2 == syslog_radio_enable) {
101 __android_log_printf(LOG_ID_RADIO, level, "%s", buf);
b.liu94baa7c2023-09-26 16:26:10 +0800102
103 if(!log_level_printed) {
104 __android_log_printf(LOG_ID_RADIO, LOG_ERR_LEVEL, "sloglevel = %d", get_service_log_level());
105 log_level_printed = TRUE;
106 }
liubin281ac462023-07-19 14:22:54 +0800107 } else if (-1 != tlog_fd) {
108 char tmp[50] = {0};
109 gettimeofday(&log_time, NULL);
110 struct tm* tm_t = localtime(&(log_time.tv_sec));
111 strftime(tmp, 50, "%F %T", tm_t);
112 snprintf(tmp + strlen(tmp), sizeof(tmp) - strlen(tmp), " %d<%d>:", (int)(log_time.tv_usec / 1000), level);
113 write(tlog_fd, tmp, strlen(tmp));
114 write(tlog_fd, buf, length);
115 if (buf[length - 1] != '\n') {
116 write(tlog_fd, "\n", 1);
117 }
118 if (tlog_fd > 2) {
119 fsync(tlog_fd);
120 }
121 }
122
123 va_end(ap);
124}
125
126void log_hex(const char* tag, const void* data, int data_len)
127{
128 char buffer[60];
129 char str[17];
130 int size = 0;
131 uint8* ptr = (uint8*)data;
132 int i, j;
133 memset(buffer, 0x0, 60);
134 memset(str, 0x0, 17);
135 LOGI("%s,Length-%d:", tag, data_len);
136 LOGI(" 0 1 2 3 4 5 6 7 8 9 a b c d e f");
137 size += snprintf(buffer, 60, "%04x| ", 0);
138 for (i = 0; i < data_len; i++) {
139 size += snprintf(buffer + size, 60 - size, "%02x ", ptr[i]);
140 if (isprint(ptr[i])) {
141 str[i % 16] = ptr[i];
142 } else {
143 str[i % 16] = '.';
144 }
145 if ((i + 1) % 16 == 0 || i == data_len - 1) {
146 for (j = size; j < 54; j++) {
147 buffer[j] = ' ';
148 }
149 LOGI("%s| %s", buffer, str);
150
151 memset(buffer, 0x0, 60);
152 memset(str, 0x0, 17);
153 size = 0;
154 size += snprintf(buffer, 60, "%04x| ", (i + 1) / 16);
155 }
156 }
157}
158
159#define _MOPEN_RILD_SOCKET "/tmp/logd_socket"
160
161int mbtk_signal_log(char *data)
162{
163 char buff[256];
164 int size = 0;
165 int ret = 0;
166 int i = 0;
167 static struct sockaddr_un srv_addr;
168
169 if(signal_fd < 0) {
170 if (access(_MOPEN_RILD_SOCKET, F_OK) == -1) {
171 LOGW("Service not running...");
172 return -1;
173 }
174
175 signal_fd = socket(PF_UNIX, SOCK_STREAM, 0);
176 if (signal_fd < 0) {
177 LOGE("cannot creat socket");
178 return -1;
179 }
180
181 srv_addr.sun_family = AF_UNIX;
182 strcpy(srv_addr.sun_path, _MOPEN_RILD_SOCKET);
183 ret = connect(signal_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
184 if (ret < 0) {
185 LOGE("cannot connect server, ret=%d, errno=%d", ret, errno);
186 close(signal_fd);
187 signal_fd = -1;
188 return -1;
189 }
190 }
191
192 memset(buff, 0, sizeof(buff));
193 snprintf(buff, sizeof(buff), "%s\n", data);
194 size = write(signal_fd, buff, sizeof(buff));
195 if (size < 0 || size == 0) {
196 LOGE("cannot write , ret=%d, errno=%d\n", ret, errno);
197 return 1;
198 }
199
200 // close(signal_fd);
201
202 return 0;
203}