1 /*
2  * Copyright (C) 2016 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 ///////////////////////////////////////////////////////////////
18 /*
19  * Logging macros for printing user-debug messages.
20  */
21 ///////////////////////////////////////////////////////////////
22 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_UTIL_CAL_LOG_H_
23 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_UTIL_CAL_LOG_H_
24 
25 #include "common/math/macros.h"
26 
27 // clang-format off
28 #ifdef GCC_DEBUG_LOG
29 # include <stdio.h>
30 # define CAL_DEBUG_LOG(tag, fmt, ...) \
31    printf("%s " fmt "\n", tag, ##__VA_ARGS__);
32 #elif _OS_BUILD_
33 # include <seos.h>
34 # ifndef LOG_FUNC
35 #  define LOG_FUNC(level, fmt, ...) osLog(level, fmt, ##__VA_ARGS__)
36 # endif  // LOG_FUNC
37 # define LOGD_TAG(tag, fmt, ...) \
38    LOG_FUNC(LOG_DEBUG, "%s " fmt "\n", tag, ##__VA_ARGS__)
39 # define CAL_DEBUG_LOG(tag, fmt, ...) \
40    osLog(LOG_DEBUG, "%s " fmt, tag, ##__VA_ARGS__);
41 #elif NANOHUB_DEBUG_LOG
42 # include <chre.h>
43 # define CAL_DEBUG_LOG(tag, fmt, ...) \
44    chreLog(CHRE_LOG_INFO, "%s " fmt, tag, ##__VA_ARGS__)
45 #elif ROHAN_DEBUG_LOG
46 # include "caraway/logging.h"
47 # define CAL_DEBUG_LOG(tag, format, ...) \
48    LOG_DEBUG("%s " format, tag, ##__VA_ARGS__)
49 #else
50 // CHRE/SLPI Nanoapp Logging.
51 # include "chre/util/nanoapp/log.h"
52 # ifndef LOG_TAG
53 #  define LOG_TAG ""
54 # endif  // LOG_TAG
55 # define CAL_DEBUG_LOG(tag, format, ...) \
56    LOGI("%s " format, tag, ##__VA_ARGS__)
57 #endif  // GCC_DEBUG_LOG
58 // clang-format on
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 /*
65  * On some embedded software platforms numerical string formatting is not fully
66  * supported. Defining CAL_NO_FLOAT_FORMAT_STRINGS will enable a workaround that
67  * prints floating-point values having a specified number of digits using the
68  * CAL_ENCODE_FLOAT macro.
69  *   Examples:
70  *     - Nanohub does not support floating-point format strings.
71  *     - CHRE/SLPI allows %.Xf for printing float values.
72  */
73 #ifdef CAL_NO_FLOAT_FORMAT_STRINGS
74 // Macro used to print floating point numbers with a specified number of digits.
75 # define CAL_ENCODE_FLOAT(x, num_digits)           \
76   ((x < 0) ? "-" : ""), (int)NANO_FLOOR(fabsf(x)),  \
77       (int)((fabsf(x) - NANO_FLOOR(fabsf(x))) *     \
78             powf(10, num_digits))  // NOLINT
79 
80 // Helper definitions for CAL_ENCODE_FLOAT to specify the print format with
81 // desired significant digits.
82 # define CAL_FORMAT_3DIGITS "%s%d.%03d"
83 # define CAL_FORMAT_6DIGITS "%s%d.%06d"
84 # define CAL_FORMAT_3DIGITS_TRIPLET "%s%d.%03d, %s%d.%03d, %s%d.%03d"
85 # define CAL_FORMAT_6DIGITS_TRIPLET "%s%d.%06d, %s%d.%06d, %s%d.%06d"
86 #else
87 // Pass-through when float string formatting (e.g., %.6f) is available.
88 # define CAL_ENCODE_FLOAT(x, num_digits) (x)
89 
90 // Float string formatting helpers.
91 # define CAL_FORMAT_3DIGITS "%.3f"
92 # define CAL_FORMAT_6DIGITS "%.6f"
93 # define CAL_FORMAT_3DIGITS_TRIPLET "%.3f, %.3f, %.3f"
94 # define CAL_FORMAT_6DIGITS_TRIPLET "%.6f, %.6f, %.6f"
95 #endif  // CAL_NO_FLOAT_FORMAT_STRINGS
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_UTIL_CAL_LOG_H_
102