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