source: CMT/HEAD/source/cmt_log.h @ 467

Last change on this file since 467 was 467, checked in by rybkin, 16 years ago

See C.L. 368

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1//-----------------------------------------------------------
2// Copyright Christian Arnault LAL-Orsay CNRS
3// arnault@lal.in2p3.fr
4// See the complete license in cmt_license.txt "http://www.cecill.info".
5//-----------------------------------------------------------
6
7#ifndef __cmt_log_h__
8#define __cmt_log_h__
9
10#include "cmt_string.h"
11
12class CmtLogEnd
13{
14public:
15};
16
17class CmtLogDummy
18{
19public:
20};
21
22class CmtLog
23{
24public:
25  CmtLog ();
26
27  static CmtLogEnd& end ();
28  CmtLog& operator << (const cmt_string& s);
29  CmtLog& operator << (const char* c);
30  CmtLog& operator << (int i);
31  CmtLog& operator << (double d);
32  CmtLog& operator << (void* p);
33  CmtLog& operator << (CmtLogEnd& end);
34  CmtLog& operator << (CmtLogDummy& dummy);
35
36private:
37  bool check ();
38};
39
40#define Log static CmtLog log_instance
41#define log_endl CmtLog::end ()
42#define log      if (Cmt::get_debug ()) log_instance << CmtMessage::prefix () + " (" << __FILE__ << ":" << __LINE__ << ") "
43//#define log      if (Cmt::get_debug ()) log_instance << "#CMT> (" << __FILE__ << "-" << __LINE__ << ") "
44#define log_cont if (Cmt::get_debug ()) log_instance
45
46
47enum CmtMsgLevel
48  {
49    Nil = 0,
50    Debug,
51    Verbose,
52    Info,
53    Warning,
54    Error,
55    Fatal
56  };
57
58class CmtMessage
59{
60public:
61  static cmt_string& prefix ();
62  static void set_prefix (const cmt_string& prefix);
63  static CmtMsgLevel& level ();
64  static void set_level (const CmtMsgLevel& level);
65  static bool active (const CmtMsgLevel& level);
66  static void info (const cmt_string& message);
67  static void warning (const cmt_string& message);
68  static void error (const cmt_string& message);
69  static void fatal (const cmt_string& message);
70
71private:
72  CmtMessage ();
73  static CmtMessage& instance ();
74  cmt_string m_prefix;
75  CmtMsgLevel m_level;
76};
77
78/**----------------------------------------------------------
79   The CmtMessage default constructor
80   Here are primarily constructed all default definitions
81*/
82inline CmtMessage::CmtMessage ()
83  : m_prefix ("#CMT--->"), m_level (Info)
84{ }
85
86/**----------------------------------------------------------
87   The CmtMessage singleton
88*/
89inline CmtMessage& CmtMessage::instance ()
90{
91  static CmtMessage me;
92  return me;
93}
94
95inline cmt_string& CmtMessage::prefix ()
96{
97  return instance ().m_prefix;
98}
99
100inline void CmtMessage::set_prefix (const cmt_string& prefix)
101{
102  instance ().m_prefix = prefix;
103}
104
105inline CmtMsgLevel& CmtMessage::level ()
106{
107  return instance ().m_level;
108}
109
110inline void CmtMessage::set_level (const CmtMsgLevel& level)
111{
112  instance ().m_level = level;
113}
114
115inline bool CmtMessage::active (const CmtMsgLevel& lvl)
116{
117  return level () <= lvl;
118}
119
120inline void CmtMessage::info (const cmt_string& message)
121{
122  if (active (Info))
123    cerr << prefix () << " Info: " << message << endl;
124}
125
126inline void CmtMessage::warning (const cmt_string& message)
127{
128  if (active (Warning))
129    cerr << prefix () << " Warning: " << message << endl;
130}
131
132inline void CmtMessage::error (const cmt_string& message)
133{
134  if (active (Error))
135    cerr << prefix () << " Error: " << message << endl;
136}
137
138inline void CmtMessage::fatal (const cmt_string& message)
139{
140  if (active (Fatal))
141    cerr << prefix () << " Fatal: " << message << endl;
142}
143#endif
Note: See TracBrowser for help on using the repository browser.