NCBI C++ ToolKit
diag_console_list.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: diag_console_list.cpp 45932 2021-01-13 21:59:38Z asztalos $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Roman Katargin
27 *
28 * File Description:
29 *
30 */
31 
32 #include <ncbi_pch.hpp>
33 
34 #include <wx/artprov.h>
35 #include <wx/imaglist.h>
36 #include <wx/textbuf.h>
37 
39 
43 
45 
46 #include <wx/utils.h>
47 #include <wx/settings.h>
48 
50 
54 
55 BEGIN_EVENT_TABLE( CDiagConsoleList, wxListCtrl )
56  EVT_CONTEXT_MENU( CDiagConsoleList::OnContextMenu )
58 
59 CDiagConsoleList::CDiagConsoleList() : m_Filter(0), m_Cache(500)
60 {
61 }
62 
64  wxWindow* parent,
65  wxWindowID id,
66  const wxPoint& pos,
67  const wxSize& size,
68  long style)
69 : m_Filter(eShowAll), m_Cache(500)
70 {
71  Create(parent, id, pos, size, style);
72 }
73 
75 {
77 }
78 
79 /// Creation
81  wxWindow* parent,
82  wxWindowID id,
83  const wxPoint& pos,
84  const wxSize& size,
85  long style)
86 {
87  if (!wxListCtrl::Create(parent, id, pos, size, style|wxLC_REPORT|wxLC_VIRTUAL))
88  return false;
89 
90 #ifdef __WXOSX_COCOA__
91  SetBackgroundStyle(wxBG_STYLE_COLOUR);
92  SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
93 #endif
94 
95  this->InsertColumn(0, wxT("Message"));
96  this->InsertColumn(1, wxT("Time"));
97  this->InsertColumn(2, wxT("Source"));
98 
99  this->SetColumnWidth(0, 320);
100  this->SetColumnWidth(1, 64);
101  this->SetColumnWidth(2, 400);
102 
104 
106 
107  return true;
108 }
109 
110 void CDiagConsoleList::SetFilter(size_t filter)
111 {
112  m_Filter = filter;
114 }
115 
116 static size_t s_CountLines(const wxString& msg)
117 {
118  wxString tmp = msg;
119  tmp.Trim();
120  return 1 + std::count(tmp.begin(), tmp.end(), wxChar('\n'));
121 }
122 
123 static wxString s_GetLine(const wxString& msg, size_t line)
124 {
125  wxString str;
126  for (size_t pos = 0, pos2;; pos = pos2 + 1) {
127  pos2 = msg.find_first_of(wxChar('\n'), pos);
128  if (line-- == 0) {
129  return msg.substr(pos, pos2 - pos);
130  break;
131  }
132  if (pos2 == wxString::npos)
133  break;
134  }
135 
136  size_t i, indent = 0;
137  for (i = 0; i < str.Length(); ++i) {
138  if (str[i] == wxChar('\t'))
139  indent += 8;
140  else if (str[i] == wxChar(' '))
141  ++indent;
142  else
143  break;
144  }
145 
146  if (i > 0) {
147  str = str.Mid(i);
148  str.Pad(indent, ' ', false);
149  }
150 
151  return str;
152 }
153 
154 wxString CDiagConsoleList::GetItemRawMsg(long item) const
155 {
156  if (item < 0 || (size_t)item >= m_Items.size())
157  return wxEmptyString;
158 
159  auto index = x_GetIndex(item);
160 
161  wxString str = s_GetLine(m_Cache.GetItemMessage(get<0>(index)), get<1>(index));
162  if (get<1>(index) > 0)
163  return str;
164 
165  int image = m_Cache.GetItemImage(get<0>(index));
166 
167  wxString retVal;
168  switch(image) {
169  case 0:
170  retVal = wxT("Error: ") + str;
171  break;
172  case 1:
173  retVal = wxT("Warning: ") + str;
174  break;
175  case 2:
176  retVal = wxT("Info: ") + str;
177  break;
178  default:
179  retVal = wxT("Other: ") + str;
180  break;
181  }
182 
183  return retVal;
184 }
185 
186 void CDiagConsoleList::OnContextMenu( wxContextMenuEvent& anEvt )
187 {
188  void* data = anEvt.GetClientData();
189  unique_ptr<wxMenu> menu(data ? (wxMenu*)data : new wxMenu());
190 
191  AppendMenuItems(*menu);
192 
194  anEvt.SetClientData(menu.get());
195  anEvt.Skip();
196  return;
197  }
198 
199  anEvt.SetClientData(NULL);
200 
201  CleanupSeparators(*menu);
202  PopupMenu(menu.get());
203 }
204 
206 {
207  aMenu.Append( wxID_SEPARATOR, wxT("Edit") );
208  CUICommandRegistry::GetInstance().AppendMenuItem( aMenu, wxID_COPY );
209 }
210 
211 
213 {
214  if (item < 0 || (size_t)item >= m_Items.size())
215  return -1;
216 
217  auto index = x_GetIndex(item);
218  return (get<1>(index) > 0) ? 4 : m_Cache.GetItemImage(get<0>(index));
219 }
220 
221 wxString CDiagConsoleList::OnGetItemText(long item, long column) const
222 {
223  if (item < 0 || (size_t)item >= m_Items.size())
224  return wxEmptyString;
225 
226  auto index = x_GetIndex(item);
227 
228  if (column == 1)
229  return (get<1>(index) > 0) ? wxString() : m_Cache.GetItemTime(get<0>(index));
230  else if (column == 2)
231  return (get<1>(index) > 0) ? wxString() : m_Cache.GetItemSource(get<0>(index));
232 
233  if (column != 0)
234  return wxEmptyString;
235 
236  return s_GetLine(m_Cache.GetItemMessage(get<0>(index)), get<1>(index));
237 }
238 
240 {
241  wxBusyCursor wait;
243  size_t i, total, buffered;
244  log_handler.GetMsgCount(total, buffered);
245 
246  m_Items.clear();
247 
248  for (i = total - buffered; i < total; ++i) {
250  if (!log_handler.GetMessage(i, msg, total)) {
251  m_Items.clear();
252  continue;
253  }
254 
255  int flag = 0;
256  switch(msg.severity) {
257  case eDiag_Error:
258  flag = eShowErrors;
259  break;
260  case eDiag_Warning:
261  flag = eShowWarnings;
262  break;
263  case eDiag_Info:
264  flag = eShowInfo;
265  break;
266  default:
267  flag = eShowOther;
268  break;
269  }
270  flag |= msg.wxMsg ? eShowWxWdidgets : eShowNCBI;
271 
272  if ((flag&m_Filter&eShowFlags) && (flag&m_Filter&eShowNCBIWxWdidgets)) {
273  size_t lines = s_CountLines(wxString::FromUTF8(msg.message.c_str()));
274  while (lines--)
275  m_Items.push_back(i);
276  }
277  }
278 
279  SetItemCount((long)m_Items.size());
280  SetColumnWidth(1, wxLIST_AUTOSIZE);
281  if (m_Items.size() > 0)
282  EnsureVisible((long)(m_Items.size()-1));
283  Refresh();
284 }
285 
287 {
289  size_t i, total, buffered;
290  log_handler.GetMsgCount(total, buffered);
291 
292  bool update = false;
293  if (m_Items.size() > 0) {
294  for (i = 0; i < m_Items.size() && m_Items[i] < total - buffered; ++i);
295  if (i > 0) {
296  m_Items.erase(m_Items.begin(), m_Items.begin() + i);
297  update = true;
298  }
299  }
300 
301  i = (m_Items.size() > 0) ? m_Items.back() + 1 : total - buffered;
302 
303  for (; i < total; ++i) {
305  if (!log_handler.GetMessage(i, msg, total)) {
307  return;
308  }
309 
310  int flag = 0;
311  switch(msg.severity) {
312  case eDiag_Error:
313  flag = eShowErrors;
314  break;
315  case eDiag_Warning:
316  flag = eShowWarnings;
317  break;
318  case eDiag_Info:
319  flag = eShowInfo;
320  break;
321  default:
322  flag = eShowOther;
323  break;
324  }
325  flag |= msg.wxMsg ? eShowWxWdidgets : eShowNCBI;
326 
327  if ((flag&m_Filter&eShowFlags) && (flag&m_Filter&eShowNCBIWxWdidgets)) {
328  size_t lines = s_CountLines(wxString::FromUTF8(msg.message.c_str()));
329  while (lines--)
330  m_Items.push_back(i);
331  update = true;
332  }
333  }
334 
335  if (update) {
336  SetItemCount((long)m_Items.size());
337  SetColumnWidth(1, wxLIST_AUTOSIZE);
338  if (m_Items.size() > 0)
339  EnsureVisible((long)(m_Items.size()-1));
340  Refresh();
341  }
342 }
343 
344 static const char* kFilterTag = "Filter";
345 
347 {
348  if( ! m_RegPath.empty()) {
350  CRegistryReadView view = gui_reg.GetReadView(m_RegPath);
351 
352  m_Filter = view.GetInt(kFilterTag, (int)m_Filter);
354  }
355 }
356 
358 {
359  if( ! m_RegPath.empty()) {
361  CRegistryWriteView view = gui_reg.GetWriteView(m_RegPath);
362 
363  view.Set(kFilterTag, (int)m_Filter);
364  }
365 }
366 
367 ///////////////////////////////////////////////////////////////////////////////
368 /// CDiagConsoleCache
369 
371 {
372  return x_GetItem(index).m_Image;
373 }
374 
375 wxString CDiagConsoleCache::GetItemMessage(size_t index)
376 {
377  return x_GetItem(index).m_Message;
378 }
379 
380 wxString CDiagConsoleCache::GetItemSource(size_t index)
381 {
382  return x_GetItem(index).m_Source;
383 }
384 
385 wxString CDiagConsoleCache::GetItemTime(size_t index)
386 {
387  return x_GetItem(index).m_Time;
388 }
389 
391 {
393  m_Data.clear();
394  m_AccessTime = 0;
395  }
396 
397  TData::iterator it = m_Data.find(index);
398  if (it != m_Data.end()) {
399  it->second.m_AccessTime = m_AccessTime;
400  return it->second;
401  }
402 
403  if (m_Size == m_Data.size()) {
404  TData::iterator it_erase = it = m_Data.begin();
405  for (++it;it != m_Data.end(); ++it) {
406  if (it->second.m_AccessTime < it_erase->second.m_AccessTime)
407  it_erase = it;
408  }
409  m_Data.erase(it_erase);
410  }
411 
414  size_t total;
415  CItem item;
416 
417  if (log_handler.GetMessage(index, msg, total)) {
418  wxString text = wxString::FromUTF8(msg.message.c_str());
419 
420  item.m_Message = text;
421  item.m_Time = ToWxString(msg.time.AsString("h:m:s"));
422  item.m_Source = ToWxString(msg.source);
423 
424  switch(msg.severity) {
425  case eDiag_Error:
426  item.m_Image = 0;
427  break;
428  case eDiag_Warning:
429  item.m_Image = 1;
430  break;
431  case eDiag_Info:
432  item.m_Image = 2;
433  break;
434  default:
435  item.m_Image = 3;
436  break;
437  }
438  }
439  else {
440  item.m_Message = wxT("Wrong item requested (problem with the console view).");
441  item.m_Image = -1;
442  }
443 
444  return m_Data[index] = item;
445 }
446 
448 
CDiagConsoleList.
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
void ConnectToControl(wxListCtrl &listCtrl)
Definition: wx_utils.cpp:1167
CRegistryWriteView GetWriteView(const string &section)
get a read-write view at a particular level.
Definition: registry.cpp:462
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
CRegistryReadView GetReadView(const string &section) const
get a read-only view at a particular level.
Definition: registry.cpp:428
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
int GetInt(const string &key, int default_val=0) const
access a named key at this level, with no recursion
Definition: reg_view.cpp:230
void Set(const string &key, int val)
access a named key at this level, with no recursion
Definition: reg_view.cpp:533
static CUICommandRegistry & GetInstance()
the main instance associated with the application
Definition: ui_command.cpp:176
wxMenuItem * AppendMenuItem(wxMenu &menu, TCmdID cmd_id) const
Definition: ui_command.cpp:300
an event for child notification
Definition: log_gbench.hpp:83
CwxLogDiagHandler - provides a centralized logging facility that integrates both with C++ Toolkit (CD...
Definition: log_gbench.hpp:59
bool GetMessage(size_t index, SMessage &msg, size_t &total) const
Definition: log_gbench.cpp:100
void GetMsgCount(size_t &total, size_t &buffered)
Definition: log_gbench.cpp:93
static CwxLogDiagHandler * GetInstance()
Definition: log_gbench.cpp:44
void erase(iterator pos)
Definition: map.hpp:167
size_type size() const
Definition: map.hpp:148
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
static size_t s_CountLines(const wxString &msg)
static wxString s_GetLine(const wxString &msg, size_t line)
static const char * kFilterTag
#define NULL
Definition: ncbistd.hpp:225
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
@ eDiag_Warning
Warning message.
Definition: ncbidiag.hpp:652
const CItem & x_GetItem(size_t index)
virtual wxString OnGetItemText(long item, long column) const
wxString GetItemRawMsg(long item) const
void OnContextMenu(wxContextMenuEvent &event)
CDiagConsoleCache m_Cache
void x_OnNewItem(CEvent *)
virtual void AppendMenuItems(wxMenu &aMenu)
int GetItemImage(size_t index)
CDiagConsoleCache.
wxString GetItemMessage(size_t index)
bool ShouldPropagateContextMenu() const
virtual void LoadSettings()
CFixGenericListCtrl m_FixGenericListCtrl
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxBORDER_NONE)
Creation.
wxString GetItemTime(size_t index)
virtual int OnGetItemImage(long item) const
unsigned long m_AccessTime
void SetFilter(size_t filter)
virtual void SaveSettings() const
wxString GetItemSource(size_t index)
tuple< size_t, size_t > x_GetIndex(long item) const
virtual void RemoveListener(CEventHandler *listener)
Remove a listener.
#define ON_EVENT(type, id, handler)
#define END_EVENT_MAP()
Ends definition of Command Map.
#define BEGIN_EVENT_MAP(thisClass, baseClass)
Begins definition of Command Map for CEventHandler-derived class.
virtual void AddListener(CEventHandler *listener, int pool_name=ePool_Default)
Add a listener.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
END_EVENT_TABLE()
int i
static void text(MDB_val *v)
Definition: mdb_dump.c:62
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
T max(T x_, T y_)
static char tmp[2048]
Definition: utf8.c:42
string indent(" ")
static const char * str(char *buf, int n)
Definition: stats.c:84
static const char * column
Definition: stats.c:23
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
void CleanupSeparators(wxMenu &menu)
Removes extra separators (in the begining or at the end of the menu, ot those that precede other sepa...
Definition: wx_utils.cpp:668
Modified on Tue Dec 05 02:08:27 2023 by modify_doxy.py rev. 669887