The vbs tools - vbs_ls, vbs_rm, vbs_fs - for listing, removing and mounting vbs and Mark6 format scattered VLBI recordings on FlexBuff and Mark6 systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

178 lines
5.1 KiB

  1. // 'wrapper' around calling systemcalls.
  2. // Copyright (C) 2007-2008 Harro Verkouter
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful, but WITHOUT ANY
  10. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. // PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // Author: Harro Verkouter - verkouter@jive.nl
  17. // Joint Institute for VLBI in Europe
  18. // P.O. Box 2
  19. // 7990 AA Dwingeloo
  20. //
  21. // In case of a failed condition, it will throw and will
  22. // automatically add location details as well as the call that failed and
  23. // the system error message (as indicated by errno).
  24. //
  25. // Defines:
  26. // ASSERT_<cond>( <expression> )
  27. // assertion macros which will throw if
  28. // the <expression> does not meet <condition>
  29. // ASSERT2_<cond>( <expression>, <cleanupcode> )
  30. // id. as above but will execute <cleanupcode> immediately
  31. // before throwing
  32. #ifndef EVLBI5A_DOSYSCALL_H
  33. #define EVLBI5A_DOSYSCALL_H
  34. #include <iostream>
  35. #include <sstream>
  36. #include <string>
  37. #include <exception>
  38. // the constructor of this baby captures 'errno' and the associated message
  39. struct lastsyserror_type {
  40. lastsyserror_type();
  41. int sys_errno;
  42. std::string sys_errormessage;
  43. };
  44. // an exception of this type is thrown
  45. struct syscallexception :
  46. public std::exception
  47. {
  48. syscallexception( const std::string& s );
  49. virtual const char* what() const throw();
  50. virtual ~syscallexception() throw();
  51. const std::string msg;
  52. };
  53. // if errno == 0, don't show anything
  54. std::ostream& operator<<( std::ostream& os, const lastsyserror_type& lse );
  55. // Set up the defines to make it all readable (ahem) and usable
  56. #define SYSCALLLOCATION \
  57. std::string fn_( __FILE__); int ln_(__LINE__);
  58. #define SYSCALLSTUFF(fubarvar) \
  59. lastsyserror_type lse; std::ostringstream lclSvar_0a;\
  60. lclSvar_0a << fn_ << "@" << ln_ << " [" << fubarvar << "] fails " << lse;
  61. // SCINFO [short for SysCallInfo]:
  62. // can be used to add extra info to the errormessage. Use as (one of) the
  63. // entries in the ASSERT2_*() macros: eg:
  64. //
  65. // int fd;
  66. // string proto;
  67. //
  68. // <... open file and store fd ...>
  69. //
  70. // ASSERT2_NZERO( getprotobyname(proto.c_str()), // see if this works
  71. // ::close(fd); SCINFO("proto.c_str()="<<proto.c_str()) ); // if not, execute this
  72. //
  73. #define SCINFO(a) \
  74. lclSvar_0a << a;
  75. // If you want to save the returnvalue of the function
  76. // that can be easily done via:
  77. //
  78. // int fd;
  79. //
  80. // ASSERT_POS( (fd=open("<some>/<path>/<to>/<file>", O_RDONLY)) );
  81. // read(fd, <buffer>, <size>);
  82. //
  83. // NOTE: You should use ()'s around the assignment...
  84. // Generic condition; if expression !(a) evaluates to true
  85. // (ie: (a) evaluates to false, ie 'condition not met')
  86. // then throw up
  87. //
  88. // The ASSERT2_* defines take *two* arguments; the 2nd expression will be evaluated/executed
  89. // just before the exception is thrown. It is for cleanup code:
  90. //
  91. // int fd;
  92. // char* sptr;
  93. //
  94. // // open a file
  95. // ASSERT_POS( (fd=open("/some/file", O_RDONLY)) );
  96. // // alloc memory, close file in case of error
  97. // ASSERT2_NZERO( (sptr=(char*)malloc(hugeval)), close(fd) );
  98. //
  99. #define ASSERT2_COND(a, b) \
  100. do {\
  101. SYSCALLLOCATION;\
  102. if( !(a) ) { \
  103. SYSCALLSTUFF(#a);\
  104. b;\
  105. throw syscallexception( lclSvar_0a.str() ); \
  106. } \
  107. } while( 0 );
  108. // w/o cleanup is just "with cleanup" where the cleanup is a nop
  109. #define ASSERT_COND(a) \
  110. ASSERT2_COND(a, ;)
  111. //
  112. // Now define shortcuts for most often used conditions
  113. //
  114. // For systemcalls that should return 0 on success
  115. #define ASSERT2_ZERO(a, b) \
  116. do {\
  117. SYSCALLLOCATION;\
  118. if( (a)!=0 ) { \
  119. SYSCALLSTUFF(#a);\
  120. b; \
  121. throw syscallexception( lclSvar_0a.str() ); \
  122. } \
  123. } while( 0 );
  124. #define ASSERT_ZERO(a) \
  125. ASSERT2_ZERO(a, ;)
  126. // For functions that should NOT return zero
  127. // (note: you can also stick functions in here that
  128. // return a pointer. Long live C++! ;))
  129. #define ASSERT2_NZERO(a, b) \
  130. do {\
  131. SYSCALLLOCATION;\
  132. if( (a)==0 ) { \
  133. SYSCALLSTUFF(#a);\
  134. b; \
  135. throw syscallexception( lclSvar_0a.str() ); \
  136. } \
  137. } while( 0 );
  138. #define ASSERT_NZERO(a) \
  139. ASSERT2_NZERO(a, ;)
  140. // functions that should not return a negative number
  141. // Note: 0 is not an error in this case (eg semget(2))
  142. #define ASSERT2_POS(a, b) \
  143. do {\
  144. SYSCALLLOCATION;\
  145. if( (a)<0 ) { \
  146. SYSCALLSTUFF(#a);\
  147. b; \
  148. throw syscallexception( lclSvar_0a.str() ); \
  149. } \
  150. } while( 0 );
  151. #define ASSERT_POS(a) \
  152. ASSERT2_POS(a, ;)
  153. #endif // includeguard