File: Logger.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (147 lines) | stat: -rw-r--r-- 4,837 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/***************************************************************************
             Logger.cpp  -  Kwave log file handling
                             -------------------
    begin                : Sat May 17 2014
    copyright            : (C) 2014 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <new>

#include <QApplication>
#include <QDateTime>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QtGlobal>

#include <KAboutData>
#include <KLocalizedString>

#include "libkwave/Logger.h"
#include "libkwave/MessageBox.h"
#include "libkwave/String.h"

// static initializers
QFile *Kwave::Logger::m_logfile = nullptr;
static Kwave::Logger g_logger;

/** helper macro: returns the number of elements in an array */
#define ELEMENTS_OF(__array__) (sizeof(__array__) / sizeof(__array__[0]))

//***************************************************************************
Kwave::Logger::Logger()
{
}

//***************************************************************************
Kwave::Logger::~Logger()
{
    if (m_logfile) {
        log(nullptr, Kwave::Logger::Info,
            _("--- CLOSED / APPLICATION SHUTDOWN ---"));
        m_logfile->flush();
        delete m_logfile;
        m_logfile = nullptr;
    }
}

//***************************************************************************
bool Kwave::Logger::open(const QString& filename)
{
    if (m_logfile) {
        qWarning("reopening log file");
        log(nullptr, Kwave::Logger::Info, _("--- CLOSED / REOPEN ---"));
        m_logfile->flush();
        delete m_logfile;
    }
    qDebug("logging to file: '%s'", DBG(filename));

    QString name(filename);
    m_logfile = new(std::nothrow) QFile(name);
    Q_ASSERT(m_logfile);

    if (m_logfile) m_logfile->open(
        QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
    if (!m_logfile || (!m_logfile->isWritable())) {
        if (Kwave::MessageBox::warningContinueCancel(nullptr,
            i18n("Failed opening the log file '%1' for writing",
            filename)) != KMessageBox::Continue)
        {
            return false;
        }
    }

    /*
     * provide the "header" of an extended log file
     * see http://www.w3.org/TR/WD-logfile.html
     */

    QTextStream out(m_logfile);
    const KAboutData about_data = KAboutData::applicationData();

    out << "#Version: 1.0" << Qt::endl;
    out << "#Fields: x-status date time x-pid x-message" << Qt::endl;
    out << "#Software: " << about_data.displayName() << " "
    << about_data.version() << Qt::endl;
    QDateTime now = QDateTime::currentDateTime();
    out << "#Start-Date: "
        << now.toString(_("yyyy-MM-dd hh:mm:ss"))
        << Qt::endl;

    return true;
}

//***************************************************************************
void Kwave::Logger::log(const QObject *sender,
                        Kwave::Logger::LogLevel level,
                        const QString &msg)
{
    static const char *str_level[] = {
        "DBG", "INF", "WAR", "ERR", "FAT"
    };
    if (!m_logfile) return;

    // NOTE: it would be fine to have a way to find out the instance
    //       which this message belongs to (TopLevelWidget + MainWidget)
    Q_UNUSED(sender)

    // translate the log level into a text (syslog format)
    const char *x_status = str_level[qBound(
        Q_UINT64_C(0),
        static_cast<quint64>(level),
        static_cast<quint64>(ELEMENTS_OF(str_level)))
    ];

    // get the time stamp
    QDateTime now = QDateTime::currentDateTime();

    // get the PID of the application
    long int x_pid = qApp ? static_cast<long int>(qApp->applicationPid()) : -1;

    // format the log log message
    // x-status date time x-pid x-message
    {
        QTextStream out(m_logfile);

        out << "<" << x_status << "> " <<
        now.toString(_("yyyy-MM-dd hh:mm:ss.zzz")) << " " <<
        x_pid << " " << msg << Qt::endl;
    }

    m_logfile->flush();
}

//***************************************************************************
//***************************************************************************