1 /*
2  * Copyright (C) 2017 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 #include "slicer/common.h"
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <cstdarg>
22 #include <set>
23 #include <utility>
24 
25 namespace slicer {
26 
27 // Helper for the default SLICER_CHECK() policy
_checkFailed(const char * expr,int line,const char * file)28 void _checkFailed(const char* expr, int line, const char* file) {
29   printf("\nSLICER_CHECK failed [%s] at %s:%d\n\n", expr, file, line);
30   abort();
31 }
32 
33 // keep track of the failures we already saw to avoid spamming with duplicates
34 thread_local std::set<std::pair<int, const char*>> weak_failures;
35 
36 // Helper for the default SLICER_WEAK_CHECK() policy
37 //
38 // TODO: implement a modal switch (abort/continue)
39 //
_weakCheckFailed(const char * expr,int line,const char * file)40 void _weakCheckFailed(const char* expr, int line, const char* file) {
41   auto failure_id = std::make_pair(line, file);
42   if (weak_failures.find(failure_id) == weak_failures.end()) {
43     printf("\nSLICER_WEAK_CHECK failed [%s] at %s:%d\n\n", expr, file, line);
44     weak_failures.insert(failure_id);
45   }
46 }
47 
48 // Prints a formatted message and aborts
_fatal(const char * format,...)49 void _fatal(const char* format, ...) {
50   va_list args;
51   va_start(args, format);
52   vprintf(format, args);
53   va_end(args);
54   abort();
55 }
56 
57 } // namespace slicer
58 
59