kwave  18.07.70
Logger.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Logger.cpp - Kwave log file handling
3  -------------------
4  begin : Sat May 17 2014
5  copyright : (C) 2014 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <new>
21 
22 #include <QApplication>
23 #include <QDateTime>
24 #include <QFile>
25 #include <QString>
26 #include <QTextStream>
27 #include <QtGlobal>
28 
29 #include <KAboutData>
30 #include <KLocalizedString>
31 
32 #include "libkwave/Logger.h"
33 #include "libkwave/MessageBox.h"
34 #include "libkwave/String.h"
35 
36 // static initializers
37 QFile *Kwave::Logger::m_logfile = Q_NULLPTR;
39 
41 #define ELEMENTS_OF(__array__) (sizeof(__array__) / sizeof(__array__[0]))
42 
43 //***************************************************************************
45 {
46 }
47 
48 //***************************************************************************
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 }
57 
58 //***************************************************************************
59 bool Kwave::Logger::open(const QString& filename)
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 }
101 
102 //***************************************************************************
103 void Kwave::Logger::log(const QObject *sender,
105  const QString& msg)
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 }
143 
144 //***************************************************************************
145 //***************************************************************************
#define ELEMENTS_OF(__array__)
Definition: Logger.cpp:41
static bool Q_DECL_EXPORT open(const QString &filename)
Definition: Logger.cpp:59
static QFile * m_logfile
Definition: Logger.h:86
virtual ~Logger()
Definition: Logger.cpp:49
const char name[16]
Definition: memcpy.c:510
static Kwave::Logger g_logger
Definition: Logger.cpp:38
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
#define UTF8(qs)
Definition: String.h:48