From 902703efa9425a0630053c10229de911e23a2a43 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 1 Aug 2016 03:43:23 -0400 Subject: Show colorized log output in messages window --- ingen/Log.hpp | 5 +++ src/Log.cpp | 6 ++- src/gui/App.cpp | 16 ++++---- src/gui/MessagesWindow.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++--- src/gui/MessagesWindow.hpp | 23 +++++++++--- src/gui/ingen_gui.ui | 4 +- wscript | 6 +++ 7 files changed, 133 insertions(+), 20 deletions(-) diff --git a/ingen/Log.hpp b/ingen/Log.hpp index a868e714..189754e2 100644 --- a/ingen/Log.hpp +++ b/ingen/Log.hpp @@ -17,6 +17,7 @@ #ifndef INGEN_LOG_HPP #define INGEN_LOG_HPP +#include #include #include @@ -33,6 +34,8 @@ class URIs; class INGEN_API Log { public: + typedef std::function Sink; + Log(LV2_Log_Log* log, URIs& uris); struct Feature : public LV2Features::Feature { @@ -60,12 +63,14 @@ public: void set_flush(bool f) { _flush = f; } void set_trace(bool f) { _trace = f; } + void set_sink(Sink s) { _sink = s; } private: void print(FILE* stream, const std::string& msg); LV2_Log_Log* _log; URIs& _uris; + Sink _sink; bool _flush; bool _trace; }; diff --git a/src/Log.cpp b/src/Log.cpp index b2d30b7d..46b88aec 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -98,7 +98,11 @@ int Log::vtprintf(LV2_URID type, const char* fmt, va_list args) { int ret = 0; - if (_log) { + if (type == _uris.log_Trace && !_trace) { + return 0; + } else if (_sink) { + ret = _sink(type, fmt, args); + } else if (_log) { ret = _log->vprintf(_log->handle, type, fmt, args); } else if (type == _uris.log_Error) { ColorContext ctx(stderr, ColorContext::Color::RED); diff --git a/src/gui/App.cpp b/src/gui/App.cpp index 2624d1e9..fad2f195 100644 --- a/src/gui/App.cpp +++ b/src/gui/App.cpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2015 David Robillard + Copyright 2007-2016 David Robillard Ingen is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free @@ -91,6 +91,9 @@ App::App(Ingen::World* world) PluginModel::set_rdf_world(*world->rdf_world()); PluginModel::set_lilv_world(world->lilv_world()); + + using namespace std::placeholders; + world->log().set_sink(std::bind(&MessagesWindow::log, _messages_window, _1, _2, _3)); } App::~App() @@ -214,12 +217,7 @@ App::response(int32_t id, Status status, const std::string& subject) void App::error_message(const string& str) { - _messages_window->post(str); - - if (!_messages_window->is_visible()) - _messages_window->present(); - - _messages_window->set_urgency_hint(true); + _messages_window->post_error(str); } void @@ -321,6 +319,10 @@ App::gtk_main_iteration() if (!_client) return false; + if (_messages_window) { + _messages_window->flush(); + } + if (_world->engine()) { if (!_world->engine()->main_iteration()) { Gtk::Main::quit(); diff --git a/src/gui/MessagesWindow.cpp b/src/gui/MessagesWindow.cpp index 262cb817..fb29716c 100644 --- a/src/gui/MessagesWindow.cpp +++ b/src/gui/MessagesWindow.cpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2015 David Robillard + Copyright 2007-2016 David Robillard Ingen is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free @@ -14,9 +14,13 @@ along with Ingen. If not, see . */ -#include "MessagesWindow.hpp" #include +#include "ingen/URIs.hpp" + +#include "App.hpp" +#include "MessagesWindow.hpp" + namespace Ingen { namespace GUI { using std::string; @@ -31,17 +35,96 @@ MessagesWindow::MessagesWindow(BaseObjectType* cobject, _clear_button->signal_clicked().connect(sigc::mem_fun(this, &MessagesWindow::clear_clicked)); _close_button->signal_clicked().connect(sigc::mem_fun(this, &Window::hide)); + + for (int s = Gtk::STATE_NORMAL; s <= Gtk::STATE_INSENSITIVE; ++s) { + _textview->modify_base((Gtk::StateType)s, Gdk::Color("#000000")); + _textview->modify_text((Gtk::StateType)s, Gdk::Color("#EEEEEC")); + } +} + +void +MessagesWindow::init_window(App& app) +{ + Glib::RefPtr tag = Gtk::TextTag::create(); + tag->property_foreground() = "#EF2929"; + _tags.emplace(app.uris().log_Error, tag); + _error_tag = tag; + + tag = Gtk::TextTag::create(); + tag->property_foreground() = "#FCAF3E"; + _tags.emplace(app.uris().log_Warning, tag); + + tag = Gtk::TextTag::create(); + tag->property_foreground() = "#8AE234"; + _tags.emplace(app.uris().log_Trace, tag); + + for (const auto& t : _tags) { + _textview->get_buffer()->get_tag_table()->add(t.second); + } } void -MessagesWindow::post(const string& msg) +MessagesWindow::post_error(const string& msg) { Glib::RefPtr text_buf = _textview->get_buffer(); - text_buf->insert(text_buf->end(), msg); + text_buf->insert_with_tag(text_buf->end(), msg, _error_tag); text_buf->insert(text_buf->end(), "\n"); - if (!_clear_button->is_sensitive()) + if (!_clear_button->is_sensitive()) { + _clear_button->set_sensitive(true); + } + + set_urgency_hint(true); + if (!is_visible()) { + present(); + } +} + +int +MessagesWindow::log(LV2_URID type, const char* fmt, va_list args) +{ + std::lock_guard lock(_mutex); + +#ifdef HAVE_VASPRINTF + char* buf = NULL; + const int len = vasprintf(&buf, fmt, args); +#else + char* buf = g_strdup_vprintf(fmt, args); + const int len = strlen(buf); +#endif + + _stream << type << ' ' << buf << '\0'; + free(buf); + + return len; +} + +void +MessagesWindow::flush() +{ + LV2_URID type; + std::string line; + { + std::lock_guard lock(_mutex); + if (!_stream.rdbuf()->in_avail()) { + return; + } + _stream >> type; + std::getline(_stream, line, '\0'); + } + + Glib::RefPtr text_buf = _textview->get_buffer(); + + auto t = _tags.find(type); + if (t != _tags.end()) { + text_buf->insert_with_tag(text_buf->end(), line, t->second); + } else { + text_buf->insert(text_buf->end(), line); + } + + if (!_clear_button->is_sensitive()) { _clear_button->set_sensitive(true); + } } void diff --git a/src/gui/MessagesWindow.hpp b/src/gui/MessagesWindow.hpp index f3215daf..193ef18d 100644 --- a/src/gui/MessagesWindow.hpp +++ b/src/gui/MessagesWindow.hpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2015 David Robillard + Copyright 2007-2016 David Robillard Ingen is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free @@ -17,11 +17,14 @@ #ifndef INGEN_GUI_MESSAGESWINDOW_HPP #define INGEN_GUI_MESSAGESWINDOW_HPP +#include +#include #include #include #include #include +#include "lv2/lv2plug.in/ns/ext/log/log.h" #include "Window.hpp" @@ -41,14 +44,24 @@ public: MessagesWindow(BaseObjectType* cobject, const Glib::RefPtr& xml); - void post(const std::string& str); + void init_window(App& app); + + int log(LV2_URID type, const char* fmt, va_list args); + void flush(); + + void post_error(const std::string& str); private: void clear_clicked(); - Gtk::TextView* _textview; - Gtk::Button* _clear_button; - Gtk::Button* _close_button; + std::mutex _mutex; + std::stringstream _stream; + Gtk::TextView* _textview; + Gtk::Button* _clear_button; + Gtk::Button* _close_button; + + Glib::RefPtr _error_tag; + std::map< LV2_URID, Glib::RefPtr > _tags; }; } // namespace GUI diff --git a/src/gui/ingen_gui.ui b/src/gui/ingen_gui.ui index 646cd34d..7d250e69 100644 --- a/src/gui/ingen_gui.ui +++ b/src/gui/ingen_gui.ui @@ -1876,8 +1876,8 @@ See COPYING file included with this distribution, or http://www.gnu.org/licenses True True - 5 - 5 + 1 + 1 False word 5 diff --git a/wscript b/wscript index f898b24a..20a2037d 100644 --- a/wscript +++ b/wscript @@ -92,6 +92,12 @@ def configure(conf): define_name = 'HAVE_ISATTY', mandatory = False) + conf.check(function_name = 'vasprintf', + header_name = 'stdio.h', + defines = '_GNU_SOURCE=1', + define_name = 'HAVE_VASPRINTF', + mandatory = False) + if not Options.options.no_socket: conf.check(function_name = 'socket', header_name = 'sys/socket.h', -- cgit v1.2.1