The VisAO Camera
VisAOApp_standalone.h
Go to the documentation of this file.
1 /************************************************************
2 * VisAOApp_standalone.h
3 *
4 * Author: Jared R. Males (jrmales@email.arizona.edu)
5 *
6 * Declarations for the standalone VisAO application.
7 *
8 * Developed as part of the Magellan Adaptive Optics system.
9 ************************************************************/
10 
11 /** \file VisAOApp_standalone.h
12  * \author Jared R. Males
13  * \brief Declarations for the standalone VisAO application.
14  *
15 */
16 
17 #ifndef __VisAOApp_standalone_h__
18 #define __VisAOApp_standalone_h__
19 
20 //Adopt includes:
21 #include "Logger.h"
22 #include "stdconfig.h"
23 
24 #include <fstream>
25 
26 #include "VisAOApp_base.h"
27 
28 
29 #define SIG_MAINTHREAD (SIGRTMIN+6)
30 
31 #ifndef ERROR_REPORT
32 #define ERROR_REPORT(er) if(global_error_report) (*global_error_report)(er,__FILE__,__LINE__);
33 #endif
34 
35 #ifndef LOG_INFO
36 #define LOG_INFO(li) if(global_log_info) (*global_log_info)(li);
37 #endif
38 
39 
40 
41 namespace VisAO
42 {
43 /// The standalone VisAO application, does not interface with the AO Supervisor.
44 /** This class reproduces the configuration and logging capabilities of the Arcetri::AOApp, but does
45  * not connect to the AO Supervisor.
46  *
47  * Standard config options, none of which are required, are listed below. Default values are given between [] and units, where applicable, are given between ().
48  * - <b>LogLevel</b> <tt>string</tt> [INF] - the logger verbosity level: VTR, TRA, DEB, INF, WAR, ERR, FAT.
49  * - <b>RT_priority</b> <tt>int</tt> [0] - The real time priority, should only be changed if you are an
50  * expert user. Only works if the executable is setuid.
51  * - <b>wait_to</b> <tt>double</tt> [0.2] (seconds) - The timeout for fifo reads.
52  * - <b>com_path</b> <tt>string</tt> [fifos] - the directory for the command fifos, relative to VISAO_ROOT
53  * - <b>profile_path</b> <tt>string</tt> [profile] - the directory for profiler output, relative to VISAO_ROOT
54  * - <b>use_profiler</b> <tt>int</tt> [0] - flag specifying whether or no the profiler is used.
55  * - <b>pause_time</b> <tt>double</tt> [1.0] - the time between statusboard updates and other housekeeping in
56  * the main loop.
57  * - <b>init_path</b> <tt>string</tt> [init] - the directory to search for an initialization file, relative to
58  * VISAO_ROOT
59  * - <b>data_save_path</b> <tt>string</tt> [data/syslogs] - the directory relative to VISAO_ROOT where the process data log is written
60  * - <b>data_file_prefix</b> <tt>string</tt> [full app name] - the prefix for data logs
61  * - <b>data_log_time_length</b> <tt>double</tt> [120.0] - the length of time, in seconds, to allow a single data log file to remain open.
62  *
63  * A VisAOApp accepts at least the following command line options:
64  * - <b>-i</b> identity, used for config lookup etc. This is often not required, but it is good practice to
65  * use it.
66  * - <b>-f</b> configuration (if not specified, takes the identity as prefix) [optional].
67  * - <b>-v</b> increase verbosity over default level (can appear multiple times) [optional].
68  * - <b>-q</b> decrease verbosity from default level (can appear multiple times) [optional].
69  * - <b>-h</b> prints a help message documenting these options, and exits [optional].
70  *
71  */
73 {
74 public:
75  /// Default constructor
76  VisAOApp_standalone() throw (AOException);
77 
78  /// Complicated constructor
79  VisAOApp_standalone( string name, int id, int logLevel = Logger::LOG_LEV_INFO) throw (AOException);
80 
81  void set_conffile( std::string name) throw (AOException);
82 
83  /// Config file constructor
84  VisAOApp_standalone( std::string name, const std::string& conffile) throw (AOException);
85 
86  ///For use if default constructed
87  void set_conffile(std::string name, const std::string& conffile) throw (AOException);
88 
89  /// Command line constructor
90  VisAOApp_standalone( int argc, char**argv) throw (AOException);
91 
92 
93  virtual ~VisAOApp_standalone();
94 
95  static const std::string & ConfigFile() {return _configFile;}
96  static const std::string & MyName() {return _myName;}
97  static const std::string & MyFullName() {return _myFullName;}
98  static int ID() {return _ID;}
99  static Config_File & ConfigDictionary() {return (*_cfg);}
100  static int Verbosity() {return _logger->getLevel();}
101 
102 
103  /// Return configuration file path based on identity
104  static string getConffile( string identity);
105 
106  void usage();
107 
108  virtual std::string get_app_name() {return _myName;}
109 protected:
110  static void SetMyName(const std::string& name) {_myName=name;}
111  static void SetConfigFile(const std::string& conffile) throw (Config_File_Exception);
112  static void SetID(int id) {_ID=id;}
113 
114  static Logger* _logger;
115 
116  std::string init_file;
117  Config_File* init_vars;
118 
119  private:
120  static int CONFIG_LOADER_LOG_LEVEL; // Used to log config file loading
121 
122  static int DEFAULT_LOG_LEVEL;
123 
124  void setLogFile(string fileName);
125 
126  void initTempLog();
127 
128  void CreateVisAOApp_standalone(int logLevel) throw (AOException);
129 
130  static std::string _configFile;
131  static std::string _logFile;
132  static std::string _myName;
133  static int _ID;
134  static std::string _myFullName;
135  static Config_File* _cfg;
136 
137 
138  public:
139  /// Report an error. Also calls log_msg. Overloaded from VisAOApp_base.
140  /** \param emsg the message to report.
141  * \param LogLevel is the logger level for the error report. For erros this is usually Logger::LOG_LEVEL_ERROR or Logger::LOG_LEVEL_FATAL
142  */
143  //void error_report(int LogLevel, std::string emsg);
144 
145  /// Log a message.
146  /** \param lmsg the message to log. Overloaded from VisAOApp_base.
147  * \param LogLevel is the logger level for the message.
148  */
149  void log_msg(int LogLevel, std::string lmsg);
150 
151 
152 
153  //Execution of a VisAOApp:
154  protected:
155  pthread_t main_thread; ///<Identifier for the main thread
156  pthread_t signal_thread; ///<Identifier for the separate signal handling thread
157  pthread_mutex_t signal_mutex; ///<Mutex for the condition signaling
158  pthread_cond_t signal_cond; ///<Condition for telling main thread that something changed
159 
160  double signalth_sleeptime;
161 
162  public:
163  ///Installs the term and XXXX signal handlers, and calls Run().
164  /** Normaly doesn't need to be re-implemented.
165  */
166  virtual int Exec();
167 
168  ///The application main loop, to be re-implemented in derived classes.
169  virtual int Run();
170 
171  ///Signal loop, normally won't need to be overridden.
172  /** This does need to be started by the Run() function if it is desired.
173  * Before starting, must have setup and connected the fifo list.
174  * Can be started with start_signal_catcher(). The main loop
175  * should then block SIGIO and RTSIGIO.
176  */
177  void signal_catcher();
178 
179  ///Starts the signal catching loop
180  /** \param inherit_sched if true (default) then the signal_catcher is started with the same RT priority as the main loop.
181  */
182  virtual int start_signal_catcher(bool inherit_sched = true);
183 
184  ///Install the SIG_MAINTHREAD signal catcher
185  virtual int install_sig_mainthread_catcher();
186 
187  ///Sets the signal mask to block signal signum
188  virtual int block_signal(int signum);
189 
190  ///Sets the signal mask to block SIGIO and RTSIGIO
191  virtual int block_sigio();
192 
193  ///Handle a timetodie condition upon exiting the signal catcher thread (e.g. tell main thread it is about to die)
194  /** This isn't usually necessary, but see framewriter for an example of using it.
195  */
196  virtual int kill_me();
197 };
198 
199 
200 ///Thread starter for the signal catcher.
201 /** Casts the void pointer to VisAOApp_standalone and calls start_signal_catcher.
202  */
203 void * __start_signal_catcher(void *);
204 
205 ///Catches the signal to the main thread.
206 /** Does absolutely nothing, just provides a facility to wake the main thread up if it is blocking or paused.
207  */
208 void sig_mainthread_catcher(int signum, siginfo_t *siginf, void *ucont);
209 
210 ///Catches SIGTERM and sets TimeToDie
211 void sigterm_handler(int signum, siginfo_t *siginf, void *ucont);
212 
213 } //namespace VisAO
214 
215 
216 #endif //__VisAOApp_standalone_h__
217 
VisAOApp_standalone()
Default constructor.
void log_msg(int LogLevel, std::string lmsg)
Report an error. Also calls log_msg. Overloaded from VisAOApp_base.
virtual int Run()
The application main loop, to be re-implemented in derived classes.
virtual int start_signal_catcher(bool inherit_sched=true)
Starts the signal catching loop.
pthread_mutex_t signal_mutex
Mutex for the condition signaling.
The standalone VisAO application, does not interface with the AO Supervisor.
void signal_catcher()
Signal loop, normally won't need to be overridden.
virtual std::string get_app_name()
Get the application name.
void * __start_signal_catcher(void *ptr)
Thread starter for the signal catcher.
pthread_cond_t signal_cond
Condition for telling main thread that something changed.
virtual int block_signal(int signum)
Sets the signal mask to block signal signum.
pthread_t signal_thread
Identifier for the separate signal handling thread.
virtual int block_sigio()
Sets the signal mask to block SIGIO and RTSIGIO.
static string getConffile(string identity)
Return configuration file path based on identity.
The base class for VisAO applications.
Definition: VisAOApp_base.h:48
Declarations for VisAOApp_base.
virtual int install_sig_mainthread_catcher()
Install the SIG_MAINTHREAD signal catcher.
virtual int Exec()
Installs the term and XXXX signal handlers, and calls Run().
The namespace of VisAO software.
pthread_t main_thread
Identifier for the main thread.
virtual int kill_me()
Handle a timetodie condition upon exiting the signal catcher thread (e.g. tell main thread it is abou...