Skip to content

Commit

Permalink
Add debugging options to trace kernel<->client messages. Ensure LaTeX…
Browse files Browse the repository at this point in the history
… cells stay open when microtex throws an exception, and report an error.
  • Loading branch information
Kasper Peeters committed Nov 21, 2024
1 parent 973a5df commit fda46f1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 33 deletions.
19 changes: 19 additions & 0 deletions client_server/ComputeThread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
std::cerr << "cadabra-client: cannot parse message." << std::endl;
return;
}
if(getenv("CDB_SHOW_RECEIVED")) {
std::cerr << "RECV: " << root.dump(3) << std::endl;
}

if(root.count("header")==0) {
std::cerr << "cadabra-client: received message without 'header'." << std::endl;
return;
Expand Down Expand Up @@ -498,6 +502,9 @@ void ComputeThread::execute_interactive(uint64_t id, const std::string& code)

std::ostringstream oss;
oss << req << std::endl;
if(getenv("CDB_SHOW_SENT")) {
std::cerr << "SEND: " << req.dump(3) << std::endl;
}
wsclient.send(our_connection_hdl, oss.str(), websocketpp::frame::opcode::text);
interactive_cells.insert(id);
}
Expand Down Expand Up @@ -557,6 +564,9 @@ void ComputeThread::execute_cell(DTree::iterator it)
gui->on_kernel_runstatus(true);
std::ostringstream str;
str << req << std::endl;
if(getenv("CDB_SHOW_SENT")) {
std::cerr << "SEND: " << req.dump(3) << std::endl;
}
wsclient.send(our_connection_hdl, str.str(), websocketpp::frame::opcode::text);
// NOTE: we can get a return message in on_message at any point after this,
// it will come in on a different thread!
Expand Down Expand Up @@ -597,6 +607,9 @@ void ComputeThread::stop()
// std::cerr << str.str() << std::endl;

server_pid=0;
if(getenv("CDB_SHOW_SENT")) {
std::cerr << "SEND: " << req.dump(3) << std::endl;
}
wsclient.send(our_connection_hdl, str.str(), websocketpp::frame::opcode::text);
all_cells_nonrunning();
}
Expand Down Expand Up @@ -626,6 +639,9 @@ void ComputeThread::restart_kernel()

// std::cerr << str.str() << std::endl;

if(getenv("CDB_SHOW_SENT")) {
std::cerr << "SEND: " << req.dump(3) << std::endl;
}
wsclient.send(our_connection_hdl, str.str(), websocketpp::frame::opcode::text);
docthread->on_interactive_output(req);
}
Expand Down Expand Up @@ -669,6 +685,9 @@ bool ComputeThread::complete(DTree::iterator it, int pos, int alternative)
// std::cerr << str.str() << std::endl;

server_pid=0;
if(getenv("CDB_SHOW_SENT")) {
std::cerr << "SEND: " << req.dump(3) << std::endl;
}
wsclient.send(our_connection_hdl, str.str(), websocketpp::frame::opcode::text);

return true;
Expand Down
10 changes: 7 additions & 3 deletions client_server/Server.hh
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ class Server {
Stopwatch sympy_stopwatch;

/// Raw code to send a string (which must be JSON formatted) as
/// a message to the client. Handles communication of the result back to the
/// client in JSON format. This is always of the form
/// a message to the client. Handles communication of the result
/// back to the client in JSON format. This is always of the
/// form
///
/// { "header": { "parent_id": "...",
/// "parent_origin": "client" | "server",
/// "cell_id": "...",
/// "cell_origin": "client" | "server"
/// },
/// "content": { "output": "..." },
/// "content": { "output": "...",
/// "width": int (optional),
/// "height": int (optional)
/// },
/// "msg_type": "..."
/// }
///
Expand Down
74 changes: 48 additions & 26 deletions frontend/gtkmm/NotebookWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,6 @@ bool NotebookWindow::cell_content_execute(DTree::iterator it, int canvas_number,
set_stop_sensitive(true);
follow_cell=it;
// std::cerr << "Executing cell " << it->id().id << std::endl;
compute->execute_cell(it);

// If this is a LaTeX input cell, and auto-close is turned on, close
// the input cell. Make sure to also feed that into the document
Expand All @@ -1871,35 +1870,58 @@ bool NotebookWindow::cell_content_execute(DTree::iterator it, int canvas_number,
}
}
}


// Execute the cell. Make sure this comes after the hiding logic above.
compute->execute_cell(it);

return true;
}

