kwave  18.07.70
Kwave::Logger Class Reference

#include <Logger.h>

Collaboration diagram for Kwave::Logger:
Collaboration graph

Public Types

enum  LogLevel {
  Debug = 0, Info, Warning, Error,
  Fatal
}
 

Public Member Functions

 Logger ()
 
virtual ~Logger ()
 

Static Public Member Functions

static bool Q_DECL_EXPORT open (const QString &filename)
 
static void Q_DECL_EXPORT log (const QObject *sender, LogLevel level, const QString &msg)
 

Static Private Attributes

static QFile * m_logfile = Q_NULLPTR
 

Detailed Description

This is the main application class for Kwave. It contains functions for opening and saving files, opening new windows and holds global configuration data.

Definition at line 40 of file Logger.h.

Member Enumeration Documentation

◆ LogLevel

Enumerator
Debug 

debug message

Info 

info

Warning 

warning, something might have went wrong

Error 

error, something failed, but is recoverable

Fatal 

fatal error, not recoverable, have to exit

Definition at line 44 of file Logger.h.

Constructor & Destructor Documentation

◆ Logger()

Kwave::Logger::Logger ( )

Constructor

Definition at line 44 of file Logger.cpp.

45 {
46 }

◆ ~Logger()

Kwave::Logger::~Logger ( )
virtual

Destructor.

Definition at line 49 of file Logger.cpp.

References _, Info, log(), and m_logfile.

50 {
51  if (m_logfile) {
52  log(Q_NULLPTR, Kwave::Logger::Info, _("--- CLOSED / APPLICATION SHUTDOWN ---"));
53  m_logfile->flush();
54  delete m_logfile;
55  }
56 }
static QFile * m_logfile
Definition: Logger.h:86
#define _(m)
Definition: memcpy.c:66
static void Q_DECL_EXPORT log(const QObject *sender, LogLevel level, const QString &msg)
Definition: Logger.cpp:103
Here is the call graph for this function:

Member Function Documentation

◆ log()

void Kwave::Logger::log ( const QObject *  sender,
LogLevel  level,
const QString &  msg 
)
static

log a message to the log file

Parameters
senderpointer to the sender of the message, must be derived from QObject
levelthe log level / severity
msgthe message to log

Definition at line 103 of file Logger.cpp.

References _, ELEMENTS_OF, m_logfile, and UTF8.

Referenced by Kwave::FileContext::executeCommand(), Kwave::TopWidget::loadFile(), open(), Kwave::MainWidget::saveLabels(), Kwave::CurveWidget::savePreset(), Kwave::DebugPlugin::screenshot(), and ~Logger().

106 {
107  static const char *str_level[] = {
108  "DBG", "INF", "WAR", "ERR", "FAT"
109  };
110  if (!m_logfile) return;
111 
112  // NOTE: it would be fine to have a way to find out the instance
113  // which this message belongs to (TopLevelWidget + MainWidget)
114  Q_UNUSED(sender);
115 
116  // translate the log level into a text (syslog format)
117  const char *x_status = str_level[qBound(
118  Q_UINT64_C(0),
119  static_cast<quint64>(level),
120  static_cast<quint64>(ELEMENTS_OF(str_level)))
121  ];
122 
123  // get the time stamp
124  QDateTime now = QDateTime::currentDateTime();
125  QString date_time = now.toString(_("yyyy-MM-dd hh:mm:ss.zzz"));
126 
127  // get the PID of the application
128  long int x_pid = qApp ? static_cast<long int>(qApp->applicationPid()) : -1;
129 
130  // format the log log message
131  // x-status date time x-pid x-message
132  QString line;
133  line.sprintf("<%s> %s %ld %s\n",
134  x_status,
135  UTF8(date_time),
136  x_pid,
137  UTF8(msg)
138  );
139 
140  m_logfile->write(line.toUtf8().constData());
141  m_logfile->flush();
142 }
#define ELEMENTS_OF(__array__)
Definition: Logger.cpp:41
static QFile * m_logfile
Definition: Logger.h:86
#define _(m)
Definition: memcpy.c:66
#define UTF8(qs)
Definition: String.h:48
Here is the caller graph for this function:

◆ open()

bool Kwave::Logger::open ( const QString &  filename)
static

open a log file for writing

Note
must only be called once, if there already is a log file, then the old log file will be closed and the new one will be opened.
Parameters
filenamename of the log file
Returns
true if succeed, false if failed

Definition at line 59 of file Logger.cpp.

References _, Kwave::Append, DBG, Info, log(), m_logfile, name, and Kwave::MessageBox::warningContinueCancel().

Referenced by Kwave::App::newInstance().

60 {
61  if (m_logfile) {
62  qWarning("reopening log file");
63  log(Q_NULLPTR, Kwave::Logger::Info, _("--- CLOSED / REOPEN ---"));
64  m_logfile->flush();
65  delete m_logfile;
66  }
67  qDebug("logging to file: '%s'", DBG(filename));
68 
69  QString name(filename);
70  m_logfile = new (std::nothrow) QFile(name);
71  Q_ASSERT(m_logfile);
72 
73  if (m_logfile) m_logfile->open(
74  QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
75  if (!m_logfile || (!m_logfile->isWritable())) {
77  i18n("Failed opening the log file '%1' for writing",
78  filename)) != KMessageBox::Continue)
79  {
80  return false;
81  }
82  }
83 
84  /*
85  * provide the "header" of an extended log file
86  * see http://www.w3.org/TR/WD-logfile.html
87  */
88 
89  QTextStream out(m_logfile);
90  const KAboutData about_data = KAboutData::applicationData();
91 
92  out << "#Version: 1.0" << endl;
93  out << "#Fields: x-status date time x-pid x-message" << endl;
94  out << "#Software: " << about_data.displayName() << " "
95  << about_data.version() << endl;
96  QDateTime now = QDateTime::currentDateTime();
97  out << "#Start-Date: " << now.toString(_("yyyy-MM-dd hh:mm:ss")) << endl;
98 
99  return true;
100 }
static QFile * m_logfile
Definition: Logger.h:86
const char name[16]
Definition: memcpy.c:510
static int warningContinueCancel(QWidget *widget, QString message, QString caption=QString(), const QString buttonContinue=QString(), const QString buttonCancel=QString(), const QString &dontAskAgainName=QString())
Definition: MessageBox.cpp:115
#define _(m)
Definition: memcpy.c:66
#define DBG(qs)
Definition: String.h:55
static void Q_DECL_EXPORT log(const QObject *sender, LogLevel level, const QString &msg)
Definition: Logger.cpp:103
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_logfile

QFile * Kwave::Logger::m_logfile = Q_NULLPTR
staticprivate

log file that receives the log messages (Null is allowed)

Definition at line 86 of file Logger.h.

Referenced by log(), open(), and ~Logger().


The documentation for this class was generated from the following files: