1 /** 2 * Copyright (C) Mellanox Technologies Ltd. 2018. ALL RIGHTS RESERVED. 3 * 4 * See file LICENSE for terms. 5 */ 6 7 #ifndef SA_UTIL_H_ 8 #define SA_UTIL_H_ 9 10 #include <iostream> 11 #include <sstream> 12 #include <string> 13 #include <vector> 14 15 16 /* runtime error exception */ 17 class error : public std::exception { 18 public: 19 error(const std::string& message); 20 21 virtual ~error() throw(); 22 23 virtual const char* what() const throw(); 24 25 private: 26 std::string m_message; 27 }; 28 29 30 /* system error exception */ 31 class sys_error : public error { 32 public: 33 virtual ~sys_error() throw(); 34 35 sys_error(const std::string& message, int errn); 36 }; 37 38 39 /* file descriptor wrapper which closes the file automatically */ 40 class file_desc { 41 public: 42 file_desc(int fd); 43 44 virtual ~file_desc(); 45 46 operator int() const; 47 48 private: 49 file_desc(const file_desc&); 50 51 const file_desc& operator=(const file_desc&); 52 53 int m_fd; 54 }; 55 56 57 /* event poll set */ 58 class evpoll_set : public file_desc { 59 public: 60 struct event { 61 int fd; 62 uint32_t ev_flags; 63 }; 64 65 evpoll_set(); 66 67 void add(int fd, uint32_t ev_flags); 68 69 void wait(std::vector<event>& events, int timeout_ms = -1) const; 70 71 private: 72 static int create_epfd(); 73 }; 74 75 #define LOG_INFO \ 76 log(log::INFO, __FILE__, __LINE__) 77 #define LOG_DEBUG \ 78 log(log::DEBUG, __FILE__, __LINE__) 79 80 /* logger */ 81 class log { 82 public: 83 typedef enum { 84 INFO, 85 DEBUG 86 } level_t; 87 88 log(level_t level, const std::string& file, int line); 89 ~log(); 90 91 template <typename T> 92 log& operator<<(const T& value) { 93 m_msg << value; 94 return *this; 95 } 96 97 static void more_verbose(); 98 99 private: 100 static std::string level_str(level_t level); 101 102 static level_t m_log_level; 103 const bool m_enabled; 104 std::ostringstream m_msg; 105 }; 106 107 #endif 108