1 /*
2  * Copyright (C) 2023, 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 #pragma once
18 
19 #include <chrono>
20 #include <string>
21 
22 #define LOGI(...)                     \
23     do {                              \
24         fprintf(stdout, "[INFO] ");   \
25         fprintf(stdout, __VA_ARGS__); \
26     } while (0)
27 
28 #ifdef DEBUG
29 #define LOGD(...)                               \
30     do {                                        \
31         if (DEBUG) fprintf(stdout, "[DEBUG] "); \
32         fprintf(stdout, __VA_ARGS__);           \
33     } while (0)
34 #else
35 #define LOGD(...)
36 #endif
37 
38 #define LOGE(...)                     \
39     do {                              \
40         fprintf(stderr, "[ERROR] ");  \
41         fprintf(stderr, __VA_ARGS__); \
42     } while (0)
43 
44 namespace android {
45 namespace express {
46 
47 #ifdef DEBUG
48 
49 class ExecutionMeasure final {
50 public:
ExecutionMeasure(std::string name)51     ExecutionMeasure(std::string name)
52         : mName(std::move(name)), mStart(std::chrono::high_resolution_clock::now()) {
53     }
54 
~ExecutionMeasure()55     ~ExecutionMeasure() {
56         const auto stop = std::chrono::high_resolution_clock::now();
57         const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - mStart);
58         LOGI("%s took to complete %lld microseconds\n", mName.c_str(), duration.count());
59     }
60 
61 private:
62     const std::string mName;
63     const std::chrono::time_point<std::chrono::high_resolution_clock> mStart;
64 };
65 
66 #define MEASURE_FUNC() ExecutionMeasure func_execution_measure(__func__);
67 #else
68 #define MEASURE_FUNC()
69 #endif
70 }  // namespace express
71 }  // namespace android
72