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