ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHLog.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHLog.h
1 #ifndef PHOOL_PHLOG_H
2 #define PHOOL_PHLOG_H
3 
4 #include <log4cpp/Appender.hh>
5 #include <log4cpp/Category.hh>
6 #include <log4cpp/FileAppender.hh>
7 #include <log4cpp/OstreamAppender.hh>
8 #include <log4cpp/PatternLayout.hh>
9 #include <log4cpp/Priority.hh>
10 #include <log4cpp/PropertyConfigurator.hh>
11 
12 #include <pwd.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 
16 class PHLog
17 {
18  public:
19  static void Init()
20  {
21  const std::string initFileName = "phlog.conf";
22  const char* env_file = std::getenv("PHLOGCONF");
23  if (_file_exists(initFileName))
24  {
25  log4cpp::PropertyConfigurator::configure(initFileName);
26  }
27  else if (_file_exists(std::string(_get_homedir()) + "/" + initFileName))
28  {
29  log4cpp::PropertyConfigurator::configure(std::string(_get_homedir()) + "/" + initFileName);
30  }
31  else if (env_file && _file_exists(env_file))
32  {
33  log4cpp::PropertyConfigurator::configure(env_file);
34  }
35  else
36  {
37  log4cpp::PatternLayout* layout1 = new log4cpp::PatternLayout();
38  layout1->setConversionPattern("%d [%p] %c: %m%n");
39  log4cpp::Appender* appender1 = new log4cpp::OstreamAppender("console", &std::cout);
40  appender1->setLayout(layout1);
41  log4cpp::Category& root = log4cpp::Category::getRoot();
42  root.setPriority(log4cpp::Priority::FATAL);
43  root.addAppender(appender1);
44  }
45  };
46 
47  static void Deinit()
48  {
49  log4cpp::Category::shutdown();
50  };
51 
52  static log4cpp::Category& get(const std::string& category = "")
53  {
54  static bool phlog_is_initialized = false;
55  if (!phlog_is_initialized)
56  {
57  Init();
58  phlog_is_initialized = true;
59  }
60  return log4cpp::Category::getInstance(category);
61  }
62 
63  private:
64  static const char* _get_homedir()
65  {
66  struct passwd* pw = getpwuid(getuid());
67  return pw->pw_dir;
68  };
69 
70  static inline bool _file_exists(const std::string& filename)
71  {
72  struct stat buffer;
73  return (stat(filename.c_str(), &buffer) == 0);
74  };
75 };
76 
77 // conditional logging
78 
79 #define LOG_DEBUG_IF(category, cond) \
80  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::DEBUG) && (cond)) \
81  PHLog::get(category) << log4cpp::Priority::DEBUG
82 
83 #define LOG_INFO_IF(category, cond) \
84  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::INFO) && (cond)) \
85  PHLog::get(category) << log4cpp::Priority::INFO
86 
87 #define LOG_NOTICE_IF(category, cond) \
88  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::NOTICE) && (cond)) \
89  PHLog::get(category) << log4cpp::Priority::NOTICE
90 
91 #define LOG_WARN_IF(category, cond) \
92  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::WARN) && (cond)) \
93  PHLog::get(category) << log4cpp::Priority::WARN
94 
95 #define LOG_ERROR_IF(category, cond) \
96  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::ERROR) && (cond)) \
97  PHLog::get(category) << log4cpp::Priority::ERROR
98 
99 #define LOG_CRIT_IF(category, cond) \
100  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::CRIT) && (cond)) \
101  PHLog::get(category) << log4cpp::Priority::CRIT
102 
103 #define LOG_ALERT_IF(category, cond) \
104  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::ALERT) && (cond)) \
105  PHLog::get(category) << log4cpp::Priority::ALERT
106 
107 #define LOG_FATAL_IF(category, cond) \
108  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::FATAL) && (cond)) \
109  PHLog::get(category) << log4cpp::Priority::FATAL
110 
111 // non-conditional logging
112 
113 #define LOG_DEBUG(category) \
114  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::DEBUG)) \
115  PHLog::get(category) << log4cpp::Priority::DEBUG
116 
117 #define LOG_INFO(category) \
118  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::INFO)) \
119  PHLog::get(category) << log4cpp::Priority::INFO
120 
121 #define LOG_NOTICE(category) \
122  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::NOTICE)) \
123  PHLog::get(category) << log4cpp::Priority::NOTICE
124 
125 #define LOG_WARN(category) \
126  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::WARN)) \
127  PHLog::get(category) << log4cpp::Priority::WARN
128 
129 #define LOG_ERROR(category) \
130  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::ERROR)) \
131  PHLog::get(category) << log4cpp::Priority::ERROR
132 
133 #define LOG_CRIT(category) \
134  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::CRIT)) \
135  PHLog::get(category) << log4cpp::Priority::CRIT
136 
137 #define LOG_ALERT(category) \
138  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::ALERT)) \
139  PHLog::get(category) << log4cpp::Priority::ALERT
140 
141 #define LOG_FATAL(category) \
142  if (PHLog::get(category).isPriorityEnabled(log4cpp::Priority::FATAL)) \
143  PHLog::get(category) << log4cpp::Priority::FATAL
144 
145 #endif // PHOOL_PHLOG_H