1/*
2 * Copyright (C) 2010 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/* trace debugging */
18
19#include "sles_allinclusive.h"
20
21#ifdef USE_TRACE
22
23// This should be the only global variable
24static unsigned slTraceEnabled = SL_TRACE_DEFAULT;
25
26
27void slTraceSetEnabled(unsigned enabled)
28{
29    slTraceEnabled = enabled;
30}
31
32
33void slTraceEnterGlobal(const char *function)
34{
35    if (SL_TRACE_ENTER & slTraceEnabled) {
36        SL_LOGD("Entering %s", function);
37    }
38}
39
40
41void slTraceLeaveGlobal(const char *function, SLresult result)
42{
43    if (SL_RESULT_SUCCESS == result) {
44        if (SL_TRACE_LEAVE_SUCCESS & slTraceEnabled) {
45            SL_LOGD("Leaving %s", function);
46        }
47    } else {
48        if (SL_TRACE_LEAVE_FAILURE & slTraceEnabled) {
49            const char *str = slesutResultToString(result);
50            if (NULL != str) {
51                SL_LOGW("Leaving %s (%s)", function, str);
52            } else {
53                SL_LOGW("Leaving %s (0x%X)", function, result);
54            }
55        }
56    }
57}
58
59
60void slTraceEnterInterface(const char *function)
61{
62    if (!(SL_TRACE_ENTER & slTraceEnabled)) {
63        return;
64    }
65    if (*function == 'I') {
66        ++function;
67    }
68    const char *underscore = function;
69    while (*underscore != '\0') {
70        if (*underscore == '_') {
71            if (/*(strcmp(function, "BufferQueue_Enqueue") &&
72                strcmp(function, "BufferQueue_GetState") &&
73                strcmp(function, "OutputMixExt_FillBuffer")) &&*/
74                true) {
75                SL_LOGD("Entering %.*s::%s", (int) (underscore - function), function,
76                    &underscore[1]);
77            }
78            return;
79        }
80        ++underscore;
81    }
82    SL_LOGV("Entering %s", function);
83}
84
85
86void slTraceLeaveInterface(const char *function, SLresult result)
87{
88    if (!((SL_TRACE_LEAVE_SUCCESS | SL_TRACE_LEAVE_FAILURE) & slTraceEnabled)) {
89        return;
90    }
91    if (*function == 'I') {
92        ++function;
93    }
94    const char *underscore = function;
95    while (*underscore != '\0') {
96        if (*underscore == '_') {
97            break;
98        }
99        ++underscore;
100    }
101    if (SL_RESULT_SUCCESS == result) {
102        if (SL_TRACE_LEAVE_SUCCESS & slTraceEnabled) {
103            if (*underscore == '_') {
104                SL_LOGD("Leaving %.*s::%s", (int) (underscore - function), function,
105                    &underscore[1]);
106            } else {
107                SL_LOGD("Leaving %s", function);
108            }
109        }
110    } else {
111        if (SL_TRACE_LEAVE_FAILURE & slTraceEnabled) {
112            const char *str = slesutResultToString(result);
113            if (*underscore == '_') {
114                if (NULL != str) {
115                    SL_LOGW("Leaving %.*s::%s (%s)", (int) (underscore - function), function,
116                        &underscore[1], str);
117                } else {
118                    SL_LOGW("Leaving %.*s::%s (0x%X)", (int) (underscore - function), function,
119                        &underscore[1], result);
120                }
121            } else {
122                if (NULL != str) {
123                    SL_LOGW("Leaving %s (%s)", function, str);
124                } else {
125                    SL_LOGW("Leaving %s (0x%X)", function, result);
126                }
127            }
128        }
129    }
130}
131
132
133void slTraceEnterInterfaceVoid(const char *function)
134{
135    if (SL_TRACE_ENTER & slTraceEnabled) {
136        slTraceEnterInterface(function);
137    }
138}
139
140
141void slTraceLeaveInterfaceVoid(const char *function)
142{
143    if (SL_TRACE_LEAVE_VOID & slTraceEnabled) {
144        slTraceLeaveInterface(function, SL_RESULT_SUCCESS);
145    }
146}
147
148#else
149
150void slTraceSetEnabled(unsigned enabled)
151{
152}
153
154#endif // USE_TRACE
155