Skip to content

Commit

Permalink
changed the config file to allow maxFileSize to use symbols like kb, …
Browse files Browse the repository at this point in the history
…changed the Json output implementation to use a simple text output, corrected the file creation for json outputs as well, some comments were added and some constants renamed
  • Loading branch information
sfadiga committed Sep 27, 2018
1 parent e7474cd commit db665c0
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 148 deletions.
7 changes: 6 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ Example of a configuration file for QLogger:
level=warn
outputType=text

# 10mb file
# in this option is possible to pass file sizes in plain int (number of bytes) or with the following
# options: x Mb, xM, xm, xMB, x mb, xKB, xk, xK, xKb, xkb, xg, xG, xgb, xGb, xGB as x being a number.
# examples: 100 kb, 100k, 10M, 1MB, 5g, 10 Mb...
# 100 kb file
maxFileSize=100k
path=c:/temp/
maxFileSize=10000
path=c:/temp/

Expand Down
32 changes: 12 additions & 20 deletions qlogger/qlogger.pro.user
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.7.1, 2018-09-26T00:44:36. -->
<!-- Written by QtCreator 4.7.0, 2018-09-27T00:35:31. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{12a5a9ae-37ff-4cb6-b988-27b4d7c799ec}</value>
<value type="QByteArray">{8d848d11-0b44-43ea-9d97-d412183d6d2f}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
Expand Down Expand Up @@ -59,27 +59,19 @@
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">false</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.TidyAndClazy</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs">
<value type="QString">D:/Dev/Source/QLogger/QLogger/QLogger.pro</value>
</valuelist>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.11.2 MSVC2017 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.11.2 MSVC2017 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5112.win64_msvc2017_64_kit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.11.1 MinGW 32bit2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.11.1 MinGW 32bit2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5111.win32_mingw53_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Dev/Source/QLogger/build-QLogger-Desktop_Qt_5_11_2_MSVC2017_64bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/sfadiga/Embraer/Source/QLogger/build-qlogger-Desktop_Qt_5_11_1_MinGW_32bit2-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand Down Expand Up @@ -133,7 +125,7 @@
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Dev/Source/QLogger/build-QLogger-Desktop_Qt_5_11_2_MSVC2017_64bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/sfadiga/Embraer/Source/QLogger/build-qlogger-Desktop_Qt_5_11_1_MinGW_32bit2-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand Down Expand Up @@ -187,7 +179,7 @@
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Dev/Source/QLogger/build-QLogger-Desktop_Qt_5_11_2_MSVC2017_64bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/sfadiga/Embraer/Source/QLogger/build-qlogger-Desktop_Qt_5_11_1_MinGW_32bit2-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand Down Expand Up @@ -297,14 +289,14 @@
</valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">QLoggerTester</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">qloggertester</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/Dev/Source/QLogger/QLoggerTester/QLoggerTester.pro</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/sfadiga/Embraer/Source/QLogger/qloggertester/qloggertester.pro</value>
<value type="bool" key="QmakeProjectManager.QmakeRunConfiguration.UseLibrarySearchPath">true</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">../QLoggerTester/QLoggerTester.pro</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">../qloggertester/qloggertester.pro</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">D:/Dev/Source/QLogger/QLoggerTester</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">C:/Users/sfadiga/Embraer/Source/QLogger/qloggertester</value>
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
Expand Down
14 changes: 6 additions & 8 deletions qloggerlib/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ Configuration::Configuration(QString logOwner,
QString fileMask,
QString fileTimestampFormat,
QString path,
int maxSizeInKb)
qint64 maxSizeInBytes)
: logOwner(std::move(logOwner)), logLevel(lvl),
logTextMask(std::move(textMask)), timestampFormat(std::move(timestampFormat)),
fileNameMask(std::move(fileMask)), fileNameTimestampFormat(std::move(fileTimestampFormat)),
filePath(std::move(path)), fileMaxSizeInKb(maxSizeInKb)
filePath(std::move(path)), fileMaxSizeInBytes(maxSizeInBytes)
{
}

Expand All @@ -50,14 +50,14 @@ bool Configuration::validate()
return !(logOwner.isEmpty() || logTextMask.isEmpty() || timestampFormat.isEmpty());
}

int Configuration::getFileMaxSizeInKb() const
qint64 Configuration::getFileMaxSizeInBytes() const
{
return fileMaxSizeInKb;
return fileMaxSizeInBytes;
}

void Configuration::setFileMaxSizeInKb(int value)
void Configuration::setFileMaxSizeInBytes(qint64 value)
{
fileMaxSizeInKb = value;
fileMaxSizeInBytes = value;
}

QString Configuration::getFilePath() const
Expand Down Expand Up @@ -130,6 +130,4 @@ void Configuration::setLogOwner(const QString &value)
logOwner = value;
}



}
12 changes: 6 additions & 6 deletions qloggerlib/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class Configuration final
//!
Configuration(QString logOwner, Level lvl = q1ERROR,
QString logTextMask = DEFAULT_TEXT_MASK,
QString timestampFormat = TIMESTAMP_QLOGGER_FORMAT,
QString timestampFormat = DEFAULT_TIMESTAMP_FORMAT,
QString fileNameMask = TEXT_FILE_NAME_MASK,
QString fileNameTimestampFormat = FILE_NAME_TIMESTAMP_FORMAT,
QString filePath = DEFAULT_LOG_PATH,
int fileMaxSizeInKb = DEFAULT_FILE_SIZE);
qint64 maxSizeInBytes = DEFAULT_FILE_SIZE_MB);


