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#ifndef CHRE_PLATFORM_ASSERT_H_
18#define CHRE_PLATFORM_ASSERT_H_
19
20#include "chre/platform/log.h"
21
22/**
23 * @file
24 * Includes the platform-specific header file that supplies an assertion macro.
25 * The platform header must supply the following symbol as a macro or free
26 * function:
27 *
28 *   CHRE_ASSERT(scalar expression)
29 *
30 * Where expression will be checked to be false (ie: compares equal to zero) and
31 * terminate the program if found to be the case.
32 */
33
34#if defined(CHRE_ASSERTIONS_ENABLED)
35
36#include "chre/target_platform/assert.h"
37
38#ifndef CHRE_ASSERT
39#error "CHRE_ASSERT must be defined by the target platform's assert.h"
40#endif  // CHRE_ASSERT
41
42#elif defined(CHRE_ASSERTIONS_DISABLED)
43
44#define CHRE_ASSERT(condition) ((void) (condition))
45
46#else
47#error "CHRE_ASSERTIONS_ENABLED or CHRE_ASSERTIONS_DISABLED must be defined"
48#endif  // CHRE_ASSERTIONS_ENABLED
49
50/**
51 * Combination macro that always logs an error message if the condition
52 * evaluates to false.
53 *
54 * Note that the supplied condition may be evaluated more than once.
55 *
56 * @param condition Boolean expression which evaluates to false in the failure
57 *        case
58 * @param fmt Format string to pass to LOGE
59 * @param ... Arguments to pass to LOGE
60 */
61#define CHRE_ASSERT_LOG(condition, fmt, ...) do { \
62  if (!(condition)) {                             \
63    LOGE("Assert: " fmt, ##__VA_ARGS__);          \
64    CHRE_ASSERT(condition);                       \
65  }                                               \
66} while (0)
67
68#endif  // CHRE_PLATFORM_ASSERT_H_
69