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 #else
46 // CHRE/SLPI Nanoapp Logging.
47 # include "chre/util/nanoapp/log.h"
48 # ifndef LOG_TAG
49 #  define LOG_TAG ""
50 # endif  // LOG_TAG
51 # define CAL_DEBUG_LOG(tag, format, ...) \
52    LOGI("%s " format, tag, ##__VA_ARGS__)
53 #endif  // GCC_DEBUG_LOG
54 // clang-format on
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /*
61  * On some embedded software platforms numerical string formatting is not fully
62  * supported. Defining CAL_NO_FLOAT_FORMAT_STRINGS will enable a workaround that
63  * prints floating-point values having a specified number of digits using the
64  * CAL_ENCODE_FLOAT macro.
65  *   Examples:
66  *     - Nanohub does not support floating-point format strings.
67  *     - CHRE/SLPI allows %.Xf for printing float values.
68  */
69 #ifdef CAL_NO_FLOAT_FORMAT_STRINGS
70 // Macro used to print floating point numbers with a specified number of digits.
71 # define CAL_ENCODE_FLOAT(x, num_digits)           \
72   ((x < 0) ? "-" : ""), (int)NANO_FLOOR(fabsf(x)),  \
73       (int)((fabsf(x) - NANO_FLOOR(fabsf(x))) *     \
74             powf(10, num_digits))  // NOLINT
75 
76 // Helper definitions for CAL_ENCODE_FLOAT to specify the print format with
77 // desired significant digits.
78 # define CAL_FORMAT_3DIGITS "%s%d.%03d"
79 # define CAL_FORMAT_6DIGITS "%s%d.%06d"
80 # define CAL_FORMAT_3DIGITS_TRIPLET "%s%d.%03d, %s%d.%03d, %s%d.%03d"
81 # define CAL_FORMAT_6DIGITS_TRIPLET "%s%d.%06d, %s%d.%06d, %s%d.%06d"
82 #else
83 // Pass-through when float string formatting (e.g., %.6f) is available.
84 # define CAL_ENCODE_FLOAT(x, num_digits) (x)
85 
86 // Float string formatting helpers.
87 # define CAL_FORMAT_3DIGITS "%.3f"
88 # define CAL_FORMAT_6DIGITS "%.6f"
89 # define CAL_FORMAT_3DIGITS_TRIPLET "%.3f, %.3f, %.3f"
90 # define CAL_FORMAT_6DIGITS_TRIPLET "%.6f, %.6f, %.6f"
91 #endif  // CAL_NO_FLOAT_FORMAT_STRINGS
92 
93 #ifdef __cplusplus
94 }
95 #endif
96 
97 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_UTIL_CAL_LOG_H_
98