Audacity  3.0.3
AudacityLogger.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  AudacityLogger.cpp
6 
7 ******************************************************************//*******************************************************************/
15 
16 
17 #include "AudacityLogger.h"
18 
19 #include "Internat.h"
20 #include "MemoryX.h"
21 
22 #include <memory>
23 #include <mutex>
24 #include <wx/ffile.h>
25 #include <wx/log.h>
26 #include <wx/tokenzr.h>
27 
28 //
29 // AudacityLogger class
30 //
31 // By providing an Audacity specific logging class, it can be made thread-safe and,
32 // as such, can be used by the ever growing threading within Audacity.
33 //
34 
36 {
37  static std::once_flag flag;
38  std::call_once( flag, []{
39  // wxWidgets will clean up the logger for the main thread, so we can say
40  // safenew. See:
41  // http://docs.wxwidgets.org/3.0/classwx_log.html#a2525bf54fa3f31dc50e6e3cd8651e71d
42  std::unique_ptr < wxLog > // DELETE any previous logger
43  { wxLog::SetActiveTarget(safenew AudacityLogger) };
44  } );
45 
46  // Use dynamic_cast so that we get a NULL ptr in case our logger
47  // is no longer the target.
48  return dynamic_cast<AudacityLogger *>(wxLog::GetActiveTarget());
49 }
50 
52 : wxEvtHandler(),
53  wxLog()
54 {
55  mUpdated = false;
56 }
57 
59 
61 {
62  if (mUpdated && mListener && mListener())
63  mUpdated = false;
64 }
65 
67 {
68  auto result = std::move(mListener);
69  mListener = std::move(listener);
70  return result;
71 }
72 
73 void AudacityLogger::DoLogText(const wxString & str)
74 {
75  if (!wxIsMainThread()) {
76  wxMutexGuiEnter();
77  }
78 
79  if (mBuffer.empty()) {
80  wxString stamp;
81 
82  TimeStamp(&stamp);
83 
84  mBuffer << stamp << _TS("Audacity ") << AUDACITY_VERSION_STRING << wxT("\n");
85  }
86 
87  mBuffer << str << wxT("\n");
88 
89  mUpdated = true;
90 
91  Flush();
92 
93  if (!wxIsMainThread()) {
94  wxMutexGuiLeave();
95  }
96 }
97 
98 bool AudacityLogger::SaveLog(const wxString &fileName) const
99 {
100  wxFFile file(fileName, wxT("w"));
101 
102  if (file.IsOpened()) {
103  file.Write(mBuffer);
104  file.Close();
105  return true;
106  }
107 
108  return false;
109 }
110 
112 {
113  mBuffer = wxEmptyString;
114  DoLogText(wxT("Log Cleared."));
115 
116  return true;
117 }
118 
119 wxString AudacityLogger::GetLog(int count)
120 {
121  if (count == 0)
122  {
123  return mBuffer;
124  }
125 
126  wxString buffer;
127 
128  auto lines = wxStringTokenize(mBuffer, wxT("\r\n"), wxTOKEN_RET_DELIMS);
129  for (int index = lines.size() - 1; index >= 0 && count > 0; --index, --count)
130  {
131  buffer.Prepend(lines[index]);
132  }
133 
134  return buffer;
135 }
flag
static std::once_flag flag
Definition: WaveformView.cpp:1119
str
#define str(a)
Definition: DBConnection.cpp:30
AudacityLogger
AudacityLogger is a thread-safe logger class.
Definition: AudacityLogger.h:22
AudacityLogger::mUpdated
bool mUpdated
Definition: AudacityLogger.h:58
AudacityLogger::SetListener
Listener SetListener(Listener listener)
Set the unique listener, returning any previous one.
Definition: AudacityLogger.cpp:66
AudacityLogger::DoLogText
void DoLogText(const wxString &msg) override
Definition: AudacityLogger.cpp:73
_TS
#define _TS(s)
Definition: Internat.h:27
AudacityLogger::ClearLog
bool ClearLog()
Definition: AudacityLogger.cpp:111
AudacityLogger::Listener
std::function< bool() > Listener
Type of function called by Flush.
Definition: AudacityLogger.h:45
AudacityLogger::mListener
Listener mListener
Definition: AudacityLogger.h:56
AudacityLogger::Get
static AudacityLogger * Get()
Definition: AudacityLogger.cpp:35
AudacityLogger::SaveLog
bool SaveLog(const wxString &fileName) const
Definition: AudacityLogger.cpp:98
AudacityLogger::AudacityLogger
AudacityLogger()
Definition: AudacityLogger.cpp:51
Internat.h
AudacityLogger.h
AudacityLogger::Flush
void Flush() override
Definition: AudacityLogger.cpp:60
MemoryX.h
AudacityLogger::~AudacityLogger
~AudacityLogger() override
AudacityLogger::mBuffer
wxString mBuffer
Definition: AudacityLogger.h:57
safenew
#define safenew
Definition: MemoryX.h:10
AudacityLogger::GetLog
wxString GetLog(int count=0)
Retrieve all or some of the lines since most recent ClearLog or start of program.
Definition: AudacityLogger.cpp:119