1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef AUDIO_UTILS_C_H
16 #define AUDIO_UTILS_C_H
17 
18 #include <inttypes.h>
19 #include <securec.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 #define SPRINTF_STRING_LEN 256
25 #define AUTO_CLEANUP(func) __attribute__((cleanup(func)))
26 #define AUTO_CLEAR AUTO_CLEANUP(CallEndAndClear)
27 
28 typedef struct CTrace CTrace;
29 
30 #define AUTO_NAME_LINE_INNER(name, line) name##line
31 
32 #define AUTO_NAME_LINE(name, line) AUTO_NAME_LINE_INNER(name, line)
33 
34 #define AUTO_NAME(name) AUTO_NAME_LINE(name, __LINE__)
35 
36 // must use string length less than 256
37 #define AUTO_CTRACE(fmt, args...)                                           \
38     char AUTO_NAME(str)[SPRINTF_STRING_LEN] = {0};                                     \
39     int AUTO_NAME(ret) = sprintf_s(AUTO_NAME(str), SPRINTF_STRING_LEN, fmt, ##args);              \
40     AUTO_CLEAR CTrace *AUTO_NAME(tmpCtrace) = (AUTO_NAME(ret) >= 0 ? GetAndStart(AUTO_NAME(str)) : NULL);    \
41     (void)AUTO_NAME(tmpCtrace)
42 
43 // must call with AUTO_CLEAR
44 CTrace *GetAndStart(const char *traceName);
45 
46 void EndCTrace(CTrace *cTrace);
47 
48 void CTraceCount(const char *traceName, int64_t count);
49 
50 void CallEndAndClear(CTrace **cTrace);
51 
52 #ifdef __cplusplus
53 }
54 #endif
55 
56 #endif // AUDIO_UTILS_C_H