1 /**
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <ImsMediaTrace.h>
18 #include <log/log_properties.h>
19 #include <string.h>
20 #include <utils/Log.h>
21
22 #ifdef IM_FILE_LOG
23 #include <stdio.h>
24 #include <stdlib.h>
25 #define IM_LOG_FILE "/data/IM.txt"
26 static uint IM_remove_log = 1;
27 #endif
28
29 #define TRACEMAXSTRING 1024
30 #define IM_TAG "libimsmedia"
31 #define IM_DEBUG_TAG "libimsmedia_d"
32
33 #ifdef IM_FILE_LOG
34 #define __IMLOG__(IMLOGTYPE, TAG) \
35 do \
36 { \
37 va_list args; \
38 char szBuffer[TRACEMAXSTRING]; \
39 va_start(args, format); \
40 vsnprintf(szBuffer, TRACEMAXSTRING, format, args); \
41 va_end(args); \
42 android_printLog(IMLOGTYPE, TAG, "%s", szBuffer); \
43 FILE* fp_IM_log = nullptr; \
44 if (IM_remove_log) \
45 { \
46 remove(IM_LOG_FILE); \
47 IM_remove_log = 0; \
48 } \
49 fp_IM_log = fopen(IM_LOG_FILE, "a+"); \
50 if (fp_IM_log != nullptr) \
51 { \
52 fprintf(fp_IM_log, "%s", szBuffer); \
53 fclose(fp_IM_log); \
54 } \
55 } while (0)
56 #else
57 #define __IMLOG__(IMLOGTYPE, TAG) \
58 do \
59 { \
60 va_list args; \
61 char szBuffer[TRACEMAXSTRING]; \
62 va_start(args, format); \
63 vsnprintf(szBuffer, TRACEMAXSTRING, format, args); \
64 va_end(args); \
65 android_printLog(IMLOGTYPE, TAG, "%s", szBuffer); \
66 } while (0)
67 #endif
68
69 #define IM_IS_DEBUGGABLE() (gLogMode <= kLogEnableDebug && __android_log_is_debuggable())
70
71 static uint gLogMode = kLogEnableDebug;
72 static uint gDebugLogMode = 0;
73
IMLOGD_PACKET_ARG(IM_PACKET_LOG_TYPE type,const char * format,...)74 void ImsMediaTrace::IMLOGD_PACKET_ARG(IM_PACKET_LOG_TYPE type, const char* format, ...)
75 {
76 if (gDebugLogMode & type && IM_IS_DEBUGGABLE())
77 {
78 __IMLOG__(ANDROID_LOG_DEBUG, IM_DEBUG_TAG);
79 }
80 }
81
IMSetLogMode(uint mode)82 void ImsMediaTrace::IMSetLogMode(uint mode)
83 {
84 gLogMode = mode;
85 }
86
IMSetDebugLogMode(uint type)87 void ImsMediaTrace::IMSetDebugLogMode(uint type)
88 {
89 gDebugLogMode = type;
90 }
91
IMGetDebugLog()92 uint ImsMediaTrace::IMGetDebugLog()
93 {
94 return gDebugLogMode;
95 }
96
IMLOGD_ARG(const char * format,...)97 void ImsMediaTrace::IMLOGD_ARG(const char* format, ...)
98 {
99 if (IM_IS_DEBUGGABLE())
100 {
101 __IMLOG__(ANDROID_LOG_DEBUG, IM_TAG);
102 }
103 }
104
IMLOGI_ARG(const char * format,...)105 void ImsMediaTrace::IMLOGI_ARG(const char* format, ...)
106 {
107 if (gLogMode <= kLogEnableInfo)
108 {
109 __IMLOG__(ANDROID_LOG_INFO, IM_TAG);
110 }
111 }
112
IMLOGW_ARG(const char * format,...)113 void ImsMediaTrace::IMLOGW_ARG(const char* format, ...)
114 {
115 if (gLogMode <= kLogEnableWarning)
116 {
117 __IMLOG__(ANDROID_LOG_WARN, IM_TAG);
118 }
119 }
120
IMLOGE_ARG(const char * format,...)121 void ImsMediaTrace::IMLOGE_ARG(const char* format, ...)
122 {
123 if (gLogMode <= kLogEnableError)
124 {
125 __IMLOG__(ANDROID_LOG_ERROR, IM_TAG);
126 }
127 }
128
129 #define MAX_PRINT_STRING_LEN 2048
130 static char buffer[MAX_PRINT_STRING_LEN];
131
hex_char(char nibble)132 static char hex_char(char nibble)
133 {
134 const static char buf[16] = {
135 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
136 return buf[nibble & 0xF];
137 }
138
IMTrace_Bin2String(const char * s,int length)139 char* ImsMediaTrace::IMTrace_Bin2String(const char* s, int length)
140 {
141 const char* input = s;
142 char* output = buffer;
143 int i;
144
145 if (length < 0)
146 return 0;
147 if (length * 4 > (MAX_PRINT_STRING_LEN - 5))
148 length = (MAX_PRINT_STRING_LEN / 4) - 5;
149
150 for (i = 0; i < length; i++)
151 {
152 *output++ = hex_char(*input >> 4);
153 *output++ = hex_char(*input++ & 0xF);
154 *output++ = ' ';
155 if ((i & 0x03) == 0x03)
156 *output++ = ' ';
157 }
158
159 *output = 0;
160 return buffer;
161 }
162
IMLOGD_BINARY(const char * msg,const char * s,int length)163 void ImsMediaTrace::IMLOGD_BINARY(const char* msg, const char* s, int length)
164 {
165 #define IMLOG_BIN_LINE_WIDTH 32
166 const char* curr = s;
167 if (msg)
168 IMLOGD1("%s", msg);
169 while (length > 0)
170 {
171 int curr_len = length < IMLOG_BIN_LINE_WIDTH ? length : IMLOG_BIN_LINE_WIDTH;
172 IMLOGD1("\t%s", IMTrace_Bin2String(curr, curr_len));
173 length -= curr_len;
174 curr += curr_len;
175 }
176 }
177
IM_StripFileName(char * pcFileName)178 char* ImsMediaTrace::IM_StripFileName(char* pcFileName)
179 {
180 char* pcTemp = nullptr;
181 pcTemp = strrchr(pcFileName, '/');
182
183 (pcTemp != nullptr) ? pcTemp++ : pcTemp = pcFileName;
184
185 return pcTemp;
186 }
187