| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2024> Alf |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE ApvlvLog.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QFile> |
| 28 |
#include <QFileInfo> |
| 29 |
#include <QLoggingCategory> |
| 30 |
#include <QTime> |
| 31 |
#include <iostream> |
| 32 |
#include <string> |
| 33 |
|
| 34 |
#include "ApvlvLog.h" |
| 35 |
|
| 36 |
namespace apvlv |
| 37 |
{ |
| 38 |
void |
| 39 |
ApvlvLog::setLogFile (const std::string &path) |
| 40 |
{ |
| 41 |
using FileFlag = QIODevice::OpenModeFlag; |
| 42 |
if (!path.empty ()) |
| 43 |
{ |
| 44 |
mFile.setFileName (QString::fromLocal8Bit (path)); |
| 45 |
if (mFile.open (FileFlag::Text | FileFlag::WriteOnly | FileFlag::Append) |
| 46 |
== false) |
| 47 |
{ |
| 48 |
std::cerr << "Open log file: " << path |
| 49 |
<< "error: " << mFile.errorString ().toStdString () |
| 50 |
<< std::endl; |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
mTextStream.setDevice (&mFile); |
| 55 |
} |
| 56 |
|
| 57 |
QLoggingCategory::setFilterRules ("qt.*=false\n" |
| 58 |
"default.debug=true\n" |
| 59 |
"default.*=true"); |
| 60 |
qInstallMessageHandler (ApvlvLog::logMessage); |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
ApvlvLog::writeMessage (const QString &msg) |
| 65 |
{ |
| 66 |
std::lock_guard<std::mutex> lock (mMutex); |
| 67 |
#ifdef _DEBUG |
| 68 |
std::cout << msg.toStdString () << std::endl; |
| 69 |
#endif |
| 70 |
|
| 71 |
auto endstr = "\n"; |
| 72 |
#ifdef WIN32 |
| 73 |
endstr = "\r\n"; |
| 74 |
#endif |
| 75 |
if (mTextStream.device () && mTextStream.device ()->isOpen ()) |
| 76 |
{ |
| 77 |
mTextStream << msg << endstr; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
ApvlvLog::~ApvlvLog () |
| 82 |
{ |
| 83 |
if (mFile.isOpen ()) |
| 84 |
{ |
| 85 |
mFile.close (); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
ApvlvLog * |
| 90 |
ApvlvLog::instance () |
| 91 |
{ |
| 92 |
static ApvlvLog log; |
| 93 |
return &log; |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
ApvlvLog::logMessage (QtMsgType type, const QMessageLogContext &context, |
| 98 |
const QString &msg) |
| 99 |
{ |
| 100 |
auto now = QTime::currentTime (); |
| 101 |
auto nowstr = now.toString ("hh:mm:ss.zzz"); |
| 102 |
QString log = nowstr + " "; |
| 103 |
|
| 104 |
if (context.file) |
| 105 |
{ |
| 106 |
auto filename = QFileInfo (context.file).fileName ().toStdString (); |
| 107 |
log += QString::asprintf ("%s:%d ", filename.c_str (), context.line); |
| 108 |
log += QString::asprintf ("%s ", context.function); |
| 109 |
} |
| 110 |
log += msg; |
| 111 |
|
| 112 |
ApvlvLog::instance ()->writeMessage (log); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// Local Variables: |
| 117 |
// mode: c++ |
| 118 |
// End: |