Panda3D
textStats.cxx
1 // Filename: textStats.cxx
2 // Created by: drose (12Jul00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "textStats.h"
16 #include "textMonitor.h"
17 
18 #include "pStatServer.h"
19 #include "config_pstats.h"
20 #include "pystub.h"
21 
22 #include <signal.h>
23 
24 static bool user_interrupted = false;
25 
26 // This simple signal handler lets us know when the user has pressed
27 // control-C, so we can clean up nicely.
28 static void signal_handler(int) {
29  user_interrupted = true;
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: TextStats::Constructor
34 // Access: Public
35 // Description:
36 ////////////////////////////////////////////////////////////////////
37 TextStats::
38 TextStats() {
39  set_program_brief("text-based PStats client");
40  set_program_description
41  ("This is a simple PStats server that listens on a TCP port for a "
42  "connection from a PStatClient in a Panda player. It will then report "
43  "frame rate and timing information sent by the player.");
44 
45  add_option
46  ("p", "port", 0,
47  "Specify the TCP port to listen for connections on. By default, this "
48  "is taken from the pstats-host Config variable.",
49  &TextStats::dispatch_int, NULL, &_port);
50 
51  add_option
52  ("r", "", 0,
53  "Show the raw frame data, in addition to boiling it down to a total "
54  "time per collector.",
55  &TextStats::dispatch_none, &_show_raw_data, NULL);
56 
57  add_option
58  ("o", "filename", 0,
59  "Filename where to print. If not given then stderr is being used.",
60  &TextStats::dispatch_string, &_got_outputFileName, &_outputFileName);
61 
62  _outFile = NULL;
63  _port = pstats_port;
64 }
65 
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: TextStats::make_monitor
69 // Access: Public
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 PStatMonitor *TextStats::
73 make_monitor() {
74 
75  return new TextMonitor(this, _outFile, _show_raw_data);
76 }
77 
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: TextStats::run
81 // Access: Public
82 // Description:
83 ////////////////////////////////////////////////////////////////////
84 void TextStats::
85 run() {
86  // Set up a global signal handler to catch Interrupt (Control-C) so
87  // we can clean up nicely if the user stops us.
88  signal(SIGINT, &signal_handler);
89 
90  if (!listen(_port)) {
91  nout << "Unable to open port.\n";
92  exit(1);
93  }
94 
95  nout << "Listening for connections.\n";
96 
97  if (_got_outputFileName) {
98  _outFile = new ofstream(_outputFileName.c_str(), ios::out);
99  } else {
100  _outFile = &(nout);
101  }
102 
103  main_loop(&user_interrupted);
104  nout << "Exiting.\n";
105 }
106 
107 
108 int main(int argc, char *argv[]) {
109  // A call to pystub() to force libpystub.so to be linked in.
110  pystub();
111 
112  TextStats prog;
113  prog.parse_command_line(argc, argv);
114  prog.run();
115  return 0;
116 }
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
A simple, scrolling-text stats server.
Definition: textStats.h:31
This is an abstract class that presents the interface to any number of different front-ends for the s...
Definition: pStatMonitor.h:43
A simple, scrolling-text stats monitor.
Definition: textMonitor.h:32
bool listen(int port=-1)
Establishes a port number that the manager will listen on for TCP connections.
Definition: pStatServer.cxx:59
void main_loop(bool *interrupt_flag=NULL)
An alternative to repeatedly calling poll(), this function yields control of the program to the PStat...