summaryrefslogtreecommitdiff
path: root/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/utils/ImsMediaTrace.cpp
blob: aa0a011e91452637d08acff3b08edb268fe75fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <ImsMediaTrace.h>
#include <string.h>
#include <utils/Log.h>
#include <sys/time.h>

#ifdef IM_FILE_LOG
#include <stdio.h>
#include <stdlib.h>
#define IM_LOG_FILE "/data/IM.txt"
static uint IM_remove_log = 1;
#endif

#define TRACEMAXSTRING 1024
#define IM_TAG         "libimsmedia"
#define IM_DEBUG_TAG   "libimsmedia_d"

#ifdef IM_FILE_LOG
#define __IMLOG__(IMLOGTYPE, TAG)                          \
    do                                                     \
    {                                                      \
        va_list args;                                      \
        char szBuffer[TRACEMAXSTRING];                     \
        va_start(args, format);                            \
        vsnprintf(szBuffer, TRACEMAXSTRING, format, args); \
        va_end(args);                                      \
        android_printLog(IMLOGTYPE, TAG, "%s", szBuffer);  \
        FILE* fp_IM_log = nullptr;                         \
        if (IM_remove_log)                                 \
        {                                                  \
            remove(IM_LOG_FILE);                           \
            IM_remove_log = 0;                             \
        }                                                  \
        fp_IM_log = fopen(IM_LOG_FILE, "a+");              \
        if (fp_IM_log != nullptr)                          \
        {                                                  \
            fprintf(fp_IM_log, "%s", szBuffer);            \
            fclose(fp_IM_log);                             \
        }                                                  \
    } while (0)
#else
#define __IMLOG__(IMLOGTYPE, TAG)                          \
    do                                                     \
    {                                                      \
        va_list args;                                      \
        char szBuffer[TRACEMAXSTRING];                     \
        va_start(args, format);                            \
        vsnprintf(szBuffer, TRACEMAXSTRING, format, args); \
        va_end(args);                                      \
        android_printLog(IMLOGTYPE, TAG, "%s", szBuffer);  \
    } while (0)
#endif

static uint gLogMode = kLogEnableInfo;
static uint gDebugLogMode = 0;

void ImsMediaTrace::IMLOGD_PACKET_ARG(IM_PACKET_LOG_TYPE type, const char* format, ...)
{
    if (gDebugLogMode & type && gLogMode <= kLogEnableDebug)
    {
        __IMLOG__(ANDROID_LOG_DEBUG, IM_DEBUG_TAG);
    }
}

void ImsMediaTrace::IMSetLogMode(uint mode)
{
    gLogMode = mode;
}

void ImsMediaTrace::IMSetDebugLogMode(uint type)
{
    gDebugLogMode = type;
}

uint ImsMediaTrace::IMGetDebugLog()
{
    return gDebugLogMode;
}

void ImsMediaTrace::IMLOGD_ARG(const char* format, ...)
{
    if (gLogMode <= kLogEnableDebug)
    {
        __IMLOG__(ANDROID_LOG_DEBUG, IM_TAG);
    }
}

void ImsMediaTrace::IMLOGI_ARG(const char* format, ...)
{
    if (gLogMode <= kLogEnableInfo)
    {
        __IMLOG__(ANDROID_LOG_INFO, IM_TAG);
    }
}

void ImsMediaTrace::IMLOGW_ARG(const char* format, ...)
{
    if (gLogMode <= kLogEnableWarning)
    {
        __IMLOG__(ANDROID_LOG_WARN, IM_TAG);
    }
}

void ImsMediaTrace::IMLOGE_ARG(const char* format, ...)
{
    if (gLogMode <= kLogEnableError)
    {
        __IMLOG__(ANDROID_LOG_ERROR, IM_TAG);
    }
}

#define MAX_PRINT_STRING_LEN 2048
static char buffer[MAX_PRINT_STRING_LEN];

static char hex_char(char nibble)
{
    const static char buf[16] = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    return buf[nibble & 0xF];
}

char* ImsMediaTrace::IMTrace_Bin2String(const char* s, int length)
{
    const char* input = s;
    char* output = buffer;
    int i;

    if (length < 0)
        return 0;
    if (length * 4 > (MAX_PRINT_STRING_LEN - 5))
        length = (MAX_PRINT_STRING_LEN / 4) - 5;

    for (i = 0; i < length; i++)
    {
        *output++ = hex_char(*input >> 4);
        *output++ = hex_char(*input++ & 0xF);
        *output++ = ' ';
        if ((i & 0x03) == 0x03)
            *output++ = ' ';
    }

    *output = 0;
    return buffer;
}

void ImsMediaTrace::IMLOGD_BINARY(const char* msg, const char* s, int length)
{
#define IMLOG_BIN_LINE_WIDTH 32
    const char* curr = s;
    if (msg)
        IMLOGD1("%s", msg);
    while (length > 0)
    {
        int curr_len = length < IMLOG_BIN_LINE_WIDTH ? length : IMLOG_BIN_LINE_WIDTH;
        IMLOGD1("\t%s", IMTrace_Bin2String(curr, curr_len));
        length -= curr_len;
        curr += curr_len;
    }
}

char* ImsMediaTrace::IM_StripFileName(char* pcFileName)
{
    char* pcTemp = nullptr;
    pcTemp = strrchr(pcFileName, '/');

    (pcTemp != nullptr) ? pcTemp++ : pcTemp = pcFileName;

    return pcTemp;
}