41
41
#include < cstdio>
42
42
#include < stdexcept>
43
43
#include " util.hpp"
44
+ #include " log.hpp"
44
45
45
46
Config::Config () :
46
47
mWatch(),
@@ -63,21 +64,27 @@ Config::Config(std::istream& file) :
63
64
64
65
Config::~Config ()
65
66
{
67
+ size_t i;
66
68
if (mCompile .command != NULL ) {
67
- if ( mCompile .command [0 ] != NULL )
68
- delete [] mCompile .command [0 ];
69
+ for (i = 0 ; mCompile .command [i]; i++ )
70
+ delete [] mCompile .command [i ];
69
71
delete [] mCompile .command ;
70
72
}
71
73
if (mTest .command != NULL ) {
72
- if ( mTest .command [0 ] != NULL )
73
- delete [] mTest .command [0 ];
74
+ for (i = 0 ; mTest .command [i]; i++ )
75
+ delete [] mTest .command [i ];
74
76
delete [] mTest .command ;
75
77
}
76
78
if (mProgram .command != NULL ) {
77
- if ( mProgram .command [0 ] != NULL )
78
- delete [] mProgram .command [0 ];
79
+ for (i = 0 ; mProgram .command [i]; i++ )
80
+ delete [] mProgram .command [i ];
79
81
delete [] mProgram .command ;
80
82
}
83
+ for (Process* proc : processes)
84
+ {
85
+ if (proc)
86
+ delete proc;
87
+ }
81
88
}
82
89
83
90
std::ifstream Config::findConfig ()
@@ -102,7 +109,7 @@ void Config::parseConfig(std::istream& file)
102
109
// Tester
103
110
for (YAML::const_iterator it=config.begin ();it!=config.end ();++it) {
104
111
assert (it->second .Type () == YAML::NodeType::Map);
105
- // std::cout << it->first << '-' << it->second << '\n' ;
112
+ // Logger::getLogger()->log(DEBUG, "%s - %s\n", it->first.as<std::string>().c_str(), it->second.as<std::string>().c_str()) ;
106
113
std::string key = Util::lowercase_r (it->first .as <std::string>());
107
114
if (key == " watch" )
108
115
{
@@ -111,17 +118,26 @@ void Config::parseConfig(std::istream& file)
111
118
else if (key == " program" )
112
119
{
113
120
this ->parseCommand (it->second , mProgram );
121
+ if (mProgram .command && mProgram .enabled )
122
+ processes.push_back (new Process (mProgram .command , true ));
114
123
}
115
124
else if (key == " test" )
116
125
{
117
126
this ->parseCommand (it->second , mTest );
127
+ if (mTest .command && mTest .enabled )
128
+ processes.push_back (new Process (mTest .command , true ));
118
129
}
119
130
else if (key == " compile" )
120
131
{
121
132
this ->parseCommand (it->second , mCompile );
133
+ if (mCompile .command && mCompile .enabled )
134
+ processes.push_back (new Process (mCompile .command , true ));
122
135
}
123
136
else
137
+ {
124
138
std::cerr << " Found an error! -" << key << ' \n ' ;
139
+ throw std::runtime_error (" Found a config error!" );
140
+ }
125
141
}
126
142
}
127
143
@@ -139,6 +155,9 @@ void Config::parseCommand(const YAML::Node& node, iCommandConfig& config)
139
155
{
140
156
if (value.IsScalar ())
141
157
{
158
+ Logger::getLogger ()->log (DEBUG, " Proc: %s" , value.as <std::string>().c_str ());
159
+ config.command = Util::parseCommand (value.as <std::string>());
160
+ #if 0
142
161
// TODO: We need to take quotes from the user and send them all as
143
162
// a single argument instead of multiple arguments.
144
163
std::string cnfval = value.as<std::string>();
@@ -164,6 +183,7 @@ void Config::parseCommand(const YAML::Node& node, iCommandConfig& config)
164
183
}
165
184
}
166
185
}
186
+ #endif
167
187
}
168
188
}
169
189
else if ( key == " enabled" ) {
@@ -175,14 +195,27 @@ void Config::parseCommand(const YAML::Node& node, iCommandConfig& config)
175
195
// std::cout << "==" << config.command[i] << '\n';
176
196
// }
177
197
}
198
+
199
+ void Config::restartProcesses ()
200
+ {
201
+ for (Process* proc : processes)
202
+ {
203
+ if (proc->kill ())
204
+ if (proc != processes.back ())
205
+ proc->runAndWait ();
206
+ else
207
+ proc->run ();
208
+ }
209
+ }
210
+
178
211
void Config::parseWatcher (const YAML::Node& node)
179
212
{
213
+ size_t asterisk_count;
180
214
if (node.size () >= 1 && node.IsMap ())
181
215
for (YAML::const_iterator iter=node.begin ();iter!=node.end ();++iter) {
182
216
std::string key = iter->first .as <std::string>();
183
217
YAML::Node value = iter->second ;
184
218
Util::lowercase (key);
185
- // std::cout << key << std::endl;
186
219
if (key == " filter" )
187
220
{
188
221
if (!value.IsSequence ())
@@ -191,8 +224,15 @@ void Config::parseWatcher(const YAML::Node& node)
191
224
filter_iter!=value.end ();
192
225
++filter_iter)
193
226
{
194
- // std::cout << "Filter: " << filter_iter->as<std::string>() << '\n';
195
- mWatch .filters .push_back (filter_iter->as <std::string>());
227
+ asterisk_count = 0 ;
228
+ std::string val = filter_iter->as <std::string>();
229
+ for (size_t i = 0 ; i < val.length (); i++)
230
+ if (val[i] == ' *' )
231
+ asterisk_count++;
232
+ Logger::getLogger ()->log (DEBUG, " Filter: %s" , val.c_str ());
233
+ if (asterisk_count > 1 )
234
+ throw std::runtime_error (" Could not open file" );
235
+ mWatch .filters .push_back (val);
196
236
}
197
237
}
198
238
else if (key == " include" )
@@ -205,13 +245,13 @@ void Config::parseWatcher(const YAML::Node& node)
205
245
filter_iter!=value.end ();
206
246
++filter_iter)
207
247
{
208
- // std::cout << "Include: " << filter_iter->as<std::string>() << '\n' ;
248
+ Logger::getLogger ()-> log (DEBUG, " Include: %s " , filter_iter->as <std::string>(). c_str ()) ;
209
249
mWatch .include .push_back (filter_iter->as <std::string>());
210
250
}
211
251
}
212
252
else if (value.IsScalar ())
213
253
{
214
- // std::cout << "Include: " << value.as<std::string>() << '\n' ;
254
+ Logger::getLogger ()-> log (DEBUG, " Include: %s " , value.as <std::string>(). c_str ()) ;
215
255
mWatch .include .push_back (value.as <std::string>());
216
256
}
217
257
}
@@ -225,18 +265,18 @@ void Config::parseWatcher(const YAML::Node& node)
225
265
filter_iter!=value.end ();
226
266
++filter_iter)
227
267
{
228
- // std::cout << "Exclude: " << filter_iter->as<std::string>() << '\n' ;
268
+ Logger::getLogger ()-> log (DEBUG, " Exclude: %s " , filter_iter->as <std::string>(). c_str ()) ;
229
269
mWatch .exclude .push_back (filter_iter->as <std::string>());
230
270
}
231
271
}
232
272
else if (value.IsScalar ())
233
273
{
234
- // std::cout << "Exclude: " << value.as<std::string>() << '\n' ;
274
+ Logger::getLogger ()-> log (DEBUG, " Exclude: %s " , value.as <std::string>(). c_str ()) ;
235
275
mWatch .exclude .push_back (value.as <std::string>());
236
276
}
237
277
}
238
278
else
239
- std::cout << " Value: " << value.as <std::string>() << ' \n ' ;
279
+ Logger::getLogger ()-> log (DEBUG, " Value: %s \n " , value.as <std::string>(). c_str ()) ;
240
280
}
241
281
}
242
282
0 commit comments