diff options
Diffstat (limited to 'grapher/grapher.cxx')
-rw-r--r-- | grapher/grapher.cxx | 200 |
1 files changed, 104 insertions, 96 deletions
diff --git a/grapher/grapher.cxx b/grapher/grapher.cxx index 46182178..a0d35017 100644 --- a/grapher/grapher.cxx +++ b/grapher/grapher.cxx @@ -1,127 +1,135 @@ #include "GraphWidget.hxx" +#include "StapParser.hxx" +#include <cerrno> #include <cmath> +#include <cstdio> +#include <iostream> #include <sstream> #include <string> #include <map> +#include <gtkmm.h> +#include <gtkmm/stock.h> #include <gtkmm/main.h> #include <gtkmm/window.h> #include <unistd.h> #include <poll.h> +#include <signal.h> +#include <sys/types.h> +#include <sys/wait.h> using namespace systemtap; -class StapParser +class GrapherWindow : public Gtk::Window { - Glib::ustring _buffer; - typedef std::map<std::string, std::tr1::shared_ptr<GraphData> > DataMap; - DataMap _dataSets; - Gtk::Window& _win; - GraphWidget& _widget; public: - StapParser(Gtk::Window& win, - GraphWidget& widget) : _win(win), _widget(widget) {} - - bool ioCallback(Glib::IOCondition ioCondition) - { - if ((ioCondition & Glib::IO_IN) == 0) - return true; - char buf[256]; - ssize_t bytes_read = 0; - bytes_read = read(0, buf, sizeof(buf) - 1); - if (bytes_read <= 0) - { - _win.hide(); - return true; - } - buf[bytes_read] = '\0'; - _buffer += buf; - Glib::ustring::size_type ret = Glib::ustring::npos; - while ((ret = _buffer.find('\n')) != Glib::ustring::npos) - { - Glib::ustring dataString(_buffer, 0, ret); - if (dataString[0] == '%') - { - size_t found; - if ((found = dataString.find("%Title:") == 0)) - { - std::string title = dataString.substr(7); - _widget.setTitle(title); - } - else if ((found = dataString.find("%XAxisTitle:") == 0)) - { - _widget.setXAxisText(dataString.substr(12)); - } - else if ((found = dataString.find("%YAxisTitle:") == 0)) - { - _widget.setYAxisText(dataString.substr(12)); - } - else if ((found = dataString.find("%YMax:") == 0)) - { - double ymax; - std::istringstream stream(dataString.substr(6)); - stream >> ymax; - // _gdata->scale = ymax; - } - else if ((found = dataString.find("%DataSet:") == 0)) - { - std::tr1::shared_ptr<GraphData> dataSet(new GraphData); - std::string setName; - int hexColor; - std::string style; - std::istringstream stream(dataString.substr(9)); - stream >> setName >> dataSet->scale >> std::hex >> hexColor - >> style; - dataSet->color[0] = (hexColor >> 16) / 255.0; - dataSet->color[1] = ((hexColor >> 8) & 0xff) / 255.0; - dataSet->color[2] = (hexColor & 0xff) / 255.0; - if (style == "dot") - dataSet->style = GraphData::DOT; - _dataSets.insert(std::make_pair(setName, dataSet)); - _widget.addGraphData(dataSet); - } - } - else - { - std::string dataSet; - double time; - double data; - std::istringstream stream(dataString); - stream >> dataSet >> time >> data; - DataMap::iterator itr = _dataSets.find(dataSet); - if (itr != _dataSets.end()) - itr->second->data.push_back(std::make_pair(time, data)); - } - _buffer.erase(0, ret + 1); - } - return true; - } + GrapherWindow(); + virtual ~GrapherWindow() {} + Gtk::VBox m_Box; + GraphWidget w; +protected: + virtual void on_menu_file_quit(); + // menu support + Glib::RefPtr<Gtk::UIManager> m_refUIManager; + Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup; + }; +GrapherWindow::GrapherWindow() +{ + set_title("systemtap grapher"); + add(m_Box); + + + //Create actions for menus and toolbars: + m_refActionGroup = Gtk::ActionGroup::create(); + //File menu: + m_refActionGroup->add(Gtk::Action::create("FileMenu", "File")); + m_refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT), + sigc::mem_fun(*this, &GrapherWindow::on_menu_file_quit)); + m_refUIManager = Gtk::UIManager::create(); + m_refUIManager->insert_action_group(m_refActionGroup); + + add_accel_group(m_refUIManager->get_accel_group()); + //Layout the actions in a menubar and toolbar: + Glib::ustring ui_info = + "<ui>" + " <menubar name='MenuBar'>" + " <menu action='FileMenu'>" + " <menuitem action='FileQuit'/>" + " </menu>" + " </menubar>" + "</ui>"; + try + { + m_refUIManager->add_ui_from_string(ui_info); + } + catch(const Glib::Error& ex) + { + std::cerr << "building menus failed: " << ex.what(); + } + Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar"); + if(pMenubar) + m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK); + m_Box.pack_start(w, Gtk::PACK_EXPAND_WIDGET); + w.show(); + + show_all_children(); + +} +void GrapherWindow::on_menu_file_quit() +{ + hide(); +} + int main(int argc, char** argv) { - Gtk::Main app(argc, argv); + Gtk::Main app(argc, argv); - Gtk::Window win; + GrapherWindow win; - win.set_title("Grapher"); - win.set_default_size(600, 200); + win.set_title("Grapher"); + win.set_default_size(600, 200); - GraphWidget w; - - w.setExtents(0.0, 1.0, 5.0, 0.0); - w.setLineWidth(2); + StapParser stapParser(win, win.w); - StapParser stapParser(win, w); + int childPid = -1; + if (argc > 1) + { + int pipefd[2]; + if (pipe(pipefd) < 0) + { + std::perror("pipe"); + exit(1); + } + if ((childPid = fork()) == -1) + { + exit(1); + } + else if (childPid) + { + dup2(pipefd[0], 0); + close(pipefd[0]); + } + else + { + dup2(pipefd[1], 1); + close(pipefd[1]); + execlp("stap", "stap", argv[1], static_cast<char*>(0)); + exit(1); + return 1; + } + } Glib::signal_io().connect(sigc::mem_fun(stapParser, &StapParser::ioCallback), 0, Glib::IO_IN); - win.add(w); - w.show(); - Gtk::Main::run(win); - + if (childPid > 0) + kill(childPid, SIGTERM); + int status; + while (wait(&status) != -1) + ; return 0; } |