bool NotebookWindow::on_tex_error(const std::string& str, DTree::iterator )
bool NotebookWindow::on_tex_error(const std::string& str, DTree::iterator it)
{
// Gtk::Dialog md;
Gtk::MessageDialog md("Generic TeX error", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
md.set_resizable(true);
// Gtk::Button ok(Gtk::Stock::OK);
md.set_transient_for(*this);
md.set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG);
auto box = md.get_message_area();
// md.add_button(Gtk::Stock::OK, 1);
Gtk::ScrolledWindow sw;
Gtk::TextView tv;
auto buffer = tv.get_buffer();
buffer->set_text(str);
// auto iter = buffer->get_iter_at_offset(0);
// buffer->insert(iter, str);
tv.set_editable(false);
box->add(sw);
sw.add(tv);
auto context = tv.get_style_context();
context->add_class("error");
auto screen = Gdk::Screen::get_default();
sw.set_size_request(screen->get_width()/4, screen->get_width()/4);
sw.show_all();
md.run();
// Re-open the input cell if it was hidden, so we can fix the error.
DTree::iterator pit = doc.parent(it);
for(unsigned int i=0; i<canvasses.size(); ++i) {
auto vis = canvasses[i]->visualcells.find(&(*pit));
if(vis==canvasses[i]->visualcells.end()) {
}
else {
Gtk::Widget& w = (*vis).second.inbox->edit;
// Probably a bug: if we call show() without the delay below, we can have
// the hide() and show() happen very quickly after each other, which does not
// work (widget stays hidden).
Glib::signal_timeout().connect_once([&w]() { w.show(); }, 50);

pit->hidden=false;
}
}

if(!prefs.microtex) {
// Show the error in a dialog.
Gtk::MessageDialog md("Generic TeX error", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
md.set_resizable(true);
// Gtk::Button ok(Gtk::Stock::OK);
md.set_transient_for(*this);
md.set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG);
auto box = md.get_message_area();
// md.add_button(Gtk::Stock::OK, 1);
Gtk::ScrolledWindow sw;
Gtk::TextView tv;
auto buffer = tv.get_buffer();
buffer->set_text(str);
// auto iter = buffer->get_iter_at_offset(0);
// buffer->insert(iter, str);
tv.set_editable(false);
box->add(sw);
sw.add(tv);
auto context = tv.get_style_context();
context->add_class("error");
auto screen = Gdk::Screen::get_default();
sw.set_size_request(screen->get_width()/4, screen->get_width()/4);
sw.show_all();
md.run();
}

return true;
}

Expand Down
22 changes: 19 additions & 3 deletions frontend/gtkmm/TeXView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
using namespace cadabra;

TeXView::TeXView(TeXEngine& eng, DTree::iterator it, bool use_microtex_, int hmargin)
: content(0), datacell(it), vbox(false, 10), hbox(false, hmargin), image(use_microtex_), engine(eng), use_microtex(use_microtex_)
: content(0)
, datacell(it)
, vbox(false, 10)
, hbox(false, hmargin)
, image(use_microtex_, this)
, engine(eng)
, use_microtex(use_microtex_)
{
// Still need to checkin even when using MicroTeX, otherwise
// all requests will be empty.
Expand Down Expand Up @@ -247,7 +253,16 @@ void TeXView::TeXArea::layout_latex() const
0xff424242);
}
catch(std::exception& err) {
std::cerr << "MicroTeX::parse: exception parsing input (" << fixed << "): " << err.what() << std::endl;
// std::cerr << "MicroTeX::parse: exception parsing input (" << fixed << "): " << err.what() << std::endl;
owner->tex_error.emit(err.what());

// Instead put an error message in the output cell.
_render = microtex::MicroTeX::parse(
"\\text{\\textcolor{red}{\\LaTeX error, probably mismatching brackets.}}", //microtex::utf82wide(fixed),
rendering_width,
_text_size,
_text_size / 3.f,
0xff424242);
}
}

Expand Down Expand Up @@ -441,8 +456,9 @@ void TeXView::TeXArea::set_latex(const std::string& latex)
// std::cout << "**** fixed to " << fixed << std::endl;
}

TeXView::TeXArea::TeXArea(bool use_microtex_)
TeXView::TeXArea::TeXArea(bool use_microtex_, TeXView *owner_)
: rendering_width(1), use_microtex(use_microtex_)
, owner(owner_)
, _render(nullptr), _text_size(5.f)
, padding_x(15), padding_y(10)
{
Expand Down
4 changes: 3 additions & 1 deletion frontend/gtkmm/TeXView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace cadabra {

class TeXArea : public Gtk::DrawingArea {
public:
TeXArea(bool use_microtex);
TeXArea(bool use_microtex, TeXView *owner_);
~TeXArea();

virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
Expand Down Expand Up @@ -72,6 +72,7 @@ namespace cadabra {
void layout_latex() const;

bool use_microtex;

protected:
Gtk::SizeRequestMode get_request_mode_vfunc() const override;
void get_preferred_height_for_width_vfunc(int width, int& minimum_height,
Expand All @@ -81,6 +82,7 @@ namespace cadabra {
void on_size_allocate(Gtk::Allocation& allocation) override;

private:
TeXView *owner;
mutable int rendering_width;
int padding_x, padding_y;

Expand Down

0 comments on commit fda46f1

Please sign in to comment.