Skip to content

Commit 7e2fa1a

Browse files
committed
pcm-iio: add date/time to csv output
1 parent 667d1ee commit 7e2fa1a

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/pcm-iio.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,14 @@ vector<string> build_csv(vector<struct iio_stacks_on_socket>& iios, vector<struc
676676
if (show_root_port)
677677
header.insert(header.begin(), "Root Port");
678678
header.insert(header.begin(), "Socket");
679+
auto insertDateTime = [&csv_delimiter](vector<string> & out, CsvOutputType type) {
680+
std::string dateTime;
681+
printDateForCSV(type, csv_delimiter, &dateTime);
682+
// remove last delimiter
683+
dateTime.pop_back();
684+
out.insert(out.begin(), dateTime);
685+
};
686+
insertDateTime(header, CsvOutputType::Header2);
679687
result.push_back(build_csv_row(header, csv_delimiter));
680688
std::map<uint32_t,map<uint32_t,struct iio_counter*>> v_sort;
681689
//re-organize data collection to be row wise
@@ -721,6 +729,7 @@ vector<string> build_csv(vector<struct iio_stacks_on_socket>& iios, vector<struc
721729
uint64_t raw_data = hunit->second->data[0][socket->socket_id][stack_id][std::pair<h_id,v_id>(hh_id,vv_id)];
722730
current_row.push_back(human_readable ? unit_format(raw_data) : std::to_string(raw_data));
723731
}
732+
insertDateTime(current_row, CsvOutputType::Data);
724733
result.push_back(build_csv_row(current_row, csv_delimiter));
725734
}
726735
}

src/utils.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,31 +405,37 @@ inline void choose(const CsvOutputType outputType, H1 h1Func, H2 h2Func, D dataF
405405
}
406406
}
407407

408-
inline void printDateForCSV(const CsvOutputType outputType, std::string separator = std::string(","))
408+
inline void printDateForCSV(const CsvOutputType outputType, std::string separator = std::string(","), std::string * out = nullptr)
409409
{
410+
std::ostringstream strstr;
411+
std::ostream & stdcout = out ? strstr : std::cout;
410412
choose(outputType,
411-
[&separator]() {
412-
std::cout << separator << separator; // Time
413+
[&separator, &stdcout]() {
414+
stdcout << separator << separator; // Time
413415
},
414-
[&separator]() {
415-
std::cout << "Date" << separator << "Time" << separator;
416+
[&separator, &stdcout]() {
417+
stdcout << "Date" << separator << "Time" << separator;
416418
},
417-
[&separator]() {
419+
[&separator, &stdcout]() {
418420
std::pair<tm, uint64> tt{ pcm_localtime() };
419-
std::cout.precision(3);
420-
char old_fill = std::cout.fill('0');
421-
std::cout <<
421+
stdcout.precision(3);
422+
char old_fill = stdcout.fill('0');
423+
stdcout <<
422424
std::setw(4) << 1900 + tt.first.tm_year << '-' <<
423425
std::setw(2) << 1 + tt.first.tm_mon << '-' <<
424426
std::setw(2) << tt.first.tm_mday << separator <<
425427
std::setw(2) << tt.first.tm_hour << ':' <<
426428
std::setw(2) << tt.first.tm_min << ':' <<
427429
std::setw(2) << tt.first.tm_sec << '.' <<
428430
std::setw(3) << tt.second << separator; // milliseconds
429-
std::cout.fill(old_fill);
430-
std::cout.setf(std::ios::fixed);
431-
std::cout.precision(2);
431+
stdcout.fill(old_fill);
432+
stdcout.setf(std::ios::fixed);
433+
stdcout.precision(2);
432434
});
435+
if (out)
436+
{
437+
*out = strstr.str();
438+
}
433439
}
434440

435441
inline void printDateForJson(const std::string& separator, const std::string &jsonSeparator)

0 commit comments

Comments
 (0)