1 | //****************************************************************
|
---|
2 | /** @class DataLogger
|
---|
3 |
|
---|
4 | @brief Logs all message and infos between the services
|
---|
5 |
|
---|
6 | This is the main logging class facility.
|
---|
7 | It derives from StateMachineDim and DimInfoHandler. the first parent is here to enforce
|
---|
8 | a state machine behaviour, while the second one is meant to make the dataLogger receive
|
---|
9 | dim services to which it subscribed from.
|
---|
10 | The possible states and transitions of the machine are:
|
---|
11 | \dot
|
---|
12 | digraph datalogger {
|
---|
13 | node [shape=record, fontname=Helvetica, fontsize=10];
|
---|
14 | e [label="Error" color="red"];
|
---|
15 | r [label="Ready"]
|
---|
16 | d [label="DailyOpen"]
|
---|
17 | w [label="WaitingRun"]
|
---|
18 | l [label="Logging"]
|
---|
19 | b [label="BadDailyconfig" color="red"]
|
---|
20 | c [label="BadRunConfig" color="red"]
|
---|
21 |
|
---|
22 | e -> r
|
---|
23 | r -> e
|
---|
24 | r -> d
|
---|
25 | r -> b
|
---|
26 | d -> w
|
---|
27 | d -> r
|
---|
28 | w -> r
|
---|
29 | l -> r
|
---|
30 | l -> w
|
---|
31 | b -> d
|
---|
32 | w -> c
|
---|
33 | w -> l
|
---|
34 | b -> r
|
---|
35 | c -> r
|
---|
36 | c -> l
|
---|
37 | }
|
---|
38 | \enddot
|
---|
39 |
|
---|
40 | @todo
|
---|
41 | - Retrieve also the messages, not only the infos
|
---|
42 | */
|
---|
43 | //****************************************************************
|
---|
44 | #include "Event.h"
|
---|
45 | #include "Time.h"
|
---|
46 | #include "StateMachineDim.h"
|
---|
47 | #include "WindowLog.h"
|
---|
48 | #include "Configuration.h"
|
---|
49 | #include "ServiceList.h"
|
---|
50 | #include "Converter.h"
|
---|
51 | #include "MessageImp.h"
|
---|
52 | #include "LocalControl.h"
|
---|
53 |
|
---|
54 | //#define HAS_FITS
|
---|
55 |
|
---|
56 | #include <fstream>
|
---|
57 |
|
---|
58 | #include <boost/bind.hpp>
|
---|
59 |
|
---|
60 | #ifdef HAS_FITS
|
---|
61 | #include <astroroot.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | class DataLogger : public StateMachineDim, DimInfoHandler
|
---|
65 | {
|
---|
66 | public:
|
---|
67 | /// The list of existing states specific to the DataLogger
|
---|
68 | enum
|
---|
69 | {
|
---|
70 | kSM_DailyOpen = 20, ///< Daily file openned and writing
|
---|
71 | kSM_WaitingRun = 30, ///< waiting for the run number to open the run file
|
---|
72 | kSM_Logging = 40, ///< both files openned and writing
|
---|
73 | kSM_BadDailyConfig = 0x101, ///< the folder specified for daily logging does not exist or has bad permissions
|
---|
74 | kSM_BadRunConfig = 0x102, ///< the folder specified for the run logging does not exist or has wrong permissions or no run number
|
---|
75 | } localstates_t;
|
---|
76 |
|
---|
77 | DataLogger(std::ostream &out);
|
---|
78 | ~DataLogger();
|
---|
79 |
|
---|
80 | private:
|
---|
81 | //Define all the data structure specific to the DataLogger here
|
---|
82 | /// ofstream for the dailyLogfile
|
---|
83 | std::ofstream fDailyLogFile;
|
---|
84 | /// ofstream for the run-specific Log file
|
---|
85 | std::ofstream fRunLogFile;
|
---|
86 |
|
---|
87 | /// ofstream for the daily report file
|
---|
88 | std::ofstream fDailyReportFile;
|
---|
89 | /// ofstream for the run-specific report file
|
---|
90 | std::ofstream fRunReportFile;
|
---|
91 | /// base path of the dailyfile
|
---|
92 | std::string fDailyFileName;
|
---|
93 | ///base path of the run file
|
---|
94 | std::string fRunFileName;
|
---|
95 | ///run number (-1 means no run number specified)
|
---|
96 | int fRunNumber;
|
---|
97 | ///Current year
|
---|
98 | short fYear;
|
---|
99 | ///Current Month
|
---|
100 | short fMonth;
|
---|
101 | ///Current Day
|
---|
102 | short fDay;
|
---|
103 | ///Current Hour
|
---|
104 | short fHour;
|
---|
105 | ///Current Minute
|
---|
106 | short fMin;
|
---|
107 | ///Current Second
|
---|
108 | short fSec;
|
---|
109 | ///Current Milliseconds
|
---|
110 | int fMs;
|
---|
111 | ///Current Service Quality
|
---|
112 | int fQuality;
|
---|
113 | ///Modified Julian Date
|
---|
114 | double fMjD;
|
---|
115 |
|
---|
116 | ///Define all the static names
|
---|
117 | static const char* fConfigDay;
|
---|
118 | static const char* fConfigRun;
|
---|
119 | static const char* fConfigRunNumber;
|
---|
120 | static const char* fConfigLog;
|
---|
121 | static const char* fTransStart;
|
---|
122 | static const char* fTransStop;
|
---|
123 | static const char* fTransStartRun;
|
---|
124 | static const char* fTransStopRun;
|
---|
125 | static const char* fTransReset;
|
---|
126 | static const char* fTransWait;
|
---|
127 | static const char* fRunNumberInfo; ///< This is the name of the dimInfo received to specify the run number. It must be updated once the final name will be defined
|
---|
128 | ///Inherited from state machine impl
|
---|
129 | int Execute();
|
---|
130 |
|
---|
131 | ///Inherited from state machine impl
|
---|
132 | int Transition(const Event& evt);
|
---|
133 |
|
---|
134 | ///Inherited from state machine impl
|
---|
135 | int Configure(const Event& evt);
|
---|
136 |
|
---|
137 | //overloading of DIM's infoHandler function
|
---|
138 | void infoHandler();
|
---|
139 |
|
---|
140 | ///for obtaining the name of the existing services
|
---|
141 | ServiceList fServiceList;
|
---|
142 |
|
---|
143 |
|
---|
144 | ///A std pair to store both the DimInfo name and the actual DimInfo pointer
|
---|
145 | // typedef std::pair<std::string, DimStampedInfo*> subscriptionType;
|
---|
146 | ///All the services to which we've subscribed to. Sorted by server name
|
---|
147 | // std::map<const std::string, std::vector<subscriptionType > > fServiceSubscriptions;
|
---|
148 |
|
---|
149 | ///A std pair to store both the DimInfo pointer and the corresponding outputted fits file
|
---|
150 | struct SubscriptionType
|
---|
151 | {
|
---|
152 | #ifdef HAS_FITS
|
---|
153 | ///daily FITS output file
|
---|
154 | AstroRootIo dailyFile;
|
---|
155 | ///run-specific FITS output file
|
---|
156 | AstroRootIo runFile;
|
---|
157 | #endif
|
---|
158 | ///the actual dimInfo pointer
|
---|
159 | DimStampedInfo* dimInfo;
|
---|
160 | ///the number of existing handlers to this structure.
|
---|
161 | ///This is required otherwise I MUST handle the deleting of dimInfo outside from the destructor
|
---|
162 | int* numCopies;
|
---|
163 | void operator = (const SubscriptionType& other)
|
---|
164 | {
|
---|
165 | #ifdef HAS_FITS
|
---|
166 | dailyFile = other.dailyFile;
|
---|
167 | runFile = other.runFile;
|
---|
168 | #endif
|
---|
169 | dimInfo = other.dimInfo;
|
---|
170 | numCopies = other.numCopies;
|
---|
171 | (*numCopies)++;
|
---|
172 | }
|
---|
173 | SubscriptionType(const SubscriptionType& other)
|
---|
174 | {
|
---|
175 | #ifdef HAS_FITS
|
---|
176 | dailyFile = other.dailyFile;
|
---|
177 | runFile = other.runFile;
|
---|
178 | #endif
|
---|
179 | dimInfo = other.dimInfo;
|
---|
180 | numCopies = other.numCopies;
|
---|
181 | (*numCopies)++;
|
---|
182 | }
|
---|
183 | SubscriptionType(DimStampedInfo* info)
|
---|
184 | {
|
---|
185 | dimInfo = info;
|
---|
186 | numCopies = new int(1);
|
---|
187 | }
|
---|
188 | SubscriptionType()
|
---|
189 | {
|
---|
190 | dimInfo = NULL;
|
---|
191 | numCopies = new int(1);
|
---|
192 | }
|
---|
193 | ~SubscriptionType()
|
---|
194 | {
|
---|
195 | if (numCopies)
|
---|
196 | (*numCopies)--;
|
---|
197 | if (numCopies)
|
---|
198 | if (*numCopies < 1)
|
---|
199 | {
|
---|
200 | #ifdef HAS_FITS
|
---|
201 | if (dailyFile.IsOpen())
|
---|
202 | dailyFile.Close();
|
---|
203 | if (runFile.IsOpen())
|
---|
204 | runFile.Close();
|
---|
205 | #endif
|
---|
206 | if (dimInfo)
|
---|
207 | delete dimInfo;
|
---|
208 | if (numCopies)
|
---|
209 | delete numCopies;
|
---|
210 | dimInfo = NULL;
|
---|
211 | numCopies = NULL;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | };
|
---|
215 | typedef std::map<const std::string, std::map<std::string, SubscriptionType>> SubscriptionsListType;
|
---|
216 | ///All the services to which we have subscribed to, sorted by server name.
|
---|
217 | SubscriptionsListType fServiceSubscriptions;
|
---|
218 |
|
---|
219 |
|
---|
220 | ///Reporting method for the services info received
|
---|
221 | void ReportPlease(DimInfo* I, SubscriptionType& sub);
|
---|
222 |
|
---|
223 | ///Configuration of the daily file path
|
---|
224 | int ConfigureDailyFileName(const Event& evt);
|
---|
225 | ///Configuration fo the file name
|
---|
226 | int ConfigureRunFileName(const Event& evt);
|
---|
227 | ///DEPREC - configuration of the run number
|
---|
228 | int ConfigureRunNumber(const Event& evt);
|
---|
229 | ///logging method for the messages
|
---|
230 | int LogMessagePlease(const Event& evt);
|
---|
231 | ///checks whether or not the current info being treated is a run number
|
---|
232 | void CheckForRunNumber(DimInfo* I);
|
---|
233 |
|
---|
234 | /// start transition
|
---|
235 | int StartPlease();
|
---|
236 | ///from waiting to logging transition
|
---|
237 | int StartRunPlease();
|
---|
238 | /// from logging to waiting transition
|
---|
239 | int StopRunPlease();
|
---|
240 | ///stop and reset transition
|
---|
241 | int GoToReadyPlease();
|
---|
242 | ///from dailyOpen to waiting transition
|
---|
243 | int DailyToWaitRunPlease();
|
---|
244 | #ifdef HAS_FITS
|
---|
245 | ///Open fits files
|
---|
246 | void OpenFITSFilesPlease(SubscriptionType& sub);
|
---|
247 | ///Write data to FITS files
|
---|
248 | void WriteToFITS(SubscriptionType& sub);
|
---|
249 | ///Allocate the buffers required for fits
|
---|
250 | void AllocateFITSBuffers(SubscriptionType& sub);
|
---|
251 | #endif
|
---|
252 | public:
|
---|
253 | ///checks with fServiceList whether or not the services got updated
|
---|
254 | void CheckForServicesUpdate();
|
---|
255 |
|
---|
256 | }; //DataLogger
|
---|
257 |
|
---|
258 | //static members initialization
|
---|
259 | //since I do not check the transition/config names any longer, indeed maybe these could be hard-coded... but who knows what will happen in the future ?
|
---|
260 | const char* DataLogger::fConfigDay = "CONFIG_DAY";
|
---|
261 | const char* DataLogger::fConfigRun = "CONFIG_RUN";
|
---|
262 | const char* DataLogger::fConfigRunNumber = "CONFIG_RUN_NUMBER";
|
---|
263 | const char* DataLogger::fConfigLog = "LOG";
|
---|
264 | const char* DataLogger::fTransStart = "START";
|
---|
265 | const char* DataLogger::fTransStop = "STOP";
|
---|
266 | const char* DataLogger::fTransStartRun = "START_RUN";
|
---|
267 | const char* DataLogger::fTransStopRun = "STOP_RUN";
|
---|
268 | const char* DataLogger::fTransReset = "RESET";
|
---|
269 | const char* DataLogger::fTransWait = "WAIT_RUN_NUMBER";
|
---|
270 | const char* DataLogger::fRunNumberInfo = "RUN_NUMBER";
|
---|
271 |
|
---|
272 | // --------------------------------------------------------------------------
|
---|
273 | //
|
---|
274 | //! Default constructor. The name of the machine is given DATA_LOGGER
|
---|
275 | //! and the state is set to kSM_Ready at the end of the function.
|
---|
276 | //
|
---|
277 | //!Setup the allows states, configs and transitions for the data logger
|
---|
278 | //
|
---|
279 | DataLogger::DataLogger(std::ostream &out) : StateMachineDim(out, "DATA_LOGGER")
|
---|
280 | {
|
---|
281 | //initialize member data
|
---|
282 | fDailyFileName = "/home/lyard/log";
|
---|
283 | fRunFileName = "/home/lyard/log";
|
---|
284 | fRunNumber = 12345;
|
---|
285 | //Give a name to this machine's specific states
|
---|
286 | AddStateName(kSM_DailyOpen, "DailyFileOpen");
|
---|
287 | AddStateName(kSM_WaitingRun, "WaitForRun");
|
---|
288 | AddStateName(kSM_Logging, "Logging");
|
---|
289 | AddStateName(kSM_BadDailyConfig, "ErrDailyFolder");
|
---|
290 | AddStateName(kSM_BadRunConfig, "ErrRunFolder");
|
---|
291 |
|
---|
292 | /*Add the possible transitions for this machine*/
|
---|
293 | AddTransition(kSM_DailyOpen, fTransStart, kSM_Ready, kSM_BadDailyConfig) //start the daily logging. daily file location must be specified already
|
---|
294 | ->AssignFunction(boost::bind(&DataLogger::StartPlease, this));
|
---|
295 | AddTransition(kSM_Ready, fTransStop, kSM_DailyOpen, kSM_WaitingRun, kSM_Logging) //stop the data logging
|
---|
296 | ->AssignFunction(boost::bind(&DataLogger::GoToReadyPlease, this));
|
---|
297 | AddTransition(kSM_Logging, fTransStartRun, kSM_WaitingRun, kSM_BadRunConfig) //start the run logging. run file location must be specified already.
|
---|
298 | ->AssignFunction(boost::bind(&DataLogger::StartRunPlease, this));
|
---|
299 | AddTransition(kSM_WaitingRun, fTransStopRun, kSM_Logging)
|
---|
300 | ->AssignFunction(boost::bind(&DataLogger::StopRunPlease, this));
|
---|
301 | AddTransition(kSM_Ready, fTransReset, kSM_Error, kSM_BadDailyConfig, kSM_BadRunConfig, kSM_Error) //transition to exit error states. dunno if required or not, would close the daily file if already openned.
|
---|
302 | ->AssignFunction(boost::bind(&DataLogger::GoToReadyPlease, this));
|
---|
303 | AddTransition(kSM_WaitingRun, fTransWait, kSM_DailyOpen)
|
---|
304 | ->AssignFunction(boost::bind(&DataLogger::DailyToWaitRunPlease, this));
|
---|
305 | /*Add the possible configurations for this machine*/
|
---|
306 | AddConfiguration(fConfigDay, "C", kSM_Ready, kSM_BadDailyConfig) //configure the daily file location. cannot be done before the file is actually opened
|
---|
307 | ->AssignFunction(boost::bind(&DataLogger::ConfigureDailyFileName, this, _1));
|
---|
308 | AddConfiguration(fConfigRun, "C", kSM_Ready, kSM_BadDailyConfig, kSM_DailyOpen, kSM_WaitingRun, kSM_BadRunConfig) //configure the run file location. cannot be done before the file is actually opened, and not in a dailly related error.
|
---|
309 | ->AssignFunction(boost::bind(&DataLogger::ConfigureRunFileName, this, _1));
|
---|
310 |
|
---|
311 | //Provide a logging command
|
---|
312 | //I get the feeling that I should be going through the EventImp
|
---|
313 | //instead of DimCommand directly, mainly because the commandHandler
|
---|
314 | //is already done in StateMachineImp.cc
|
---|
315 | //Thus I'll simply add a configuration, which I will treat as the logging command
|
---|
316 | AddConfiguration(fConfigLog, "C", kSM_DailyOpen, kSM_Logging, kSM_WaitingRun, kSM_BadRunConfig)
|
---|
317 | ->AssignFunction(boost::bind(&DataLogger::LogMessagePlease, this, _1));
|
---|
318 |
|
---|
319 | fServiceList.SetHandler(this);
|
---|
320 | CheckForServicesUpdate();
|
---|
321 | }
|
---|
322 | // --------------------------------------------------------------------------
|
---|
323 | //
|
---|
324 | //! Checks for changes in the existing services.
|
---|
325 | //! Any new service will be added to the service list, while the ones which disappeared are removed.
|
---|
326 | //! @todo
|
---|
327 | //! add the configuration (using the conf class ?)
|
---|
328 | //
|
---|
329 | void DataLogger::CheckForServicesUpdate()
|
---|
330 | {
|
---|
331 |
|
---|
332 | //get the current server list
|
---|
333 | const std::vector<std::string> serverList = fServiceList.GetServerList();
|
---|
334 | //first let's remove the servers that may have disapeared
|
---|
335 | //can't treat the erase on maps the same way as for vectors. Do it the safe way instead
|
---|
336 | std::vector<std::string> toBeDeleted;
|
---|
337 | for (SubscriptionsListType::iterator cListe = fServiceSubscriptions.begin(); cListe != fServiceSubscriptions.end(); cListe++)
|
---|
338 | {
|
---|
339 | std::vector<std::string>::const_iterator givenServers;
|
---|
340 | for (givenServers=serverList.begin(); givenServers!= serverList.end(); givenServers++)
|
---|
341 | if (cListe->first == *givenServers)
|
---|
342 | break;
|
---|
343 | if (givenServers == serverList.end())//server vanished. Remove it
|
---|
344 | toBeDeleted.push_back(cListe->first);
|
---|
345 | }
|
---|
346 | for (std::vector<std::string>::const_iterator it = toBeDeleted.begin(); it != toBeDeleted.end(); it++)
|
---|
347 | fServiceSubscriptions.erase(*it);
|
---|
348 |
|
---|
349 | //now crawl through the list of servers, and see if there was some updates
|
---|
350 | for (std::vector<std::string>::const_iterator i=serverList.begin(); i!=serverList.end();i++)
|
---|
351 | {
|
---|
352 | //skip the two obvious excluded services
|
---|
353 | if ((i->find("DIS_DNS") != std::string::npos) ||
|
---|
354 | (i->find("DATA_LOGGER") != std::string::npos))
|
---|
355 | continue;
|
---|
356 | //find the current server in our subscription list
|
---|
357 | SubscriptionsListType::iterator cSubs = fServiceSubscriptions.find(*i);
|
---|
358 | //get the service list of the current server
|
---|
359 | std::vector<std::string> cServicesList = fServiceList.GetServiceList(*i);
|
---|
360 | if (cSubs != fServiceSubscriptions.end())//if the current server already is in our subscriptions
|
---|
361 | { //then check and update our list of subscriptions
|
---|
362 | //first, remove the services that may have dissapeared.
|
---|
363 | std::map<std::string, SubscriptionType>::iterator serverSubs;
|
---|
364 | std::vector<std::string>::const_iterator givenSubs;
|
---|
365 | toBeDeleted.clear();
|
---|
366 | for (serverSubs=cSubs->second.begin(); serverSubs != cSubs->second.end(); serverSubs++)
|
---|
367 | {
|
---|
368 | for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
|
---|
369 | if (serverSubs->first == *givenSubs)
|
---|
370 | break;
|
---|
371 | if (givenSubs == cServicesList.end())
|
---|
372 | {
|
---|
373 | toBeDeleted.push_back(serverSubs->first);
|
---|
374 | }
|
---|
375 | }
|
---|
376 | for (std::vector<std::string>::const_iterator it = toBeDeleted.begin(); it != toBeDeleted.end(); it++)
|
---|
377 | cSubs->second.erase(*it);
|
---|
378 | //now check for new services
|
---|
379 | for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
|
---|
380 | {
|
---|
381 | if (*givenSubs == "SERVICE_LIST")
|
---|
382 | continue;
|
---|
383 | if (cSubs->second.find(*givenSubs) == cSubs->second.end())
|
---|
384 | {//service not found. Add it
|
---|
385 | cSubs->second[*givenSubs].dimInfo = new DimStampedInfo(((*i) + "/" + *givenSubs).c_str(), const_cast<char*>(""), this);
|
---|
386 | }
|
---|
387 | }
|
---|
388 | }
|
---|
389 | else //server not found in our list. Create its entry
|
---|
390 | {
|
---|
391 | fServiceSubscriptions[*i] = std::map<std::string, SubscriptionType>();
|
---|
392 | std::map<std::string, SubscriptionType>& liste = fServiceSubscriptions[*i];
|
---|
393 | for (std::vector<std::string>::const_iterator j = cServicesList.begin(); j!= cServicesList.end(); j++)
|
---|
394 | {
|
---|
395 | if (*j == "SERVICE_LIST")
|
---|
396 | continue;
|
---|
397 | liste[*j].dimInfo = new DimStampedInfo(((*i) + "/" + (*j)).c_str(), const_cast<char*>(""), this);
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 | }
|
---|
402 | // --------------------------------------------------------------------------
|
---|
403 | //
|
---|
404 | //! Destructor
|
---|
405 | //
|
---|
406 | DataLogger::~DataLogger()
|
---|
407 | {
|
---|
408 | //close the files
|
---|
409 | if (fDailyLogFile.is_open())
|
---|
410 | fDailyLogFile.close();
|
---|
411 | if (fDailyReportFile.is_open())
|
---|
412 | fDailyReportFile.close();
|
---|
413 | if (fRunLogFile.is_open())
|
---|
414 | fRunLogFile.close();
|
---|
415 | if (fRunReportFile.is_open())
|
---|
416 | fRunReportFile.close();
|
---|
417 | //release the services subscriptions
|
---|
418 | fServiceSubscriptions.clear();
|
---|
419 | }
|
---|
420 | // --------------------------------------------------------------------------
|
---|
421 | //
|
---|
422 | //! Execute
|
---|
423 | //! Shouldn't be run as we use callbacks instead
|
---|
424 | //
|
---|
425 | int DataLogger::Execute()
|
---|
426 | {
|
---|
427 | //due to the callback mecanism, this function should never be called
|
---|
428 | return kSM_FatalError;
|
---|
429 |
|
---|
430 | switch (GetCurrentState())
|
---|
431 | {
|
---|
432 | case kSM_Error:
|
---|
433 | case kSM_Ready:
|
---|
434 | case kSM_DailyOpen:
|
---|
435 | case kSM_WaitingRun:
|
---|
436 | case kSM_Logging:
|
---|
437 | case kSM_BadDailyConfig:
|
---|
438 | case kSM_BadRunConfig:
|
---|
439 | return GetCurrentState();
|
---|
440 | }
|
---|
441 | //this line below should never be hit. It here mainly to remove warnings at compilation
|
---|
442 | return kSM_FatalError;
|
---|
443 | }
|
---|
444 | // --------------------------------------------------------------------------
|
---|
445 | //
|
---|
446 | //! Shouldn't be run as we use callbacks instead
|
---|
447 | //
|
---|
448 | int DataLogger::Transition(const Event& evt)
|
---|
449 | {
|
---|
450 | //due to the callback mecanism, this function should never be called
|
---|
451 | return kSM_FatalError;
|
---|
452 |
|
---|
453 | switch (evt.GetTargetState())
|
---|
454 | {
|
---|
455 | case kSM_Ready:
|
---|
456 | /*here we must figure out whether the STOP or RESET command was sent*/
|
---|
457 | /*close opened files and go back to ready state*/
|
---|
458 | switch (GetCurrentState())
|
---|
459 | {
|
---|
460 | case kSM_BadDailyConfig:
|
---|
461 | case kSM_BadRunConfig:
|
---|
462 | case kSM_Error:
|
---|
463 | return GoToReadyPlease();
|
---|
464 |
|
---|
465 | case kSM_Logging:
|
---|
466 | case kSM_WaitingRun:
|
---|
467 | case kSM_DailyOpen:
|
---|
468 | return GoToReadyPlease();
|
---|
469 | }
|
---|
470 | break;
|
---|
471 |
|
---|
472 | case kSM_DailyOpen:
|
---|
473 | /*Attempt to open the daily file */
|
---|
474 | switch (GetCurrentState())
|
---|
475 | {
|
---|
476 | case kSM_Ready:
|
---|
477 | case kSM_BadDailyConfig:
|
---|
478 | return StartPlease();
|
---|
479 | }
|
---|
480 | break;
|
---|
481 |
|
---|
482 | case kSM_WaitingRun:
|
---|
483 | /*either close the run file, or just go to the waitingrun state (if coming from daily open*/
|
---|
484 | switch (GetCurrentState())
|
---|
485 | {
|
---|
486 | case kSM_DailyOpen:
|
---|
487 | return kSM_WaitingRun;
|
---|
488 |
|
---|
489 | case kSM_Logging:
|
---|
490 | return StopRunPlease();
|
---|
491 | }
|
---|
492 | break;
|
---|
493 |
|
---|
494 | case kSM_Logging:
|
---|
495 | /*Attempt to open run file */
|
---|
496 | switch (GetCurrentState())
|
---|
497 | {
|
---|
498 | case kSM_WaitingRun:
|
---|
499 | case kSM_BadRunConfig:
|
---|
500 | return StartRunPlease();
|
---|
501 | }
|
---|
502 | break;
|
---|
503 | }
|
---|
504 | //Getting here means that an invalid transition has been asked.
|
---|
505 | //TODO Log an error message
|
---|
506 | //and return the fatal error state
|
---|
507 | return kSM_FatalError;
|
---|
508 | }
|
---|
509 | // --------------------------------------------------------------------------
|
---|
510 | //
|
---|
511 | //! Shouldn't be run as we use callbacks instead
|
---|
512 | //
|
---|
513 | int DataLogger::Configure(const Event& evt)
|
---|
514 | {
|
---|
515 | //due to the callback mecanism, this function should never be called
|
---|
516 | return kSM_FatalError;
|
---|
517 |
|
---|
518 | switch (evt.GetTargetState())
|
---|
519 | {
|
---|
520 | case kSM_Ready:
|
---|
521 | case kSM_BadDailyConfig:
|
---|
522 | return ConfigureDailyFileName(evt);
|
---|
523 | break;
|
---|
524 |
|
---|
525 | case kSM_WaitingRun:
|
---|
526 | case kSM_BadRunConfig:
|
---|
527 | return ConfigureRunFileName(evt);
|
---|
528 | break;
|
---|
529 |
|
---|
530 | case kSM_Logging:
|
---|
531 | case kSM_DailyOpen:
|
---|
532 | //TODO check that this is indeed correct
|
---|
533 | return 0;//LogMessagePlease(evt);
|
---|
534 | break;
|
---|
535 |
|
---|
536 | }
|
---|
537 | //Getting here means that an invalid configuration has been asked.
|
---|
538 | //TODO Log an error message
|
---|
539 | //and return the fatal error state
|
---|
540 | return kSM_FatalError;
|
---|
541 | }
|
---|
542 | // --------------------------------------------------------------------------
|
---|
543 | //
|
---|
544 | //! Inherited from DimInfo. Handles all the Infos to which we subscribed, and log them
|
---|
545 | //
|
---|
546 | void DataLogger::infoHandler()
|
---|
547 | {
|
---|
548 | DimInfo* I = getInfo();
|
---|
549 | if (I==NULL)
|
---|
550 | {
|
---|
551 | CheckForServicesUpdate();
|
---|
552 | return;
|
---|
553 | }
|
---|
554 | //check if the service pointer corresponds to something that we subscribed to
|
---|
555 | //this is a fix for a bug that provides bad Infos when a server starts
|
---|
556 | bool found = false;
|
---|
557 | SubscriptionsListType::iterator x;
|
---|
558 | std::map<std::string, SubscriptionType>::iterator y;
|
---|
559 | for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
|
---|
560 | {//instead of extracting the server and service names, I crawl through my records. dunno what's faster, but both should work
|
---|
561 | for (y=x->second.begin(); y!=x->second.end();y++)
|
---|
562 | if (y->second.dimInfo == I)
|
---|
563 | {
|
---|
564 | found = true;
|
---|
565 | break;
|
---|
566 | }
|
---|
567 | if (found)
|
---|
568 | break;
|
---|
569 | }
|
---|
570 | if (!found)
|
---|
571 | return;
|
---|
572 | if (I->getSize() <= 0)
|
---|
573 | return;
|
---|
574 | //check that the message has been updated by something, i.e. must be different from its initial value
|
---|
575 | if (I->getTimestamp() == 0)
|
---|
576 | return;
|
---|
577 |
|
---|
578 | CheckForRunNumber(I);
|
---|
579 | ReportPlease(I, y->second);
|
---|
580 | }
|
---|
581 |
|
---|
582 | // --------------------------------------------------------------------------
|
---|
583 | //
|
---|
584 | //! Checks whether or not the current info is a run number.
|
---|
585 | //! If so, then remember it. A run number is required to open the run-log file
|
---|
586 | //! @param I
|
---|
587 | //! the current DimInfo
|
---|
588 | //
|
---|
589 | void DataLogger::CheckForRunNumber(DimInfo* I)
|
---|
590 | {
|
---|
591 | return;
|
---|
592 | if (strstr(I->getName(), fRunNumberInfo) != NULL)
|
---|
593 | {//assumes that the run number is an integer
|
---|
594 | //TODO check the format here
|
---|
595 | fRunNumber = I->getInt();
|
---|
596 | }
|
---|
597 | }
|
---|
598 |
|
---|
599 | // --------------------------------------------------------------------------
|
---|
600 | //
|
---|
601 | //! write infos to log files.
|
---|
602 | //! @param I
|
---|
603 | //! The current DimInfo
|
---|
604 | //
|
---|
605 | void DataLogger::ReportPlease(DimInfo* I, SubscriptionType& sub)
|
---|
606 | {
|
---|
607 | //should we log or report this info ? (i.e. is it a message ?)
|
---|
608 | bool isItaReport = ((strstr(I->getName(), "Message") == NULL) && (strstr(I->getName(), "MESSAGE") == NULL));
|
---|
609 |
|
---|
610 | // std::ofstream & dailyFile = fDailyReportFile;//isItaReport? fDailyReportFile : fDailyLogFile;
|
---|
611 | // std::ofstream & runFile = fRunReportFile;//isItaReport? fRunReportFile : fRunLogFile;
|
---|
612 |
|
---|
613 | //TODO add service exclusion
|
---|
614 | if (!fDailyReportFile.is_open())
|
---|
615 | return;
|
---|
616 |
|
---|
617 | //construct the header
|
---|
618 | std::stringstream header;
|
---|
619 |
|
---|
620 | Time cTime((time_t)(I->getTimestamp()), I->getTimestampMillisecs());
|
---|
621 |
|
---|
622 | //Buffer them for FITS write
|
---|
623 | //TODO this has been replaced by MjD. So I guess that these member variables can go.
|
---|
624 | fYear = cTime.Y(); fMonth = cTime.M(); fDay = cTime.D();
|
---|
625 | fHour = cTime.h(); fMin = cTime.m(); fSec = cTime.s();
|
---|
626 | fMs = cTime.ms(); fQuality = I->getQuality();
|
---|
627 |
|
---|
628 | fMjD = cTime.Mjd();
|
---|
629 |
|
---|
630 | if (isItaReport)
|
---|
631 | {
|
---|
632 | //write text header
|
---|
633 | header << I->getName() << " " << fQuality << " ";
|
---|
634 | header << fYear << " " << fMonth << " " << fDay << " ";
|
---|
635 | header << fHour << " " << fMin << " " << fSec << " ";
|
---|
636 | header << fMs << " " << I->getTimestamp() << " ";
|
---|
637 |
|
---|
638 | const Converter conv(Out(), I->getFormat());
|
---|
639 |
|
---|
640 | std::string text = conv.GetString(I->getData(), I->getSize());
|
---|
641 | if (!conv.valid() || text.empty()!=conv.empty())
|
---|
642 | {
|
---|
643 | Error("Couldn't properly parse the data... ignored.");
|
---|
644 | return;
|
---|
645 | }
|
---|
646 |
|
---|
647 | if (text.empty())
|
---|
648 | return;
|
---|
649 |
|
---|
650 | //replace bizarre characters by white space
|
---|
651 | replace(text.begin(), text.end(), '\n', '\\');
|
---|
652 | replace_if(text.begin(), text.end(), std::ptr_fun<int, int>(&std::iscntrl), ' ');
|
---|
653 |
|
---|
654 | if (fDailyReportFile.is_open())
|
---|
655 | fDailyReportFile << header.str();
|
---|
656 | if (fRunReportFile.is_open())
|
---|
657 | fRunReportFile << header.str();
|
---|
658 |
|
---|
659 | if (fDailyReportFile.is_open())
|
---|
660 | fDailyReportFile << text << std::endl;
|
---|
661 | if (fRunReportFile.is_open())
|
---|
662 | fRunReportFile << text << std::endl;
|
---|
663 | }
|
---|
664 | else
|
---|
665 | {
|
---|
666 | std::string n = I->getName();
|
---|
667 | std::stringstream msg;
|
---|
668 | msg << n.substr(0, n.find_first_of('/')) << ": " << I->getString();
|
---|
669 | MessageImp dailyMess(fDailyLogFile);
|
---|
670 | dailyMess.Write(cTime, msg.str().c_str(), fQuality);
|
---|
671 | if (fRunLogFile.is_open())
|
---|
672 | {
|
---|
673 | MessageImp runMess(fRunLogFile);
|
---|
674 | runMess.Write(cTime, msg.str().c_str(), fQuality);
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | #ifdef HAS_FITS
|
---|
679 | if (!sub.dailyFile.IsOpen() || !sub.runFile.IsOpen())
|
---|
680 | OpenFITSFilesPlease(sub);
|
---|
681 | WriteToFITS(sub);
|
---|
682 | #endif
|
---|
683 |
|
---|
684 | }
|
---|
685 | // --------------------------------------------------------------------------
|
---|
686 | //
|
---|
687 | //! write messages to logs.
|
---|
688 | //! @param evt
|
---|
689 | //! the current event to log
|
---|
690 | //! @returns
|
---|
691 | //! the new state. Currently, always the current state
|
---|
692 | //!
|
---|
693 | //! @deprecated
|
---|
694 | //! I guess that this function should not be any longer
|
---|
695 | //
|
---|
696 | int DataLogger::LogMessagePlease(const Event& evt)
|
---|
697 | {
|
---|
698 | if (!fDailyLogFile.is_open())
|
---|
699 | return GetCurrentState();
|
---|
700 |
|
---|
701 | std::stringstream header;
|
---|
702 | const Time& cTime = evt.GetTime();
|
---|
703 | header << evt.GetName() << " " << cTime.Y() << " " << cTime.M() << " " << cTime.D() << " ";
|
---|
704 | header << cTime.h() << " " << cTime.m() << " " << cTime.s() << " ";
|
---|
705 | header << cTime.ms() << " ";
|
---|
706 |
|
---|
707 | // std::string text = ToString(evt.GetFormat().c_str(), evt.GetData(), evt.GetSize());
|
---|
708 | const Converter conv(Out(), evt.GetFormat());
|
---|
709 |
|
---|
710 | std::string text = conv.GetString(evt.GetData(), evt.GetSize());
|
---|
711 | if (!conv.valid() || text.empty()!=conv.empty())
|
---|
712 | {
|
---|
713 | Error("Couldn't properly parse the data... ignored.");
|
---|
714 | return GetCurrentState();
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (text.empty())
|
---|
718 | return GetCurrentState();
|
---|
719 |
|
---|
720 | //replace bizarre characters by white space
|
---|
721 | replace(text.begin(), text.end(), '\n', '\\');
|
---|
722 | replace_if(text.begin(), text.end(), std::ptr_fun<int, int>(&std::iscntrl), ' ');
|
---|
723 |
|
---|
724 | if (fDailyLogFile.is_open())
|
---|
725 | fDailyLogFile << header;
|
---|
726 | if (fRunLogFile.is_open())
|
---|
727 | fRunLogFile << header;
|
---|
728 |
|
---|
729 | if (fDailyLogFile.is_open())
|
---|
730 | fDailyLogFile << text;
|
---|
731 | if (fRunLogFile.is_open())
|
---|
732 | fRunLogFile << text;
|
---|
733 |
|
---|
734 | return GetCurrentState();
|
---|
735 | }
|
---|
736 | // --------------------------------------------------------------------------
|
---|
737 | //
|
---|
738 | //! Sets the path to use for the daily log file.
|
---|
739 | //! @param evt
|
---|
740 | //! the event transporting the path
|
---|
741 | //! @returns
|
---|
742 | //! currently only the current state.
|
---|
743 | //
|
---|
744 | int DataLogger::ConfigureDailyFileName(const Event& evt)
|
---|
745 | {
|
---|
746 | if (evt.GetText() != NULL)
|
---|
747 | fDailyFileName = std::string(evt.GetText());
|
---|
748 | else
|
---|
749 | Error("Empty daily folder");
|
---|
750 |
|
---|
751 | return GetCurrentState();
|
---|
752 | }
|
---|
753 | // --------------------------------------------------------------------------
|
---|
754 | //
|
---|
755 | //! Sets the path to use for the run log file.
|
---|
756 | //! @param evt
|
---|
757 | //! the event transporting the path
|
---|
758 | //! @returns
|
---|
759 | //! currently only the current state
|
---|
760 | int DataLogger::ConfigureRunFileName(const Event& evt)
|
---|
761 | {
|
---|
762 | if (evt.GetText() != NULL)
|
---|
763 | fRunFileName = std::string(evt.GetText());
|
---|
764 | else
|
---|
765 | Error("Empty daily folder");
|
---|
766 |
|
---|
767 | return GetCurrentState();
|
---|
768 | }
|
---|
769 | // --------------------------------------------------------------------------
|
---|
770 | //
|
---|
771 | //! Sets the run number.
|
---|
772 | //! @param evt
|
---|
773 | //! the event transporting the run number
|
---|
774 | //! @returns
|
---|
775 | //! currently only the current state
|
---|
776 | int DataLogger::ConfigureRunNumber(const Event& evt)
|
---|
777 | {
|
---|
778 | fRunNumber = evt.GetInt();
|
---|
779 |
|
---|
780 | return GetCurrentState();
|
---|
781 | }
|
---|
782 | // --------------------------------------------------------------------------
|
---|
783 | //
|
---|
784 | //! Implements the Start transition.
|
---|
785 | //! Concatenates the given path for the daily file and the filename itself (based on the day),
|
---|
786 | //! and tries to open it.
|
---|
787 | //! @returns
|
---|
788 | //! kSM_DailyOpen if success, kSM_BadDailyConfig if failure
|
---|
789 | int DataLogger::StartPlease()
|
---|
790 | {
|
---|
791 | //TODO concatenate the dailyFileName and the formatted date and extension to obtain the full file name
|
---|
792 | Time time;//(Time::utc);
|
---|
793 | std::stringstream sTime;
|
---|
794 | sTime << time.Y() << "_" << time.M() << "_" << time.D();
|
---|
795 | std::string fullName = fDailyFileName + '/' + sTime.str() + ".log";
|
---|
796 |
|
---|
797 | fDailyLogFile.open(fullName.c_str(), std::ios_base::out | std::ios_base::app); //maybe should be "app" instead of "ate" ??
|
---|
798 | fullName = fDailyFileName + '/' + sTime.str() + ".rep";
|
---|
799 | fDailyReportFile.open(fullName.c_str(), std::ios_base::out | std::ios_base::app);
|
---|
800 | if (!fDailyLogFile.is_open() || !fDailyReportFile.is_open())
|
---|
801 | {
|
---|
802 | //TODO send an error message
|
---|
803 | return kSM_BadDailyConfig;
|
---|
804 | }
|
---|
805 | return kSM_DailyOpen;
|
---|
806 | }
|
---|
807 |
|
---|
808 | #ifdef HAS_FITS
|
---|
809 | // --------------------------------------------------------------------------
|
---|
810 | //
|
---|
811 | //! open if required a the FITS files corresponding to a given subscription
|
---|
812 | //! @param sub
|
---|
813 | //! the current DimInfo subscription being examined
|
---|
814 | void DataLogger::OpenFITSFilesPlease(SubscriptionType& sub)
|
---|
815 | {
|
---|
816 | std::string serviceName(sub.dimInfo->getName());
|
---|
817 | for (unsigned int i=0;i<serviceName.size(); i++)
|
---|
818 | {
|
---|
819 | if (serviceName[i] == '/')
|
---|
820 | {
|
---|
821 | serviceName[i] = '_';
|
---|
822 | break;
|
---|
823 | }
|
---|
824 | }
|
---|
825 | Time time;
|
---|
826 | std::stringstream sTime;
|
---|
827 | sTime << time.Y() << "_" << time.M() << "_" << time.D();
|
---|
828 | //we open the dailyFile anyway, otherwise this function shouldn't have been called.
|
---|
829 | if (!sub.dailyFile.IsOpen())
|
---|
830 | {
|
---|
831 | std::string partialName = fDailyFileName + '/' + sTime.str() + '_' + serviceName + ".fits";
|
---|
832 | std::string fullName = fDailyFileName + '/' + sTime.str() + '_' + serviceName + ".fits[" + serviceName + "]";
|
---|
833 |
|
---|
834 | AllocateFITSBuffers(sub);
|
---|
835 | //currently, the FITS are written in the same directory as the text files.
|
---|
836 | //thus the write permissions have already been checked by the text files.
|
---|
837 | //if the target folder changes, then I should check the write permissions here
|
---|
838 | //now we only check whether the target file exists or not
|
---|
839 | std::ifstream readTest(partialName.c_str());
|
---|
840 | if (readTest.is_open())
|
---|
841 | {
|
---|
842 | readTest.close();
|
---|
843 | sub.dailyFile.Open(fullName.c_str(), "UPDATE");
|
---|
844 | }
|
---|
845 | else {
|
---|
846 | sub.dailyFile.Open(fullName.c_str(), "CREATE");
|
---|
847 | }
|
---|
848 |
|
---|
849 | //TODO Write the header's attributes
|
---|
850 | }
|
---|
851 | if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging))
|
---|
852 | {//buffer for the run file have already been allocated when doing the daily file
|
---|
853 | std::stringstream sRun;
|
---|
854 | sRun << fRunNumber;
|
---|
855 | std::string partialName = fRunFileName + '/' + sRun.str() + '_' + serviceName + ".fits";
|
---|
856 | std::string fullName = fRunFileName + '/' + sRun.str() + '_' + serviceName + ".fits[" + serviceName + "]";
|
---|
857 |
|
---|
858 | std::ifstream readTest(partialName.c_str());
|
---|
859 | if (readTest.is_open())
|
---|
860 | {
|
---|
861 | readTest.close();
|
---|
862 | sub.runFile.Open(fullName.c_str(), "UPDATE");
|
---|
863 | }
|
---|
864 | else
|
---|
865 | sub.runFile.Open(fullName.c_str(), "CREATE");
|
---|
866 | //TODO Write the header's attributes
|
---|
867 | }
|
---|
868 | }
|
---|
869 | // --------------------------------------------------------------------------
|
---|
870 | //
|
---|
871 | void DataLogger::AllocateFITSBuffers(SubscriptionType& sub)
|
---|
872 | {
|
---|
873 | const char* format = sub.dimInfo->getFormat();
|
---|
874 | const int size = sub.dimInfo->getSize();
|
---|
875 |
|
---|
876 | //Init the time columns of the file
|
---|
877 | sub.dailyFile.InitCol("Date", "double", &fMjD);
|
---|
878 | sub.runFile.InitCol("Date", "double", &fMjD);
|
---|
879 |
|
---|
880 | // sub.dailyFile.InitCol("Year", "short", &fYear);
|
---|
881 | // sub.dailyFile.InitCol("Month", "short", &fMonth);
|
---|
882 | // sub.dailyFile.InitCol("Day", "short", &fDay);
|
---|
883 | // sub.dailyFile.InitCol("Hour", "short", &fHour);
|
---|
884 | // sub.dailyFile.InitCol("Minute", "short", &fMin);
|
---|
885 | // sub.dailyFile.InitCol("Second", "short", &fSec);
|
---|
886 | // sub.dailyFile.InitCol("MilliSec", "int", &fMs);
|
---|
887 | sub.dailyFile.InitCol("QoS", "int", &fQuality);
|
---|
888 |
|
---|
889 | // sub.runFile.InitCol("Year", "short", &fYear);
|
---|
890 | // sub.runFile.InitCol("Month", "short", &fMonth);
|
---|
891 | // sub.runFile.InitCol("Day", "short", &fDay);
|
---|
892 | // sub.runFile.InitCol("Hour", "short", &fHour);
|
---|
893 | // sub.runFile.InitCol("Minute", "short", &fMin);
|
---|
894 | // sub.runFile.InitCol("Second", "short", &fSec);
|
---|
895 | // sub.runFile.InitCol("MilliSec", "int", &fMs);
|
---|
896 | sub.runFile.InitCol("QoS", "int", &fQuality);
|
---|
897 |
|
---|
898 | const Converter::FormatList flist = Converter::Compile(Out(), format);
|
---|
899 |
|
---|
900 | // Compilation failed
|
---|
901 | if (fList.empty() || fList.back().first.second!=0)
|
---|
902 | {
|
---|
903 | Error("Compilation of format string failed.");
|
---|
904 | return;
|
---|
905 | }
|
---|
906 |
|
---|
907 | //we've got a nice structure describing the format of this service's messages.
|
---|
908 | //Let's create the appropriate FITS columns
|
---|
909 | for (unsigned int i=0;i<flist.size();i++)
|
---|
910 | {
|
---|
911 | std::stringstream colName;
|
---|
912 | std::stringstream dataQualifier;
|
---|
913 | void * dataPointer = static_cast<char*>(sub.dimInfo->getData()) + flist[i].second.second;
|
---|
914 | colName << "Data" << i;
|
---|
915 | dataQualifier << flist[i].second.first;
|
---|
916 | switch (flist[i].first.first)
|
---|
917 | {
|
---|
918 | case 'c':
|
---|
919 | dataQualifier << "S";
|
---|
920 | break;
|
---|
921 | case 's':
|
---|
922 | dataQualifier << "I";
|
---|
923 | break;
|
---|
924 | case 'i':
|
---|
925 | dataQualifier << "J";
|
---|
926 | break;
|
---|
927 | case 'l':
|
---|
928 | dataQualifier << "J";
|
---|
929 | //TODO triple check that in FITS, long = int
|
---|
930 | break;
|
---|
931 | case 'f':
|
---|
932 | dataQualifier << "E";
|
---|
933 | break;
|
---|
934 | case 'd':
|
---|
935 | dataQualifier << "D";
|
---|
936 | break;
|
---|
937 | case 'x':
|
---|
938 | dataQualifier << "K";
|
---|
939 | break;
|
---|
940 | case 'S':
|
---|
941 | //for strings, the number of elements I get is wrong. Correct it
|
---|
942 | dataQualifier.str(""); //clear
|
---|
943 | dataQualifier << size-1 << "A";
|
---|
944 | break;
|
---|
945 |
|
---|
946 | default:
|
---|
947 | Error("THIS SHOULD NEVER BE REACHED. dataLogger.cc ln 962.");
|
---|
948 | };
|
---|
949 | sub.dailyFile.InitCol(colName.str().c_str(), dataQualifier.str().c_str(), dataPointer);
|
---|
950 | sub.runFile.InitCol(colName.str().c_str(), dataQualifier.str().c_str(), dataPointer);
|
---|
951 | }
|
---|
952 |
|
---|
953 | //TODO init the attributes
|
---|
954 | }
|
---|
955 | // --------------------------------------------------------------------------
|
---|
956 | //
|
---|
957 | //! write a dimInfo data to its corresponding FITS files
|
---|
958 | //
|
---|
959 | void DataLogger::WriteToFITS(SubscriptionType& sub)
|
---|
960 | {
|
---|
961 | //dailyFile status (open or not) already checked
|
---|
962 | if (sub.dailyFile.IsOpen())
|
---|
963 | sub.dailyFile.Write();
|
---|
964 | if (sub.runFile.IsOpen())
|
---|
965 | sub.runFile.Write();
|
---|
966 | }
|
---|
967 | #endif //if has_fits
|
---|
968 | // --------------------------------------------------------------------------
|
---|
969 | //
|
---|
970 | //! Implements the StartRun transition.
|
---|
971 | //! Concatenates the given path for the run file and the filename itself (based on the run number),
|
---|
972 | //! and tries to open it.
|
---|
973 | //! @returns
|
---|
974 | //! kSM_Logging if success, kSM_BadRunConfig if failure.
|
---|
975 | int DataLogger::StartRunPlease()
|
---|
976 | {
|
---|
977 | //attempt to open run file with current parameters
|
---|
978 | if (fRunNumber == -1)
|
---|
979 | return kSM_BadRunConfig;
|
---|
980 | std::stringstream sRun;
|
---|
981 | sRun << fRunNumber;
|
---|
982 | std::string fullName = fRunFileName + '/' + sRun.str() + ".log";
|
---|
983 | fRunLogFile.open(fullName.c_str(), std::ios_base::out | std::ios_base::app); //maybe should be app instead of ate
|
---|
984 |
|
---|
985 | fullName = fRunFileName + '/' + sRun.str() + ".rep";
|
---|
986 | fRunReportFile.open(fullName.c_str(), std::ios_base::out | std::ios_base::app);
|
---|
987 |
|
---|
988 | if (!fRunLogFile.is_open() || !fRunReportFile.is_open())
|
---|
989 | {
|
---|
990 | //TODO send an error message
|
---|
991 | return kSM_BadRunConfig;
|
---|
992 | }
|
---|
993 |
|
---|
994 | return kSM_Logging;
|
---|
995 | }
|
---|
996 | // --------------------------------------------------------------------------
|
---|
997 | //
|
---|
998 | //! Implements the StopRun transition.
|
---|
999 | //! Attempts to close the run file.
|
---|
1000 | //! @returns
|
---|
1001 | //! kSM_WaitingRun if success, kSM_FatalError otherwise
|
---|
1002 | int DataLogger::StopRunPlease()
|
---|
1003 | {
|
---|
1004 | if (!fRunLogFile.is_open() || !fRunReportFile.is_open())
|
---|
1005 | return kSM_FatalError;
|
---|
1006 |
|
---|
1007 | fRunLogFile.close();
|
---|
1008 | fRunReportFile.close();
|
---|
1009 | #ifdef HAS_FITS
|
---|
1010 | for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
|
---|
1011 | for (std::map<std::string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
|
---|
1012 | {
|
---|
1013 | if (j->second.runFile.IsOpen())
|
---|
1014 | j->second.runFile.Close();
|
---|
1015 | }
|
---|
1016 | #endif
|
---|
1017 | return kSM_WaitingRun;
|
---|
1018 |
|
---|
1019 | }
|
---|
1020 | // --------------------------------------------------------------------------
|
---|
1021 | //
|
---|
1022 | //! Implements the Stop and Reset transitions.
|
---|
1023 | //! Attempts to close any openned file.
|
---|
1024 | //! @returns
|
---|
1025 | //! kSM_Ready
|
---|
1026 | int DataLogger::GoToReadyPlease()
|
---|
1027 | {
|
---|
1028 | if (fDailyLogFile.is_open())
|
---|
1029 | fDailyLogFile.close();
|
---|
1030 | if (fDailyReportFile.is_open())
|
---|
1031 | fDailyReportFile.close();
|
---|
1032 |
|
---|
1033 | if (fRunLogFile.is_open())
|
---|
1034 | fRunLogFile.close();
|
---|
1035 | if (fRunReportFile.is_open())
|
---|
1036 | fRunReportFile.close();
|
---|
1037 |
|
---|
1038 | #ifdef HAS_FITS
|
---|
1039 | for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
|
---|
1040 | for (std::map<std::string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
|
---|
1041 | {
|
---|
1042 | if (j->second.dailyFile.IsOpen())
|
---|
1043 | j->second.dailyFile.Close();
|
---|
1044 | if (j->second.runFile.IsOpen())
|
---|
1045 | j->second.runFile.Close();
|
---|
1046 | }
|
---|
1047 | #endif
|
---|
1048 | return kSM_Ready;
|
---|
1049 | }
|
---|
1050 | // --------------------------------------------------------------------------
|
---|
1051 | //
|
---|
1052 | //! Implements the transition towards kSM_WaitingRun
|
---|
1053 | //! Does nothing really.
|
---|
1054 | //! @returns
|
---|
1055 | //! kSM_WaitingRun
|
---|
1056 | int DataLogger::DailyToWaitRunPlease()
|
---|
1057 | {
|
---|
1058 | return kSM_WaitingRun;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | // --------------------------------------------------------------------------
|
---|
1062 |
|
---|
1063 | int RunDim(Configuration &conf)
|
---|
1064 | {
|
---|
1065 | WindowLog wout;
|
---|
1066 |
|
---|
1067 | //log.SetWindow(stdscr);
|
---|
1068 | if (conf.Has("log"))
|
---|
1069 | if (!wout.OpenLogFile(conf.Get<std::string>("log")))
|
---|
1070 | wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<std::string>("log") << ": " << strerror(errno) << std::endl;
|
---|
1071 |
|
---|
1072 | // Start io_service.Run to use the StateMachineImp::Run() loop
|
---|
1073 | // Start io_service.run to only use the commandHandler command detaching
|
---|
1074 | DataLogger logger(wout);
|
---|
1075 | logger.Run(true);
|
---|
1076 |
|
---|
1077 | return 0;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | template<class T>
|
---|
1081 | int RunShell(Configuration &conf)
|
---|
1082 | {
|
---|
1083 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
1084 |
|
---|
1085 | WindowLog &win = shell.GetStreamIn();
|
---|
1086 | WindowLog &wout = shell.GetStreamOut();
|
---|
1087 |
|
---|
1088 | if (conf.Has("log"))
|
---|
1089 | if (!wout.OpenLogFile(conf.Get<std::string>("log")))
|
---|
1090 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<std::string>("log") << ": " << strerror(errno) << std::endl;
|
---|
1091 |
|
---|
1092 | DataLogger logger(wout);
|
---|
1093 |
|
---|
1094 | shell.SetReceiver(logger);
|
---|
1095 |
|
---|
1096 | logger.SetReady();
|
---|
1097 | shell.Run(); // Run the shell
|
---|
1098 | logger.SetNotReady();
|
---|
1099 |
|
---|
1100 | return 0;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | void SetupConfiguration(Configuration &conf)
|
---|
1104 | {
|
---|
1105 | const std::string n = conf.GetName()+".log";
|
---|
1106 |
|
---|
1107 | po::options_description config("Program options");
|
---|
1108 | config.add_options()
|
---|
1109 | ("dns", var<std::string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
---|
1110 | ("log,l", var<std::string>(n), "Write log-file")
|
---|
1111 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
1112 | ;
|
---|
1113 |
|
---|
1114 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
1115 |
|
---|
1116 | conf.AddOptions(config);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | int main(int argc, char* argv[])
|
---|
1120 | {
|
---|
1121 | Configuration conf(argv[0]);
|
---|
1122 | SetupConfiguration(conf);
|
---|
1123 |
|
---|
1124 | po::variables_map vm;
|
---|
1125 | try
|
---|
1126 | {
|
---|
1127 | vm = conf.Parse(argc, argv);
|
---|
1128 | }
|
---|
1129 | #if BOOST_VERSION > 104000
|
---|
1130 | catch (po::multiple_occurrences &e)
|
---|
1131 | {
|
---|
1132 | std::cout << "Error: " << e.what() << " of '" << e.get_option_name() << "' option." << std::endl;
|
---|
1133 | std::cout << std::endl;
|
---|
1134 | return -1;
|
---|
1135 | }
|
---|
1136 | #endif
|
---|
1137 | catch (std::exception &e)
|
---|
1138 | {
|
---|
1139 | std::cout << "Error: " << e.what() << std::endl;
|
---|
1140 | std::cout << std::endl;
|
---|
1141 |
|
---|
1142 | return -1;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | if (conf.HasHelp() || conf.HasPrint())
|
---|
1146 | return -1;
|
---|
1147 |
|
---|
1148 | // To allow overwriting of DIM_DNS_NODE set 0 to 1
|
---|
1149 | setenv("DIM_DNS_NODE", conf.Get<std::string>("dns").c_str(), 1);
|
---|
1150 |
|
---|
1151 | try
|
---|
1152 | {
|
---|
1153 | // No console access at all
|
---|
1154 | if (!conf.Has("console"))
|
---|
1155 | return RunDim(conf);
|
---|
1156 |
|
---|
1157 | // Cosole access w/ and w/o Dim
|
---|
1158 | if (conf.Get<int>("console")==0)
|
---|
1159 | return RunShell<LocalShell>(conf);
|
---|
1160 | else
|
---|
1161 | return RunShell<LocalConsole>(conf);
|
---|
1162 | }
|
---|
1163 | catch (std::exception& e)
|
---|
1164 | {
|
---|
1165 | cerr << "Exception: " << e.what() << endl;
|
---|
1166 | return -1;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | return 0;
|
---|
1170 | }
|
---|