Code::Blocks  SVN r11506
logmanager.h
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 
6 #ifndef LOGMGR_H
7 #define LOGMGR_H
8 
9 #include "manager.h"
10 #include "logger.h"
11 #include <map>
12 
13 //namespace cb
14 //{
20  inline wxString F(const wxChar* msg, ...)
21  {
22  va_list arg_list;
23  va_start(arg_list, msg);
24 #if wxCHECK_VERSION(3, 0, 0) && wxUSE_UNICODE
25 // in wx >= 3.0 unicode-build (default) we need the %ls here, or the strings get
26 // cut after the first character
27  ::temp_string = msg;
28  ::temp_string.Replace(_T("%s"), _T("%ls"));
29  msg = ::temp_string.wx_str();
30 #endif
31  ::temp_string = wxString::FormatV(msg, arg_list);
32  va_end(arg_list);
33 
34  return ::temp_string;
35  }
36 
37  inline wxString F(const wxString &msg, ...)
38  {
39  va_list arg_list;
40  va_start(arg_list, msg);
41 #if wxCHECK_VERSION(2,9,0) && wxUSE_UNICODE
42 // in wx >= 2.9 unicode-build (default) we need the %ls here, or the strings get
43 // cut after the first character
44  ::temp_string = msg;
45  ::temp_string.Replace(_T("%s"), _T("%ls"));
46  ::temp_string = wxString::FormatV(::temp_string, arg_list);
47 #else
48  ::temp_string = wxString::FormatV(msg, arg_list);
49 #endif
50  va_end(arg_list);
51 
52  return ::temp_string;
53  }
54 //} // namespace cb
55 
56 
58 {
59  friend class LogManager;
60 
62  size_t index;
65 
66  LogSlot();
67  ~LogSlot();
68 
69  size_t Index() const;
70 
71  void SetLogger(Logger* in);
72  Logger* GetLogger() const;
73 };
74 
75 
76 class DLLIMPORT LogManager : public Mgr<LogManager>
77 {
78 public:
80  {
81  virtual Logger* New() { return nullptr; };
82  virtual bool RequiresFilename() const { return false; };
83  virtual ~InstantiatorBase() { ; };
84  };
85  template<typename type, bool requires_filename = false> struct Instantiator : public InstantiatorBase
86  {
87  Logger* New() override { return new type; };
88  bool RequiresFilename() const override { return requires_filename; };
89  };
90 
91  enum { max_logs = 32 };
92 private:
93  typedef std::map<wxString, InstantiatorBase*> inst_map_t;
94  inst_map_t instMap;
95 
96  LogSlot slot[max_logs+1];
97 
98  LogManager();
99  ~LogManager() override;
100 
101 
102  friend class Mgr<LogManager>;
103  friend class Manager;
104 
105  void ClearLogInternal(int i);
106  void LogInternal(const wxString& msg, int i, Logger::level lv);
107 
108 public:
109  enum { no_index = -1, invalid_log, stdout_log, app_log, debug_log};
110 
111  /* ------------------------------------------------------------------------------------------------------
112  * Management functions
113  * ------------------------------------------------------------------------------------------------------
114  * Unless you are writing your own loggers, you will probably not need any of these.
115  * SetLog() transfers ownership of the Logger object to the LogManager. Loggers must be heap allocated.
116  * On error, SetLog() returns invalid_log
117  */
118  size_t SetLog(Logger* l, int index = no_index);
119  void DeleteLog(int i);
120  LogSlot& Slot(int i);
121  size_t FindIndex(Logger* l);
122 
123  /* ------------------------------------------------------------------------------------------------------
124  * Logging functions
125  * ------------------------------------------------------------------------------------------------------
126  * This section is what most people will be interested in.
127  * - Log(), LogWarning(), and LogError() output "info", "warning", or "error" messages to a log.
128  * Log() is almost certainly the function that you want to use, if you don't know what to pick.
129  * By default, logging is directed to the application log.
130  * - DebugLog() and DebugLogError() direct their output into the debug log.
131  * - LogToStdOut() outputs a message on stdout. Normally you will not want to use this function, it
132  * exists solely for some special cases.
133  * - Panic() signals a condition that does not allow proper continuation of the application or significant
134  * parts of it, but it is slightly less harsh than simply bailing out with an unhandled exception.
135  * Currently, Panic() is simply a wrapper around wxSafeShowMessage(), but it might do something else, too.
136  * When signalling panic, you will usually want to shut down the application as soon as appropriate.
137  * Plugins should call Panic() with the plugin's name as the component argument.
138  */
139 
140  void Log(const wxString& msg, int i = app_log, Logger::level lv = Logger::info) { LogInternal(msg, i, lv); };
141  void LogWarning(const wxString& msg, int i = app_log) { Log(msg, i, Logger::warning); };
142  void LogError(const wxString& msg, int i = app_log) { Log(msg, i, Logger::error); };
143 
144  void Panic(const wxString& msg, const wxString& component = wxEmptyString);
145 
146  void DebugLog(const wxString& msg, Logger::level lv = Logger::info) { Log(msg, debug_log, lv); };
147  void DebugLogError(const wxString& msg) { DebugLog(msg, Logger::error); };
148 
149  void LogToStdOut(const wxString& msg, Logger::level lv = Logger::info) { Log(msg, stdout_log, lv); };
150 
151  void ClearLog(int i) { ClearLogInternal(i); };
152 
153  /* ------------------------------------------------------------------------------------------------------
154  * Logger registry and RTTI
155  * ------------------------------------------------------------------------------------------------------
156  * These functions allow to obtain a list of names for all generic Loggers that are presently available
157  * and to create a new Logger by name without knowing the type at compile time.
158  *
159  * logptr = LogManager::Get()->New(_T("stdout")); // does exactly the same as
160  * logptr = new StdoutLogger();
161  *
162  * You normally do not need to worry about creating Loggers. Only ever consider using these functions if you
163  * really have to (want to) change the global behaviour of Code::Blocks for some reason.
164  */
165  wxArrayString ListAvailable();
166  Logger* New(const wxString& name);
167  bool FilenameRequired(const wxString& name);
168  /*
169  * Add a logger to the registry of "available Logger types". Unless you are adding a general Logger which should
170  * be accessible by name, you will not need this function. Having said that, you probably NEVER need this function.
171  */
172  void Register(const wxString& name, InstantiatorBase* ins);
173 
174  /* ------------------------------------------------------------------------------------------------------
175  * Unless your name is "main.cpp" by any chance, you don't ever need to call this.
176  * If you use it, and your name is not "main.cpp", then you better come up with a good excuse, if someone asks.
177  * ------------------------------------------------------------------------------------------------------
178  */
179  void NotifyUpdate();
180 };
181 
182 #endif
183 
wxString F(const wxChar *msg,...)
sprintf-like function
Definition: logmanager.h:20
inst_map_t instMap
Definition: logmanager.h:94
void DebugLogError(const wxString &msg)
Definition: logmanager.h:147
static wxString FormatV(const wxString &format, va_list argptr)
void LogWarning(const wxString &msg, int i=app_log)
Definition: logmanager.h:141
void LogToStdOut(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:149
bool RequiresFilename() const override
Definition: logmanager.h:88
#define _T(string)
The base class for all kinds of loggers, see loggers.h for its derived classes.
Definition: logger.h:23
Logger * log
Definition: logmanager.h:61
virtual Logger * New()
Definition: logmanager.h:81
#define DLLIMPORT
Definition: settings.h:16
void LogError(const wxString &msg, int i=app_log)
Definition: logmanager.h:142
size_t index
Definition: logmanager.h:62
wxUSE_UNICODE_dependent wxChar
void DebugLog(wxString cn, int blockSize, int poolSize, int max_refs, int total_refs, int ref_count)
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
wxString title
Definition: logmanager.h:64
wxString wxEmptyString
Definition: manager.h:183
void ClearLog(int i)
Definition: logmanager.h:151
level
Definition: logger.h:37
void Log(const wxString &msg, int i=app_log, Logger::level lv=Logger::info)
Definition: logmanager.h:140
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
virtual bool RequiresFilename() const
Definition: logmanager.h:82
Logger * New() override
Definition: logmanager.h:87
wxBitmap * icon
Definition: logmanager.h:63
std::map< wxString, InstantiatorBase * > inst_map_t
Definition: logmanager.h:93