Audacity  3.0.3
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
AudacityLogger Class Referencefinal

AudacityLogger is a thread-safe logger class. More...

#include <AudacityLogger.h>

Inheritance diagram for AudacityLogger:
[legend]
Collaboration diagram for AudacityLogger:
[legend]

Public Types

using Listener = std::function< bool() >
 Type of function called by Flush. More...
 

Public Member Functions

 ~AudacityLogger () override
 
bool SaveLog (const wxString &fileName) const
 
bool ClearLog ()
 
wxString GetLog (int count=0)
 Retrieve all or some of the lines since most recent ClearLog or start of program. More...
 
const wxString & GetBuffer () const
 Get all the accumulated text since program start or last ClearLog() More...
 
void Flush () override
 
Listener SetListener (Listener listener)
 Set the unique listener, returning any previous one. More...
 

Static Public Member Functions

static AudacityLoggerGet ()
 

Protected Member Functions

void DoLogText (const wxString &msg) override
 

Private Member Functions

 AudacityLogger ()
 

Private Attributes

Listener mListener
 
wxString mBuffer
 
bool mUpdated
 

Detailed Description

AudacityLogger is a thread-safe logger class.

Provides thread-safe logging based on the wxWidgets log facility.

Definition at line 20 of file AudacityLogger.h.

Member Typedef Documentation

◆ Listener

using AudacityLogger::Listener = std::function< bool() >

Type of function called by Flush.

Returns
true if flush completed

Definition at line 45 of file AudacityLogger.h.

Constructor & Destructor Documentation

◆ ~AudacityLogger()

AudacityLogger::~AudacityLogger ( )
overridedefault

◆ AudacityLogger()

AudacityLogger::AudacityLogger ( )
private

Definition at line 51 of file AudacityLogger.cpp.

52 : wxEvtHandler(),
53  wxLog()
54 {
55  mUpdated = false;
56 }

References mUpdated.

Member Function Documentation

◆ ClearLog()

bool AudacityLogger::ClearLog ( )

Definition at line 111 of file AudacityLogger.cpp.

112 {
113  mBuffer = wxEmptyString;
114  DoLogText(wxT("Log Cleared."));
115 
116  return true;
117 }

References DoLogText(), and mBuffer.

Here is the call graph for this function:

◆ DoLogText()

void AudacityLogger::DoLogText ( const wxString &  msg)
overrideprotected

Definition at line 73 of file AudacityLogger.cpp.

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 }

References _TS, Flush(), mBuffer, mUpdated, and str.

Referenced by ClearLog().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Flush()

void AudacityLogger::Flush ( )
override

Definition at line 60 of file AudacityLogger.cpp.

61 {
62  if (mUpdated && mListener && mListener())
63  mUpdated = false;
64 }

References mListener, and mUpdated.

Referenced by DoLogText().

Here is the caller graph for this function:

◆ Get()

AudacityLogger * AudacityLogger::Get ( )
static

Definition at line 35 of file AudacityLogger.cpp.

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 }

References flag, and safenew.

Referenced by SaveLogCommand::Apply(), ClearLogCommand::Apply(), anonymous_namespace{LogWindow.cpp}::OnClear(), AudacityApp::OnInit(), QuitAudacity(), DBConnection::SetDBError(), DBConnection::SetError(), and LogWindow::Show().

Here is the caller graph for this function:

◆ GetBuffer()

const wxString& AudacityLogger::GetBuffer ( ) const
inline

Get all the accumulated text since program start or last ClearLog()

Definition at line 38 of file AudacityLogger.h.

38 { return mBuffer; }

◆ GetLog()

wxString AudacityLogger::GetLog ( int  count = 0)

Retrieve all or some of the lines since most recent ClearLog or start of program.

If count == 0 or is more than the number of lines, return all; else return the last count lines

Definition at line 119 of file AudacityLogger.cpp.

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 }

References mBuffer.

◆ SaveLog()

bool AudacityLogger::SaveLog ( const wxString &  fileName) const

Definition at line 98 of file AudacityLogger.cpp.

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 }

References mBuffer.

◆ SetListener()

auto AudacityLogger::SetListener ( Listener  listener)

Set the unique listener, returning any previous one.

Definition at line 66 of file AudacityLogger.cpp.

67 {
68  auto result = std::move(mListener);
69  mListener = std::move(listener);
70  return result;
71 }

Member Data Documentation

◆ mBuffer

wxString AudacityLogger::mBuffer
private

Definition at line 57 of file AudacityLogger.h.

Referenced by ClearLog(), DoLogText(), GetLog(), and SaveLog().

◆ mListener

Listener AudacityLogger::mListener
private

Definition at line 56 of file AudacityLogger.h.

Referenced by Flush().

◆ mUpdated

bool AudacityLogger::mUpdated
private

Definition at line 58 of file AudacityLogger.h.

Referenced by AudacityLogger(), DoLogText(), and Flush().


The documentation for this class was generated from the following files:
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::DoLogText
void DoLogText(const wxString &msg) override
Definition: AudacityLogger.cpp:73
_TS
#define _TS(s)
Definition: Internat.h:27
AudacityLogger::mListener
Listener mListener
Definition: AudacityLogger.h:56
AudacityLogger::Flush
void Flush() override
Definition: AudacityLogger.cpp:60
AudacityLogger::mBuffer
wxString mBuffer
Definition: AudacityLogger.h:57
safenew
#define safenew
Definition: MemoryX.h:10