-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLogDisplayer.cpp
243 lines (197 loc) · 5.57 KB
/
LogDisplayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "LogDisplayer.h"
#include <wx/menu.h>
#include <wx/window.h>
#include <wx/file.h>
#include <wx/filedlg.h>
#include <wx/sizer.h>
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EXPORTED_EVENT_TYPE("C", wxEVT_UPDATE_GUI_LOG, -1)
END_DECLARE_EVENT_TYPES()
DEFINE_EVENT_TYPE(wxEVT_UPDATE_GUI_LOG)
enum
{
ID_SAVE_AS = 2013,
ID_CLEAR_AS,
ID_SELECT_ALL
};
BEGIN_EVENT_TABLE(LogDisplayer,LogPanel)
EVT_RIGHT_UP(LogDisplayer::OnRightClicked)
EVT_MENU(ID_SAVE_AS, LogDisplayer::OnSaveAs)
EVT_MENU(ID_CLEAR_AS, LogDisplayer::OnClear)
EVT_MENU(ID_SELECT_ALL, LogDisplayer::OnSelectAll)
EVT_COMMAND(-1,wxEVT_UPDATE_GUI_LOG,LogDisplayer::OnUpdateGuiLog)
END_EVENT_TABLE()
LogDisplayer::LogDisplayer(wxWindow*parent,wxWindowID id /* = wxID_ANY */)
:LogPanel(parent)
,m_wxChainRedirectOn(false)
,m_pChain(NULL)
{
InitWxLogChain();
EnableWxLogChain();
// Connect Right Click Event
ConnectRightClickEvent(wxMouseEventHandler(LogDisplayer::OnRightClicked), this->GetEventHandler());
}
LogDisplayer::~LogDisplayer()
{
EnableWxLogChain(false);
}
void LogDisplayer::OnRightClicked(wxMouseEvent&evt)
{
wxPoint pos = evt.GetPosition();
wxMenu right_click_menu;
right_click_menu.Append(ID_SAVE_AS, "Save As");
right_click_menu.Append(ID_CLEAR_AS, "Clear");
right_click_menu.AppendSeparator();
right_click_menu.Append(wxID_COPY, _("&Copy"));
right_click_menu.AppendSeparator();
right_click_menu.Append(ID_SELECT_ALL, _("Select &All"));
wxWindow::PopupMenu(&right_click_menu, pos.x, pos.y);
}
void LogDisplayer::OnUpdateGuiLog(wxCommandEvent&evt)
{
LogMessage(evt.GetString());
}
void LogDisplayer::AddLog(const char*log)
{
wxDateTime now = wxDateTime::Now();
wxString msg = wxT(" [") + now.FormatISODate() + wxT("_") + now.FormatISOTime() + wxT("]: ") + log;
wxCommandEvent evt(wxEVT_UPDATE_GUI_LOG);
evt.SetId(this->GetId());
evt.SetString(msg);
this->AddPendingEvent(evt);
}
void LogDisplayer::AddLog(std::string& log)
{
AddLog(log.c_str());
}
void LogDisplayer::Clear()
{
CleanUp();
}
void LogDisplayer::SaveAs(const std::string& path)
{
wxFile file(path.c_str(),wxFile::write);
wxString msg;
GetMessage(msg);
file.Write(msg);
}
void LogDisplayer::OnSaveAs(wxCommandEvent&evt)
{
wxFileDialog fd(this, "Save As...","", "",
"TXT(*.txt)|*.txt|All files (*.*)|*.*", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
if (fd.ShowModal() != wxID_OK) return;
SaveAs(std::string(fd.GetPath()));
}
void LogDisplayer::EnableWxLogChain(bool enable /* = true */)
{
if(enable == true)
{
if(m_wxChainRedirectOn == false && m_pChain == NULL)
{
m_wxChainRedirectOn = true;
m_pChain = new wxLogChain(new wxLogCatcher(this));
}
else
{
m_wxChainRedirectOn = true;
wxLog::SetActiveTarget(m_pChain);
}
}
else
{
if(m_wxChainRedirectOn == true)
{
m_wxChainRedirectOn = false;
wxLog::SetActiveTarget(NULL);
}
}
}
void LogDisplayer::InitWxLogChain()
{
//tricky here.
//for linux gui, default wx log target is null after the application start up.
//calling wxLogWarning(and other wxLogXXX function) will force wxLog to create a log target automatically in runtime.
//so ,if we want to maintain the expected behavior(wxLogXXX output to a messagebox) we must ensure there is an log target
// before installing our LogDisplayer to intercept log.
wxLog::GetActiveTarget();
}
void LogDisplayer::OnClear( wxCommandEvent& evt )
{
Clear();
}
void LogDisplayer::OnSelectAll( wxCommandEvent& evt )
{
SelctAll();
}
//wxLogCatcher
//
LogDisplayer::wxLogCatcher::wxLogCatcher(LogDisplayer* Operand):wxLog()
,m_displayer(Operand)
{
}
LogDisplayer::wxLogCatcher::~wxLogCatcher()
{
}
//wxLogxx,chain.
void LogDisplayer::wxLogCatcher::DoLogString(const wxChar *szString, time_t t)
{
m_displayer->AddLog(wxString("<wxLog>")+szString);
}
BEGIN_EVENT_TABLE(LogPanel, wxPanel)
END_EVENT_TABLE()
LogPanel::LogPanel( wxWindow* parent, wxWindowID winid,
const wxPoint& pos, const wxSize& size )
:wxPanel(parent,winid, pos, size)
,mw_outputTC(NULL)
{
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
mw_outputTC = new wxTextCtrl(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH | wxTE_LEFT | wxTE_BESTWRAP | wxTE_READONLY);
mainSizer->Add(mw_outputTC, 1, wxEXPAND | wxALL, 2);
SetSizer(mainSizer);
Layout();
}
LogPanel::~LogPanel()
{
Clear();
}
void LogPanel::Clear()
{
}
void LogPanel::LogMessage( const wxString& data, int lines/*=-1*/ )
{
int old_lines = mw_outputTC->GetNumberOfLines();
mw_outputTC->AppendText( data + "\n" );
int new_lines = mw_outputTC->GetNumberOfLines();
if (lines>=0 && lines<=new_lines)
{
wxString lastStr;
for ( int i = new_lines - lines; i < new_lines; ++i )
{
lastStr += mw_outputTC->GetLineText( i );
lastStr += "\n";
}
mw_outputTC->SetValue(lastStr);
mw_outputTC->ScrollLines(lines - 1);
}
else
{
mw_outputTC->ScrollLines( new_lines - old_lines );
}
Refresh();
}
void LogPanel::CleanUp()
{
mw_outputTC->Clear();
}
void LogPanel::GetMessage(wxString& content)
{
content = mw_outputTC->GetValue();
}
void LogPanel::ConnectRightClickEvent(wxObjectEventFunction function, wxEvtHandler* eventSink)
{
mw_outputTC->Connect(wxEVT_RIGHT_DOWN, function, 0, eventSink);
}
void LogPanel::SelctAll()
{
mw_outputTC->SetSelection(0, mw_outputTC->GetLastPosition());
}