Skip to content

Commit

Permalink
major fixes and updates
Browse files Browse the repository at this point in the history
LIST OF CHANGES ON THIS COMMIT:
- Changed the internal application configuration system to use the standard QSettings from Qt (instead of a cfg file an ini file)
- Move some of the syntax to C++ 11
- Used smart pointers where necessary/recommended
- Fixed some mem leaks around the code
- Enhanced the abstraction of the architecture, removed some unecessary classes added some necessary ones.
- Corrected the function that searches for the configuration file (using proper Qt syntax QDir, iterators, etc)
- Corrected the XML output file to properly close the XML structure
- Corrected some errors on the file handling of the log, and cleanup routines
- Created the JSON output file format
- Created the SIGNAL/SLOT (Qt) version of an output - SignalOutput
- Changed the constants to an proper global include file to easy application config and update
- Added extra documentation to the source code
- Added a test project to the qlogger project
- Changed the qlogger project to a proper static lib project
  • Loading branch information
sfadiga committed Sep 26, 2018
1 parent 9ae1bb5 commit 18267c5
Show file tree
Hide file tree
Showing 38 changed files with 2,659 additions and 1,401 deletions.
30 changes: 0 additions & 30 deletions QLogger.pro

This file was deleted.

88 changes: 88 additions & 0 deletions QLoggerLib/jsonoutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* QLogger - A tiny Qt logging framework.
*
* MIT License
* Copyright (c) 2013 sandro fadiga
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "jsonoutput.h"

namespace qlogger
{

JSONOutput::JSONOutput(Configuration *cfg) : PlainTextOutput (cfg)
{
this->configuration->setFileNameMask(JSON_FILE_NAME_MASK);
}

JSONOutput::~JSONOutput()
{
if(!outputFile.isNull() && outputFile->isOpen())
{
// end current json file.
close();
}
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)))
{
close();
}

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
{
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);
}

}
84 changes: 84 additions & 0 deletions QLoggerLib/jsonoutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* QLogger - A tiny Qt logging framework.
*
* MIT License
* Copyright (c) 2013 sandro fadiga
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef JSONOUTPUT_H
#define JSONOUTPUT_H

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

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

namespace qlogger {

//!
//! \brief The JSONOutput class - saves a text file with a standard json format with the log information
//!
class JSONOutput : public PlainTextOutput
{

public:
//!
//! \brief JSONOutput
//! \param cfg
//!
JSONOutput(Configuration* cfg);
JSONOutput() = delete;
//!
~JSONOutput();

// Output interface
public:
//!
//! \brief write
//! \param message
//! \param owner
//! \param lvl
//! \param timestamp
//! \param functionName
//! \param lineNumber
//!
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;

};

}

#endif // JSONOUTPUT_H
34 changes: 34 additions & 0 deletions QLoggerLib/output.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* QLogger - A tiny Qt logging framework.
*
* MIT License
* Copyright (c) 2013 sandro fadiga
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "output.h"

namespace qlogger
{

Output::Output(Configuration* conf) : configuration(QSharedPointer<Configuration>(conf)) {}

QSharedPointer<Configuration> Output::getConfiguration() const
{
return configuration;
}

}
53 changes: 53 additions & 0 deletions QLoggerLib/signaloutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* QLogger - A tiny Qt logging framework.
*
* MIT License
* Copyright (c) 2013 sandro fadiga
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "signaloutput.h"

#include <QDateTime>

namespace qlogger
{


SignalOutput::SignalOutput(Configuration *cfg) : Output(cfg)
{
}

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

QString logtext = formatLogText(configuration->getLogTextMask(),
message, owner, levelToString(lvl),
timestamp.toString(configuration->getTimestampFormat()),
functionName, lineNumber);
emit qlogger(logtext);

}

void SignalOutput::close()
{
}



}
80 changes: 80 additions & 0 deletions QLoggerLib/signaloutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* QLogger - A tiny Qt logging framework.
*
* MIT License
* Copyright (c) 2013 sandro fadiga
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SIGNALOUTPUT_H
#define SIGNALOUTPUT_H

#include <QObject>

#include "consoleoutput.h"

namespace qlogger
{

//!
//! \brief The SignalOutput class - An output class tha 'writes' to a SLOT, this class is also a QObject so
//! it can emit a SIGNAL when a log is written
//!
class SignalOutput : public QObject, public Output
{
Q_OBJECT

public:
//!
//! \brief SignalOutput
//! \param cfg
//!
SignalOutput(Configuration *cfg);
SignalOutput() = delete;
//!
~SignalOutput() = default;

// Output interface
public:
//!
//! \brief write
//! \param message
//! \param owner
//! \param lvl
//! \param timestamp
//! \param functionName
//! \param lineNumber
//!
void write(const QString message, const QString owner, const Level lvl,
const QDateTime timestamp, const QString functionName, const int lineNumber);

//!
//! \brief close
//!
void close();

signals:
//!
//! \brief qlogger - the signal emmited when 'write' is called
//! \param logtext - the output text written by this log to the SLOT
//!
void qlogger(QString logtext);

};

}

#endif // SIGNALOUTPUT_H
Loading

0 comments on commit 18267c5

Please sign in to comment.