@@ -255,12 +255,30 @@ namespace ctrack {
255255 stream << " \n " ;
256256 }
257257
258+ template <typename StreamType>
259+ void csvRow (StreamType& stream, const std::vector<std::string>& row) const {
260+ for (size_t i = 0 ; i < row.size (); ++i) {
261+ stream << row[i];
262+ if (i < row.size () - 1 ) {
263+ stream << " ," ;
264+ }
265+ }
266+ stream << " \n " ;
267+ }
268+
258269 public:
259270 BeautifulTable (const std::vector<std::string>& headerColumns, bool enableColor = false , const ColorScheme& colors = default_colors, const std::vector<std::pair<std::string, int >>& top_header = {})
260271 : header(headerColumns), useColor(enableColor), colors(colors), top_header(top_header) {
261272 updateColumnWidths (header);
262273 }
263274
275+ enum class ResultFormat {
276+ TABLE,
277+ CSV,
278+ JSON,
279+ DB
280+ };
281+
264282 void addRow (const std::vector<std::string>& row) {
265283 if (row.size () != header.size ()) {
266284 throw std::invalid_argument (" Row size must match header size" );
@@ -284,6 +302,17 @@ namespace ctrack {
284302 }
285303 }
286304
305+ template <typename StreamType>
306+ void csv (StreamType& stream) const {
307+ if (top_header.size () > 0 ) {
308+ // csvRow(stream, top_header);
309+ }
310+ csvRow (stream, header);
311+ for (const auto & row : rows) {
312+ csvRow (stream, row);
313+ }
314+ }
315+
287316 template <typename T>
288317 static inline std::string table_string (const T& value) {
289318 std::ostringstream oss;
@@ -319,7 +348,6 @@ namespace ctrack {
319348 // Format the percentage as a string with 2 decimal places
320349 std::ostringstream ss;
321350 ss << std::fixed << std::setprecision (2 ) << percentage << " %" ;
322-
323351 return ss.str ();
324352 }
325353
@@ -656,26 +684,57 @@ namespace ctrack {
656684 };
657685
658686 template <typename StreamType>
659- void get_summary_table (StreamType& stream, bool use_color = false ) {
687+ void get_summary (StreamType& stream, bool use_color = false , BeautifulTable::ResultFormat resultFormat = BeautifulTable::ResultFormat::TABLE) {
688+
660689 BeautifulTable info ({ " Start" ," End" ," time total" ," time ctracked" ," time ctracked %" , }, use_color, alternate_colors);
661- info.addRow ({ BeautifulTable::table_timepoint (track_start_time), BeautifulTable::table_timepoint (track_end_time),
662- BeautifulTable::table_time (time_total), BeautifulTable::table_time (sum_time_active_exclusive),
690+ info.addRow ({
691+ BeautifulTable::table_timepoint (track_start_time),
692+ BeautifulTable::table_timepoint (track_end_time),
693+ BeautifulTable::table_time (time_total),
694+ BeautifulTable::table_time (sum_time_active_exclusive),
663695 BeautifulTable::table_percentage (sum_time_active_exclusive, time_total)
664- });
696+ });
697+
698+ switch (resultFormat) {
699+ case BeautifulTable::ResultFormat::TABLE:
700+ info.print (stream);
701+ break ;
702+ case BeautifulTable::ResultFormat::CSV:
703+ info.csv (stream);
704+ break ;
705+ case BeautifulTable::ResultFormat::JSON:
706+ case BeautifulTable::ResultFormat::DB:
707+ default :
708+ std::cerr << " Invalid choice, DB and JSON not yet supported." << std::endl;
709+ break ;
710+ }
665711
666- info.print (stream);
667712 BeautifulTable table ({ " filename" , " function" , " line" ," calls" ," ae" + center_intervall_str + " %" ," ae[0-100]%" ,
668- " time ae[0-100]" ," time a[0-100]" }, use_color, alternate_colors);
713+ " time ae[0-100]" ," time a[0-100]" }, use_color, alternate_colors);
669714 for (auto & entry : sorted_events) {
670- table.addRow ({ BeautifulTable::stable_shortenPath (entry->filename ), entry->function_name ,BeautifulTable::table_string (entry->line ),
715+ table.addRow ({
716+ BeautifulTable::stable_shortenPath (entry->filename ), entry->function_name ,
717+ BeautifulTable::table_string (entry->line ),
671718 BeautifulTable::table_string (entry->all_cnt ),
672719 BeautifulTable::table_percentage (entry->center_time_active_exclusive , time_total),
673- BeautifulTable::table_percentage (entry->all_time_active_exclusive ,time_total),
720+ BeautifulTable::table_percentage (entry->all_time_active_exclusive , time_total),
674721 BeautifulTable::table_time (entry->all_time_active_exclusive ),
675722 BeautifulTable::table_time (entry->all_time_active ) });
676723 }
677724
678- table.print (stream);
725+ switch (resultFormat) {
726+ case BeautifulTable::ResultFormat::TABLE:
727+ table.print (stream);
728+ break ;
729+ case BeautifulTable::ResultFormat::CSV:
730+ table.csv (stream);
731+ break ;
732+ case BeautifulTable::ResultFormat::JSON:
733+ case BeautifulTable::ResultFormat::DB:
734+ default :
735+ std::cerr << " Invalid choice, DB and JSON not yet supported." << std::endl;
736+ break ;
737+ }
679738 };
680739
681740 template <typename StreamType>
@@ -687,21 +746,30 @@ namespace ctrack {
687746 auto & entry = sorted_events[i];
688747
689748 BeautifulTable info ({ " filename" , " function" , " line" ," time acc" ," sd" ," cv" ," calls" ," threads" }, use_color, default_colors);
690- info.addRow ({ BeautifulTable::stable_shortenPath (entry->filename ), entry->function_name ,BeautifulTable::table_string (entry->line ),
749+ info.addRow ({
750+ BeautifulTable::stable_shortenPath (entry->filename ), entry->function_name ,
751+ BeautifulTable::table_string (entry->line ),
691752 BeautifulTable::table_time (entry->all_time_acc ),
692- BeautifulTable::table_time (sorted_events[i]->all_st ),BeautifulTable::table_string (sorted_events[i]->all_cv ),
693- BeautifulTable::table_string (sorted_events[i]->all_cnt ), BeautifulTable::table_string (sorted_events[i]->all_thread_cnt ) });
753+ BeautifulTable::table_time (sorted_events[i]->all_st ),
754+ BeautifulTable::table_string (sorted_events[i]->all_cv ),
755+ BeautifulTable::table_string (sorted_events[i]->all_cnt ),
756+ BeautifulTable::table_string (sorted_events[i]->all_thread_cnt ) });
694757
695758 BeautifulTable table ({ " min" , " mean" , " min" ," mean" ," med" ," time a" ," time ae" ," max" ," mean" ," max" }, use_color, default_colors,
696759 { {" fastest[0-" + std::to_string (settings.non_center_percent ) + " ]%" ,2 },{" center" + center_intervall_str + " %" ,6 },
697760 {" slowest[" + std::to_string (100 - settings.non_center_percent ) + " -100]%" ,2 } });
698761
699- table.addRow ({ BeautifulTable::table_time (entry->fastest_min ),BeautifulTable::table_time (entry->fastest_mean ),
700- BeautifulTable::table_time (entry->center_min ),BeautifulTable::table_time (entry->center_mean ),
701- BeautifulTable::table_time (entry->center_med ),BeautifulTable::table_time (entry->center_time_active ),
762+ table.addRow ({
763+ BeautifulTable::table_time (entry->fastest_min ),
764+ BeautifulTable::table_time (entry->fastest_mean ),
765+ BeautifulTable::table_time (entry->center_min ),
766+ BeautifulTable::table_time (entry->center_mean ),
767+ BeautifulTable::table_time (entry->center_med ),
768+ BeautifulTable::table_time (entry->center_time_active ),
702769 BeautifulTable::table_time (entry->center_time_active_exclusive ),
703770 BeautifulTable::table_time (entry->center_max ),
704- BeautifulTable::table_time (entry->slowest_mean ),BeautifulTable::table_time (entry->slowest_max ) });
771+ BeautifulTable::table_time (entry->slowest_mean ),
772+ BeautifulTable::table_time (entry->slowest_max ) });
705773
706774 info.print (stream);
707775 table.print (stream);
@@ -930,14 +998,20 @@ namespace ctrack {
930998 std::cout << " Details" << std::endl;
931999 res.get_detail_table (std::cout, true );
9321000 std::cout << " Summary" << std::endl;
933- res.get_summary_table (std::cout, true );
1001+ res.get_summary (std::cout, true );
1002+ }
1003+
1004+ inline void result_csv (ctrack_result_settings settings = {}, const std::filesystem::path& path = std::filesystem::path(" result.csv" )) {
1005+ auto res = calc_stats_and_clear (settings);
1006+ res.get_summary (std::cout, false , BeautifulTable::ResultFormat::CSV);
1007+ // TODO write to file
9341008 }
9351009
9361010 inline std::string result_as_string (ctrack_result_settings settings = {}) {
9371011 auto res = calc_stats_and_clear (settings);
9381012 std::stringstream ss;
9391013 ss << " Summary\n " ;
940- res.get_summary_table (ss, false );
1014+ res.get_summary (ss, false );
9411015 ss << " Details\n " ;
9421016 res.get_detail_table (ss, false , true );
9431017
@@ -984,4 +1058,4 @@ namespace ctrack {
9841058#define CTRACK_NAME (name )
9851059#endif // CTRACK_DISABLE
9861060
987- #endif
1061+ #endif
0 commit comments