The Battle for Wesnoth  1.19.0-dev
log_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "gettext.hpp"
21 #include "gui/widgets/grid.hpp"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/text_box.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 #include "log.hpp"
28 
29 namespace gui2::dialogs
30 {
31 
32 REGISTER_DIALOG(log_settings)
33 
35  : modal_dialog(window_id())
36  , last_words_()
37 {
38  //list of names must match those in logging.cfg
39  widget_id_.push_back("none");
40  widget_id_.push_back("err");
41  widget_id_.push_back("warn");
42  widget_id_.push_back("info");
43  widget_id_.push_back("debug");
44 
45 
46  //empty string is the filter (in other words, this grabs the whole list of domains)
47  std::string temp_string = lg::list_log_domains("");
48  //std::cout<<temp_string; //use to print the full log domain list
49  std::string one_domain;
50 
51  std::istringstream iss(temp_string, std::istringstream::in);
52 
53  while(iss >> one_domain){
54  domain_list_.push_back(one_domain);
55  }
56 }
57 
59 {
60  listbox& logger_box = find_widget<listbox>(&window, "logger_listbox", false);
61 
62  for(unsigned int i = 0; i < domain_list_.size(); i++){
63  std::string this_domain = domain_list_[i];
66 
67  item["label"] = this_domain;
68  data["label"] = item;
69 
70  logger_box.add_row(data);
71  group<std::string>& group = groups_[this_domain];
72 
73  grid* this_grid = logger_box.get_row_grid(i);
74  for(std::string this_id : widget_id_){
75  widget* this_widget = this_grid->find(this_id, false);
76  toggle_button* button = dynamic_cast<toggle_button*>(this_widget);
77  if(button != nullptr) {
78  group.add_member(button, this_id);
79  }
80  }
81  lg::severity current_sev;
83  if (lg::get_log_domain_severity(this_domain, current_sev)) {
84  if (current_sev <= max_sev) {
85  group.set_member_states(widget_id_[static_cast<int>(current_sev) + 1]);
86  }
87  }
88  }
89 
90  text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
91  filter->set_text_changed_callback(std::bind(&log_settings::filter_text_changed, this, std::placeholders::_2));
92 
93  window.keyboard_capture(filter);
94  window.add_to_keyboard_chain(&logger_box);
95 }
96 
97 void log_settings::filter_text_changed(const std::string& text)
98 {
99  listbox& list = find_widget<listbox>(get_window(), "logger_listbox", false);
100 
101  const std::vector<std::string> words = utils::split(text, ' ');
102 
103  if(words == last_words_) {
104  return;
105  }
106 
107  last_words_ = words;
108 
109  boost::dynamic_bitset<> show_items;
110  show_items.resize(list.get_item_count(), true);
111 
112  if(!text.empty()) {
113  for(unsigned int i = 0; i < list.get_item_count(); i++) {
114  assert(i < domain_list_.size());
115 
116  bool found = false;
117 
118  for(const auto& word : words)
119  {
120  found = translation::ci_search(domain_list_[i], word);
121  if(!found) {
122  break;
123  }
124  }
125 
126  show_items[i] = found;
127  }
128  }
129 
130  list.set_row_shown(show_items);
131 }
132 
134 {
135  for(std::string this_domain : domain_list_){
136  set_logger(this_domain);
137  }
138 }
139 
140 void log_settings::set_logger(const std::string log_domain)
141 {
142  std::string active_value = groups_[log_domain].get_active_member_value();
143 
144  if(active_value == widget_id_[2]){ //default value, level1: warning
145  lg::set_log_domain_severity(log_domain, lg::warn());
146  } else if(active_value == widget_id_[4]){ //level3: debug
147  lg::set_log_domain_severity(log_domain, lg::debug());
148  } else if(active_value == widget_id_[3]){ //level2: info
149  lg::set_log_domain_severity(log_domain, lg::info());
150  } else if(active_value == widget_id_[1]){ //level0: error
151  lg::set_log_domain_severity(log_domain, lg::err());
152  } else if(active_value == widget_id_[0]){ //level-1: disable
154  }
155 }
156 
157 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
void set_logger(const std::basic_string< char > log_domain)
The display function.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
std::vector< std::string > domain_list_
void filter_text_changed(const std::string &text)
std::vector< std::string > last_words_
std::map< std::string, group< std::string > > groups_
std::vector< std::string > widget_id_
Abstract base class for all modal dialogs.
window * get_window()
Returns a pointer to the dialog's window.
Base container class.
Definition: grid.hpp:32
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
void add_member(selectable_item *w, const T &value)
Adds a widget/value pair to the group map.
Definition: group.hpp:42
void set_member_states(const T &value)
Sets the toggle values for all widgets besides the one associated with the specified value to false.
Definition: group.hpp:111
The listbox class.
Definition: listbox.hpp:43
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:136
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:59
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:230
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:124
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
Class for a single line text area.
Definition: text_box.hpp:142
Class for a toggle button.
Base class for all widgets.
Definition: widget.hpp:53
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1215
void add_to_keyboard_chain(widget *widget)
Adds the widget to the keyboard chain.
Definition: window.cpp:1221
std::size_t i
Definition: function.cpp:968
unsigned in
If equal to search_counter, the node is off the list.
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
REGISTER_DIALOG(tod_new_schedule)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
bool get_log_domain_severity(const std::string &name, severity &severity)
Definition: log.cpp:366
logger & err()
Definition: log.cpp:302
severity
Definition: log.hpp:82
std::string list_log_domains(const std::string &filter)
Definition: log.cpp:375
logger & debug()
Definition: log.cpp:320
logger & warn()
Definition: log.cpp:308
logger & info()
Definition: log.cpp:314
bool set_log_domain_severity(const std::string &name, severity severity)
Definition: log.cpp:342
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:565
std::vector< std::string > split(const config_attribute_value &val)
std::string_view data
Definition: picture.cpp:194