2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2006-09-18 01:26:49 +00:00
|
|
|
#include <errno.h>
|
2007-07-19 03:55:00 +00:00
|
|
|
#include <stdio.h>
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <map>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/i18n.h>
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2013-01-25 23:34:20 +00:00
|
|
|
#include <libtransmission/log.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
2021-11-15 12:26:44 +00:00
|
|
|
#include "Actions.h"
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "MessageLogWindow.h"
|
|
|
|
#include "Prefs.h"
|
|
|
|
#include "PrefsDialog.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
class MessageLogColumnsModel : public Gtk::TreeModelColumnRecord
|
2007-06-22 20:59:23 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
|
|
|
MessageLogColumnsModel()
|
|
|
|
{
|
|
|
|
add(sequence);
|
|
|
|
add(name);
|
|
|
|
add(message);
|
|
|
|
add(tr_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
Gtk::TreeModelColumn<unsigned int> sequence;
|
|
|
|
Gtk::TreeModelColumn<Glib::ustring> name;
|
|
|
|
Gtk::TreeModelColumn<Glib::ustring> message;
|
|
|
|
Gtk::TreeModelColumn<tr_log_message*> tr_msg;
|
2008-03-13 19:34:08 +00:00
|
|
|
};
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
MessageLogColumnsModel const message_log_cols;
|
|
|
|
|
|
|
|
class MessageLogWindow::Impl
|
2007-06-22 20:59:23 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
2022-09-07 22:25:04 +00:00
|
|
|
Impl(MessageLogWindow& window, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core);
|
2021-10-18 20:22:31 +00:00
|
|
|
~Impl();
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(Impl)
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
|
|
|
bool onRefresh();
|
|
|
|
|
|
|
|
void onSaveRequest();
|
2021-12-14 08:43:27 +00:00
|
|
|
void onSaveDialogResponse(std::shared_ptr<Gtk::FileChooserDialog>& d, int response);
|
|
|
|
void doSave(Gtk::Window& parent, Glib::ustring const& filename);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
void onClearRequest();
|
2022-09-07 22:25:04 +00:00
|
|
|
void onPauseToggled(Gio::SimpleAction& action);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
void scroll_to_bottom();
|
|
|
|
void level_combo_changed_cb(Gtk::ComboBox* combo_box);
|
2022-09-07 22:25:04 +00:00
|
|
|
void level_combo_init(Gtk::ComboBox* level_combo) const;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
bool is_pinned_to_new() const;
|
|
|
|
bool isRowVisible(Gtk::TreeModel::const_iterator const& iter) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MessageLogWindow& window_;
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2022-09-07 22:25:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::TreeView* view_ = nullptr;
|
|
|
|
Glib::RefPtr<Gtk::ListStore> store_;
|
|
|
|
Glib::RefPtr<Gtk::TreeModelFilter> filter_;
|
|
|
|
Glib::RefPtr<Gtk::TreeModelSort> sort_;
|
2022-03-12 17:42:43 +00:00
|
|
|
tr_log_level maxLevel_ = TR_LOG_INFO;
|
2021-10-18 20:22:31 +00:00
|
|
|
bool isPaused_ = false;
|
|
|
|
sigc::connection refresh_tag_;
|
2022-03-15 14:52:16 +00:00
|
|
|
std::map<tr_log_level, char const*> const level_names_;
|
2008-03-13 19:34:08 +00:00
|
|
|
};
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
tr_log_message* myTail = nullptr;
|
|
|
|
tr_log_message* myHead = nullptr;
|
|
|
|
|
|
|
|
} // namespace
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2011-01-15 07:57:01 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
/* is the user looking at the latest messages? */
|
2021-10-18 20:22:31 +00:00
|
|
|
bool MessageLogWindow::Impl::is_pinned_to_new() const
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
bool pinned_to_new = false;
|
2011-01-15 07:57:01 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (view_ == nullptr)
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
pinned_to_new = true;
|
2011-01-15 07:57:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::TreeModel::Path first_visible;
|
|
|
|
Gtk::TreeModel::Path last_visible;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (view_->get_visible_range(first_visible, last_visible))
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const row_count = sort_->children().size();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (auto const iter = sort_->children()[row_count - 1]; iter)
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
pinned_to_new = last_visible == sort_->get_path(TR_GTK_TREE_MODEL_CHILD_ITER(iter));
|
2011-01-15 07:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pinned_to_new;
|
2011-01-15 07:57:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MessageLogWindow::Impl::scroll_to_bottom()
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2022-10-13 23:19:37 +00:00
|
|
|
auto const row_count = sort_->children().size();
|
|
|
|
if (row_count == 0)
|
2011-01-15 07:57:01 +00:00
|
|
|
{
|
2022-10-13 23:19:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-10-13 23:19:37 +00:00
|
|
|
if (auto const iter = sort_->children()[row_count - 1]; iter)
|
|
|
|
{
|
|
|
|
view_->scroll_to_row(sort_->get_path(TR_GTK_TREE_MODEL_CHILD_ITER(iter)), 1);
|
2011-01-15 07:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
2006-08-20 22:45:29 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
void MessageLogWindow::Impl::level_combo_init(Gtk::ComboBox* level_combo) const
|
2022-03-15 14:52:16 +00:00
|
|
|
{
|
|
|
|
auto items = std::vector<std::pair<Glib::ustring, int>>{};
|
|
|
|
for (auto const& [level, name] : level_names_)
|
|
|
|
{
|
|
|
|
items.emplace_back(name, level);
|
|
|
|
}
|
2022-09-07 22:25:04 +00:00
|
|
|
gtr_combo_box_set_enum(*level_combo, items);
|
|
|
|
gtr_combo_box_set_active_enum(*level_combo, gtr_pref_int_get(TR_KEY_message_level));
|
2022-03-15 14:52:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MessageLogWindow::Impl::level_combo_changed_cb(Gtk::ComboBox* combo_box)
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const level = static_cast<tr_log_level>(gtr_combo_box_get_active_enum(*combo_box));
|
|
|
|
bool const pinned_to_new = is_pinned_to_new();
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logSetLevel(level);
|
2021-10-18 20:22:31 +00:00
|
|
|
core_->set_pref(TR_KEY_message_level, level);
|
|
|
|
maxLevel_ = level;
|
|
|
|
filter_->refilter();
|
2011-01-15 07:57:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pinned_to_new)
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
scroll_to_bottom();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2006-08-20 22:45:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2011-03-24 18:29:07 +00:00
|
|
|
/* similar to asctime, but is utf8-clean */
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring gtr_asctime(time_t t)
|
2011-03-24 18:29:07 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return Glib::DateTime::create_now_local(t).format("%a %b %e %T %Y"); /* ctime equiv */
|
2011-03-24 18:29:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
void MessageLogWindow::Impl::doSave(Gtk::Window& parent, Glib::ustring const& filename)
|
2007-06-22 20:59:23 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto* fp = fopen(filename.c_str(), "w+");
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (fp == nullptr)
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
auto const errcode = errno;
|
2021-12-14 08:43:27 +00:00
|
|
|
auto w = std::make_shared<Gtk::MessageDialog>(
|
|
|
|
parent,
|
2022-03-15 14:52:16 +00:00
|
|
|
fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
2022-03-21 14:15:48 +00:00
|
|
|
fmt::arg("path", filename),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error", g_strerror(errcode)),
|
|
|
|
fmt::arg("error_code", errcode)),
|
2021-10-18 20:22:31 +00:00
|
|
|
false,
|
2022-09-10 13:19:54 +00:00
|
|
|
TR_GTK_MESSAGE_TYPE(ERROR),
|
|
|
|
TR_GTK_BUTTONS_TYPE(CLOSE));
|
2021-10-18 20:22:31 +00:00
|
|
|
w->set_secondary_text(Glib::strerror(errno));
|
2021-12-14 08:43:27 +00:00
|
|
|
w->signal_response().connect([w](int /*response*/) mutable { w.reset(); });
|
2021-10-18 20:22:31 +00:00
|
|
|
w->show();
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
for (auto const& row : store_->children())
|
2009-07-22 15:55:48 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const* const node = row.get_value(message_log_cols.tr_msg);
|
|
|
|
auto const date = gtr_asctime(node->when);
|
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
auto const it = level_names_.find(node->level);
|
|
|
|
auto const* const level_str = it != std::end(level_names_) ? it->second : "???";
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-10-15 16:13:50 +00:00
|
|
|
fmt::print(fp, "{}\t{}\t{}\t{}\n", date, level_str, node->name, node->message);
|
2009-07-22 15:55:48 +00:00
|
|
|
}
|
2008-03-13 19:34:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
fclose(fp);
|
2007-06-22 20:59:23 +00:00
|
|
|
}
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
void MessageLogWindow::Impl::onSaveDialogResponse(std::shared_ptr<Gtk::FileChooserDialog>& d, int response)
|
2007-06-22 20:59:23 +00:00
|
|
|
{
|
2022-09-10 13:19:54 +00:00
|
|
|
if (response == TR_GTK_RESPONSE_TYPE(ACCEPT))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
doSave(*d, d->get_file()->get_path());
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
d.reset();
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MessageLogWindow::Impl::onSaveRequest()
|
2007-06-22 20:59:23 +00:00
|
|
|
{
|
2022-09-10 13:19:54 +00:00
|
|
|
auto d = std::make_shared<Gtk::FileChooserDialog>(window_, _("Save Log"), TR_GTK_FILE_CHOOSER_ACTION(SAVE));
|
|
|
|
d->add_button(_("_Cancel"), TR_GTK_RESPONSE_TYPE(CANCEL));
|
|
|
|
d->add_button(_("_Save"), TR_GTK_RESPONSE_TYPE(ACCEPT));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
d->signal_response().connect([this, d](int response) mutable { onSaveDialogResponse(d, response); });
|
2021-10-18 20:22:31 +00:00
|
|
|
d->show();
|
2007-06-22 20:59:23 +00:00
|
|
|
}
|
2006-09-18 01:26:49 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MessageLogWindow::Impl::onClearRequest()
|
2007-06-22 20:59:23 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
store_->clear();
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logFreeQueue(myHead);
|
2021-10-06 16:32:17 +00:00
|
|
|
myHead = myTail = nullptr;
|
2006-09-18 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
void MessageLogWindow::Impl::onPauseToggled(Gio::SimpleAction& action)
|
2008-03-19 15:30:21 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
bool value = false;
|
|
|
|
action.get_state(value);
|
|
|
|
|
|
|
|
action.set_state(Glib::Variant<bool>::create(!value));
|
|
|
|
|
|
|
|
isPaused_ = !value;
|
2008-03-19 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-03-12 17:42:43 +00:00
|
|
|
void setForegroundColor(Gtk::CellRendererText* renderer, tr_log_level level)
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2022-03-12 17:42:43 +00:00
|
|
|
switch (level)
|
2021-11-24 21:36:23 +00:00
|
|
|
{
|
2022-03-12 17:42:43 +00:00
|
|
|
case TR_LOG_CRITICAL:
|
|
|
|
case TR_LOG_ERROR:
|
|
|
|
case TR_LOG_WARN:
|
2021-11-24 21:36:23 +00:00
|
|
|
renderer->property_foreground() = "red";
|
2022-03-12 17:42:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_LOG_DEBUG:
|
|
|
|
case TR_LOG_TRACE:
|
|
|
|
renderer->property_foreground() = "forestgreen";
|
|
|
|
break;
|
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
default:
|
2021-11-24 21:36:23 +00:00
|
|
|
renderer->property_foreground_set() = false;
|
2022-03-12 17:42:43 +00:00
|
|
|
break;
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void renderText(
|
|
|
|
Gtk::CellRendererText* renderer,
|
2022-10-08 22:50:03 +00:00
|
|
|
Gtk::TreeModel::const_iterator const& iter,
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::TreeModelColumn<Glib::ustring> const& col)
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const* const node = iter->get_value(message_log_cols.tr_msg);
|
|
|
|
renderer->property_text() = iter->get_value(col);
|
2022-09-10 13:19:54 +00:00
|
|
|
renderer->property_ellipsize() = TR_PANGO_ELLIPSIZE_MODE(END);
|
2021-11-24 21:36:23 +00:00
|
|
|
setForegroundColor(renderer, node->level);
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
void renderTime(Gtk::CellRendererText* renderer, Gtk::TreeModel::const_iterator const& iter)
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const* const node = iter->get_value(message_log_cols.tr_msg);
|
|
|
|
renderer->property_text() = Glib::DateTime::create_now_local(node->when).format("%T");
|
2021-11-24 21:36:23 +00:00
|
|
|
setForegroundColor(renderer, node->level);
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void appendColumn(Gtk::TreeView* view, Gtk::TreeModelColumnBase const& col)
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::TreeViewColumn* c;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (col == message_log_cols.name)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto* r = Gtk::make_managed<Gtk::CellRendererText>();
|
|
|
|
c = Gtk::make_managed<Gtk::TreeViewColumn>(_("Name"), *r);
|
|
|
|
c->set_cell_data_func(*r, [r](auto* /*renderer*/, auto const& iter) { renderText(r, iter, message_log_cols.name); });
|
2022-09-10 13:19:54 +00:00
|
|
|
c->set_sizing(TR_GTK_TREE_VIEW_COLUMN_SIZING(FIXED));
|
2021-10-18 20:22:31 +00:00
|
|
|
c->set_fixed_width(200);
|
|
|
|
c->set_resizable(true);
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
2021-10-18 20:22:31 +00:00
|
|
|
else if (col == message_log_cols.message)
|
|
|
|
{
|
|
|
|
auto* r = Gtk::make_managed<Gtk::CellRendererText>();
|
|
|
|
c = Gtk::make_managed<Gtk::TreeViewColumn>(_("Message"), *r);
|
|
|
|
c->set_cell_data_func(*r, [r](auto* /*renderer*/, auto const& iter) { renderText(r, iter, message_log_cols.message); });
|
2022-09-10 13:19:54 +00:00
|
|
|
c->set_sizing(TR_GTK_TREE_VIEW_COLUMN_SIZING(FIXED));
|
2021-10-18 20:22:31 +00:00
|
|
|
c->set_fixed_width(500);
|
|
|
|
c->set_resizable(true);
|
|
|
|
}
|
|
|
|
else if (col == message_log_cols.sequence)
|
|
|
|
{
|
|
|
|
auto* r = Gtk::make_managed<Gtk::CellRendererText>();
|
|
|
|
c = Gtk::make_managed<Gtk::TreeViewColumn>(_("Time"), *r);
|
|
|
|
c->set_cell_data_func(*r, [r](auto* /*renderer*/, auto const& iter) { renderTime(r, iter); });
|
|
|
|
c->set_resizable(true);
|
|
|
|
}
|
|
|
|
else
|
2008-03-13 19:34:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
g_assert_not_reached();
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
view->append_column(*c);
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
bool MessageLogWindow::Impl::isRowVisible(Gtk::TreeModel::const_iterator const& iter) const
|
|
|
|
{
|
|
|
|
auto const* const node = iter->get_value(message_log_cols.tr_msg);
|
|
|
|
return node != nullptr && node->level <= maxLevel_;
|
2008-03-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
MessageLogWindow::Impl::~Impl()
|
2008-03-16 15:18:19 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
refresh_tag_.disconnect();
|
2008-03-16 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
tr_log_message* addMessages(Glib::RefPtr<Gtk::ListStore> const& store, tr_log_message* head)
|
2008-03-16 15:18:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* i;
|
|
|
|
static unsigned int sequence = 0;
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const default_name = Glib::get_application_name();
|
2008-03-16 15:18:19 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
for (i = head; i != nullptr && i->next != nullptr; i = i->next)
|
2008-03-16 15:18:19 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
char const* name = !std::empty(i->name) ? i->name.c_str() : default_name.c_str();
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
auto row_it = store->prepend();
|
|
|
|
auto& row = *row_it;
|
2021-10-18 20:22:31 +00:00
|
|
|
row[message_log_cols.tr_msg] = i;
|
|
|
|
row[message_log_cols.name] = name;
|
|
|
|
row[message_log_cols.message] = i->message;
|
|
|
|
row[message_log_cols.sequence] = ++sequence;
|
2012-02-04 23:59:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if it's an error message, dump it to the terminal too */
|
|
|
|
if (i->level == TR_LOG_ERROR)
|
2012-02-04 23:59:42 +00:00
|
|
|
{
|
2022-08-26 00:51:13 +00:00
|
|
|
auto gstr = fmt::format("{}:{} {}", i->file, i->line, i->message);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if (!std::empty(i->name))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-26 00:51:13 +00:00
|
|
|
gstr += fmt::format(" ({})", i->name.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
g_warning("%s", gstr.c_str());
|
2012-02-04 23:59:42 +00:00
|
|
|
}
|
2008-03-16 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return i; /* tail */
|
2008-03-16 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool MessageLogWindow::Impl::onRefresh()
|
2008-03-16 15:18:19 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
bool const pinned_to_new = is_pinned_to_new();
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (!isPaused_)
|
2008-03-16 15:18:19 +00:00
|
|
|
{
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto* msgs = tr_logGetQueue(); msgs != nullptr)
|
2008-03-19 15:30:21 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* add the new messages and append them to the end of
|
|
|
|
* our persistent list */
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_log_message* tail = addMessages(store_, msgs);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (myTail != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
myTail->next = msgs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
myHead = msgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
myTail = tail;
|
2008-03-19 15:30:21 +00:00
|
|
|
}
|
2011-01-15 07:57:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pinned_to_new)
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
scroll_to_bottom();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-03-16 15:18:19 +00:00
|
|
|
}
|
2011-03-04 14:31:23 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
return true;
|
2008-03-16 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2008-03-19 15:30:21 +00:00
|
|
|
/**
|
|
|
|
*** Public Functions
|
|
|
|
**/
|
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
std::unique_ptr<MessageLogWindow> MessageLogWindow::create(Gtk::Window& parent, Glib::RefPtr<Session> const& core)
|
2008-03-19 15:30:21 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const builder = Gtk::Builder::create_from_resource(gtr_get_full_resource_path("MessageLogWindow.ui"));
|
|
|
|
return std::unique_ptr<MessageLogWindow>(
|
|
|
|
gtr_get_widget_derived<MessageLogWindow>(builder, "MessageLogWindow", parent, core));
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
MessageLogWindow::MessageLogWindow(
|
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Gtk::Window& parent,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
|
|
|
: Gtk::Window(cast_item)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, builder, core))
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
|
|
|
set_transient_for(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageLogWindow::~MessageLogWindow() = default;
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
MessageLogWindow::Impl::Impl(
|
|
|
|
MessageLogWindow& window,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
2021-10-18 20:22:31 +00:00
|
|
|
: window_(window)
|
|
|
|
, core_(core)
|
2022-09-07 22:25:04 +00:00
|
|
|
, view_(gtr_get_widget<Gtk::TreeView>(builder, "messages_view"))
|
2022-10-11 15:39:41 +00:00
|
|
|
, level_names_{ {
|
|
|
|
{ TR_LOG_CRITICAL, C_("Logging level", "Critical") },
|
|
|
|
{ TR_LOG_ERROR, C_("Logging level", "Error") },
|
|
|
|
{ TR_LOG_WARN, C_("Logging level", "Warning") },
|
|
|
|
{ TR_LOG_INFO, C_("Logging level", "Information") },
|
|
|
|
{ TR_LOG_DEBUG, C_("Logging level", "Debug") },
|
|
|
|
{ TR_LOG_TRACE, C_("Logging level", "Trace") },
|
|
|
|
} }
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** toolbar
|
|
|
|
**/
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const action_group = Gio::SimpleActionGroup::create();
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const save_action = Gio::SimpleAction::create("save-message-log");
|
|
|
|
save_action->signal_activate().connect([this](auto const& /*value*/) { onSaveRequest(); });
|
|
|
|
action_group->add_action(save_action);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const clear_action = Gio::SimpleAction::create("clear-message-log");
|
|
|
|
clear_action->signal_activate().connect([this](auto const& /*value*/) { onClearRequest(); });
|
|
|
|
action_group->add_action(clear_action);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const pause_action = Gio::SimpleAction::create_bool("pause-message-log");
|
2022-09-30 22:15:21 +00:00
|
|
|
pause_action->signal_activate().connect([this, &action = *pause_action.get()](auto const& /*value*/)
|
2022-09-07 22:25:04 +00:00
|
|
|
{ onPauseToggled(action); });
|
|
|
|
action_group->add_action(pause_action);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
auto* const level_combo = gtr_get_widget<Gtk::ComboBox>(builder, "level_combo");
|
|
|
|
level_combo_init(level_combo);
|
|
|
|
level_combo->signal_changed().connect([this, level_combo]() { level_combo_changed_cb(level_combo); });
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
window_.insert_action_group("win", action_group);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** messages
|
|
|
|
**/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
store_ = Gtk::ListStore::create(message_log_cols);
|
|
|
|
|
|
|
|
addMessages(store_, myHead);
|
|
|
|
onRefresh(); /* much faster to populate *before* it has listeners */
|
|
|
|
|
|
|
|
filter_ = Gtk::TreeModelFilter::create(store_);
|
|
|
|
sort_ = Gtk::TreeModelSort::create(filter_);
|
2022-09-10 13:19:54 +00:00
|
|
|
sort_->set_sort_column(message_log_cols.sequence, TR_GTK_SORT_TYPE(ASCENDING));
|
2021-10-18 20:22:31 +00:00
|
|
|
maxLevel_ = static_cast<tr_log_level>(gtr_pref_int_get(TR_KEY_message_level));
|
2021-11-12 09:12:50 +00:00
|
|
|
filter_->set_visible_func(sigc::mem_fun(*this, &Impl::isRowVisible));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
view_->set_model(sort_);
|
2022-10-08 22:50:03 +00:00
|
|
|
setup_tree_view_button_event_handling(
|
|
|
|
*view_,
|
|
|
|
{},
|
|
|
|
[this](double view_x, double view_y) { return on_tree_view_button_released(*view_, view_x, view_y); });
|
2021-10-18 20:22:31 +00:00
|
|
|
appendColumn(view_, message_log_cols.sequence);
|
|
|
|
appendColumn(view_, message_log_cols.name);
|
|
|
|
appendColumn(view_, message_log_cols.message);
|
|
|
|
|
|
|
|
refresh_tag_ = Glib::signal_timeout().connect_seconds(
|
2021-11-12 09:12:50 +00:00
|
|
|
sigc::mem_fun(*this, &Impl::onRefresh),
|
2021-10-18 20:22:31 +00:00
|
|
|
SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS);
|
|
|
|
|
|
|
|
scroll_to_bottom();
|
2006-08-20 22:45:29 +00:00
|
|
|
}
|