Shadow Stack  0.2
 All Classes Namespaces Files Functions Variables Typedefs Macros
utilities.hpp
Go to the documentation of this file.
1 
2 #ifndef __UTILITIES_HPP__
3 #define __UTILITIES_HPP__
4 
5 #include <unistd.h>
6 #include <sstream>
7 
8 
9 // Undefine macro assert, it clobbers the class member function assert
10 #undef assert
11 
12 
13 /*********************************************************/
14 /* */
15 /* Function Declarations */
16 /* */
17 /*********************************************************/
18 
19 
21 class Utilities {
22 
25 
28  static int log_fd;
29 
32  static const int stdout_fd;
33 
36  static const int error_fd;
37 
38  public:
40  static constexpr const int invalid_fd = -2;
41 
45  static void setup( const bool clear_log );
46 
50  static pid_t get_tid();
51 
53  struct Unused {
55  template<typename ...unused> Unused(unused const & ... ) {}
56  };
57 
58 
59  /*********************************************************/
60  /* */
61  /* Error checking */
62  /* */
63  /*********************************************************/
64 
65 
69  [[noreturn]] static void err( const char *const s );
70 
72  static void assert( const bool b, const char *const s );
73 
74 
75  /*********************************************************/
76  /* */
77  /* Logging */
78  /* */
79  /*********************************************************/
80 
81 
85  template <typename... Args> static void log( Args &&... args ) {
86  write_log( log_fd, std::forward<Args>( args )... );
87  }
88 
92  template <typename... Args> static void verbose_log( Args &&... args ) {
93 #ifdef DEBUG_MODE
94 # ifdef VERBOSE
95  log( std::forward<Args>( args )... );
96 # endif
97 #endif
98  Unused { args... };
99  }
100 
105  template <typename... Args> static void message( Args &&... args ) {
106  write_log( log_fd, std::forward<Args>( args )... );
107  write_log( stdout_fd, std::forward<Args>( args )... );
108  }
109 
114  template <typename... Args> static void log_error( Args &&... args ) {
115  write_log( log_fd, std::forward<Args>( args )... );
116  write_log( error_fd, std::forward<Args>( args )... );
117  }
118 
119 #ifdef DEBUG_MODE
120 
123  template <typename... Args> static void debug( Args &&... args ) {
124  log_error( std::forward<Args>( args )... );
125  }
126 #endif
127 
130 
131  private:
136  template <typename... Args> static void write_log( const int fd, Args &&... args );
137 
141  template <typename... Args>
142  static void write_log_helper( std::stringstream &stream, Args &&... args );
143 
144 
145  /*********************************************************/
146  /* */
147  /* Template specialization implementations */
148  /* */
149  /*********************************************************/
150 
151 
153  template <typename T>
154  static void write_log_helper( std::stringstream &stream, T &&val ) {
155  stream << val;
156  }
157 
159  template <typename Head, typename... Tail>
160  static void write_log_helper( std::stringstream &stream, Head &&head,
161  Tail &&... tail ) {
162  stream << head;
163  write_log_helper( stream, std::forward<Tail>( tail )... );
164  }
165 };
166 
167 /*********************************************************/
168 /* */
169 /* Template non-specialization implementations */
170 /* */
171 /*********************************************************/
172 
173 
174 // Write the arguments to f if f is not null
175 // On failure, silently fails (since it cannot write out)
176 template <typename... Args> void Utilities::write_log( const int fd, Args &&... args ) {
177  if ( fd != invalid_fd ) {
178 
179  // Create the stream and note the tid if is_multi_thread_or_proccess
180  std::stringstream stream;
182  stream << get_tid() << ": ";
183  }
184 
185  write_log_helper( stream, std::forward<Args>( args )... );
186  stream << '\n';
187 
188  // Write the string then flush the buffer
189  // Assert message errors must be nullptr to prevent a loop
190  const std::string str = stream.str();
191  const ssize_t len = str.size();
192  Utilities::assert( write( fd, str.c_str(), len ) == (ssize_t) len, nullptr );
193  }
194 }
195 
196 
197 #endif /* utilities_h */
static void enable_multi_thread_or_process_mode()
Once this is called, TIDs will be printed with each message.
Definition: utilities.cpp:116
static void write_log_helper(std::stringstream &stream, T &&val)
A specialization of write_log_helper that has only one template argument.
Definition: utilities.hpp:154
A general class for utilities.
Definition: utilities.hpp:21
static pid_t get_tid()
Define a gettid function On failure, disables multi_threaded / functionality (to continue logging) th...
Definition: utilities.cpp:106
static void log_error(Args &&...args)
Prints the arguments as cout would to the error and log files Ends the printed line(s) with a newline...
Definition: utilities.hpp:114
static constexpr const int invalid_fd
A known invalid file descriptor.
Definition: utilities.hpp:40
Unused(unused const &...)
The constructor.
Definition: utilities.hpp:55
A struct used to eat unused arguments.
Definition: utilities.hpp:53
static void write_log(const int fd, Args &&...args)
A wrapper that writes args to f if f is not null Ends the printed line(s) with a newline then flushes...
Definition: utilities.hpp:176
static void assert(const bool b, const char *const s)
assert b, if false call program_err(s)
Definition: utilities.cpp:89
static void err(const char *const s)
To be called in case of an error Prints log s to the error file, perrors, then kills the process grou...
Definition: utilities.cpp:80
static void verbose_log(Args &&...args)
Logs the arguments as cout would to the log file if not null Ends the printed line(s) with a newline ...
Definition: utilities.hpp:92
static int log_fd
The log file descriptor This must be defined before main()
Definition: utilities.hpp:28
static bool is_multi_thread_or_proccess
Notes whether or not any threading / forking has happened yet.
Definition: utilities.hpp:24
static void message(Args &&...args)
Prints the arguments as cout would to the stdout and log files Ends the printed line(s) with a newlin...
Definition: utilities.hpp:105
static const int stdout_fd
The stdout file descriptor This must be defined before main()
Definition: utilities.hpp:32
static void write_log_helper(std::stringstream &stream, Head &&head, Tail &&...tail)
A specialization of write_log_helper that has multiple template arguments.
Definition: utilities.hpp:160
static void write_log_helper(std::stringstream &stream, Args &&...args)
Adds the arguments to the stream Ends the printed line(s) with a newline then flushes the buffer If t...
static void log(Args &&...args)
Logs the arguments as cout would to the log file if not null Ends the printed line(s) with a newline ...
Definition: utilities.hpp:85
A struct returned by the argument parser.
Definition: parse_args.hpp:35
static const int error_fd
The error file descriptor This must be defined before main()
Definition: utilities.hpp:36
static void setup(const bool clear_log)
Sets up the utilities class If clear_log, the log will be cleared on setup If this function fails...
Definition: utilities.cpp:43