11#include " Consumer.h"
22#include " ReadoutUtils.h"
3-
3+ # include < iomanip >
44
55class ConsumerFileRecorder : public Consumer {
66 public:
7+
78 ConsumerFileRecorder (ConfigFile &cfg, std::string cfgEntryPoint):Consumer(cfg,cfgEntryPoint) {
89 counterBytesTotal=0 ;
910 fp=NULL ;
1011
1112 fileName=cfg.getValue <std::string>(cfgEntryPoint + " .fileName" );
1213 if (fileName.length ()>0 ) {
13- theLog.log (" Recording to %s" ,fileName.c_str ());
14- fp=fopen (fileName.c_str ()," wb" );
15- if (fp==NULL ) {
16- theLog.log (" Failed to create file" );
17- }
14+ theLog.log (" Recording path = %s" ,fileName.c_str ());
15+ createFile ();
1816 }
1917 if (fp==NULL ) {
2018 theLog.log (" Recording disabled" );
@@ -34,6 +32,86 @@ class ConsumerFileRecorder: public Consumer {
3432 closeRecordingFile ();
3533 }
3634
35+ int createFile () {
36+ // create the file name according to specified path
37+
38+ // parse the string, and subst variables:
39+ // ${XXX} -> get variable XXX from environment
40+ // %t -> unix timestamp (seconds since epoch)
41+ // %T -> formatted date/time
42+
43+ std::string newFileName;
44+
45+ int parseError=0 ;
46+ for (std::string::iterator it=fileName.begin ();it!=fileName.end ();++it) {
47+ // subst environment variable
48+ if (*it==' $' ) {
49+ ++it;
50+ int varNameComplete=0 ;
51+ if (it!=fileName.end ()) {
52+ if (*it==' {' ) {
53+ std::string varName;
54+
55+ for (++it;it!=fileName.end ();++it) {
56+ if (*it==' }' ) {
57+ varNameComplete=1 ;
58+ break ;
59+ } else {
60+ varName+=*it;
61+ }
62+ }
63+ if (varNameComplete) {
64+ const char *val=getenv (varName.c_str ());
65+ if (val!=nullptr ) {
66+ newFileName+=val;
67+ // theLog.log((varName + " = " + val).c_str());
68+ }
69+ }
70+ }
71+ }
72+ if (!varNameComplete) {
73+ parseError++;
74+ }
75+ } else if (*it==' %' ) {
76+ // escape characters
77+ ++it;
78+ if (it!=fileName.end ()) {
79+ if (*it==' t' ) {
80+ newFileName+=std::to_string (std::time (nullptr ));
81+ } else if (*it==' T' ) {
82+ std::time_t t = std::time (nullptr );
83+ std::tm tm = *std::localtime (&t);
84+ std::stringstream buffer;
85+ buffer << std::put_time (&tm, " %Y_%m_%d__%H_%M_%S__" );
86+ newFileName+=buffer.str ();
87+ } else {
88+ parseError++;
89+ }
90+ } else {
91+ parseError++;
92+ }
93+ } else {
94+ // normal char - copy it
95+ newFileName+=*it;
96+ }
97+ if (parseError) {
98+ break ;
99+ }
100+ }
101+ if (parseError) {
102+ theLog.log (" Failed to parse recording file path" );
103+ return -1 ;
104+ }
105+ theLog.log ((" Opening file for writing: " + newFileName).c_str ());
106+
107+ fp=fopen (newFileName.c_str ()," wb" );
108+ if (fp==NULL ) {
109+ theLog.log (" Failed to create file" );
110+ return -1 ;
111+ }
112+ return 0 ;
113+ }
114+
37115 // fwrite function with partial write auto-retry
38116 int writeToFile (FILE *fp, void *data, size_t numberOfBytes) {
39117 unsigned char *buffer=(unsigned char *)data;
0 commit comments