Code::Blocks  SVN r11506
logmanager.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 9426 $
6  * $Id: logmanager.cpp 9426 2013-11-02 19:42:20Z alpha0010 $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/logmanager.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef WX_PRECOMP
13 #include <wx/bitmap.h>
14 #endif
15 
16 #ifndef CB_PRECOMP
17 #include <wx/log.h>
18 #endif
19 
20 #include "cbcolourmanager.h"
21 #include "logmanager.h"
22 #include "loggers.h"
23 
24 template<> LogManager* Mgr<LogManager>::instance = nullptr;
25 template<> bool Mgr<LogManager>::isShutdown = false;
26 
28 
30  log(nullptr),
31  icon(nullptr)
32 {
33 }
34 
36 {
37  if (log != &g_null_log)
38  delete log;
39  delete icon;
40 }
41 
42 size_t LogSlot::Index() const
43 {
44  return index;
45 }
46 
48 {
49  if (log != &g_null_log)
50  delete log;
51  log = in;
52 }
53 
55 {
56  return log;
57 }
58 
60 
62 {
63  for (size_t i = 0; i < max_logs; ++i)
64  {
65  slot[i].index = i;
66  slot[i].log = &g_null_log;
67  }
68 
69  SetLog(new StdoutLogger, stdout_log);
70  SetLog(new StdoutLogger, app_log);
71  SetLog(new StdoutLogger, debug_log);
72  slot[stdout_log].title = _T("stdout");
73  slot[app_log].title = _T("Code::Blocks");
74  slot[debug_log].title = _T("Code::Blocks Debug");
75 
77  manager->RegisterColour(_("Logs"), _("Success text"), wxT("logs_success_text"), *wxBLUE);
78  manager->RegisterColour(_("Logs"), _("Warning text"), wxT("logs_warning_text"), *wxBLUE);
79  manager->RegisterColour(_("Logs"), _("Error text"), wxT("logs_error_text"), wxColour(0xf0, 0x00, 0x00));
80  manager->RegisterColour(_("Logs"), _("Critical text"), wxT("logs_critical_text"), *wxWHITE);
81  manager->RegisterColour(_("Logs"), _("Critical background"), wxT("logs_critical_back"), *wxRED);
82  manager->RegisterColour(_("Logs"), _("Critical text (ListCtrl)"),
83  wxT("logs_critical_text_listctrl"), wxColour(0x0a, 0x00, 0x00));
84  manager->RegisterColour(_("Logs"), _("Failure text"), wxT("logs_failure_text"), wxColour(0x00, 0x00, 0xa0));
85 
86  Register(_T("null"), new Instantiator<NullLogger>);
87  Register(_T("stdout"), new Instantiator<StdoutLogger>);
88  Register(_T("text"), new Instantiator<TextCtrlLogger>);
89  Register(_T("file"), new Instantiator<FileLogger, true>);
90 }
91 
93 {
94  for (inst_map_t::iterator i = instMap.begin(); i != instMap.end(); ++i)
95  delete i->second;
96 }
97 
99 {
100  if (i >= 0 && i <= max_logs && slot[i].log != &g_null_log)
101  slot[i].log->Clear();
102 }
103 
104 void LogManager::LogInternal(const wxString& msg, int i, Logger::level lv)
105 {
106  if (i >= 0 && i <= max_logs && slot[i].log != &g_null_log)
107  slot[i].log->Append(msg, lv);
108 }
109 
110 size_t LogManager::SetLog(Logger* l, int i)
111 {
112  unsigned int index = i;
113 
114  if (i <= no_index)
115  {
116  for (index = debug_log + 1; index < max_logs; ++index)
117  {
118  if (slot[index].GetLogger() == &g_null_log)
119  {
120  slot[index].SetLogger(l);
121  return index;
122  }
123  }
124 
125  delete l;
126  return invalid_log;
127  }
128 
129  slot[index].SetLogger(l);
130  return index;
131 }
132 
134 {
135  for (size_t i = 0; i < max_logs; ++i)
136  {
137  if (slot[i].log)
138  slot[i].log->UpdateSettings();
139  }
140 }
141 
143 {
144  SetLog(&g_null_log, i);
145 }
146 
148 {
149  return slot[i];
150 }
151 
153 {
154  for (unsigned int i = invalid_log; i < max_logs; ++i)
155  {
156  if (slot[i].log == l)
157  return i;
158  }
159  return invalid_log;
160 }
161 
163 {
164  wxArrayString as;
165 
166  for (inst_map_t::iterator i = instMap.begin(); i != instMap.end(); ++i)
167  as.Add(i->first);
168 
169  return as;
170 }
171 
173 {
174  inst_map_t::iterator i = instMap.find(name);
175 
176  if (i != instMap.end())
177  return i->second->RequiresFilename();
178 
179  return false;
180 }
181 
183 {
184  inst_map_t::iterator i;
185 
186  if ((i = instMap.find(name)) != instMap.end())
187  return i->second->New();
188 
189  return new NullLogger;
190 }
191 
193 {
194  instMap[name] = ins;
195 }
196 
197 void LogManager::Panic(const wxString& msg, const wxString& component)
198 {
199  wxString title(_T("Panic: "));
200  title.Append(component);
201 
202  if (!component)
203  title.Append(_T("Code::Blocks"));
204 
205  wxSafeShowMessage(title, msg);
206 }
void wxSafeShowMessage(const wxString &title, const wxString &text)
void Register(const wxString &name, InstantiatorBase *ins)
Definition: logmanager.cpp:192
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
void RegisterColour(const wxString &category, const wxString &name, const wxString &id, const wxColour &defaultColour)
a logger simply does nothing
Definition: loggers.h:22
size_t FindIndex(Logger *l)
Definition: logmanager.cpp:152
size_t Index() const
Definition: logmanager.cpp:42
#define _T(string)
The base class for all kinds of loggers, see loggers.h for its derived classes.
Definition: logger.h:23
wxArrayString ListAvailable()
Definition: logmanager.cpp:162
#define wxT(string)
Logger * log
Definition: logmanager.h:61
void Panic(const wxString &msg, const wxString &component=wxEmptyString)
Definition: logmanager.cpp:197
size_t index
Definition: logmanager.h:62
static NullLogger g_null_log
Definition: logmanager.cpp:27
void DeleteLog(int i)
Definition: logmanager.cpp:142
null_pointer_t nullptr
Definition: nullptr.cpp:16
wxColour * wxRED
a logger which prints messages to the standard console IO
Definition: loggers.h:29
wxString title
Definition: logmanager.h:64
wxColour * wxWHITE
Definition: manager.h:183
const wxString & _(const wxString &string)
void SetLogger(Logger *in)
Definition: logmanager.cpp:47
ColourManager * GetColourManager() const
Definition: manager.cpp:489
wxString & Append(const char *psz)
LogSlot & Slot(int i)
Definition: logmanager.cpp:147
wxColour * wxBLUE
level
Definition: logger.h:37
Logger * GetLogger() const
Definition: logmanager.cpp:54
bool FilenameRequired(const wxString &name)
Definition: logmanager.cpp:172
size_t Add(const wxString &str, size_t copies=1)
void LogInternal(const wxString &msg, int i, Logger::level lv)
Definition: logmanager.cpp:104
Logger * New(const wxString &name)
Definition: logmanager.cpp:182
size_t SetLog(Logger *l, int index=no_index)
Definition: logmanager.cpp:110
wxBitmap * icon
Definition: logmanager.h:63
~LogManager() override
Definition: logmanager.cpp:92
void ClearLogInternal(int i)
Definition: logmanager.cpp:98
void NotifyUpdate()
Definition: logmanager.cpp:133