Proto  3.2
Proto_PAssert.H
Go to the documentation of this file.
1 #pragma once
2 #ifndef __PROTO_ASSERT__
3 #define __PROTO_ASSERT__
4 #include <cassert>
5 #include <cstdio>
6 #include <stdio.h>
7 #include <cstdlib>
8 #include <string>
9 #include <sstream>
10 #include <vector>
11 
12 namespace ProtoUtils
13 {
14  inline std::vector<std::string> split_string (const std::string &s, char delim) {
15  std::vector<std::string> result;
16  std::stringstream ss (s);
17  std::string item;
18 
19  while (getline (ss, item, delim)) {
20  result.push_back (item);
21  }
22 
23  return result;
24  }
25 }
26 // Custom Assert macro with printf-style reporting
27 #ifdef __CUDA_ARCH__
28 
29 #define PROTO_ASSERT(stmt,args...)
30 #define PR_DEBUG_MSG(N, stmt, args...)
31 #define PR_assert(stmt)
32 #define PR_error(stmt)
33 #define PR_warning(stmt)
34 
35 #elif NDEBUG
36 
37 #define PROTO_ASSERT(stmt,args...)
38 #define PR_DEBUG_MSG(N, stmt, args...)
39 #define PR_assert(stmt)
40 #define PR_error(stmt)
41 #define PR_warning(stmt)
42 
43 #else
44 
45 
46 #ifndef PROTO_HIP
47 
48 #define PROTO_ASSERT(stmt,args...) \
49  assert( (stmt) || \
50  (fprintf(stderr,"%s:%d: error: ",__FILE__,__LINE__) && \
51  fprintf(stderr,"Assertion `" #stmt "` failed.\n\t") && \
52  fprintf(stderr,args) && \
53  fprintf(stderr,"\n"))); \
54  if (!(stmt)){std::abort();}
55 
56 #define PR_DEBUG_MSG(N, args...) \
57  if (N <= PR_VERBOSE) \
58  { \
59  auto words = ProtoUtils::split_string(__FILE__, '/'); \
60  string filename = words[words.size()-1]; \
61  char prefix[100]; \
62  char message[500]; \
63  (fprintf(stdout, "DEBUG | %s:%d: ", filename.c_str(), __LINE__) && \
64  fprintf(stdout, args) && \
65  fprintf(stdout, "\n")); \
66  }
67 
68 #define PR_assert(stmt) assert( (stmt) );
69 
70 
71 #define PR_error(stmt) \
72  fprintf(stderr,"error thrown = `" #stmt "`!!!\n\t"); \
73  std::abort();
74 
75 #define PR_warning(stmt) \
76  fprintf(stderr,"warning thrown = `" #stmt "` !!!\n\t");
77 
78 #else // PROTO_HIP
79 #define PROTO_ASSERT(stmt,args...)
80 #define PR_DEBUG_MSG(N, stmt, args...)
81 #define PR_assert(stmt)
82 #define PR_error(stmt)
83 #define PR_warning(stmt)
84 
85 #endif
86 #endif
87 #endif // end include guard
std::vector< std::string > split_string(const std::string &s, char delim)
Definition: Proto_PAssert.H:14
Definition: Proto_PAssert.H:12