File indexing completed on 2025-04-19 09:09:52
0001 #ifndef ATOOLS_Org_Message_H
0002 #define ATOOLS_Org_Message_H
0003
0004
0005 #include <string>
0006 #include <iostream>
0007 #include <fstream>
0008 #include <sstream>
0009 #include <map>
0010 #include <set>
0011 #include "ATOOLS/Org/STL_Tools.H"
0012 #include "ATOOLS/Org/CXXFLAGS.H"
0013
0014 namespace ATOOLS {
0015
0016 struct bm {
0017
0018 enum code {
0019 none = 0,
0020 back = 1,
0021 cr = 2,
0022 bell = 4
0023 };
0024
0025 };
0026
0027 struct om {
0028
0029 enum code {
0030 none = 0,
0031 reset = 1,
0032 bold = 2,
0033 blink = 4,
0034 underln = 8,
0035 blackbg = 16,
0036 red = 32,
0037 green = 64,
0038 blue = 128,
0039 brown = 256,
0040 violet = 512,
0041 lblue = 1024,
0042 grey = 2048,
0043 redbg = 4096,
0044 greenbg = 8192,
0045 bluebg = 16384,
0046 brownbg = 32768,
0047 violetbg = 65536,
0048 lbluebg = 131072,
0049 greybg = 262144
0050 };
0051
0052 };
0053
0054 struct mm {
0055
0056 enum code {
0057 none = 0,
0058 up = 1,
0059 down = 2,
0060 left = 4,
0061 right = 8
0062 };
0063
0064 int m_num, m_code;
0065
0066
0067 inline mm(const int num,const code type):
0068 m_num(num), m_code(type) {}
0069
0070 };
0071
0072 struct tm {
0073
0074 enum code {
0075 none = 0,
0076 curon = 1,
0077 curoff = 2
0078 };
0079
0080 };
0081
0082 struct fm {
0083
0084 enum code {
0085 upperleft = 0,
0086 upperright,
0087 horizontal,
0088 vertical,
0089 lowerleft,
0090 lowerright,
0091 centerleft,
0092 centerright
0093 };
0094
0095 };
0096
0097 std::ostream &operator<<(std::ostream &str,const bm::code modifier);
0098 std::ostream &operator<<(std::ostream &str,const om::code modifier);
0099
0100 std::ostream &operator<<(std::ostream &str,const mm modifier);
0101
0102 std::ostream &operator<<(std::ostream &str,const tm::code modifier);
0103
0104 std::ostream &operator<<(std::ostream &str,const fm::code modifier);
0105
0106 struct Frame_Header {
0107 Frame_Header(int width = 80) : m_width {width} {}
0108 int m_width;
0109 };
0110
0111 struct Frame_Footer {
0112 Frame_Footer(int width = 80) : m_width {width} {}
0113 int m_width;
0114 };
0115
0116 struct Frame_Separator {
0117 Frame_Separator(int width = 80) : m_width {width} {}
0118 int m_width;
0119 };
0120
0121 struct Frame_Line {
0122 Frame_Line(const std::string &s, int width = 80)
0123 : m_textline{s}, m_width{width} {}
0124 std::string m_textline;
0125 int m_width;
0126 };
0127
0128 std::ostream &operator<<(std::ostream &str, Frame_Header f);
0129 std::ostream &operator<<(std::ostream &str, Frame_Footer f);
0130 std::ostream &operator<<(std::ostream &str, Frame_Separator f);
0131 std::ostream &operator<<(std::ostream &str, const Frame_Line &f);
0132
0133 class Indentation;
0134
0135 class indentbuf : public std::streambuf {
0136 public:
0137 indentbuf(std::streambuf* basebuf);
0138 ~indentbuf();
0139 void Indent(size_t i=2);
0140 void DeIndent(size_t i=2);
0141 inline std::streambuf* BaseBuf() { return m_basebuf; }
0142 inline void SetBaseBuf(std::streambuf *buf) { m_basebuf=buf; }
0143
0144 protected:
0145 std::streambuf* m_basebuf;
0146 size_t m_indent;
0147 bool at_start;
0148
0149 int_type overflow(int_type ch);
0150 inline int sync() { return m_basebuf->pubsync(); }
0151 };
0152
0153 class Message {
0154 private:
0155
0156 std::ofstream m_devnull;
0157 std::ofstream *p_log;
0158 std::ostream m_output, m_error;
0159 indentbuf m_buf;
0160
0161 int m_level;
0162 std::string m_logfile;
0163
0164 std::set<std::string,String_Sort> m_contextevents;
0165 std::set<std::string,String_Sort> m_contextinfo;
0166 std::set<std::string,String_Sort> m_contexttracking;
0167 std::set<std::string,String_Sort> m_contextdebugging;
0168 std::set<std::string,String_Sort> m_contextiodebugging;
0169
0170 std::map<std::string,unsigned int> m_log_stats;
0171
0172 int m_modifiable, m_mpimode;
0173 int m_limit {1};
0174
0175 public:
0176
0177
0178 Message();
0179
0180
0181 ~Message();
0182
0183
0184 void Init();
0185 void SetStandard();
0186 void SetLimit(int limit) { m_limit = limit; }
0187 void PrintRates() const;
0188
0189 std::ostream &Out();
0190 std::ostream &Error();
0191 std::ostream &Events();
0192 std::ostream &Info();
0193 std::ostream &Tracking();
0194 std::ostream &Debugging();
0195 std::ostream &IODebugging();
0196
0197 std::string ExtractMethodName(std::string cmethod) const;
0198
0199 bool CheckRate(const std::string& cmethod);
0200
0201
0202 inline void SetLevel(const int level) { m_level = level; }
0203 inline void SetModifiable(const bool mod) { m_modifiable = mod; }
0204 inline void SetPrecision(const int precision) {
0205 m_output.precision(precision);
0206 }
0207
0208 inline int Level() const { return m_level; }
0209 inline bool Modifiable() const { return m_modifiable; }
0210 inline size_t Precision() const {
0211 return m_output.precision();
0212 }
0213
0214
0215 inline void Indent(size_t col) { m_buf.Indent(col); }
0216 inline void DeIndent(size_t col) { m_buf.DeIndent(col); }
0217
0218 inline void SetOutStream(std::ostream& output) {
0219 m_output.rdbuf(output.rdbuf());
0220 }
0221 inline void SetOutStream(ATOOLS::indentbuf& buf) {
0222 m_output.rdbuf(&buf);
0223 }
0224 inline void SetErrStream(std::ostream& error) {
0225 m_error.rdbuf(error.rdbuf());
0226 }
0227 inline void SetErrStream(ATOOLS::indentbuf& buf) {
0228 m_error.rdbuf(&buf);
0229 }
0230
0231 inline const std::string& LogFile() const { return m_logfile; }
0232
0233 inline bool LevelIsError() const { return (m_level >= 0); }
0234 inline bool LevelIsEvents() const { return (m_level & 1); }
0235 inline bool LevelIsInfo() const { return (m_level & 2); }
0236 inline bool LevelIsTracking() const { return (m_level & 4); }
0237 inline bool LevelIsDebugging() const { return (m_level & 8); }
0238 inline bool LevelIsIODebugging() const { return (m_level & 32); }
0239
0240 bool LevelIsEvents(const std::string& context) const;
0241 bool LevelIsInfo(const std::string& context) const;
0242 bool LevelIsTracking(const std::string& context) const;
0243 bool LevelIsDebugging(const std::string& context) const;
0244 bool LevelIsIODebugging(const std::string& context) const;
0245
0246 inline const std::set<std::string,String_Sort>& ContextEvents() {
0247 return m_contextevents;
0248 }
0249 inline const std::set<std::string,String_Sort>& ContextInfo() {
0250 return m_contextinfo;
0251 }
0252 inline const std::set<std::string,String_Sort>& ContextTracking() {
0253 return m_contexttracking;
0254 }
0255 inline const std::set<std::string,String_Sort>& ContextDebugging() {
0256 return m_contextdebugging;
0257 }
0258 inline const std::set<std::string,String_Sort>& ContextIODebugging() {
0259 return m_contextiodebugging;
0260 }
0261 };
0262
0263 extern Message *msg;
0264
0265 class Indentation {
0266 private:
0267
0268 size_t m_col;
0269 int m_mode;
0270
0271 public:
0272
0273
0274 inline Indentation(const size_t col=2) : m_col(col), m_mode(0) {
0275 }
0276
0277 inline void Activate(int mode) {
0278 m_mode=mode;
0279 if (m_mode & 2) msg->Out()<<om::red<<"{"<<om::reset<<std::endl;
0280 if (m_mode & 1) msg->Indent(m_col);
0281 }
0282
0283
0284
0285 inline ~Indentation() {
0286 if (m_mode & 1) msg->DeIndent(m_col);
0287 if (m_mode & 2) msg->Out()<<om::red<<"}"<<om::reset<<std::endl;
0288 }
0289
0290 };
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313 }
0314
0315 #define msg_CheckRate() \
0316 (ATOOLS::msg->CheckRate(__func__))
0317 #define msg_LevelIsEvents() \
0318 (ATOOLS::msg->LevelIsEvents() || \
0319 (!ATOOLS::msg->ContextEvents().empty() && ATOOLS::msg->LevelIsEvents(__PRETTY_FUNCTION__)))
0320 #define msg_LevelIsInfo() \
0321 (ATOOLS::msg->LevelIsInfo() || \
0322 (!ATOOLS::msg->ContextInfo().empty() && ATOOLS::msg->LevelIsInfo(__PRETTY_FUNCTION__)))
0323 #define msg_LevelIsTracking() \
0324 (ATOOLS::msg->LevelIsTracking() || \
0325 (!ATOOLS::msg->ContextTracking().empty() && ATOOLS::msg->LevelIsTracking(__PRETTY_FUNCTION__)))
0326 #define msg_LevelIsDebugging() \
0327 (ATOOLS::msg->LevelIsDebugging() || \
0328 (!ATOOLS::msg->ContextDebugging().empty() && ATOOLS::msg->LevelIsDebugging(__PRETTY_FUNCTION__)))
0329 #define msg_LevelIsIODebugging() \
0330 (ATOOLS::msg->LevelIsIODebugging() || \
0331 (!ATOOLS::msg->ContextIODebugging().empty() && ATOOLS::msg->LevelIsIODebugging(__PRETTY_FUNCTION__)))
0332
0333 #define msg_Error() if (msg_CheckRate()) ATOOLS::msg->Error()
0334 #define msg_Out() ATOOLS::msg->Out()
0335 #define msg_Events() if (msg_LevelIsEvents()) ATOOLS::msg->Out()
0336 #define msg_Info() if (msg_LevelIsInfo()) ATOOLS::msg->Out()
0337 #define msg_Tracking() if (msg_LevelIsTracking()) ATOOLS::msg->Out()
0338 #define msg_Debugging() if (msg_LevelIsDebugging()) ATOOLS::msg->Out()
0339 #define msg_IODebugging() if (msg_LevelIsIODebugging()) ATOOLS::msg->Out()
0340
0341 #define msg_Indent() ATOOLS::Indentation indent; indent.Activate(1)
0342 #define msg_Indentation(COL) ATOOLS::Indentation indent(COL);indent.Activate(1);
0343
0344 #define bm_back ATOOLS::bm::back
0345 #define bm_cr ATOOLS::bm::cr
0346 #define bm_bell ATOOLS::bm::bell
0347
0348 #define mm_up(LINES) ATOOLS::mm(LINES,mm::up)
0349 #define mm_down(LINES) ATOOLS::mm(LINES,mm::down)
0350 #define mm_left(COLUMNS) ATOOLS::mm(COLUMNS,mm::left)
0351 #define mm_right(COLUMNS) ATOOLS::mm(COLUMNS,mm::right)
0352
0353 #define METHOD \
0354 ATOOLS::msg->ExtractMethodName(__PRETTY_FUNCTION__)
0355
0356 #define PRINT_METHOD \
0357 msg_Out()<<ATOOLS::om::blue<<METHOD<<ATOOLS::om::reset<<std::endl
0358
0359 #define PRINT_INFO(INFO) \
0360 msg_Out()<<ATOOLS::om::blue<<METHOD<<ATOOLS::om::reset \
0361 <<":("<<ATOOLS::om::green<<"\""<<INFO<<"\"" \
0362 <<ATOOLS::om::reset<<")"<<std::endl
0363
0364 #define VAR(ARG) #ARG<<"="<<ARG
0365
0366 #define PRINT_VAR(VAR) \
0367 msg_Out()<<ATOOLS::om::blue<<__LINE__<<":" \
0368 <<#VAR<<ATOOLS::om::reset<<"="<<ATOOLS::om::green \
0369 <<VAR<<ATOOLS::om::reset<<std::endl
0370
0371 #define PRINT_FUNC(a1) \
0372 msg_Out()<<ATOOLS::om::red<<METHOD<<"("<<ATOOLS::om::green \
0373 <<a1<<ATOOLS::om::red<<")"<<ATOOLS::om::reset; \
0374 ATOOLS::Indentation indent; \
0375 indent.Activate(3)
0376
0377 #define DEBUG_INFO(INFO) \
0378 msg_Debugging()<<ATOOLS::om::blue<<METHOD<<":"<<__LINE__ \
0379 <<"("<<ATOOLS::om::green<<"\""<<INFO<<"\"" \
0380 <<ATOOLS::om::reset<<")"<<std::endl
0381
0382 #define DEBUG_VAR(VAR) \
0383 msg_Debugging()<<ATOOLS::om::blue<<__LINE__<<":" \
0384 <<#VAR<<ATOOLS::om::reset<<"="<<ATOOLS::om::green \
0385 <<VAR<<ATOOLS::om::reset<<std::endl
0386
0387 #define DEBUG_FUNC(a1) \
0388 msg_Debugging()<<ATOOLS::om::red<<METHOD<<"("<<ATOOLS::om::green \
0389 <<a1<<ATOOLS::om::red<<") "<<ATOOLS::om::reset; \
0390 ATOOLS::Indentation indent; \
0391 if (msg_LevelIsDebugging()) indent.Activate(3)
0392
0393 #endif
0394