source: trunk/FACT++/src/datalogger.cc@ 14538

Last change on this file since 14538 was 14438, checked in by tbretz, 12 years ago
Removed some obsolete arguments.
File size: 74.7 KB
Line 
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 // FIXME FIXME: Error states missing...
13 digraph datalogger
14 {
15 node [shape=record, fontname=Helvetica, fontsize=10];
16
17 srt [label="Start" style="rounded"]
18 rdy [label="Ready"]
19 nop [label="NightlyOpen"]
20 wait [label="WaitingRun"]
21 log [label="Logging"]
22
23 //e [label="Error" color="red"];
24 //c [label="BadFolder" color="red"]
25
26
27 cmd_start [label="START" shape="none" height="0"]
28 cmd_stop [label="STOP" shape="none" height="0"]
29 cmd_stopr [label="STOP_RUN_LOGGING" shape="none" height="0"]
30 cmd_startr [label="START_RUN_LOGGING" shape="none" height="0"]
31
32 { rank=same; cmd_startr cmd_stopr }
33 { rank=same; cmd_start cmd_stop }
34
35
36 srt -> rdy
37
38 rdy -> cmd_start [ arrowhead="open" dir="both" arrowtail="tee" weight=10 ]
39 cmd_start -> nop
40
41 nop -> cmd_stop [ arrowhead="none" dir="both" arrowtail="inv" ]
42 wait -> cmd_stop [ arrowhead="none" dir="both" arrowtail="inv" ]
43 log -> cmd_stop [ arrowhead="none" dir="both" arrowtail="inv" ]
44 cmd_stop -> rdy
45
46 wait -> cmd_stopr [ arrowhead="none" dir="both" arrowtail="inv" ]
47 log -> cmd_stopr [ arrowhead="none" dir="both" arrowtail="inv" ]
48 cmd_stopr -> nop
49
50 nop -> cmd_startr [ arrowhead="none" dir="both" arrowtail="inv" weight=10 ]
51 rdy -> cmd_startr [ arrowhead="none" dir="both" arrowtail="inv" ]
52 cmd_startr -> wait [ weight=10 ]
53
54
55 wait -> log
56 log -> wait
57 }
58 \enddot
59
60 For questions or bug report, please contact Etienne Lyard (etienne.lyard@unige.ch) or Thomas Bretz.
61 */
62 //****************************************************************
63#include <unistd.h> //for getting stat of opened files
64//#include <sys/statvfs.h> //for getting disk free space
65//#include <sys/stat.h> //for getting files sizes
66#include <fstream>
67#include <functional>
68
69#include <boost/filesystem.hpp>
70
71#include "Dim.h"
72#include "Event.h"
73#include "StateMachineDim.h"
74#include "LocalControl.h"
75#include "Configuration.h"
76#include "Converter.h"
77#include "DimWriteStatistics.h"
78
79#include "Description.h"
80#include "DimNetwork.h"
81
82#ifdef HAVE_FITS
83#include "Fits.h"
84#endif
85
86#include "DimState.h"
87
88//Dim structures
89///distributes the number of opened subscriptions and fits files
90struct NumSubAndFitsType {
91 uint32_t numSubscriptions;
92 uint32_t numOpenFits;
93};
94///distributes which files were opened.
95struct OpenFileToDim {
96 uint32_t code;
97 char fileName[FILENAME_MAX];
98};
99
100///Run number record. Used to keep track of which run numbers are still active
101struct RunNumberType {
102
103 ///the actual run number
104 int32_t runNumber;
105 ///the time at which the run number was received
106 Time time;
107 ///default constructor
108 RunNumberType()
109 {
110 runNumber = 0;
111 }
112 ///default destructor
113 ~RunNumberType()
114 {
115
116 }
117};
118
119EventImp nullEventImp;
120///Dim subscription type. Stores all the relevant info to handle a Dim subscription
121struct SubscriptionType
122{
123#ifdef HAVE_FITS
124 ///Nightly FITS output file
125 Fits nightlyFile;
126#endif
127 ///the server
128 string server;
129 ///the service
130 string service;
131 ///the converter for outputting the data according to the format
132 shared_ptr<Converter> fConv;
133 ///the current run number used by this subscription
134 int32_t runNumber;
135 ///time of the latest received event
136 Time lastReceivedEvent;
137 ///whether or not the fits buffer was allocated already
138 bool fitsBufferAllocated;
139 ///the actual dimInfo pointer (must be the last in the list to ensure
140 /// that it is the first which is deleted -- and consequently none of
141 /// the other members can still be in use in an infoHandler)
142 //DIM_REPLACE
143 //shared_ptr<DimStampedInfo> dimInfo;
144 unsigned int index;
145
146 ///Dim info constructor
147 //DIM_REPLACE
148// SubscriptionType(DimStampedInfo* info=NULL)
149 SubscriptionType()
150 {
151 fConv = shared_ptr<Converter>();
152 runNumber = 0;
153 lastReceivedEvent = Time::None;
154 fitsBufferAllocated = false;
155 // Should be the last instantiated to make sure that all other
156 // variables which might be used are already initialized
157 //DIM_REPLACE
158 //dimInfo = shared_ptr<DimStampedInfo>(info);
159 index = 0;
160 }
161 ///default destructor
162 ~SubscriptionType()
163 {
164 }
165};
166
167class DataLogger : public StateMachineDim
168//DIM_REPLACE
169//, DimServiceInfoListImp
170{
171public:
172 /// The list of existing states specific to the DataLogger
173 enum
174 {
175 kSM_NightlyOpen = 20, ///< Nightly file openned and writing
176 kSM_WaitingRun = 30, ///< waiting for the run number to open the run file
177 kSM_Logging = 40, ///< both files openned and writing
178 kSM_BadFolder = 0x101, ///< the folder specified for Nightly logging does not exist or has bad permissions
179 kSM_RunWriteError = 0x103, ///< Denotes that an error occured while writing a run file (text or fits).
180 kSM_DailyWriteError = 0x103, ///< Denots that an error occured while writing a daily file (text or fits).
181 } localstates_t;
182
183 DataLogger(ostream &out);
184 ~DataLogger();
185
186 int EvalOptions(Configuration& conf);
187
188private:
189 /************************************************
190 * MEMBER VARIABLES
191 ************************************************/
192 /// ofstream for the NightlyLogfile
193 ofstream fNightlyLogFile;
194 /// ofstream for the Nightly report file
195 ofstream fNightlyReportFile;
196 /// base path of files
197 string fFilePath;
198 ///run numbers
199 list<RunNumberType> fRunNumber;
200 ///old run numbers time-out delay (in seconds)
201 uint32_t fRunNumberTimeout;
202 ///previous run number. to check if changed while logging
203 int fPreviousRunNumber;
204 ///Current Service Quality
205 int fQuality;
206 ///Modified Julian Date
207 double fMjD;
208 ///for obtaining the name of the existing services
209// ServiceList fServiceList;
210 typedef map<const string, map<string, SubscriptionType> > SubscriptionsListType;
211 ///All the services to which we have subscribed to, sorted by server name.
212 SubscriptionsListType fServiceSubscriptions;
213 ///full name of the nightly log file
214 string fFullNightlyLogFileName;
215 ///full name of the nightly report file
216 string fFullNightlyReportFileName;
217 ///variable to track when the statistic were last calculated
218// Time fPreviousStatsUpdateTime;
219 Time fPreviousOldRunNumberCheck;
220 ///boolean to know whether we should close and reopen daily files or not
221 bool fDailyFileDayChangedAlready;
222
223 DimWriteStatistics fFilesStats;
224
225 ///map and mutex for storing services description
226 map<string, vector<Description> > fServiceDescriptionList;
227 mutex fMutex;
228 int HandleDescriptions(DimDescriptions* desc);
229 vector<Description> GetDescription(const string& server, const string& service);
230private:
231 /***************************************************
232 * DIM INFO HANDLER
233 ***************************************************/
234 //overloading of DIM's infoHandler function
235 int infoCallback(const EventImp& evt, unsigned int infoIndex);
236
237 /***************************************************
238 * TRANSITION FUNCTIONS
239 ***************************************************/
240 ///Reporting method for the services info received
241 void Report(const EventImp& evt, SubscriptionType& sub);
242
243 ///Configuration of the nightly file path
244 int ConfigureFilePath(const Event& evt);
245 ///print the current state of the dataLogger
246 int PrintState(const Event& evt);
247 ///checks whether or not the current info being treated is a run number
248 void CheckForRunNumber(const EventImp& evt, unsigned int index);
249 /// start transition
250 int Start();
251 ///from waiting to logging transition
252 //int StartRun();
253 // from logging to waiting transition
254 int StopRunLogging();
255 ///stop and reset transition
256 int GoToReady();
257 ///from NightlyOpen to waiting transition
258 int NightlyToWaitRun();
259 ///from wait for run number to nightly open
260 int BackToNightlyOpen();
261#ifdef HAVE_FITS
262 ///Open fits files
263 void OpenFITSFiles(SubscriptionType& sub);
264 ///Write data to FITS files
265 void WriteToFITS(SubscriptionType& sub, const void* data);
266 ///Allocate the buffers required for fits
267 void AllocateFITSBuffers(SubscriptionType& sub);
268#endif//has_fits
269
270 /***************************************
271 * DIM SERVICES PROVIDED BY THE DATA LOGGER
272 ***************************************/
273 ///monitoring notification loop
274 void ServicesMonitoring();
275 inline void NotifyOpenedFile(const string &name, int type, DimDescribedService* service);
276 ///Service for opened files
277 DimDescribedService* fOpenedNightlyFiles;
278 DimDescribedService* fOpenedRunFiles;
279 DimDescribedService* fNumSubAndFits;
280 NumSubAndFitsType fNumSubAndFitsData;
281
282 /***************************************************
283 * DATA LOGGER's CONFIGURATION STUFF
284 ***************************************************/
285 ///black/white listing
286 set<string> fBlackList;
287 set<string> fWhiteList;
288 ///list of services to be grouped
289 set<string> fGrouping;
290 ///configuration flags
291 bool fDebugIsOn;
292 bool fOpenedFilesIsOn;
293 bool fNumSubAndFitsIsOn;
294 //functions for controlling the services behavior
295 int SetDebugOnOff(const Event& evt);
296 int SetStatsPeriod(const Event& evt);
297 int SetOpenedFilesOnOff(const Event& evt);
298 int SetNumSubsAndFitsOnOff(const Event& evt);
299 int SetRunTimeoutDelay(const Event& evt);
300
301 ///boolean to prevent DIM update while desctructing the dataLogger
302 bool fDestructing;
303 /***************************************************
304 * UTILITIES
305 ***************************************************/
306 ///vectors to keep track of opened Fits files, for grouping purposes.
307 map<string, vector<string> > fOpenedNightlyFits;
308 ///creates a group fits file based on a list of files to be grouped
309 void CreateFitsGrouping(map<string, vector<string> >& filesToGroup);
310
311 bool OpenStreamImp(ofstream &stream, const string &filename, bool mightbeopen);
312 bool OpenStream(shared_ptr<ofstream> stream, const string &filename);
313 ///Open the relevant text files related to a particular run
314 int OpenRunFile(RunNumberType& run);
315 ///add a new run number
316 void AddNewRunNumber(int64_t newRun, Time time);
317 std::vector<int64_t> previousRunNumbers;
318 ///removes the oldest run number, and close the relevant files.
319 void RemoveOldestRunNumber();
320 ///retrieves the size of a file
321 off_t GetFileSize(const string&);
322 ///Get the digits of year, month and day for filenames and paths
323 void GetYearMonthDayForFiles(unsigned short& year, unsigned short& month, unsigned short& day);
324 ///Appends the relevant year month day to a given path
325 void AppendYearMonthDaytoPath(string& path);
326 ///Form the files path
327 string CompileFileNameWithPath(const string &path, const string &service, const string & extension);
328 ///Form the file names only
329 string CompileFileName(const string& service, const string& extension, const Time& time=Time()) const;
330 ///Check whether service is in black and/or white list
331 bool ShouldSubscribe(const string& server, const string& service);
332 ///Subscribe to a given server and service
333// EventImp& SubscribeTo(const string& server, const string& service);
334 ///Open a text file and checks for ofstream status
335 bool OpenTextFile(ofstream& stream, const string& name);
336 ///Checks if the input osftream is in error state, and if so close it.
337 bool CheckForOfstreamError(ofstream& out, bool isDailyStream);
338 ///Goes to Write error states
339 void GoToRunWriteErrorState();
340 void GoToNightlyWriteErrorState();
341 ///Checks if a given path exist
342 bool DoesPathExist(string path);
343 ///Check if old run numbers can be trimmed, and if so, do it
344 void TrimOldRunNumbers();
345 ///Create a given directory
346 bool CreateDirectory(string path);
347 /***************************************************
348 * INHERITED FROM DimServiceInfoList
349 ***************************************************/
350 ///Add a new server subscription
351 void AddServer(const string& server);
352 ///Add a new service subscription
353 void AddService(const Service& svc);
354 ///Remove a given service subscription
355 //FIXME unused
356 void RemoveService(const string, const string, bool);
357 ///Remove all the services associated with a given server
358 //FIXME unused
359 void RemoveAllServices(const string&);
360 ///pointer to the dim's subscription that should distribute the run numbers.
361 //DIM_REPLACE
362 //DimInfo* fRunNumberService;
363 unsigned int fRunNumberService;
364 /***************************************************
365 * Overwritten from MessageImp
366 ***************************************************/
367 vector<string> backLogBuffer;
368 bool shouldBackLog;
369 bool fShouldAutoStart;
370 bool fAutoStarted;
371
372 //Current day variable. Used to close nightly files when night changes
373 int fCurrentDay;
374 Time lastFlush;
375
376 DimDnsServiceList fDimList;
377 vector<DimDescriptions*> fServerDescriptionsList;
378
379 //counter for keeping tracker of services
380 unsigned int servicesCounter;
381public:
382 int Write(const Time &time, const std::string &txt, int qos=kMessage);
383
384}; //DataLogger
385
386
387/**
388 * @brief the two methods below were copied from StateMachineDimControl.cc
389 *
390 */
391int DataLogger::HandleDescriptions(DimDescriptions* desc)
392{
393 fMutex.lock();
394 for (auto it=desc->descriptions.begin(); it != desc->descriptions.end(); it++) {
395 if (fDebugIsOn)
396 {
397 Debug("Adding description for service: " + it->front().name);
398 }
399 fServiceDescriptionList[it->front().name].assign(it->begin(), it->end());
400 }
401 fMutex.unlock();
402
403 return GetCurrentState();
404}
405vector<Description> DataLogger::GetDescription(const string& server, const string& service)
406{
407 const lock_guard<mutex> guard(fMutex);
408 const auto it = fServiceDescriptionList.find(server+"/"+service);
409 return it==fServiceDescriptionList.end()?vector<Description>():it->second;
410}
411// --------------------------------------------------------------------------
412//
413//! Overwritten write function. This way we directly log the datalogger's messages, without going through dim's dns,
414//! thus increasing robustness.
415//! @param time: see MessageImp class param
416//! @param txt: see MessageImp class param
417//! @param qos: see MessageImp class param
418//! @return see MessageImp class param
419//
420int DataLogger::Write(const Time&time, const std::string& txt, int qos)
421{
422 ostringstream ss;
423 ss << "datalogger: " << txt;
424 if (fNightlyLogFile.is_open())
425 {
426 MessageImp mimp(fNightlyLogFile);
427 mimp.Write(time, ss.str(), qos);
428 }
429 else if (shouldBackLog)
430 {
431 ostringstream str;
432 MessageImp mimp(str);
433 mimp.Write(time, ss.str(), qos);
434 backLogBuffer.push_back(str.str());
435 }
436 return StateMachineDim::Write(time, ss.str(), qos);
437}
438// --------------------------------------------------------------------------
439//
440//! Check if a given path exists
441//! @param path the path to be checked
442//! @return whether or not the creation has been successfull
443//
444bool DataLogger::CreateDirectory(string path)
445{
446 try
447 {
448 DimWriteStatistics::CreateDirectory(path);
449 return true;
450 }
451 catch (const runtime_error &e)
452 {
453 Error(e.what());
454 return false;
455 }
456}
457// --------------------------------------------------------------------------
458//
459//! Check if a given path exists
460//! @param path the path to be checked
461//! @return whether or not the given path exists
462//
463bool DataLogger::DoesPathExist(string path)
464{
465 return DimWriteStatistics::DoesPathExist(path, *this);
466}
467
468
469void DataLogger::AddServer(const string& server)
470{
471 Info("Got request to add server " + server );
472 if (server != "DIS_DNS")
473 {
474 DimDescriptions* d = new DimDescriptions(server);
475 d->SetCallbackDescriptions(bind(&DataLogger::HandleDescriptions, this, d));
476 d->Subscribe(*this);
477 fServerDescriptionsList.push_back(d);
478 }
479
480}
481
482// --------------------------------------------------------------------------
483//
484//! Add a new service subscription
485//! @param server the server for which the subscription should be created
486//! @param service the service for which the subscription should be created
487//! @param isCmd whether this is a Dim Command or not. Commands are not logged
488//
489void DataLogger::AddService(const Service& svc)
490{
491 const string& server = svc.server;
492 const string& service = svc.service;
493 const bool isCmd = svc.iscmd;
494
495 //dataLogger does not subscribe to commands
496 if (isCmd)
497 return;
498
499 Info("Got request to add service: "+server+"/"+service);
500
501 //check the given subscription against black and white lists
502 if (!ShouldSubscribe(server, service))
503 return;
504
505 map<string, SubscriptionType> &list = fServiceSubscriptions[server];
506
507 if (list.find(service) != list.end())
508 {
509 // Error("Service " + server + "/" + service + " is already in the dataLogger's list... ignoring update.");
510 return;
511 }
512 //DIM_REPLACE
513// list[service].dimInfo.reset(SubscribeTo(server, service));
514 if (fDebugIsOn)
515 Debug("Subscribing to service "+server+"/"+service);
516 Subscribe(server + "/" + service)
517 (bind(&DataLogger::infoCallback, this, placeholders::_1, servicesCounter));
518 list[service].server = server;
519 list[service].service = service;
520 list[service].index = servicesCounter;
521 fNumSubAndFitsData.numSubscriptions++;
522 //check if this is the run numbers service
523 if ((server == "FAD_CONTROL") && (service == "START_RUN"))
524 fRunNumberService = servicesCounter;
525 servicesCounter++;
526 Info("Added subscription to " + server + "/" + service);
527}
528// --------------------------------------------------------------------------
529//
530//! Remove a given service subscription
531//! @param server the server for which the subscription should be removed
532//! @param service the service that should be removed
533//! @param isCmd whether or not this is a command
534//
535void DataLogger::RemoveService(string server, string service, bool isCmd)
536{
537
538 Info("Got request to remove service: "+server+"/"+service);
539 if (fDestructing)//this function is called by the super class, after the destructor has deleted its own subscriptions
540 return;
541//FIXME unused
542 return;
543
544 if (isCmd)
545 return;
546
547 if (fServiceSubscriptions.find(server) == fServiceSubscriptions.end())
548 {
549 Error("Request to remove service "+service+" from server "+server+", but service not found.");
550 return;
551 }
552
553 if (fServiceSubscriptions[server].erase(service) != 1)
554 {
555 //check the given subscription against black and white lists
556 if (!ShouldSubscribe(server, service))
557 return;
558
559 Error("Subscription "+server+"/"+service+" could not be removed as it is not present");
560 return;
561 }
562 fNumSubAndFitsData.numSubscriptions--;
563
564 if ((server == "FAD_CONTROL") && (service == "START_RUN"))
565 fRunNumberService = 0;
566
567 Info("Removed subscription to " + server + "/" + service);
568}
569// --------------------------------------------------------------------------
570//
571//! Remove all the services associated with a given server
572//! @param server the server for which all the services should be removed
573//
574void DataLogger::RemoveAllServices(const string& server)
575{
576 Info("Got request for removing all services from: "+server);
577 if (fServiceSubscriptions.find(server)==fServiceSubscriptions.end())
578 {
579 Warn("Request to remove all services, but corresponding server " + server + " not found.");
580 return;
581 }
582//FIXME unused
583 return;
584 fNumSubAndFitsData.numSubscriptions -= fServiceSubscriptions[server].size();
585
586 fServiceSubscriptions[server].clear();
587 fServiceSubscriptions.erase(server);
588
589 if (server == "FAD_CONTROL")
590 fRunNumberService = 0;
591
592 if (fDebugIsOn)
593 Debug("Removed all subscriptions to " + server + "/");
594}
595
596// --------------------------------------------------------------------------
597//
598//! Checks if the given ofstream is in error state and if so, close it
599//! @param out the ofstream that should be checked
600//
601bool DataLogger::CheckForOfstreamError(ofstream& out, bool isDailyStream)
602{
603 if (out.good())
604 return true;
605
606 Error("An error occured while writing to a text file. Closing it");
607 if (out.is_open())
608 out.close();
609 if (isDailyStream)
610 GoToNightlyWriteErrorState();
611 else
612 GoToRunWriteErrorState();
613
614 return false;
615}
616
617bool DataLogger::OpenStreamImp(ofstream &stream, const string &filename, bool mightbeopen)
618{
619 if (stream.is_open())
620 {
621 if (!mightbeopen)
622 Error(filename+" was already open when trying to open it.");
623 return mightbeopen;
624 }
625
626 errno = 0;
627 stream.open(filename.c_str(), ios_base::out | ios_base::app);
628 if (!stream /*|| errno!=0*/)
629 {
630 ostringstream str;
631 str << "ofstream::open() failed for '" << filename << "': " << strerror(errno) << " [errno=" << errno << "]";
632 Error(str);
633 return false;
634 }
635
636 if (!stream.is_open())
637 {
638 Error("File "+filename+" not open as it ought to be.");
639 return false;
640 }
641
642 Info("Opened: "+filename);
643
644 return true;
645}
646
647bool DataLogger::OpenStream(shared_ptr<ofstream> stream, const string &filename)
648{
649 return OpenStreamImp(*stream, filename, false);
650}
651
652// --------------------------------------------------------------------------
653//
654//! Open a text file and checks for error code
655//! @param stream the ofstream for which the file should be opened
656//! @name the file name
657//
658bool DataLogger::OpenTextFile(ofstream& stream, const string& name)
659{
660 return OpenStreamImp(stream, name, true);
661}
662
663// --------------------------------------------------------------------------
664//
665//! Create a new dim subscription to a given server and service
666//! @param server the server name
667//! @param service the service name
668//
669/*EventImp& DataLogger::SubscribeTo(const string& server, const string& service)
670{
671
672 //DIM_REPLACE
673 //return new DimStampedInfo((server + "/" + service).c_str(), (void*)NULL, 0, this);
674 EventImp& newSubscription = Subscribe(server + "/" + service);
675 newSubscription.bind(&infoHandler, this, placeholders::_1);
676 return newSubscription;
677}*/
678// --------------------------------------------------------------------------
679//
680//! Check whether a service should be subscribed to, based on the black/white list entries
681//! @param server the server name associated with the service being checked
682//! @param service the service name associated with the service being checked
683//
684bool DataLogger::ShouldSubscribe(const string& server, const string& service)
685{
686 if ((fBlackList.find(server + "/") != fBlackList.end()) ||
687 (fBlackList.find(server + "/" + service) != fBlackList.end()) ||
688 (fBlackList.find("/" + service) != fBlackList.end()))
689 {
690 if (fWhiteList.size()>0 &&
691 (fWhiteList.find(server + "/" + service) != fWhiteList.end()))
692 {
693 if (fDebugIsOn)
694 Debug("White list saved service " + server + "/" + service + " from blacklisting");
695 return true;
696 }
697 if (fDebugIsOn)
698 Debug("Blacklist banned service " + server + "/" + service);
699 return false;
700 }
701 return true;
702}
703// --------------------------------------------------------------------------
704//
705//! Compiles a file name
706//! @param path the base path where to put the file
707//! @param time the time at which the file is created
708//! @param service the service name, if any
709//! @param extension the extension to add, if any
710//
711string DataLogger::CompileFileName(const string& service, const string& extension, const Time& time) const
712{
713 ostringstream str;
714
715 const Time ftime(time);//removed this as already done by nightAsInt: -boost::posix_time::hours(12));
716 str << ftime.NightAsInt();
717
718 if (!service.empty())
719 str << '.' << service;
720
721 if (!extension.empty())
722 str << "." << extension;
723
724 return str.str();
725}
726
727string DataLogger::CompileFileNameWithPath(const string& path, const string& service, const string& extension)
728{
729 ostringstream str;
730
731 const Time time;
732
733 //calculate time suitable for naming files.
734 const Time ftime = time-boost::posix_time::hours(12);
735
736 //output it
737 str << path << ftime.GetAsStr("/%Y/%m/%d");
738
739 //check if target directory exist
740 if (!DoesPathExist(str.str()))
741 CreateDirectory(str.str());
742
743 str << '/' << CompileFileName(service, extension, time);
744
745 return str.str();
746
747
748}
749
750// --------------------------------------------------------------------------
751//
752//!retrieves the size on disk of a file
753//! @param fileName the full file name for which the size on disk should be retrieved
754//! @return the size of the file on disk, in bytes. 0 if the file does not exist or if an error occured
755//
756off_t DataLogger::GetFileSize(const string& fileName)
757{
758 return DimWriteStatistics::GetFileSizeOnDisk(fileName, *this);
759}
760
761// --------------------------------------------------------------------------
762//
763//! Removes the oldest run number and closes the fits files that should be closed
764//! Also creates the fits grouping file
765//
766void DataLogger::RemoveOldestRunNumber()
767{
768 if (fDebugIsOn)
769 {
770 ostringstream str;
771 str << "Removing run number " << fRunNumber.front().runNumber;
772 Debug(str);
773 }
774 //remove the entry
775 fRunNumber.pop_front();
776}
777
778// --------------------------------------------------------------------------
779//
780//! Default constructor. The name of the machine is given DATA_LOGGER
781//! and the state is set to kSM_Ready at the end of the function.
782//
783//!Setup the allows states, configs and transitions for the data logger
784//
785DataLogger::DataLogger(ostream &out) : StateMachineDim(out, "DATA_LOGGER"),
786 fFilesStats("DATA_LOGGER", *this)
787{
788 shouldBackLog = true;
789
790 servicesCounter=1;
791
792 //initialize member data
793 fFilePath = ".";
794
795 fDimList.Subscribe(*this);
796 fDimList.SetCallbackServerAdd(bind(&DataLogger::AddServer, this, placeholders::_1));
797 fDimList.SetCallbackServiceAdd(bind(&DataLogger::AddService, this, placeholders::_1));
798
799 //calculate time "centered" around noon instead of midnight
800 const Time timeNow;
801 const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
802 fCurrentDay = nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
803 lastFlush = Time();
804
805 //Give a name to this machine's specific states
806 AddStateName(kSM_NightlyOpen, "NightlyFileOpen", "The summary files for the night are open.");
807 AddStateName(kSM_WaitingRun, "WaitForRun", "The summary files for the night are open and we wait for a run to be started.");
808 AddStateName(kSM_Logging, "Logging", "The summary files for the night and the files for a single run are open.");
809 AddStateName(kSM_BadFolder, "ErrInvalidFolder", "The folder for the files is not invalid.");
810 AddStateName(kSM_DailyWriteError, "ErrDailyWrite", "An error occured while writing to a daily (and run) file.");
811 AddStateName(kSM_RunWriteError, "ErrRunWrite", "An error occured while writing to a run file.");
812
813 // Add the possible transitions for this machine
814 AddEvent("START", kSM_Ready, kSM_BadFolder)
815 (bind(&DataLogger::Start, this))
816 ("Start the nightly logging. Nightly file location must be specified already");
817
818 AddEvent("STOP", kSM_NightlyOpen, kSM_WaitingRun, kSM_Logging, kSM_DailyWriteError, kSM_RunWriteError)
819 (bind(&DataLogger::GoToReady, this))
820 ("Stop all data logging, close all files.");
821
822 AddEvent("RESET", kSM_Error, kSM_BadFolder, kSM_DailyWriteError, kSM_RunWriteError)
823 (bind(&DataLogger::GoToReady, this))
824 ("Transition to exit error states. Closes the any open file.");
825
826 AddEvent("START_RUN_LOGGING", /*kSM_Logging,*/ kSM_NightlyOpen, kSM_Ready)
827 (bind(&DataLogger::NightlyToWaitRun, this))
828 ("Go to waiting for run number state. In this state with any received run-number a new file is opened.");
829
830 AddEvent("STOP_RUN_LOGGING", kSM_WaitingRun, kSM_Logging)
831 (bind(&DataLogger::BackToNightlyOpen, this))
832 ("Go from the wait for run to nightly open state.");
833
834 // Provide a print command
835 AddEvent("PRINT_INFO")
836 (bind(&DataLogger::PrintState, this, placeholders::_1))
837 ("Print information about the internal status of the data logger.");
838
839 OpenFileToDim fToDim;
840 fToDim.code = 0;
841 fToDim.fileName[0] = '\0';
842
843 fOpenedNightlyFiles = new DimDescribedService(GetName() + "/FILENAME_NIGHTLY", "I:1;C", fToDim,
844 "Path and base name used for the nightly files."
845 "|Type[int]:type of open files (1=log, 2=rep, 4=fits)"
846 "|Name[string]:path and base file name");
847
848 fOpenedRunFiles = new DimDescribedService(GetName() + "/FILENAME_RUN", "I:1;C", fToDim,
849 "Path and base name used for the run files."
850 "|Type[int]:type of open files (1=log, 2=rep, 4=fits)"
851 "|Name[string]:path and base file name");
852
853 fNumSubAndFitsData.numSubscriptions = 0;
854 fNumSubAndFitsData.numOpenFits = 0;
855 fNumSubAndFits = new DimDescribedService(GetName() + "/NUM_SUBS", "I:2", fNumSubAndFitsData,
856 "Num. open files + num. subscribed services"
857 "|NSubAndOpenFiles[int]:Num. of subs and open files");
858
859 //services parameters
860 fDebugIsOn = false;
861 fOpenedFilesIsOn = true;
862 fNumSubAndFitsIsOn = true;
863
864 // provide services control commands
865 AddEvent("SET_DEBUG_MODE", "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
866 (bind(&DataLogger::SetDebugOnOff, this, placeholders::_1))
867 ("Switch debug mode on or off. Debug mode prints information about every service written to a file."
868 "|Enable[bool]:Enable of disable debug mode (yes/no).");
869
870 AddEvent("SET_STATISTICS_UPDATE_INTERVAL", "S:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
871 (bind(&DataLogger::SetStatsPeriod, this, placeholders::_1))
872 ("Interval in which the data-logger statistics service (STATS) is updated."
873 "|Interval[ms]:Value in milliseconds (<=0: no update).");
874
875 AddEvent("ENABLE_FILENAME_SERVICES", "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
876 (bind(&DataLogger::SetOpenedFilesOnOff ,this, placeholders::_1))
877 ("Switch service which distributes information about the open files on or off."
878 "|Enable[bool]:Enable of disable filename services (yes/no).");
879
880 AddEvent("ENABLE_NUMSUBS_SERVICE", "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
881 (bind(&DataLogger::SetNumSubsAndFitsOnOff, this, placeholders::_1))
882 ("Switch the service which distributes information about the number of subscriptions and open files on or off."
883 "|Enable[bool]:Enable of disable NUM_SUBS service (yes/no).");
884
885 AddEvent("SET_RUN_TIMEOUT", "L:1", kSM_Ready, kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun)
886 (bind(&DataLogger::SetRunTimeoutDelay, this, placeholders::_1))
887 ("Set the timeout delay for old run numbers."
888 "|timeout[min]:Time out in minutes after which files for expired runs are closed.");
889
890 fDestructing = false;
891
892 fPreviousOldRunNumberCheck = Time().Mjd();
893
894 fDailyFileDayChangedAlready = true;
895 fRunNumberTimeout = 60000; //default run-timeout set to 1 minute
896 fRunNumber.push_back(RunNumberType());
897 fRunNumber.back().runNumber = -1;
898 fRunNumber.back().time = Time();
899 NotifyOpenedFile("", 0, fOpenedNightlyFiles);
900 NotifyOpenedFile("", 0, fOpenedRunFiles);
901
902 fRunNumberService = 0;
903
904 fShouldAutoStart = false;
905 fAutoStarted = false;
906
907
908 if(fDebugIsOn)
909 {
910 Debug("DataLogger Init Done.");
911 }
912}
913
914// --------------------------------------------------------------------------
915//
916//! Destructor
917//
918DataLogger::~DataLogger()
919{
920 if (fDebugIsOn)
921 Debug("DataLogger destruction starts");
922
923 //this boolean should not be required anymore
924 fDestructing = true;
925
926 //now clear the services subscriptions
927 dim_lock();
928 fServiceSubscriptions.clear();
929 dim_unlock();
930
931 //clear any remaining run number (should remain only one)
932 while (fRunNumber.size() > 0)
933 {
934 RemoveOldestRunNumber();
935 }
936 //go to the ready state. i.e. close all files, run-wise first
937 GoToReady();
938
939 Info("Will soon close the daily log file");
940
941 delete fOpenedNightlyFiles;
942 delete fOpenedRunFiles;
943 delete fNumSubAndFits;
944
945 if (fNightlyLogFile.is_open())//this file is the only one that has not been closed by GoToReady
946 {
947 fNightlyLogFile << endl;
948 fNightlyLogFile.close();
949 }
950
951 for (auto it=fServerDescriptionsList.begin(); it!= fServerDescriptionsList.end(); it++)
952 delete *it;
953
954 if (fDebugIsOn)
955 Debug("DataLogger desctruction ends");
956}
957
958// --------------------------------------------------------------------------
959//
960//! checks if old run numbers should be trimmed and if so, do it
961//
962void DataLogger::TrimOldRunNumbers()
963{
964 const Time cTime = Time();
965
966 if (cTime - fPreviousOldRunNumberCheck < boost::posix_time::milliseconds(fRunNumberTimeout))
967 return;
968
969 while (fRunNumber.size() > 1 && (cTime - fRunNumber.back().time) > boost::posix_time::milliseconds(fRunNumberTimeout))
970 {
971 RemoveOldestRunNumber();
972 }
973 fPreviousOldRunNumberCheck = cTime;
974}
975// --------------------------------------------------------------------------
976//
977//! Inherited from DimInfo. Handles all the Infos to which we subscribed, and log them
978//
979int DataLogger::infoCallback(const EventImp& evt, unsigned int subIndex)
980{
981// if (fDebugIsOn)
982// {
983// ostringstream str;
984// str << "Got infoCallback called with service index= " << subIndex;
985// Debug(str.str());
986// }
987 if ((GetCurrentState() == kSM_Ready) && (!fAutoStarted) && fShouldAutoStart)
988 {
989 fAutoStarted = true;
990 SetCurrentState(Start());
991 }
992 else
993 {
994 if (GetCurrentState() > kSM_Ready)
995 fAutoStarted = true;
996 }
997
998
999 //check if the service pointer corresponds to something that we subscribed to
1000 //this is a fix for a bug that provides bad Infos when a server starts
1001 bool found = false;
1002 SubscriptionsListType::iterator x;
1003 map<string, SubscriptionType>::iterator y;
1004 for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
1005 {//find current service is subscriptions
1006 //Edit: this should be useless now... remove it sometimes ?
1007 for (y=x->second.begin(); y!=x->second.end();y++)
1008 if (y->second.index == subIndex)
1009 {
1010 found = true;
1011 break;
1012 }
1013 if (found)
1014 break;
1015 }
1016 if (!found)
1017 return GetCurrentState();
1018
1019 if (evt.GetSize() == 0)
1020 return GetCurrentState();
1021 if (evt.GetFormat() == "")
1022 return GetCurrentState();
1023
1024// cout.precision(20);
1025// cout << "Orig timestamp: " << Time(I->getTimestamp(), I->getTimestampMillisecs()*1000).Mjd() << endl;
1026 // FIXME: Here we have to check if we have received the
1027 // service with the run-number.
1028 // CheckForRunNumber(I); has been removed because we have to
1029 // subscribe to this service anyway and hence we have the pointer
1030 // (no need to check for the name)
1031 CheckForRunNumber(evt, subIndex);
1032
1033 Report(evt, y->second);
1034
1035 //remove old run numbers
1036 TrimOldRunNumbers();
1037
1038 return GetCurrentState();
1039}
1040
1041// --------------------------------------------------------------------------
1042//
1043//! Add a new active run number
1044//! @param newRun the new run number
1045//! @param time the time at which the new run number was issued
1046//
1047void DataLogger::AddNewRunNumber(int64_t newRun, Time time)
1048{
1049
1050 if (newRun > 0xffffffff)
1051 {
1052 Error("New run number too large, out of range. Ignoring.");
1053 return;
1054 }
1055 for (std::vector<int64_t>::const_iterator it=previousRunNumbers.begin(); it != previousRunNumbers.end(); it++)
1056 {
1057 if (*it == newRun)
1058 {
1059 Error("Newly provided run number has already been used (or is still in use). Going to error state");
1060 SetCurrentState(kSM_BadFolder);
1061 return;
1062 }
1063 }
1064 if (fDebugIsOn)
1065 {
1066 ostringstream str;
1067 str << "Adding new run number " << newRun << " issued at " << time;
1068 Debug(str);
1069 }
1070 //Add new run number to run number list
1071 fRunNumber.push_back(RunNumberType());
1072 fRunNumber.back().runNumber = int32_t(newRun);
1073 fRunNumber.back().time = time;
1074
1075 ostringstream str;
1076 str << "The new run number is: " << fRunNumber.back().runNumber;
1077 Message(str);
1078
1079 if (GetCurrentState() != kSM_Logging && GetCurrentState() != kSM_WaitingRun )
1080 return;
1081
1082 if (newRun > 0 && GetCurrentState() == kSM_WaitingRun)
1083 SetCurrentState(kSM_Logging);
1084 if (newRun < 0 && GetCurrentState() == kSM_Logging)
1085 SetCurrentState(kSM_WaitingRun);
1086}
1087// --------------------------------------------------------------------------
1088//
1089//! Checks whether or not the current info is a run number.
1090//! If so, then remember it. A run number is required to open the run-log file
1091//! @param I
1092//! the current DimInfo
1093//
1094void DataLogger::CheckForRunNumber(const EventImp& evt, unsigned int index)
1095{
1096 if (index != fRunNumberService)
1097 return;
1098// int64_t newRun = reinterpret_cast<const uint64_t*>(evt.GetData())[0];
1099 AddNewRunNumber(evt.GetXtra(), evt.GetTime());
1100}
1101
1102// --------------------------------------------------------------------------
1103//
1104//! write infos to log files.
1105//! @param I
1106//! The current DimInfo
1107//! @param sub
1108//! The dataLogger's subscription corresponding to this DimInfo
1109//
1110void DataLogger::Report(const EventImp& evt, SubscriptionType& sub)
1111{
1112 const string fmt(evt.GetFormat());
1113
1114 const bool isItaReport = fmt!="C";
1115
1116 if (!fNightlyLogFile.is_open())
1117 return;
1118
1119 if (fDebugIsOn && string(evt.GetName())!="DATA_LOGGER/MESSAGE")
1120 {
1121 ostringstream str;
1122 str << "Logging " << evt.GetName() << " [" << evt.GetFormat() << "] (" << evt.GetSize() << ")";
1123 Debug(str);
1124 }
1125
1126 //
1127 // Check whether we should close and reopen daily text files or not
1128 // calculate time "centered" around noon instead of midnight
1129 // if number of days has changed, then files should be closed and reopenned.
1130 const Time timeNow;
1131 const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
1132 int newDayNumber = nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
1133
1134 //also check if we should flush the nightly files
1135 if (lastFlush < timeNow-boost::posix_time::minutes(1))
1136 {
1137 lastFlush = timeNow;
1138 SubscriptionsListType::iterator x;
1139 map<string, SubscriptionType>::iterator y;
1140 for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
1141 {//find current service is subscriptions
1142 for (y=x->second.begin(); y!=x->second.end();y++)
1143 if (y->second.nightlyFile.IsOpen())
1144 {
1145 y->second.nightlyFile.Flush();
1146 }
1147 }
1148 if (fDebugIsOn)
1149 Debug("Just flushed nightly fits files to the disk");
1150 }
1151
1152 if (newDayNumber != fCurrentDay)
1153 {
1154 fCurrentDay = newDayNumber;
1155 //crawl through the subcriptions and close any open nightly file
1156 SubscriptionsListType::iterator x;
1157 map<string, SubscriptionType>::iterator y;
1158 for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
1159 {//find current service is subscriptions
1160 for (y=x->second.begin(); y!=x->second.end();y++)
1161 if (y->second.nightlyFile.IsOpen())
1162 {
1163 y->second.nightlyFile.Close();
1164 }
1165 }
1166
1167 if (fDebugIsOn)
1168 Debug("Day have changed! Closing and reopening nightly files");
1169
1170 fNightlyLogFile << endl;
1171 fNightlyLogFile.close();
1172 fNightlyReportFile.close();
1173
1174 Info("Closed: "+fFullNightlyLogFileName);
1175 Info("Closed: "+fFullNightlyReportFileName);
1176
1177 fFullNightlyLogFileName = CompileFileNameWithPath(fFilePath, "", "log");
1178 if (!OpenTextFile(fNightlyLogFile, fFullNightlyLogFileName))
1179 {
1180 GoToReady();
1181 SetCurrentState(kSM_BadFolder);
1182 return;
1183 }
1184 fNightlyLogFile << endl;
1185
1186 fFullNightlyReportFileName = CompileFileNameWithPath(fFilePath, "", "rep");
1187 if (!OpenTextFile(fNightlyReportFile, fFullNightlyReportFileName))
1188 {
1189 GoToReady();
1190 SetCurrentState(kSM_BadFolder);
1191 return;
1192 }
1193 }
1194 //create the converter for that service
1195 if (!sub.fConv)
1196 {
1197 sub.fConv = shared_ptr<Converter>(new Converter(Out(), evt.GetFormat()));
1198 if (!sub.fConv->valid())
1199 {
1200 ostringstream str;
1201 str << "Couldn't properly parse the format... service " << evt.GetName() << " ignored.";
1202 Error(str);
1203 return;
1204 }
1205 }
1206 //construct the header
1207 ostringstream header;
1208 const Time cTime(evt.GetTime());
1209 fQuality = evt.GetQoS();
1210 //I had strange surprises with the quality from Dim before. Double check that the value is indeed valid.
1211 if (fQuality != kMessage &&
1212 fQuality != kInfo &&
1213 fQuality != kWarn &&
1214 fQuality != kError &&
1215 fQuality != kFatal &&
1216 fQuality != kComment &&
1217 fQuality != kDebug)
1218 fQuality = kError;
1219 fMjD = cTime.Mjd() ? cTime.Mjd()-40587 : 0;
1220
1221 if (isItaReport)
1222 {
1223 //write text header
1224 string serviceName = (sub.service == "MESSAGE") ? "" : "_"+sub.service;
1225 header << sub.server << serviceName << " " << fQuality << " ";
1226 header << evt.GetTime() << " ";
1227
1228 string text;
1229 try
1230 {
1231 text = sub.fConv->GetString(evt.GetData(), evt.GetSize());
1232 }
1233 catch (const runtime_error &e)
1234 {
1235 ostringstream str;
1236 str << "Parsing service " << evt.GetName();
1237 str << " failed: " << e.what() << " removing the subscription for now.";
1238 Error(str);
1239 //remove this subscription from the list.
1240 //because these operators use references to elements, and because they're supposed here to erase these objects on the way, I'm not too sure... so duplicate the names !
1241 RemoveService(sub.server, sub.service, false);
1242 return;
1243 }
1244
1245 if (text.empty())
1246 {
1247 ostringstream str;
1248 str << "Service " << evt.GetName() << " sent an empty string";
1249 Info(str);
1250 return;
1251 }
1252 //replace bizarre characters by white space
1253 replace(text.begin(), text.end(), '\n', '\\');
1254 replace_if(text.begin(), text.end(), ptr_fun<int, int>(&iscntrl), ' ');
1255
1256 //write entry to Nightly report
1257 if (fNightlyReportFile.is_open())
1258 {
1259 fNightlyReportFile << header.str() << text << endl;
1260 if (!CheckForOfstreamError(fNightlyReportFile, true))
1261 return;
1262 }
1263#ifdef HAVE_FITS
1264 //check if the last received event was before noon and if current one is after noon.
1265 //if so, close the file so that it gets reopened.
1266 sub.lastReceivedEvent = cTime;
1267 if (!sub.nightlyFile.IsOpen())
1268 if (GetCurrentState() != kSM_Ready)
1269 OpenFITSFiles(sub);
1270 WriteToFITS(sub, evt.GetData());
1271#endif
1272 }
1273 else
1274 {//write entry to Nightly log
1275 vector<string> strings;
1276 try
1277 {
1278 strings = sub.fConv->ToStrings(evt.GetData());
1279 }
1280 catch (const runtime_error &e)
1281 {
1282 ostringstream str;
1283 str << "Parsing service " << evt.GetName();
1284 str << " failed: " << e.what() << " removing the subscription for now.";
1285 Error(str);
1286 //remove this subscription from the list.
1287 //because these operators use references to elements, and because they're supposed here to erase these objects on the way, I'm not too sure... so duplicate the names !
1288 RemoveService(sub.server, sub.service, false);
1289 return;
1290 }
1291 if (strings.size() > 1)
1292 {
1293 ostringstream err;
1294 err << "There was more than one string message in service " << evt.GetName() << " going to fatal error state";
1295 Error(err.str());
1296 }
1297 ostringstream msg;
1298 string serviceName = (sub.service == "MESSAGE") ? "" : "_"+sub.service;
1299 msg << sub.server << serviceName << ": " << strings[0];
1300
1301 if (fNightlyLogFile.is_open())
1302 {
1303 MessageImp(fNightlyLogFile).Write(cTime, msg.str().c_str(), fQuality);
1304 if (!CheckForOfstreamError(fNightlyLogFile, true))
1305 return;
1306 }
1307
1308 sub.lastReceivedEvent = cTime;
1309 if (!sub.nightlyFile.IsOpen())
1310 if (GetCurrentState() != kSM_Ready)
1311 OpenFITSFiles(sub);
1312 WriteToFITS(sub, evt.GetData());
1313 }
1314
1315}
1316
1317// --------------------------------------------------------------------------
1318//
1319//! print the dataLogger's current state. invoked by the PRINT command
1320//! @param evt
1321//! the current event. Not used by the method
1322//! @returns
1323//! the new state. Which, in that case, is the current state
1324//!
1325int DataLogger::PrintState(const Event& )
1326{
1327 Message("------------------------------------------");
1328 Message("------- DATA LOGGER CURRENT STATE --------");
1329 Message("------------------------------------------");
1330
1331 //print the path configuration
1332#if BOOST_VERSION < 104600
1333 Message("File path: " + boost::filesystem::system_complete(boost::filesystem::path(fFilePath)).directory_string());
1334#else
1335 Message("File path: " + boost::filesystem::system_complete(boost::filesystem::path(fFilePath)).parent_path().string());
1336#endif
1337
1338 //print active run numbers
1339 ostringstream str;
1340 //timeout value
1341 str << "Timeout delay for old run numbers: " << fRunNumberTimeout << " ms";
1342 Message(str);
1343 str.str("");
1344 str << "Active Run Numbers:";
1345 for (list<RunNumberType>::const_iterator it=fRunNumber.begin(); it!=fRunNumber.end(); it++)
1346 str << " " << it->runNumber;
1347 if (fRunNumber.size()==0)
1348 str << " <none>";
1349 Message(str);
1350
1351 //print all the open files.
1352 Message("------------ OPEN FILES ----------------");
1353 if (fNightlyLogFile.is_open())
1354 Message("Nightly log-file: "+fFullNightlyLogFileName);
1355
1356 if (fNightlyReportFile.is_open())
1357 Message("Nightly report-file: "+fFullNightlyReportFileName);
1358
1359 const DimWriteStatistics::Stats statVar = fFilesStats.GetTotalSizeWritten();
1360 // /*const bool statWarning =*/ calculateTotalSizeWritten(statVar, true);
1361#ifdef HAVE_FITS
1362 str.str("");
1363 str << "Number of open FITS files: " << fNumSubAndFitsData.numOpenFits;
1364 Message(str);
1365 // FIXME: Print list of open FITS files
1366#else
1367 Message("FITS output disabled at compilation");
1368#endif
1369 Message("----------------- STATS ------------------");
1370 if (fFilesStats.GetUpdateInterval()>0)
1371 {
1372 str.str("");
1373 str << "Statistics are updated every " << fFilesStats.GetUpdateInterval() << " ms";
1374 Message(str);
1375 }
1376 else
1377 Message("Statistics updates are currently disabled.");
1378 str.str("");
1379 str << "Total Size written: " << statVar.sizeWritten/1000 << " kB";
1380 Message(str);
1381 str.str("");
1382 str << "Disk free space: " << statVar.freeSpace/1000000 << " MB";
1383 Message(str);
1384
1385 Message("------------ DIM SUBSCRIPTIONS -----------");
1386 str.str("");
1387 str << "There are " << fNumSubAndFitsData.numSubscriptions << " active DIM subscriptions.";
1388 Message(str);
1389 for (map<const string, map<string, SubscriptionType> >::const_iterator it=fServiceSubscriptions.begin(); it!= fServiceSubscriptions.end();it++)
1390 {
1391 Message("Server "+it->first);
1392 for (map<string, SubscriptionType>::const_iterator it2=it->second.begin(); it2!=it->second.end(); it2++)
1393 Message(" -> "+it2->first);
1394 }
1395 Message("--------------- BLOCK LIST ---------------");
1396 for (set<string>::const_iterator it=fBlackList.begin(); it != fBlackList.end(); it++)
1397 Message(" -> "+*it);
1398 if (fBlackList.size()==0)
1399 Message(" <empty>");
1400
1401 Message("--------------- ALLOW LIST ---------------");
1402 for (set<string>::const_iterator it=fWhiteList.begin(); it != fWhiteList.end(); it++)
1403 Message(" -> "+*it);
1404 if (fWhiteList.size()==0)
1405 Message(" <empty>");
1406
1407 Message("-------------- GROUPING LIST -------------");
1408 Message("The following servers and/or services will");
1409 Message("be grouped into a single fits file:");
1410 for (set<string>::const_iterator it=fGrouping.begin(); it != fGrouping.end(); it++)
1411 Message(" -> "+*it);
1412 if (fGrouping.size()==0)
1413 Message(" <no grouping>");
1414
1415 Message("------------------------------------------");
1416 Message("-------- END OF DATA LOGGER STATE --------");
1417 Message("------------------------------------------");
1418
1419 return GetCurrentState();
1420}
1421
1422// --------------------------------------------------------------------------
1423//
1424//! turn debug mode on and off
1425//! @param evt
1426//! the current event. contains the instruction string: On, Off, on, off, ON, OFF, 0 or 1
1427//! @returns
1428//! the new state. Which, in that case, is the current state
1429//!
1430int DataLogger::SetDebugOnOff(const Event& evt)
1431{
1432 const bool backupDebug = fDebugIsOn;
1433
1434 fDebugIsOn = evt.GetBool();
1435
1436 if (fDebugIsOn == backupDebug)
1437 Message("Debug mode was already in the requested state.");
1438
1439 ostringstream str;
1440 str << "Debug mode is now " << fDebugIsOn;
1441 Message(str);
1442
1443 fFilesStats.SetDebugMode(fDebugIsOn);
1444
1445 return GetCurrentState();
1446}
1447// --------------------------------------------------------------------------
1448//
1449//! set the statistics update period duration. 0 disables the statistics
1450//! @param evt
1451//! the current event. contains the new duration.
1452//! @returns
1453//! the new state. Which, in that case, is the current state
1454//!
1455int DataLogger::SetStatsPeriod(const Event& evt)
1456{
1457 fFilesStats.SetUpdateInterval(evt.GetShort());
1458 return GetCurrentState();
1459}
1460// --------------------------------------------------------------------------
1461//
1462//! set the opened files service on or off.
1463//! @param evt
1464//! the current event. contains the instruction string. similar to setdebugonoff
1465//! @returns
1466//! the new state. Which, in that case, is the current state
1467//!
1468int DataLogger::SetOpenedFilesOnOff(const Event& evt)
1469{
1470 const bool backupOpened = fOpenedFilesIsOn;
1471
1472 fOpenedFilesIsOn = evt.GetBool();
1473
1474 if (fOpenedFilesIsOn == backupOpened)
1475 Message("Opened files service mode was already in the requested state.");
1476
1477 ostringstream str;
1478 str << "Opened files service mode is now " << fOpenedFilesIsOn;
1479 Message(str);
1480
1481 return GetCurrentState();
1482}
1483
1484// --------------------------------------------------------------------------
1485//
1486//! set the number of subscriptions and opened fits on and off
1487//! @param evt
1488//! the current event. contains the instruction string. similar to setdebugonoff
1489//! @returns
1490//! the new state. Which, in that case, is the current state
1491//!
1492int DataLogger::SetNumSubsAndFitsOnOff(const Event& evt)
1493{
1494 const bool backupSubs = fNumSubAndFitsIsOn;
1495
1496 fNumSubAndFitsIsOn = evt.GetBool();
1497
1498 if (fNumSubAndFitsIsOn == backupSubs)
1499 Message("Number of subscriptions service mode was already in the requested state");
1500
1501 ostringstream str;
1502 str << "Number of subscriptions service mode is now " << fNumSubAndFitsIsOn;
1503 Message(str);
1504
1505 return GetCurrentState();
1506}
1507// --------------------------------------------------------------------------
1508//
1509//! set the timeout delay for old run numbers
1510//! @param evt
1511//! the current event. contains the timeout delay long value
1512//! @returns
1513//! the new state. Which, in that case, is the current state
1514//!
1515int DataLogger::SetRunTimeoutDelay(const Event& evt)
1516{
1517 if (evt.GetUInt() == 0)
1518 {
1519 Error("Timeout delays for old run numbers must be greater than 0... ignored.");
1520 return GetCurrentState();
1521 }
1522
1523 if (fRunNumberTimeout == evt.GetUInt())
1524 Message("New timeout for old run numbers is same value as previous one.");
1525
1526 fRunNumberTimeout = evt.GetUInt();
1527
1528 ostringstream str;
1529 str << "Timeout delay for old run numbers is now " << fRunNumberTimeout << " ms";
1530 Message(str);
1531
1532 return GetCurrentState();
1533}
1534
1535// --------------------------------------------------------------------------
1536//
1537//! Notifies the DIM service that a particular file was opened
1538//! @ param name the base name of the opened file, i.e. without path nor extension.
1539//! WARNING: use string instead of string& because I pass values that do not convert to string&.
1540//! this is not a problem though because file are not opened so often.
1541//! @ param type the type of the opened file. 0 = none open, 1 = log, 2 = text, 4 = fits
1542inline void DataLogger::NotifyOpenedFile(const string &name, int type, DimDescribedService* service)
1543{
1544 if (!fOpenedFilesIsOn)
1545 return;
1546
1547 if (fDebugIsOn)
1548 {
1549 ostringstream str;
1550 str << "Updating " << service->getName() << " file '" << name << "' (type=" << type << ")";
1551 Debug(str);
1552
1553 str.str("");
1554 str << "Num subscriptions: " << fNumSubAndFitsData.numSubscriptions << " Num open FITS files: " << fNumSubAndFitsData.numOpenFits;
1555 Debug(str);
1556 }
1557
1558 if (name.size()+1 > FILENAME_MAX)
1559 {
1560 Error("Provided file name '" + name + "' is longer than allowed file name length.");
1561 return;
1562 }
1563
1564 OpenFileToDim fToDim;
1565 fToDim.code = type;
1566 memcpy(fToDim.fileName, name.c_str(), name.size()+1);
1567
1568 service->setData(reinterpret_cast<void*>(&fToDim), name.size()+1+sizeof(uint32_t));
1569 service->setQuality(0);
1570 service->Update();
1571}
1572// --------------------------------------------------------------------------
1573//
1574//! Implements the Start transition.
1575//! Concatenates the given path for the Nightly file and the filename itself (based on the day),
1576//! and tries to open it.
1577//! @returns
1578//! kSM_NightlyOpen if success, kSM_BadFolder if failure
1579int DataLogger::Start()
1580{
1581 if (fDebugIsOn)
1582 {
1583 Debug("Starting...");
1584 }
1585 fFullNightlyLogFileName = CompileFileNameWithPath(fFilePath, "", "log");
1586 bool nightlyLogOpen = fNightlyLogFile.is_open();
1587 if (!OpenTextFile(fNightlyLogFile, fFullNightlyLogFileName))
1588 return kSM_BadFolder;
1589 if (!nightlyLogOpen)
1590 fNightlyLogFile << endl;
1591
1592 fFullNightlyReportFileName = CompileFileNameWithPath(fFilePath, "", "rep");
1593 if (!OpenTextFile(fNightlyReportFile, fFullNightlyReportFileName))
1594 {
1595 fNightlyLogFile.close();
1596 Info("Closed: "+fFullNightlyReportFileName);
1597 return kSM_BadFolder;
1598 }
1599
1600 fFilesStats.FileOpened(fFullNightlyLogFileName);
1601 fFilesStats.FileOpened(fFullNightlyReportFileName);
1602 //notify that a new file has been opened.
1603 const string baseFileName = CompileFileNameWithPath(fFilePath, "", "");
1604 NotifyOpenedFile(baseFileName, 3, fOpenedNightlyFiles);
1605
1606 fOpenedNightlyFits.clear();
1607
1608 return kSM_NightlyOpen;
1609}
1610
1611#ifdef HAVE_FITS
1612// --------------------------------------------------------------------------
1613//
1614//! open if required a the FITS files corresponding to a given subscription
1615//! @param sub
1616//! the current DimInfo subscription being examined
1617void DataLogger::OpenFITSFiles(SubscriptionType& sub)
1618{
1619 string serviceName(sub.server + "_" + sub.service);//evt.GetName());
1620
1621 for (unsigned int i=0;i<serviceName.size(); i++)
1622 {
1623 if (serviceName[i] == '/')
1624 {
1625 serviceName[i] = '_';
1626 break;
1627 }
1628 }
1629 //we open the NightlyFile anyway, otherwise this function shouldn't have been called.
1630 if (!sub.nightlyFile.IsOpen())
1631 {
1632 const string partialName = CompileFileNameWithPath(fFilePath, serviceName, "fits");
1633
1634 const string fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
1635 if (!sub.fitsBufferAllocated)
1636 AllocateFITSBuffers(sub);
1637 //get the size of the file we're about to open
1638 if (fFilesStats.FileOpened(partialName))
1639 fOpenedNightlyFits[fileNameOnly].push_back(serviceName);
1640
1641 if (!sub.nightlyFile.Open(partialName, serviceName, &fNumSubAndFitsData.numOpenFits, this, 0))
1642 {
1643 GoToRunWriteErrorState();
1644 return;
1645 }
1646
1647 ostringstream str;
1648 str << "Opened: " << partialName << " (Nfits=" << fNumSubAndFitsData.numOpenFits << ")";
1649 Info(str);
1650
1651 //notify the opening
1652 const string baseFileName = CompileFileNameWithPath(fFilePath, "", "");
1653 NotifyOpenedFile(baseFileName, 7, fOpenedNightlyFiles);
1654 if (fNumSubAndFitsIsOn)
1655 fNumSubAndFits->Update();
1656 }
1657
1658}
1659// --------------------------------------------------------------------------
1660//
1661//! Allocates the required memory for a given pair of fits files (nightly and run)
1662//! @param sub the subscription of interest.
1663//
1664void DataLogger::AllocateFITSBuffers(SubscriptionType& sub)
1665{
1666 //Init the time columns of the file
1667 Description dateDesc(string("Time"), string("Modified Julian Date"), string("MJD"));
1668 sub.nightlyFile.AddStandardColumn(dateDesc, "1D", &fMjD, sizeof(double));
1669
1670 Description QoSDesc("QoS", "Quality of service", "");
1671 sub.nightlyFile.AddStandardColumn(QoSDesc, "1J", &fQuality, sizeof(int));
1672
1673 // Compilation failed
1674 if (!sub.fConv->valid())
1675 {
1676 Error("Compilation of format string failed.");
1677 return;
1678 }
1679
1680 //we've got a nice structure describing the format of this service's messages.
1681 //Let's create the appropriate FITS columns
1682 const vector<string> dataFormatsLocal = sub.fConv->GetFitsFormat();
1683
1684 ostringstream str;
1685 str << "Initializing data columns for service " << sub.server << "/" << sub.service;
1686 Info(str);
1687 sub.nightlyFile.InitDataColumns(GetDescription(sub.server, sub.service), dataFormatsLocal, this);
1688
1689 sub.fitsBufferAllocated = true;
1690}
1691// --------------------------------------------------------------------------
1692//
1693//! write a dimInfo data to its corresponding FITS files
1694//
1695//FIXME: DO I REALLY NEED THE EVENT IMP HERE ???
1696void DataLogger::WriteToFITS(SubscriptionType& sub, const void* data)
1697{
1698 //nightly File status (open or not) already checked
1699 if (sub.nightlyFile.IsOpen())
1700 {
1701 if (!sub.nightlyFile.Write(*sub.fConv.get(), data))
1702 {
1703 RemoveService(sub.server, sub.service, false);
1704 GoToNightlyWriteErrorState();
1705 return;
1706 }
1707 }
1708}
1709#endif //if has_fits
1710// --------------------------------------------------------------------------
1711//
1712//! Go to Run Write Error State
1713// A write error has occurred. Checks what is the current state and take appropriate action
1714void DataLogger::GoToRunWriteErrorState()
1715{
1716 if ((GetCurrentState() != kSM_RunWriteError) &&
1717 (GetCurrentState() != kSM_DailyWriteError))
1718 SetCurrentState(kSM_RunWriteError);
1719}
1720// --------------------------------------------------------------------------
1721//
1722//! Go to Nightly Write Error State
1723// A write error has occurred. Checks what is the current state and take appropriate action
1724void DataLogger::GoToNightlyWriteErrorState()
1725{
1726 if (GetCurrentState() != kSM_DailyWriteError)
1727 SetCurrentState(kSM_DailyWriteError);
1728}
1729
1730
1731#ifdef HAVE_FITS
1732// --------------------------------------------------------------------------
1733//
1734//! Create a fits group file with all the run-fits that were written (either daily or run)
1735//! @param filesToGroup a map of filenames mapping to table names to be grouped (i.e. a
1736//! single file can contain several tables to group
1737//! @param runNumber the run number that should be used for grouping. 0 means nightly group
1738//
1739void DataLogger::CreateFitsGrouping(map<string, vector<string> > & filesToGroup)
1740{
1741 if (fDebugIsOn)
1742 {
1743 ostringstream str;
1744 str << "Creating fits group for nightly files";
1745 Debug(str);
1746 }
1747 //create the FITS group corresponding to the ending run.
1748 CCfits::FITS* groupFile;
1749 unsigned int numFilesToGroup = 0;
1750 unsigned int maxCharLength = 0;
1751 for (map<string, vector<string> >::const_iterator it=filesToGroup.begin(); it != filesToGroup.end(); it++)
1752 {
1753 //add the number of tables in this file to the total number to group
1754 numFilesToGroup += it->second.size();
1755 //check the length of all the strings to be written, to determine the max string length to write
1756 if (it->first.size() > maxCharLength)
1757 maxCharLength = it->first.size();
1758 for (vector<string>::const_iterator jt=it->second.begin(); jt != it->second.end(); jt++)
1759 if (jt->size() > maxCharLength)
1760 maxCharLength = jt->size();
1761 }
1762
1763 if (fDebugIsOn)
1764 {
1765 ostringstream str;
1766 str << "There are " << numFilesToGroup << " tables to group";
1767 Debug(str);
1768 }
1769 if (numFilesToGroup <= 1)
1770 {
1771 filesToGroup.clear();
1772 return;
1773 }
1774 const string groupName = CompileFileNameWithPath(fFilePath, "", "fits");
1775
1776 Info("Creating FITS group in: "+groupName);
1777
1778 CCfits::Table* groupTable;
1779
1780 try
1781 {
1782 groupFile = new CCfits::FITS(groupName, CCfits::RWmode::Write);
1783 //setup the column names
1784 ostringstream pathTypeName;
1785 pathTypeName << maxCharLength << "A";
1786 vector<string> names;
1787 vector<string> dataTypes;
1788 names.push_back("MEMBER_XTENSION");
1789 dataTypes.push_back("8A");
1790 names.push_back("MEMBER_URI_TYPE");
1791 dataTypes.push_back("3A");
1792 names.push_back("MEMBER_LOCATION");
1793 dataTypes.push_back(pathTypeName.str());
1794 names.push_back("MEMBER_NAME");
1795 dataTypes.push_back(pathTypeName.str());
1796 names.push_back("MEMBER_VERSION");
1797 dataTypes.push_back("1J");
1798 names.push_back("MEMBER_POSITION");
1799 dataTypes.push_back("1J");
1800
1801 groupTable = groupFile->addTable("GROUPING", numFilesToGroup, names, dataTypes);
1802//TODO handle the case when the logger was stopped and restarted during the same day, i.e. the grouping file must be updated
1803 }
1804 catch (CCfits::FitsException e)
1805 {
1806 ostringstream str;
1807 str << "Creating FITS table GROUPING in " << groupName << ": " << e.message();
1808 Error(str);
1809 return;
1810 }
1811 try
1812 {
1813 groupTable->addKey("GRPNAME", "FACT_RAW_DATA", "Data from the FACT telescope");
1814 }
1815 catch (CCfits::FitsException e)
1816 {
1817 Error("CCfits::Table::addKey failed for 'GRPNAME' in '"+groupName+"-GROUPING': "+e.message());
1818 return;
1819 }
1820 //CCfits seems to be buggy somehow: can't use the column's function "write": it create a compilation error: maybe strings were not thought about.
1821 //use cfitsio routines instead
1822 groupTable->makeThisCurrent();
1823 //create appropriate buffer.
1824 const unsigned int n = 8 + 3 + 2*maxCharLength + 1 + 8; //+1 for trailling character
1825
1826 vector<char> realBuffer(n);
1827
1828 char *startOfExtension = realBuffer.data();
1829 char *startOfURI = realBuffer.data()+8;
1830 char *startOfLocation = realBuffer.data()+8+3;
1831 char *startOfName = realBuffer.data()+8+3+maxCharLength;
1832
1833 strcpy(startOfExtension, "BINTABLE");
1834 strcpy(startOfURI, "URL");
1835
1836 realBuffer[8+3+2*maxCharLength+3] = 1;
1837 realBuffer[8+3+2*maxCharLength+7] = 1;
1838
1839 int i=1;
1840 for (map<string, vector<string> >::const_iterator it=filesToGroup.begin(); it!=filesToGroup.end(); it++)
1841 for (vector<string>::const_iterator jt=it->second.begin(); jt != it->second.end(); jt++, i++)
1842 {
1843 memset(startOfLocation, 0, 2*maxCharLength+1+8);
1844
1845 strcpy(startOfLocation, it->first.c_str());
1846 strcpy(startOfName, jt->c_str());
1847
1848 if (fDebugIsOn)
1849 {
1850 ostringstream str;
1851 str << "Grouping " << it->first << " " << *jt;
1852 Debug(str);
1853 }
1854
1855 int status = 0;
1856 fits_write_tblbytes(groupFile->fitsPointer(), i, 1, 8+3+2*maxCharLength +8,
1857 reinterpret_cast<unsigned char*>(realBuffer.data()), &status);
1858 if (status)
1859 {
1860 char text[30];//max length of cfitsio error strings (from doc)
1861 fits_get_errstatus(status, text);
1862 ostringstream str;
1863 str << "Writing FITS row " << i << " in " << groupName << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
1864 Error(str);
1865 GoToRunWriteErrorState();
1866 delete groupFile;
1867 return;
1868 }
1869 }
1870
1871 filesToGroup.clear();
1872 delete groupFile;
1873}
1874#endif //HAVE_FITS
1875
1876// --------------------------------------------------------------------------
1877//
1878//! Implements the StopRun transition.
1879//! Attempts to close the run file.
1880//! @returns
1881//! kSM_WaitingRun if success, kSM_FatalError otherwise
1882int DataLogger::StopRunLogging()
1883{
1884
1885 if (fDebugIsOn)
1886 {
1887 Debug("Stopping Run Logging...");
1888 }
1889
1890 if (fNumSubAndFitsIsOn)
1891 fNumSubAndFits->Update();
1892
1893 while (fRunNumber.size() > 0)
1894 {
1895 RemoveOldestRunNumber();
1896 }
1897 return kSM_WaitingRun;
1898}
1899// --------------------------------------------------------------------------
1900//
1901//! Implements the Stop and Reset transitions.
1902//! Attempts to close any openned file.
1903//! @returns
1904//! kSM_Ready
1905int DataLogger::GoToReady()
1906{
1907 if (fDebugIsOn)
1908 {
1909 Debug("Going to the Ready state...");
1910 }
1911 if (GetCurrentState() == kSM_Logging || GetCurrentState() == kSM_WaitingRun)
1912 StopRunLogging();
1913
1914 //it may be that dim tries to write a dimInfo while we're closing files. Prevent that
1915 const string baseFileName = CompileFileNameWithPath(fFilePath, "", "");
1916
1917 if (fNightlyReportFile.is_open())
1918 {
1919 fNightlyReportFile.close();
1920 Info("Closed: "+baseFileName+".rep");
1921 }
1922#ifdef HAVE_FITS
1923 for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
1924 for (map<string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
1925 {
1926 if (j->second.nightlyFile.IsOpen())
1927 j->second.nightlyFile.Close();
1928 }
1929#endif
1930 if (GetCurrentState() == kSM_Logging ||
1931 GetCurrentState() == kSM_WaitingRun ||
1932 GetCurrentState() == kSM_NightlyOpen)
1933 {
1934 NotifyOpenedFile("", 0, fOpenedNightlyFiles);
1935 if (fNumSubAndFitsIsOn)
1936 fNumSubAndFits->Update();
1937 }
1938#ifdef HAVE_FITS
1939 CreateFitsGrouping(fOpenedNightlyFits);
1940#endif
1941 return kSM_Ready;
1942}
1943
1944// --------------------------------------------------------------------------
1945//
1946//! Implements the transition towards kSM_WaitingRun
1947//! If current state is kSM_Ready, then tries to go to nightlyOpen state first.
1948//! @returns
1949//! kSM_WaitingRun or kSM_BadFolder
1950int DataLogger::NightlyToWaitRun()
1951{
1952 int cState = GetCurrentState();
1953
1954 if (cState == kSM_Ready)
1955 cState = Start();
1956
1957 if (cState != kSM_NightlyOpen)
1958 return GetCurrentState();
1959
1960 if (fDebugIsOn)
1961 {
1962 Debug("Going to Wait Run Number state...");
1963 }
1964 return kSM_WaitingRun;
1965}
1966// --------------------------------------------------------------------------
1967//
1968//! Implements the transition from wait for run number to nightly open
1969//! Does nothing really.
1970//! @returns
1971//! kSM_WaitingRun
1972int DataLogger::BackToNightlyOpen()
1973{
1974 if (GetCurrentState()==kSM_Logging)
1975 StopRunLogging();
1976
1977 if (fDebugIsOn)
1978 {
1979 Debug("Going to NightlyOpen state...");
1980 }
1981 return kSM_NightlyOpen;
1982}
1983// --------------------------------------------------------------------------
1984//
1985//! Setup Logger's configuration from a Configuration object
1986//! @param conf the configuration object that should be used
1987//!
1988int DataLogger::EvalOptions(Configuration& conf)
1989{
1990 fDebugIsOn = conf.Get<bool>("debug");
1991 fFilesStats.SetDebugMode(fDebugIsOn);
1992
1993 //Set the block or allow list
1994 fBlackList.clear();
1995 fWhiteList.clear();
1996
1997 //Adding entries that should ALWAYS be ignored
1998 fBlackList.insert("DATA_LOGGER/MESSAGE");
1999 fBlackList.insert("/SERVICE_LIST");
2000 fBlackList.insert("DIS_DNS/");
2001
2002 //set the black list, white list and the goruping
2003 const vector<string> vec1 = conf.Vec<string>("block");
2004 const vector<string> vec2 = conf.Vec<string>("allow");
2005 const vector<string> vec3 = conf.Vec<string>("group");
2006
2007 fBlackList.insert(vec1.begin(), vec1.end());
2008 fWhiteList.insert(vec2.begin(), vec2.end());
2009 fGrouping.insert( vec3.begin(), vec3.end());
2010
2011 //set the old run numbers timeout delay
2012 if (conf.Has("run-timeout"))
2013 {
2014 const uint32_t timeout = conf.Get<uint32_t>("run-timeout");
2015 if (timeout == 0)
2016 {
2017 Error("Time out delay for old run numbers must not be 0.");
2018 return 1;
2019 }
2020 fRunNumberTimeout = timeout;
2021 }
2022
2023 //configure the run files directory
2024 if (conf.Has("destination-folder"))
2025 {
2026 const string folder = conf.Get<string>("destination-folder");
2027 if (!fFilesStats.SetCurrentFolder(folder))
2028 return 2;
2029
2030 fFilePath = folder;
2031 fFullNightlyLogFileName = CompileFileNameWithPath(fFilePath, "", "log");
2032 if (!OpenTextFile(fNightlyLogFile, fFullNightlyLogFileName))
2033 return 3;
2034
2035 fNightlyLogFile << endl;
2036 NotifyOpenedFile(fFullNightlyLogFileName, 1, fOpenedNightlyFiles);
2037 for (vector<string>::iterator it=backLogBuffer.begin();it!=backLogBuffer.end();it++)
2038 fNightlyLogFile << *it;
2039 }
2040
2041 shouldBackLog = false;
2042 backLogBuffer.clear();
2043
2044 //configure the interval between statistics updates
2045 if (conf.Has("stats-interval"))
2046 fFilesStats.SetUpdateInterval(conf.Get<int16_t>("stats-interval"));
2047
2048 //configure if the filenames service is on or off
2049 fOpenedFilesIsOn = !conf.Get<bool>("no-filename-service");
2050
2051 //configure if the number of subscriptions and fits files is on or off.
2052 fNumSubAndFitsIsOn = !conf.Get<bool>("no-numsubs-service");
2053 //should we open the daily files at startup ?
2054 if (conf.Has("start-daily-files"))
2055 if (conf.Get<bool>("start-daily-files"))
2056 {
2057 fShouldAutoStart = true;
2058 }
2059 return -1;
2060}
2061
2062
2063#include "Main.h"
2064
2065// --------------------------------------------------------------------------
2066template<class T>
2067int RunShell(Configuration &conf)
2068{
2069 return Main::execute<T, DataLogger>(conf);//, true);
2070}
2071
2072/*
2073 Extract usage clause(s) [if any] for SYNOPSIS.
2074 Translators: "Usage" and "or" here are patterns (regular expressions) which
2075 are used to match the usage synopsis in program output. An example from cp
2076 (GNU coreutils) which contains both strings:
2077 Usage: cp [OPTION]... [-T] SOURCE DEST
2078 or: cp [OPTION]... SOURCE... DIRECTORY
2079 or: cp [OPTION]... -t DIRECTORY SOURCE...
2080 */
2081void PrintUsage()
2082{
2083 cout << "\n"
2084 "The data logger connects to all available Dim services and "
2085 "writes them to ascii and fits files.\n"
2086 "\n"
2087 "The default is that the program is started without user interaction. "
2088 "All actions are supposed to arrive as DimCommands. Using the -c "
2089 "option, a local shell can be initialized. With h or help a short "
2090 "help message about the usage can be brought to the screen.\n"
2091 "\n"
2092 "Usage: datalogger [-c type] [OPTIONS]\n"
2093 " or: datalogger [OPTIONS]\n";
2094 cout << endl;
2095
2096}
2097// --------------------------------------------------------------------------
2098void PrintHelp()
2099{
2100 /* Additional help text which is printed after the configuration
2101 options goes here */
2102 cout <<
2103 "\n"
2104 "If the allow list has any element, only the servers and/or services "
2105 "specified in the list will be used for subscription. The black list "
2106 "will disable service subscription and has higher priority than the "
2107 "allow list. If the allow list is not present by default all services "
2108 "will be subscribed."
2109 "\n"
2110 "For example, block=DIS_DNS/ will skip all the services offered by "
2111 "the DIS_DNS server, while block=/SERVICE_LIST will skip all the "
2112 "SERVICE_LIST services offered by any server and DIS_DNS/SERVICE_LIST "
2113 "will skip DIS_DNS/SERVICE_LIST.\n"
2114 << endl;
2115
2116 Main::PrintHelp<DataLogger>();
2117}
2118
2119// --------------------------------------------------------------------------
2120void SetupConfiguration(Configuration &conf)
2121{
2122 po::options_description configs("DataLogger options");
2123 configs.add_options()
2124 ("block,b", vars<string>(), "Black-list to block services")
2125 ("allow,a", vars<string>(), "White-list to only allowe certain services")
2126 ("debug,d", po_bool(), "Debug mode. Print clear text of received service reports.")
2127 ("group,g", vars<string>(), "Grouping of services into a single run-Fits")
2128 ("run-timeout", var<uint32_t>(), "Time out delay for old run numbers in milliseconds.")
2129 ("destination-folder", var<string>(), "Base path for the nightly and run files")
2130 ("stats-interval", var<int16_t>(), "Interval in milliseconds for write statistics update")
2131 ("no-filename-service", po_bool(), "Disable update of filename service")
2132 ("no-numsubs-service", po_bool(), "Disable update of number-of-subscriptions service")
2133 ("start-daily-files", po_bool(), "Starts the logger in DailyFileOpen instead of Ready")
2134 ;
2135
2136 conf.AddOptions(configs);
2137}
2138// --------------------------------------------------------------------------
2139int main(int argc, const char* argv[])
2140{
2141 Configuration conf(argv[0]);
2142 conf.SetPrintUsage(PrintUsage);
2143 Main::SetupConfiguration(conf);
2144 SetupConfiguration(conf);
2145
2146 if (!conf.DoParse(argc, argv, PrintHelp))
2147 return 127;
2148
2149 {
2150 // No console access at all
2151 if (!conf.Has("console"))
2152 return RunShell<LocalStream>(conf);
2153
2154 // Console access w/ and w/o Dim
2155 if (conf.Get<int>("console")==0)
2156 return RunShell<LocalShell>(conf);
2157 else
2158 return RunShell<LocalConsole>(conf);
2159 }
2160
2161
2162 return 0;
2163}
Note: See TracBrowser for help on using the repository browser.