CARMA C++
Program.h
1 #ifndef CARMA_UTIL_PROGRAM_H
2 #define CARMA_UTIL_PROGRAM_H
3 
21 #include <cstdlib>
22 #include <cstdio>
23 #include <string>
24 #include <map>
25 #include <memory>
26 
27 #include <sys/types.h>
28 
29 #include "carma/util/FacilityType.h"
30 
31 // Declaration of carma_main. See comments in Program.cc for more info.
32 int carma_main( int argc, char ** argv );
33 
34 namespace log4cpp {
35 
36 class Category;
37 
38 } // namespace log4cpp
39 
40 
41 namespace carma {
42 
43 namespace corba {
44 
45  class Server;
46  class Client;
47 
48 } // namespace carma::corba
49 
50 namespace util {
51 
52 class Orb;
53 class ProcessMonitorClient;
54 class Trace;
55 
56 
62 typedef struct {
64  const char * const key;
65 
69  const char * const val;
70 
75  const char * const type;
76 
80  const char * const usageValue;
81 
85  const char * const help;
86 } KeyTabEntry;
87 
88 
89 class Program;
90 
93 class ProgramBase {
94  friend int ::carma_main( int argc, char ** argv );
95 
96  public:
97 
102  bool parameterWasSpecified( const ::std::string & key );
103 
106  bool getBoolParameter( const ::std::string & key );
107 
110  double getDoubleParameter( const ::std::string & key );
111 
114  int getIntParameter( const ::std::string & key );
115 
118  ::std::string getStringParameter( const ::std::string & key );
119 
122  ::std::string getParameterRawValueString( const ::std::string & key );
123 
124 
127  int getDebugLevel( ) const;
128 
131  bool DebugLevel( int level = 0 ) const;
132 
133 
155  void setInstanceLogname( const ::std::string & logname );
156 
160  ::std::string getLogname( ) const;
161 
175  ::std::string getLogHostname( ) const;
176 
177 
179  bool haveImrHostname( ) const;
180 
185  ::std::string getImrHostname( ) const;
186 
187 
197  bool orbInit(Orb* orb);
198 
199  carma::corba::Server & getCorbaServer();
200  carma::corba::Client & getCorbaClient();
201 
202 
209  bool imrTerminationRequested( );
210 
211  // class static methods
212 
229  static log4cpp::Category & getLogger( );
230 
231  static log4cpp::Category * getLoggerIfAvailable( );
232 
233  static carma::util::Trace * getTraceObject( );
234 
235  static carma::util::Trace * getTraceObjectIfAvailable( );
236 
237 
246  static bool getUseDBMS( ) ;
247 
248 
252  static ::std::string getArg0( );
253 
254 
265  static int getExtraArgc( );
266 
277  static char ** getExtraArgv( );
278 
279 
292  static ::std::string getConfDir( );
293 
310  static ::std::string getConfFile( const ::std::string & filename );
311 
312 
324  static ::std::string getRootDir( );
325 
330  static ::std::string getExecutableDir( );
331 
335  static ::std::string getExecutable( );
336 
341  static pid_t getPid( );
342 
348  static ::std::string getHostname( bool shorten );
349 
355  static ::std::string getCwd( );
356 
357  int getTraceLevel( ) const;
358 
359  void adjustTraceLevel( int newTraceLevel );
360 
364  int getNiceLevel( ) const;
365 
369  bool getUseGMT() const;
370 
373  ::std::string getUsageString( ) const;
374 
375  protected:
376 
378  ProgramBase( );
379 
381  ~ProgramBase( );
382 
383  private:
384 
385  typedef enum {
386  // Please note that these states are in a particular order.
387 
388  BEING_CONSTRUCTED_STATE,
389  CONSTRUCTED_STATE,
390 
391  ADDING_KEY_DEFINITIONS_STATE,
392  KEY_DEFINITIONS_ADDED_STATE,
393 
394  PARSING_COMMAND_LINE_STATE,
395  COMMAND_LINE_PARSED_STATE,
396 
397  INITIALISING_SYSTEM_KEYWORD_MEMBERS_STATE,
398  SYSTEM_KEYWORD_MEMBERS_INITIALISED_STATE,
399 
400  LOGGER_AVAILABLE_STATE,
401  TRACE_OBJECT_AVAILABLE_STATE,
402 
403  FULLY_INITIALISED_STATE,
404 
405  // most of our time is spent in here between these two states
406 
407  BEING_DESTRUCTED_STATE,
408  DESTRUCTED_STATE
409  } StateType;
410 
411  typedef enum {
412  BOOL_PARAM_TYPE,
413  DOUBLE_PARAM_TYPE,
414  INT_PARAM_TYPE,
415  STRING_PARAM_TYPE
416  } ParamType;
417 
418  typedef enum {
419  NORMAL_PARAM_SPECIFY_TYPE,
420  MANDATORY_PARAM_SPECIFY_TYPE,
421  NO_DEFAULT_PARAM_SPECIFY_TYPE
422  } ParamSpecifyType;
423 
426  typedef struct {
427  bool system; // system keyword?
428  ::size_t declIndex; // index in decl order
429 
430  ::std::string defaultValueString; // default value string
431  ParamType type; // type
432  ::std::string usageValue; // usage value
433  ::std::string help; // help
434  ParamSpecifyType specifyType; // "normal", "mandatory", or
435  // "no default"
436 
437  ::std::string valueString; // value string
438  bool valueSpecified; // user specified on
439  // command line?
440  bool valueSpecifiedWasChecked;
441  bool valueWasRead; // value was read
442  } ParameterInfo;
443 
444  typedef ::std::map< ::std::string, ParameterInfo > ParameterInfoMap;
445 
446  void verifyStateIsInRange( StateType rangeBegin,
447  StateType rangeEnd ) const;
448 
449  void verifyStateIsInitializingSystemKeywordMembers( ) const;
450 
451  void verifyStateIsNormal( ) const;
452 
461  static int run( Program & program );
462 
468  int initializeCarma( );
469 
470 
475  void terminateCarma( );
476 
477  void show( );
478 
479  void addKey( const KeyTabEntry & kt,
480  bool system,
481  ::size_t declIndex );
482 
483  void docKeys( bool system, bool declOrder ) const;
484 
485  ::std::string getDescriptionString( ) const;
486 
487 
488  ::std::string getParameterValueString( const ::std::string & key,
489  bool system,
490  ParamType type,
491  bool checkType );
492 
493  bool parameterWasSpecified( const ::std::string & key,
494  bool system );
495 
496  bool getBoolParameter( const ::std::string & key,
497  bool system );
498 
499  double getDoubleParameter( const ::std::string & key,
500  bool system );
501 
502  int getIntParameter( const ::std::string & key,
503  bool system );
504 
505  ::std::string getStringParameter( const ::std::string & key,
506  bool system );
507 
508  ::std::string getParameterRawValueString( const ::std::string & key,
509  bool system );
510 
511  static void docKey( const ::std::string & name,
512  const ParameterInfo & parameterInfo );
513 
514 
530  void setLogger( );
531 
537  void setTrace( );
538 
544  void setCorba( );
545 
563  log4cpp::Category & getDefaultLogger( ) const;
564 
565 
566  static ParamType convertParameterType( const ::std::string & s );
567 
568  void advanceState( StateType newState );
569 
575  void renice( void );
576 
577  class Config;
578 
579  // class static data members
580  static const KeyTabEntry kSystemKeywords_[ ];
581 
582  static const char * const kUsage_; // one line usage
583  static const KeyTabEntry kKeywords_[ ]; // {key,val,type,help} tuples
584  // for program keys
585  static const char * const kVersion_; // one line version
586  static const char * const kDescription_; // multiple line description
587 
588  static const bool kHaveInitialLoggerInfo_;
589  static const char * const kInitialFacilityName_;
590  static const char * const kInitialLogname_;
591 
592  // instance data members
593  StateType state_;
594 
595  ::std::string progname_; // short program name without path
596 
597  ParameterInfoMap parameterInfoMap_;
598 
599  bool syslog_; // true or false
600  log4cpp::Category * logger_; // logger set up as specified by
601  // system key values
602  ::std::string logFileName_; // pathname for file log
603  ::std::string logHostName_; // logger is hosted on this machine
604  // default for logHostName is
605  // gethostname(3)
606  int corbaRrtt_;
607  bool imrHostnameWasSpecified_;// IMR info
608  ::std::string imrHostnameSpecified_;
609 
610  ::std::auto_ptr<carma::corba::Server> corbaServer_;
611  ::std::auto_ptr<carma::util::ProcessMonitorClient> processMonitorClient_;
612 
613  int debugLevel_; // debug level (0 or larger)
614  bool daemonize_; // program is a daemon ?
615 
616  int nice_; // Nice value for managing
617  // resource usage. Higher
618  // value is nicer, lower value
619  // means higher priority
620  // (less than zero requires
621  // root permission).
622  bool useGMT_; // Use GMT timezone for program
623  int traceLevel_; // trace level
624  // (>=0 ; 0 means no tracing)
625  bool traceVerbose_; // trace verbosity
626  // (true means maximum, false means
627  // log4cpp::SimpleLayout)
628  ::std::string traceFile_; // file (including stdout/syslog)
629  // where trace output will go
630  carma::util::Trace * traceObject_; // trace object
631 
632  const carma::util::facilityType facility_; // Default syslog facility
633 
634  ::std::string logname_; // logname for Logger Category.
635  bool instanceLognameSet_;
636 
637  bool useDBMS_;
638 }; // class carma::util::ProgramBase
639 
640 
645 class Program : public ProgramBase {
646  friend class ProgramBase;
647  friend int ::carma_main( int argc, char ** argv );
648 
649  public:
650 
662  static Program & getProgram( );
663 
664  private:
665 
666  Program( );
667 
668  ~Program( );
669 
675  int main( );
676 
677 }; // class carma::util::Program
678 
679 
680 } // namespace carma::util
681 } // namespace carma
682 
683 #endif
bool DebugLevel(int level=0) const
has a debug level exceeded the set value?
static int getExtraArgc()
Obtain the length of the array of command line arguments with all CARMA arguments removed...
static bool getUseDBMS()
Returns value of system keyword &quot;useDBMS&quot;.
Base class for managing resources and interfaces common across all carma programs.
Definition: Program.h:93
static pid_t getPid()
Get pid of this program.
bool orbInit(Orb *orb)
Set ORB parameters.
::std::string getExecutable()
Get absolute full path of executable.
const char *const usageValue
Usage value shown on the usage line.
Definition: Program.h:80
const char *const type
Name of the type of the value.
Definition: Program.h:75
::std::string getRootDir()
Get the root build or install directory - assumes standard carma tree.
static Program & getProgram()
Class static method to get the process-wide singleton instance of Program.
::std::string getStringParameter(const ::std::string &key)
get value of a string parameter
bool haveImrHostname() const
Was imr hostname specified on the commmand line?
const char *const val
default value for keyword
Definition: Program.h:69
ProgramBase()
Default constructor.
static char ** getExtraArgv()
Obtain the array of command line arguments with all CARMA arguments removed.
int getIntParameter(const ::std::string &key)
get value of an int parameter
::std::string getParameterRawValueString(const ::std::string &key)
get the raw value string of a parameter
::std::string getHostname(bool shorten)
Get hostname of the machine this program is running on.
Class to encapsulate CORBA client functionality in CARMA.
Definition: Client.h:26
::std::string getArg0()
Get the first argument passed to main()
~ProgramBase()
Destructor.
::std::string getImrHostname() const
Retrieve the imr hostname.
double getDoubleParameter(const ::std::string &key)
get value of a double parameter
const char *const help
Help string.
Definition: Program.h:85
static log4cpp::Category & getLogger()
Returns a reference to the process-wide default logger.
int getDebugLevel() const
debug level of the program environment
bool parameterWasSpecified(const ::std::string &key)
Query whether the value of the parameter was specified on the command line.
::std::string getLogname() const
get the log name of for the logger of this Progam
Convenience structure to hold the key=value/type/usage/help-string information.
Definition: Program.h:62
facilityType
A type for syslog facilities.
Definition: FacilityType.h:15
Class for creating, managing and serving requests to CORBA servants.
Definition: Server.h:22
const char *const key
keyword (name used on commad line)
Definition: Program.h:64
::std::string getUsageString() const
Get the usage string for this program.
bool getBoolParameter(const ::std::string &key)
get value of a bool parameter
::std::string getConfDir()
Get the configuration directory - assumes standard carma tree.
::std::string getCwd()
Get the current working directory.
::std::string getConfFile(const ::std::string &filename)
Get the absolute path of a configuration file.
::std::string getExecutableDir()
Get the directory where this program binary lives.
void setInstanceLogname(const ::std::string &logname)
set the logger logname for this instance of a Program
::std::string getLogHostname() const
Gets hostname of machine that stores the log file.
bool imrTerminationRequested()
Check for an IMR instructed termination request.
Class for managing resources and interfaces common across all carma programs.
Definition: Program.h:645
The Trace class provides an efficient means for managing debug statements throughout CARMA code...
Definition: Trace.h:97