//!destructor as default since not much to do here.
Expand Down Expand Up @@ -98,8 +98,8 @@ class Configuration final
QString getFilePath() const;
void setFilePath(const QString &value);

int getFileMaxSizeInKb() const;
void setFileMaxSizeInKb(int value);
qint64 getFileMaxSizeInBytes() const;
void setFileMaxSizeInBytes(qint64 value);

private:

Expand Down Expand Up @@ -139,9 +139,9 @@ class Configuration final
QString filePath;

//!
//! \brief fileMaxSizeInKb - the max sizes of the log output file
//! \brief fileMaxSizeInBytes - the max size in bytes of the log output file
//!
int fileMaxSizeInKb;
qint64 fileMaxSizeInBytes;

};

Expand Down
4 changes: 0 additions & 4 deletions qloggerlib/consoleoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
#include "consoleoutput.h"

#include <QDateTime>
#include <QFile>
#include <QDir>
#include <iostream>
#include <QTextCodec>

#include "configuration.h"

Expand Down
2 changes: 0 additions & 2 deletions qloggerlib/consoleoutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

#include "output.h"

#include <QCoreApplication>
#include <QTextStream>
#include <QFile>

using namespace std;

Expand Down
55 changes: 24 additions & 31 deletions qloggerlib/jsonoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
#include "jsonoutput.h"

#include <QDateTime>

namespace qlogger
{

Expand All @@ -34,55 +36,46 @@ JSONOutput::~JSONOutput()
if(!outputFile.isNull() && outputFile->isOpen())
{
// end current json file.
close();
*outputStream << endl << JSON_FILE_END << endl;
}
PlainTextOutput::close();
}

void JSONOutput::close()
{
if(!outputFile.isNull())
{
// end current json file.
jsonCurrentLog[JSON_LOGS] = jsonCurrentList;

QJsonDocument jsonDoc(jsonCurrentLog);
outputFile->write(jsonDoc.toJson());
// release the file
outputFile->close();
// clean the json objects
jsonCurrentLog.empty();
jsonCurrentList.empty();
}
}

void JSONOutput::write(const QString message, const QString owner,
const Level lvl, const QDateTime timestamp,
const QString functionName, const int lineNumber)
{

if(!outputFile.isNull()
&& outputFile->isOpen()
&& (outputFile->size() > (configuration->getFileMaxSizeInKb() * 1024)))
&& (outputFile->size() > configuration->getFileMaxSizeInBytes()))
{
close();
// end current json file.
*outputStream << endl << JSON_FILE_END << endl;
}

if(outputFile.isNull() //if there is no file
|| !outputFile->isOpen() // or the file is not opened for writing
|| (outputFile->size() > (configuration->getFileMaxSizeInKb() * 1024))) // or the file is already at max size
|| (outputFile->size() > configuration->getFileMaxSizeInBytes())) // or the file is already at max size
{
createNextFile(); // create a new file
}

QJsonObject json;
json[JSON_LINE] = lineNumber;
json[JSON_FUNCTION] = functionName;
json[JSON_DATE_TIME] = timestamp.toString(configuration->getTimestampFormat());
json[JSON_LEVEL] = levelToString(lvl);
json[JSON_MESSAGE] = message;
json[JSON_OWNER] = owner;
jsonCurrentList.append(json);
// start the xml file.
*outputStream << JSON_FILE_START << endl;
// the first line will start without adding the comma
QString lineNumberStr = QString("%1").arg(lineNumber);
*outputStream << JSON_LOG_ENTRY.arg(owner, levelToString(lvl),
message, timestamp.toString(configuration->getTimestampFormat()),
functionName, lineNumberStr);
}
else // continues the file normally
{
// adds a comma and jump to the next line, in the end of the file there will be no comma
*outputStream << "," << endl;
QString lineNumberStr = QString("%1").arg(lineNumber);
*outputStream << JSON_LOG_ENTRY.arg(owner, levelToString(lvl),
message, timestamp.toString(configuration->getTimestampFormat()),
functionName, lineNumberStr);
}
}

}
21 changes: 0 additions & 21 deletions qloggerlib/jsonoutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
#define JSONOUTPUT_H

#include "textoutput.h"
#include <QDateTime>

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

namespace qlogger {

Expand Down Expand Up @@ -61,22 +56,6 @@ class JSONOutput : public PlainTextOutput
void write(const QString message, const QString owner,
const Level lvl, const QDateTime timestamp,
const QString functionName, const int lineNumber);
// Output interface
//!
//! \brief close
//!
void close();

private:
//!
//! \brief jsonCurrentLog
//!
QJsonObject jsonCurrentLog;
//!
//! \brief jsonCurrentList
//!
QJsonArray jsonCurrentList;

};

}
Expand Down
Loading

0 comments on commit db665c0

Please sign in to comment.