CARMA C++
ConfigChecker.h
Go to the documentation of this file.
1 #ifndef CARMA_UTIL_CONFIGCHECKER_H
2 #define CARMA_UTIL_CONFIGCHECKER_H
3 
4 #include <iostream>
5 #include <iomanip>
6 #include <map>
7 #include <string>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <pthread.h>
12 
13 #include "carma/util/Time.h"
14 #include "carma/util/IoLock.h"
15 #include "carma/util/Program.h"
16 #include "carma/util/PthreadMutex.h"
17 #include "carma/util/PthreadRWLock.h"
18 
19 #include <log4cpp/Category.hh> // carma tools include
20 #include <log4cpp/Priority.hh> // carma tools include
21 
22 
23 #ifdef RH_DEBUG
24 #define DPRINT(objptr, classname, statement) \
25 { if(objptr->isDebug("All") || objptr->isDebug(classname)) {\
26  const carma::util::IoLock::ScopedCerrLock cerrLock; \
27  if (objptr->printTime()) {\
28  std::cerr << carma::util::Time::getTimeString(2) << ": "; \
29  }\
30  if (objptr->prettyFunction()) {\
31  std::cerr << "In " \
32  << __PRETTY_FUNCTION__ << ": "; \
33  } \
34  std::cerr << statement << std::endl; \
35 }}
36 #else
37 #define DPRINT(objptr, classname, statement) {}
38 #endif
39 
40 #define LOGCMD(objptr, classname, statement) \
41 {if(objptr->isLogCmd("All") || objptr->isLogCmd(classname)) {\
42  const carma::util::IoLock::ScopedCerrLock cerrLock; \
43  log4cpp::Category& log = objptr->getLogger(); \
44  std::ostringstream os; \
45  if (objptr->prettyFunction()) {\
46  os << "In " << __PRETTY_FUNCTION__ << ": "; \
47  } \
48  os << statement; \
49  log << log4cpp::Priority::INFO << os.str(); \
50 }}
51 
52 #define LOGEXCEPTION(objptr, classname, statement) \
53 { \
54  const carma::util::IoLock::ScopedCerrLock cerrLock; \
55  log4cpp::Category& log = objptr->getLogger(); \
56  std::ostringstream os; \
57  os << "In " << __PRETTY_FUNCTION__ << ": "; \
58  os << statement; \
59  log << log4cpp::Priority::ERROR << os.str(); \
60 }
61 
62 #define LOGPROBLEM(objptr, classname, statement) \
63 { \
64  const carma::util::IoLock::ScopedCerrLock cerrLock; \
65  log4cpp::Category& log = objptr->getLogger(); \
66  std::ostringstream os; \
67  os << "In " << __PRETTY_FUNCTION__ << ": "; \
68  os << statement; \
69  log << log4cpp::Priority::WARN << os.str(); \
70 }
71 
72 #define LOGINFO(objptr, classname, statement) \
73 { \
74  const carma::util::IoLock::ScopedCerrLock cerrLock; \
75  log4cpp::Category& log = objptr->getLogger(); \
76  std::ostringstream os; \
77  os << "In " << __PRETTY_FUNCTION__ << ": "; \
78  os << statement; \
79  log << log4cpp::Priority::INFO << os.str(); \
80 }
81 
87 namespace carma {
88  namespace util {
89 
107  public:
111  explicit ConfigChecker( const std::string & filename );
112 
116  virtual ~ConfigChecker();
117 
121  void start();
122 
127  std::string getValue( const ::std::string & key ) const;
128 
129  bool valueIsEmpty( const ::std::string & key ) const;
130 
131  bool valueIsOneString( const ::std::string & key ) const;
132 
133  bool valueIsZeroString( const ::std::string & key ) const;
134 
138  log4cpp::Category & getLogger( ) const;
139 
140  protected:
141  bool getConfigFileNotFound( ) const;
142 
143  private:
144  static void updateThreadEntrypoint( ConfigChecker & configChecker );
145 
146  /*
147  * Return the time in seconds between file reads.
148  * Controlled with 'delayBetweenFileReads' variable in configuration
149  * file.
150  */
151  int getDelay( ) const;
152 
153  /*
154  * Return the configuration filename to use. Default will be the
155  * name used in the constructor.
156  */
157  ::std::string getFilename( ) const;
158 
159  protected:
160  const ::std::string kOneString_;
161  const ::std::string kZeroString_;
162 
163  private:
164  const int delayBetweenFileReads_;
165  const ::std::string fileName_;
166 
167  mutable PthreadRWLock guard_;
168  bool configFileNotFound_;
169  ::std::map< ::std::string, ::std::string > pairs_;
170  bool haveFirstReadCtime_;
171  time_t firstReadCtime_;
172  log4cpp::Category & log_;
173 
174  mutable PthreadMutex updateThreadGuard_;
175  bool updateThreadStarted_;
176  ::pthread_t updateThread_;
177  }; // End class ConfigChecker
178 
179  } // End namespace util
180 } // End namespace carma
181 
182 
183 
184 #endif // End #ifndef CARMA_UTIL_CONFIGCHECKER_H
Common time functions.
A simple wrapper class that makes use of ::pthread_rwlock_t easier in a C++ world.
Definition: PthreadRWLock.h:46
ConfigChecker(const std::string &filename)
Constructor.
void start()
start reading the configuration file in a separate thread
std::string getValue(const ::std::string &key) const
return the value associated with key.
Abstract Base class for managing configuration files.
log4cpp::Category & getLogger() const
Get a logger.
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...
Definition: PthreadMutex.h:41
virtual ~ConfigChecker()
Destructor.