Code::Blocks  SVN r11506
cclogger.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 10999 $
6  * $Id: cclogger.cpp 10999 2017-02-06 19:12:25Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/plugins/codecompletion/parser/cclogger.cpp $
8  */
9 
10 #include "cclogger.h"
11 
12 #include <wx/event.h>
13 #include <wx/textfile.h>
14 #include <wx/utils.h> // wxNewId
15 
16 #include <logmanager.h> // F()
17 #include <globals.h> // cbC2U for cbAssert macro
18 
19 std::unique_ptr<CCLogger> CCLogger::s_Inst;
20 
21 bool g_EnableDebugTrace = false;
22 bool g_EnableDebugTraceFile = false; // true
23 const wxString g_DebugTraceFile = wxEmptyString;
27 #define TRACE_TO_FILE(msg) \
28  if (g_EnableDebugTraceFile && !g_DebugTraceFile.IsEmpty()) \
29  { \
30  wxTextFile f(g_DebugTraceFile); \
31  if ((f.Exists() && f.Open()) || (!f.Exists() && f.Create())) \
32  { \
33  f.AddLine(msg); \
34  bool exp = f.Write() && f.Close(); \
35  cbAssert(exp); \
36  } \
37  } \
38 
39 #define TRACE_THIS_TO_FILE(msg) \
40  if (!g_DebugTraceFile.IsEmpty()) \
41  { \
42  wxTextFile f(g_DebugTraceFile); \
43  if ((f.Exists() && f.Open()) || (!f.Exists() && f.Create())) \
44  { \
45  f.AddLine(msg); \
46  bool exp = f.Write() && f.Close() \
47  cbAssert(exp); \
48  } \
49  } \
50 
51 
53  m_Parent(nullptr),
54  m_LogId(-1),
55  m_DebugLogId(-1),
56  m_AddTokenId(-1)
57 {
58 }
59 
60 /*static*/ CCLogger* CCLogger::Get()
61 {
62  if (!s_Inst.get())
63  s_Inst.reset(new CCLogger);
64 
65  return s_Inst.get();
66 }
67 
68 // Initialized from CodeCompletion constructor
69 void CCLogger::Init(wxEvtHandler* parent, int logId, int debugLogId, int addTokenId)
70 {
71  m_Parent = parent;
72  m_LogId = logId;
73  m_DebugLogId = debugLogId;
74  m_AddTokenId = addTokenId;
75 }
76 
77 void CCLogger::AddToken(const wxString& msg)
78 {
79  if (!m_Parent || m_AddTokenId<1) return;
80 
81  CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, m_AddTokenId);
82  evt.SetString(msg);
83 #if CC_PROCESS_LOG_EVENT_TO_PARENT
84  m_Parent->ProcessEvent(evt);
85 #else
86  wxPostEvent(m_Parent, evt);
87 #endif
88 }
89 
90 void CCLogger::Log(const wxString& msg)
91 {
92  //Could crash here; should check if shutting down
94  return;
95 
96  if (!m_Parent || m_LogId<1) return;
97 
98  CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, m_LogId);
99  evt.SetString(msg);
100 #if CC_PROCESS_LOG_EVENT_TO_PARENT
101  m_Parent->ProcessEvent(evt);
102 #else
103  wxPostEvent(m_Parent, evt);
104 #endif
105 }
106 
107 void CCLogger::DebugLog(const wxString& msg)
108 {
109  // Could crash here; should check if shutting down
111  return;
112 
113  if (!m_Parent || m_DebugLogId<1) return;
114 
115  CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, m_DebugLogId);
116  evt.SetString(msg);
117 #if CC_PROCESS_LOG_EVENT_TO_PARENT
118  m_Parent->ProcessEvent(evt);
119 #else
120  wxPostEvent(m_Parent, evt);
121 #endif
122 }
int wxNewId()
static CCLogger * Get()
Definition: cclogger.cpp:60
void Log(const wxString &msg)
Definition: cclogger.cpp:90
static std::unique_ptr< CCLogger > s_Inst
Definition: cclogger.h:53
long g_idCCAddToken
Definition: cclogger.cpp:24
static bool IsAppShuttingDown()
Definition: manager.cpp:333
void AddToken(const wxString &msg)
Definition: cclogger.cpp:77
int m_DebugLogId
Definition: cclogger.h:58
void DebugLog(const wxString &msg)
Definition: cclogger.cpp:107
void wxPostEvent(wxEvtHandler *dest, const wxEvent &event)
null_pointer_t nullptr
Definition: nullptr.cpp:16
wxString wxEmptyString
wxEvtHandler * m_Parent
Definition: cclogger.h:56
int m_AddTokenId
Definition: cclogger.h:59
long g_idCCDebugLogger
Definition: cclogger.cpp:26
CCLogger()
Definition: cclogger.cpp:52
void Init(wxEvtHandler *parent, int logId, int debugLogId, int addTokenId=-1)
Definition: cclogger.cpp:69
int m_LogId
Definition: cclogger.h:57
long g_idCCLogger
Definition: cclogger.cpp:25
bool g_EnableDebugTrace
Toggles tracing into file.
Definition: cclogger.cpp:21
bool g_EnableDebugTraceFile
Definition: cclogger.cpp:22