From c9dd462b125ad259323039c563280a14c0f89b31 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 26 Sep 2008 02:08:39 +0000 Subject: [PATCH] enhancements from community member Clemens Pecinovsky to support writing output to a file --- pandatool/src/text-stats/textMonitor.cxx | 23 +++++++++++++---------- pandatool/src/text-stats/textMonitor.h | 10 +++++++++- pandatool/src/text-stats/textStats.cxx | 15 ++++++++++++++- pandatool/src/text-stats/textStats.h | 9 +++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/pandatool/src/text-stats/textMonitor.cxx b/pandatool/src/text-stats/textMonitor.cxx index e7595ee016..0ce67faa08 100644 --- a/pandatool/src/text-stats/textMonitor.cxx +++ b/pandatool/src/text-stats/textMonitor.cxx @@ -25,7 +25,9 @@ // Description: //////////////////////////////////////////////////////////////////// TextMonitor:: -TextMonitor(TextStats *server) : PStatMonitor(server) { +TextMonitor(TextStats *server, ostream *outStream, bool show_raw_data ) : PStatMonitor(server) { + _outStream = outStream; //[PECI] + _show_raw_data = show_raw_data; } //////////////////////////////////////////////////////////////////// @@ -105,30 +107,30 @@ new_data(int thread_index, int frame_number) { if (view.all_collectors_known()) { const PStatClientData *client_data = get_client_data(); - nout << "\rThread " + (*_outStream) << "\rThread " << client_data->get_thread_name(thread_index) << " frame " << frame_number << ", " << view.get_net_value() * 1000.0 << " ms (" << thread_data->get_frame_rate() << " Hz):\n"; - if (get_server()->_show_raw_data) { + if (_show_raw_data) { const PStatFrameData &frame_data = thread_data->get_frame(frame_number); - nout << "raw data:\n"; + (*_outStream) << "raw data:\n"; int num_events = frame_data.get_num_events(); for (int i = 0; i < num_events; ++i) { // The iomanipulators are much too clumsy. char formatted[32]; sprintf(formatted, "%15.06lf", frame_data.get_time(i)); - nout << formatted; + (*_outStream) << formatted; if (frame_data.is_start(i)) { - nout << " start "; + (*_outStream) << " start "; } else { - nout << " stop "; + (*_outStream) << " stop "; } int collector_index = frame_data.get_time_collector(i); - nout << client_data->get_collector_fullname(collector_index) << "\n"; + (*_outStream) << client_data->get_collector_fullname(collector_index) << "\n"; } } @@ -152,6 +154,7 @@ new_data(int thread_index, int frame_number) { } } } + _outStream->flush(); } @@ -196,7 +199,7 @@ show_ms(const PStatViewLevel *level, int indent_level) { const PStatClientData *client_data = get_client_data(); const PStatCollectorDef &def = client_data->get_collector_def(collector_index); - indent(nout, indent_level) + indent((*_outStream), indent_level) << def._name << " = " << level->get_net_value() * 1000.0 << " ms\n" ; int num_children = level->get_num_children(); @@ -217,7 +220,7 @@ show_level(const PStatViewLevel *level, int indent_level) { const PStatClientData *client_data = get_client_data(); const PStatCollectorDef &def = client_data->get_collector_def(collector_index); - indent(nout, indent_level) + indent((*_outStream), indent_level) << def._name << " = " << level->get_net_value() << " " << def._level_units << "\n"; diff --git a/pandatool/src/text-stats/textMonitor.h b/pandatool/src/text-stats/textMonitor.h index cee34ee35e..2108cd7e4d 100644 --- a/pandatool/src/text-stats/textMonitor.h +++ b/pandatool/src/text-stats/textMonitor.h @@ -18,6 +18,10 @@ #include "pandatoolbase.h" #include "pStatMonitor.h" +//[PECI] +#include +#include + class TextStats; //////////////////////////////////////////////////////////////////// @@ -27,7 +31,7 @@ class TextStats; //////////////////////////////////////////////////////////////////// class TextMonitor : public PStatMonitor { public: - TextMonitor(TextStats *server); + TextMonitor(TextStats *server, ostream *outStream, bool show_raw_data); TextStats *get_server(); virtual string get_monitor_name(); @@ -41,6 +45,10 @@ public: void show_ms(const PStatViewLevel *level, int indent_level); void show_level(const PStatViewLevel *level, int indent_level); + +private: + ostream *_outStream; //[PECI] + bool _show_raw_data; }; #include "textMonitor.I" diff --git a/pandatool/src/text-stats/textStats.cxx b/pandatool/src/text-stats/textStats.cxx index fb0fcac059..0a34978552 100644 --- a/pandatool/src/text-stats/textStats.cxx +++ b/pandatool/src/text-stats/textStats.cxx @@ -53,6 +53,12 @@ TextStats() { "time per collector.", &TextStats::dispatch_none, &_show_raw_data, NULL); + add_option + ("o", "filename", 0, + "Filename where to print. If not given then stderr is being used.", + &TextStats::dispatch_string, &_got_outputFileName, &_outputFileName); + + _outFile = NULL; _port = pstats_port; } @@ -64,7 +70,8 @@ TextStats() { //////////////////////////////////////////////////////////////////// PStatMonitor *TextStats:: make_monitor() { - return new TextMonitor(this); + + return new TextMonitor(this, _outFile, _show_raw_data); } @@ -86,6 +93,12 @@ run() { nout << "Listening for connections.\n"; + if (_got_outputFileName) { + _outFile = new ofstream(_outputFileName.c_str(), ios::out); + } else { + _outFile = &(nout); + } + main_loop(&user_interrupted); nout << "Exiting.\n"; } diff --git a/pandatool/src/text-stats/textStats.h b/pandatool/src/text-stats/textStats.h index fbdd88a786..ef6e1f20a3 100644 --- a/pandatool/src/text-stats/textStats.h +++ b/pandatool/src/text-stats/textStats.h @@ -20,6 +20,9 @@ #include "programBase.h" #include "pStatServer.h" +#include +#include + //////////////////////////////////////////////////////////////////// // Class : TextStats // Description : A simple, scrolling-text stats server. Guaranteed to @@ -33,8 +36,14 @@ public: void run(); +private: int _port; bool _show_raw_data; + + //[PECI] + bool _got_outputFileName; + string _outputFileName; + ostream *_outFile; }; #endif