10 #include <metaprogramming/UniversalReferences.hpp> 11 #include <debug/MinimalCrash.hpp> 16 #define SCOPE_INDENT(VAR) 18 ScopeIndenter NAME2(SCOPE_INDENTER,__LINE__)(VAR) 21 #define SET_FOR_CURRENT_SCOPE(NAME,VAR,...) 22 auto NAME3(SET,NAME,__LINE__)=getScopeChangeVar(VAR,__VA_ARGS__) 25 #define SCOPE_REAL_PRECISION(STREAM,VAL) 29 #define SCOPE_REAL_FORMAT(STREAM,VAL) 33 #define SCOPE_REAL_FORMAT_GENERAL(STREAM) 37 #define SCOPE_REAL_FORMAT_FIXED(STREAM) 41 #define SCOPE_REAL_FORMAT_ENGINEER(STREAM) 45 #define SCOPE_ALWAYS_PUT_SIGN(STREAM) 49 #define SCOPE_NOT_ALWAYS_PUT_SIGN(STREAM) 53 #define SCOPE_ALWAYS_PRINT_ZERO(STREAM) 57 #define SCOPE_ALWAYS_PRINT_ZERO(STREAM) 61 #define SCOPE_ALL_RANKS_CAN_PRINT(STREAM) 65 inline bool fileExists(
const std::filesystem::path& path)
68 std::filesystem::exists(path);
75 std::filesystem::file_size(path);
90 void open(
const char* path,
92 const bool crashIfFail=
true)
103 if(file==nullptr
and crashIfFail)
152 File& operator<<(
const char& c)
203 File& operator<<(
const double& d)
209 static constexpr
char realFormatString[2][2][3][7]=
210 {{{
"%.*g",
"%.*f",
"%.*e"},
211 {
"%0.*g",
"%0.*f",
"%0.*e"}},
212 {{
"%+.*g",
"%+.*f",
"%+.*e"},
213 {
"%+0.*g",
"%+0.*f",
"%+0.*e"}}};
216 fprintf(file,realFormatString[alwaysPrintSign][alwaysPrintZero][(
int)realFormat],realPrecision,d);
223 template <
typename T>
227 fprintf(file,
"%p",p);
234 template <
typename T>
238 *
this<<
' '<<forw<T>(t);
242 File& operator<<(
const char* str)
245 fprintf(file,
"%s",(str==nullptr)?
"(null)":str);
252 File& operator<<(
const std::string& str)
255 (*
this)<<str.c_str();
259 template <
int MAX_LENGTH=256>
264 char message[MAX_LENGTH];
268 vsnprintf(message,MAX_LENGTH,format,ap);
272 (rc<0
or rc>=MAX_LENGTH);
275 *
this<<message<<
" (truncated line)";
bool fileExists(const std::filesystem::path &path)
Returns whether the given path exists.
#define MINIMAL_CRASH(...)
Initialize the minimal crasher.
void minimalCrash(const char *path, const int line, const char *funcName, const char *format,...)
int getRc() const
Returns rc.
#define NAME2(X, Y)
Merges two tokens using a _.
#define NAME3(X, Y, Z)
Merges three tokens with two _.
std::uintmax_t fileSize(const std::filesystem::path &path)
Returns the size of a file.
bool isOpen() const
Check if open.
File & operator<<(const uint64_t &l)
Prints a long unsigned integer.
File & printVariadicMessage(const char *format, va_list ap)
Print a C-style variadic message.
void open(const char *path, const char *mode, const bool crashIfFail=true)
Open a file, crashing if impossible.
RealFormat
Print mode for double/float.
File & operator*(T &&t)
Prints after putting a space.
#define SET_FOR_CURRENT_SCOPE(NAME, VAR,...)
Set for current scope.