14 static bool in(
const vector<string>& list,
const string& key) {
15 for(
auto& item: list) {
16 if(item == key)
return true;
31 static string unique_name(
const string& basename,
const string& ext) {
33 vector<string> dir_contents =
list_dir(
".");
34 string full_name, new_basename;
40 if(i<10) number =
"0" + to_string(i);
41 else number = to_string(i);
43 new_basename = basename +
"_" + number;
44 full_name = new_basename + ext;
48 }
while(in(dir_contents, full_name));
57 cerr <<
"Unexpected number of audio in channels. Given: "
58 << context->analogInChannels
64 cerr <<
"Unexpected number of analog in channels. Given: "
65 << context->analogInChannels
71 cerr <<
"analogFrames should be equal to " <<
PERIOD_SIZE <<
"!" << endl;
75 if (context->audioFrames != context->analogFrames) {
76 cerr <<
"The number of audio frames should be equal to the number of "
82 if (context->digitalFrames != context->analogFrames) {
83 cerr <<
"The number of digital frames does not match analog frames! "
85 << context->digitalFrames <<
"." << endl;
92 <<
", but is set at: " << context->audioSampleRate <<
"." << endl;
98 cerr <<
"Invalid analog sampling frequency, should be "
100 <<
", but is set at: " << context->analogSampleRate <<
"." << endl;
101 fprintf(stderr,
"Diff is: %0.4e]n",
109 struct dirent *entry;
110 DIR *dir = opendir(path);
111 vector<string> dir_contents;
116 while ((entry = readdir(dir)) != NULL) {
117 dir_contents.push_back(entry->d_name);
124 struct stat file_stat;
125 if(stat(filepath.c_str(), &file_stat) != 0) {
126 throw std::runtime_error(
"Cannot stat file!");
128 return file_stat.st_mtime;
132 template <us num_cols>
135 string str_filename = filename;
138 string basename = str_filename.substr(0, str_filename.length() -4);
139 string ext = str_filename.substr(str_filename.length() -4,
140 str_filename.length());
141 str_filename = unique_name(basename, ext);
145 static_assert(num_cols > 0,
"num_cols should be > 0");
149 file =
new WriteFile();
150 if(!file)
throw std::runtime_error(
"Could not create logfile!");
151 file->setup(str_filename.c_str(),
156 file->setFileType(kText);
157 string header_str = header;
158 if(header_str[header_str.size()-1] !=
'\n') {
164 for(
us col=0; col < num_cols-1; col++) {
168 file->setFormat(fileFormat.c_str());
171 file->setHeader(header_str.c_str());
176 template <us num_cols>
178 file->log(data.data(), data.size());
181 template <us num_cols>
191 static bool existsAndIsNumber(JSONObject& json,
const std::wstring& str)
193 return (json.find(str) != json.end() && json[str]->IsNumber());
197 std::wstring ws = JSON::s2ws(str);
198 if(existsAndIsNumber(json, ws))
200 double ret = json[ws]->AsNumber();
205 throw(std::runtime_error(
"Value " + str +
"not found\n"));