| 1 |
/* -*- c++ -*- */ |
| 2 |
#ifndef UTILS_REPORT_H |
| 3 |
#define UTILS_REPORT_H |
| 4 |
|
| 5 |
#include <iostream> |
| 6 |
#include <string> |
| 7 |
using std::cerr; |
| 8 |
|
| 9 |
namespace OpenMD { |
| 10 |
//________________________________________________________ Report |
| 11 |
namespace Report { |
| 12 |
class MyStreamer; |
| 13 |
|
| 14 |
//_________________________________________ debug |
| 15 |
/** |
| 16 |
Manipulator of MyStreamer, to print out debug |
| 17 |
information with different report level importance, where |
| 18 |
1, pretty important, 10 really unimportant. |
| 19 |
*/ |
| 20 |
class debug { |
| 21 |
public: |
| 22 |
debug() : myLevel(1) {} |
| 23 |
debug(short l) : myLevel(l) {} |
| 24 |
MyStreamer &operator()(MyStreamer &stream) const; |
| 25 |
private: |
| 26 |
short myLevel; |
| 27 |
}; |
| 28 |
|
| 29 |
//_________________________________________ reportlevel |
| 30 |
/** |
| 31 |
reportlevel sets the maximal level of report, if less |
| 32 |
than 1 all debug information is suppressed. |
| 33 |
*/ |
| 34 |
class reportlevel { |
| 35 |
public: |
| 36 |
reportlevel() : myReportlevel(0) {} |
| 37 |
reportlevel(short l) : myReportlevel(l) {} |
| 38 |
MyStreamer &operator()(MyStreamer &stream) const; |
| 39 |
private: |
| 40 |
short myReportlevel; |
| 41 |
}; |
| 42 |
|
| 43 |
//_________________________________________ MyStreamer |
| 44 |
/** |
| 45 |
MyStreamer wraps the output to a given stream and provides different |
| 46 |
manipulators a la std::cout and std::cerr. report is a global instance |
| 47 |
of MyStreamer and acts as std::cout and std::cerr. In parallel |
| 48 |
environment, by default, only output from the master is passed to the |
| 49 |
actual stream. One can also pipe the putput to a file. |
| 50 |
@n |
| 51 |
It handles different report levels. Production code will by default |
| 52 |
suppress all debug output (print if level <= 0, reportlevel=0), where as |
| 53 |
debug compilation will by default print all information with level <= 1, |
| 54 |
reportlevel=1. |
| 55 |
@n |
| 56 |
NB! Do not put debug(<int>) inside forces, ok if it is inside |
| 57 |
initialization, otherwise use a type from oneAtomContraints.h to |
| 58 |
debug pair wise potentials @n @n |
| 59 |
|
| 60 |
report << debug(1) << "This is only debug inforamtion" << endr @n |
| 61 |
|
| 62 |
report << quit << plain << "Well, this storys gonna end soon ... " |
| 63 |
<< endr @n @n |
| 64 |
|
| 65 |
- plain : -4 |
| 66 |
- error : -3 |
| 67 |
- recoverable : -2 |
| 68 |
- warning : -1 |
| 69 |
- hint : 0 |
| 70 |
- debug(1) : 1 important debug information |
| 71 |
- debug(2) : 2 less important |
| 72 |
- debug(10) : 10 really not important |
| 73 |
- aborting : does an abort (MPI_Abort) |
| 74 |
- quit : does an exit (MPI_Finalize) |
| 75 |
- endr : end of report/MyStreamer output |
| 76 |
- allnodesserial : output synchronized node by node including the |
| 77 |
master |
| 78 |
- allslavesserial : output synchronized node by node, only slaves |
| 79 |
- allnodes : output unsynchronized from all nodes |
| 80 |
- donthint : suppress all hint output |
| 81 |
- dohint : enable hint output |
| 82 |
*/ |
| 83 |
class MyStreamer { |
| 84 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 85 |
// Constructors, destructors, assignment |
| 86 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 87 |
public: |
| 88 |
MyStreamer(std::ostream *); |
| 89 |
|
| 90 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 91 |
// New methods of class MyStreamer |
| 92 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 |
std::ostream *setStream(std::ostream *); |
| 94 |
void setAbort(bool); |
| 95 |
void setQuit(bool); |
| 96 |
void setAllNodes(bool); |
| 97 |
void setReportLevel(short level) {myReportLevel = level;} |
| 98 |
void setLevel(short level) {myLevel = level;} |
| 99 |
void setIAmMaster(bool master) {myIAmMaster = master;} |
| 100 |
|
| 101 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 102 |
// New methods of class MyStreamer |
| 103 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 104 |
void setf(std::ios::fmtflags flag); |
| 105 |
void setf(std::ios::fmtflags flag, std::ios::fmtflags mask); |
| 106 |
void precision(int prec); |
| 107 |
void width(int wide); |
| 108 |
void reset(); |
| 109 |
|
| 110 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 111 |
// New methods of class MyStreamer |
| 112 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 113 |
MyStreamer &operator<<(bool); |
| 114 |
MyStreamer &operator<<(char); |
| 115 |
MyStreamer &operator<<(unsigned char); |
| 116 |
MyStreamer &operator<<(signed char); |
| 117 |
MyStreamer &operator<<(char *); |
| 118 |
MyStreamer &operator<<(const char *); |
| 119 |
MyStreamer &operator<<(unsigned char *); |
| 120 |
MyStreamer &operator<<(signed char *); |
| 121 |
MyStreamer &operator<<(short); |
| 122 |
MyStreamer &operator<<(unsigned short); |
| 123 |
MyStreamer &operator<<(int); |
| 124 |
MyStreamer &operator<<(unsigned int); |
| 125 |
MyStreamer &operator<<(long long); |
| 126 |
MyStreamer &operator<<(unsigned long long); |
| 127 |
MyStreamer &operator<<(long); |
| 128 |
MyStreamer &operator<<(unsigned long); |
| 129 |
MyStreamer &operator<<(float); |
| 130 |
MyStreamer &operator<<(double); |
| 131 |
MyStreamer &operator<<(const std::string &); |
| 132 |
MyStreamer &operator<<(const std::ostream &); |
| 133 |
MyStreamer &operator<<(std::ostream *); |
| 134 |
MyStreamer &operator<<(std::ostream &(*f)(std::ostream &)); |
| 135 |
MyStreamer &operator<<(std::ios &(*f)(std::ios &)); |
| 136 |
MyStreamer &operator<<(MyStreamer &(*f)(MyStreamer &)); |
| 137 |
|
| 138 |
friend MyStreamer &allnodes(MyStreamer &stream); |
| 139 |
friend MyStreamer &plain(MyStreamer &stream); |
| 140 |
friend MyStreamer &hint(MyStreamer &stream); |
| 141 |
friend MyStreamer &quit(MyStreamer &stream); |
| 142 |
friend MyStreamer &aborting(MyStreamer &stream); |
| 143 |
friend MyStreamer &warning(MyStreamer &stream); |
| 144 |
friend MyStreamer &recoverable(MyStreamer &stream); |
| 145 |
friend MyStreamer &error(MyStreamer &stream); |
| 146 |
friend MyStreamer &allnodesserial(MyStreamer &stream); |
| 147 |
friend MyStreamer &allslavesserial(MyStreamer &stream); |
| 148 |
friend MyStreamer &endr(MyStreamer &stream); |
| 149 |
friend MyStreamer &dohint(MyStreamer &stream); |
| 150 |
friend MyStreamer &donthint(MyStreamer &stream); |
| 151 |
|
| 152 |
private: |
| 153 |
bool print() const { |
| 154 |
return mySilentHint < 1 && myLevel <= myReportLevel && |
| 155 |
(myAllNodes || myIAmMaster); |
| 156 |
} |
| 157 |
|
| 158 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 159 |
// My data members |
| 160 |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 161 |
private: |
| 162 |
std::ostream *myStream; |
| 163 |
std::ios::fmtflags myResetFlags; // Resest/default flags |
| 164 |
bool myAbort; |
| 165 |
bool myQuit; |
| 166 |
bool myAllNodes; |
| 167 |
bool myAllNodesSerial; |
| 168 |
bool myAllSlavesSerial; |
| 169 |
bool myIAmMaster; |
| 170 |
bool myDoHint; |
| 171 |
short mySilentHint; |
| 172 |
short myReportLevel; |
| 173 |
short myLevel; |
| 174 |
}; |
| 175 |
|
| 176 |
/// Plain output |
| 177 |
MyStreamer &plain(MyStreamer &stream); |
| 178 |
/// Error, abort |
| 179 |
MyStreamer &error(MyStreamer &stream); |
| 180 |
/// Recoverable error |
| 181 |
MyStreamer &recoverable(MyStreamer &stream); |
| 182 |
/// Warning |
| 183 |
MyStreamer &warning(MyStreamer &stream); |
| 184 |
/// Hint, can be suppressed by donthint/dohint |
| 185 |
MyStreamer &hint(MyStreamer &stream); |
| 186 |
|
| 187 |
MyStreamer &allnodes(MyStreamer &stream); |
| 188 |
MyStreamer &quit(MyStreamer &stream); |
| 189 |
MyStreamer &aborting(MyStreamer &stream); |
| 190 |
MyStreamer &allnodesserial(MyStreamer &stream); |
| 191 |
MyStreamer &allslavesserial(MyStreamer &stream); |
| 192 |
MyStreamer &endr(MyStreamer &stream); |
| 193 |
MyStreamer &dohint(MyStreamer &stream); |
| 194 |
MyStreamer &donthint(MyStreamer &stream); |
| 195 |
|
| 196 |
MyStreamer &operator<<(MyStreamer &stream, const reportlevel &rl); |
| 197 |
MyStreamer &operator<<(MyStreamer &stream, const debug &d); |
| 198 |
|
| 199 |
// Our global streamer |
| 200 |
extern MyStreamer report; |
| 201 |
//static MyStreamer report = &cerr; |
| 202 |
} |
| 203 |
} |
| 204 |
#endif /* REPORT_H */ |
| 205 |
|