mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-27 10:14:38 +00:00
added support show logs, and logs files
This commit is contained in:
parent
1ddda3fdf6
commit
5d80e54a11
84
params.cpp
84
params.cpp
@ -9,6 +9,7 @@
|
|||||||
#include <QVariantMap>
|
#include <QVariantMap>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <iostream>
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
#include "windows.h"
|
#include "windows.h"
|
||||||
#else
|
#else
|
||||||
@ -20,12 +21,16 @@ using namespace QuasarAppUtils;
|
|||||||
|
|
||||||
static QVariantMap params = QVariantMap();
|
static QVariantMap params = QVariantMap();
|
||||||
|
|
||||||
|
|
||||||
bool Params::isEndable(const QString& key) {
|
bool Params::isEndable(const QString& key) {
|
||||||
return params.contains(key);
|
return params.contains(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Params::verboseLog(const QString &log, VerboseLvl vLvl) {
|
void Params::verboseLog(const QString &log, VerboseLvl vLvl) {
|
||||||
|
|
||||||
|
if (!writeLoginFile(log, vLvl)) {
|
||||||
|
qWarning() << "Warning: Write log in file fail!";
|
||||||
|
}
|
||||||
|
|
||||||
if (isEndable("verbose")) {
|
if (isEndable("verbose")) {
|
||||||
|
|
||||||
auto lvl = static_cast<VerboseLvl>(getArg("verbose").toInt());
|
auto lvl = static_cast<VerboseLvl>(getArg("verbose").toInt());
|
||||||
@ -35,22 +40,22 @@ void Params::verboseLog(const QString &log, VerboseLvl vLvl) {
|
|||||||
switch (vLvl) {
|
switch (vLvl) {
|
||||||
|
|
||||||
case VerboseLvl::Error: {
|
case VerboseLvl::Error: {
|
||||||
qCritical() << "Error: " + log;
|
qCritical() << lvlToString(vLvl) + ": " + log;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case VerboseLvl::Warning: {
|
case VerboseLvl::Warning: {
|
||||||
qWarning() << "Warning: " + log;
|
qWarning() << lvlToString(vLvl) + ": " + log;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case VerboseLvl::Info: {
|
case VerboseLvl::Info: {
|
||||||
qInfo() << "Info: " + log;
|
qInfo() << lvlToString(vLvl) + ": " + log;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
qDebug() << "Verbose log: " + log ;
|
qDebug() << lvlToString(vLvl) + ": " + log;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,6 +63,71 @@ void Params::verboseLog(const QString &log, VerboseLvl vLvl) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList Params::getparamsHelp() {
|
||||||
|
return
|
||||||
|
{
|
||||||
|
{""},
|
||||||
|
{ " -verbose (level 1 - 3) : Shows debug log"},
|
||||||
|
{ " -fileLog (path to file) : Sets path of log file"},
|
||||||
|
{ " : Default it is path to executable file"},
|
||||||
|
{ " : with suffix '.log' "},
|
||||||
|
{ " noWriteInFileLog : Disables loging into file"},
|
||||||
|
{ ""}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Params::showHelp(const QStringList &help) {
|
||||||
|
for (const QString& line : help) {
|
||||||
|
std::cout << line.toStdString() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Params::lvlToString(VerboseLvl vLvl) {
|
||||||
|
switch (vLvl) {
|
||||||
|
|
||||||
|
case VerboseLvl::Error: {
|
||||||
|
return "Error";
|
||||||
|
}
|
||||||
|
|
||||||
|
case VerboseLvl::Warning: {
|
||||||
|
return "Warning";
|
||||||
|
}
|
||||||
|
|
||||||
|
case VerboseLvl::Info: {
|
||||||
|
return "Info";
|
||||||
|
}
|
||||||
|
|
||||||
|
case VerboseLvl::Debug: {
|
||||||
|
return "Verbose log";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Params::writeLoginFile(const QString &log, VerboseLvl vLvl) {
|
||||||
|
if (!isEndable("noWriteInFileLog")) {
|
||||||
|
|
||||||
|
QString path = getStrArg("appPath") + "/" + getStrArg("appName") + ".log";
|
||||||
|
if (isEndable("fileLog")) {
|
||||||
|
QString path = getStrArg("fileLog");
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile logFile(path);
|
||||||
|
|
||||||
|
if (logFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
|
||||||
|
|
||||||
|
QTextStream stream(&logFile);
|
||||||
|
stream << lvlToString(vLvl) + ": " + log;
|
||||||
|
logFile.close();
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool Params::parseParams(int argc,const char *argv[]) {
|
bool Params::parseParams(int argc,const char *argv[]) {
|
||||||
params.clear();
|
params.clear();
|
||||||
|
|
||||||
@ -65,6 +135,8 @@ bool Params::parseParams(int argc,const char *argv[]) {
|
|||||||
char buffer[MAX_PATH];
|
char buffer[MAX_PATH];
|
||||||
GetModuleFileNameA(nullptr, buffer, MAX_PATH);
|
GetModuleFileNameA(nullptr, buffer, MAX_PATH);
|
||||||
params ["appPath"] = QFileInfo(buffer).absolutePath();
|
params ["appPath"] = QFileInfo(buffer).absolutePath();
|
||||||
|
params ["appName"] = QFileInfo(buffer).fileName();
|
||||||
|
|
||||||
#else
|
#else
|
||||||
char path[2048];
|
char path[2048];
|
||||||
if (readlink("/proc/self/exe", path, 2048) < 0) {
|
if (readlink("/proc/self/exe", path, 2048) < 0) {
|
||||||
@ -72,6 +144,8 @@ bool Params::parseParams(int argc,const char *argv[]) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
params ["appPath"] = QFileInfo(path).absolutePath();
|
params ["appPath"] = QFileInfo(path).absolutePath();
|
||||||
|
params ["appName"] = QFileInfo(path).fileName();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!getStrArg("appPath").size()) {
|
if (!getStrArg("appPath").size()) {
|
||||||
|
15
params.h
15
params.h
@ -27,6 +27,9 @@ enum VerboseLvl {
|
|||||||
|
|
||||||
class QUASARAPPSHARED_EXPORT Params
|
class QUASARAPPSHARED_EXPORT Params
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
static QString lvlToString(VerboseLvl vLvl);
|
||||||
|
static bool writeLoginFile(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug);
|
||||||
public:
|
public:
|
||||||
Params() = delete;
|
Params() = delete;
|
||||||
|
|
||||||
@ -77,6 +80,18 @@ public:
|
|||||||
* @param log - printed text
|
* @param log - printed text
|
||||||
*/
|
*/
|
||||||
static void verboseLog(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug);
|
static void verboseLog(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief getparamsHelp
|
||||||
|
* @return help string of default params
|
||||||
|
*/
|
||||||
|
static QStringList getparamsHelp();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief showHelp - show all strings of help
|
||||||
|
* @param help
|
||||||
|
*/
|
||||||
|
static void showHelp(const QStringList& help);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user