#include <QCoreApplication>
#include <QtGlobal>
#include <QTextStream>
#include <QFile>
#include <QDateTime>
#include <QMap>
#include <QDebug>
#include <QVector>
#include <QList>
#include <QSet>
#include <QHash>
#include <QPair>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
template <typename SequentialContainer>
inline QTextStream& printSequentialContainer(QTextStream& os, const char *which, const SequentialContainer &c)
{
os << which << '(';
typename SequentialContainer::const_iterator it = c.begin(), end = c.end();
if (it != end) {
os << *it;
++it;
}
while (it != end) {
os << ", " << *it;
++it;
}
os << ')';
return os;
}
template <typename T>
inline QTextStream& operator<<(QTextStream& os, const QVector<T> &vec)
{
return printSequentialContainer(os, "QVector", vec);
}
template <class T>
inline QTextStream& operator<<(QTextStream& os, const QList<T> &list)
{
return printSequentialContainer(os, "QList", list);
}
template <class T>
inline QTextStream& operator<<(QTextStream& os, const QSet<T> &set)
{
return printSequentialContainer(os, "QSet", set);
}
template <class Key, class T>
inline QTextStream& operator<<(QTextStream& os, const QHash<Key, T> &hash)
{
os << "QHash(";
for (typename QHash<Key, T>::const_iterator it = hash.constBegin();
it != hash.constEnd(); ++it)
os << '(' << it.key() << ", " << it.value() << ')';
os << ')';
return os;
}
template <class T1, class T2>
inline QTextStream& operator<<(QTextStream& os, const QPair<T1, T2> &pair)
{
os << "QPair(" << pair.first << ',' << pair.second << ')';
return os;
}
QTextStream& operator<<(QTextStream& os, const QJsonObject &o)
{
if (o.isEmpty()) {
os << "QJsonObject()";
return os;
}
QJsonDocument jsonDoc(o);
QByteArray byteArray = jsonDoc.toJson(QJsonDocument::Compact);
QString jsonString = QString::fromUtf8(byteArray);
os << "QJsonObject("
<< jsonString
<< ")";
return os;
}
QTextStream& operator<<(QTextStream& os, const QJsonArray &o)
{
if (o.isEmpty()) {
os << "QJsonArray()";
return os;
}
QJsonDocument jsonDoc(o);
QByteArray byteArray = jsonDoc.toJson(QJsonDocument::Compact);
QString jsonString = QString::fromUtf8(byteArray);
os << "QJsonArray("
<< jsonString
<< ")";
return os;
}
template <class K, class V>
QTextStream& operator<<(QTextStream& os, const QMap<K, V>& m) {
os << "QMap(";
for (typename QMap<K, V>::const_iterator it = m.constBegin();
it != m.constEnd(); ++it) {
os << '(' << it.key() << ", " << it.value() << ')';
}
os << ')';
return os;
}
class YLog
{
private:
QFile file;
QTextStream out;
int minlevel_;
public:
enum Type{
ADD = 0,
OVER
};
enum Level{
DEBUG = 0,
INFO,
ERR
};
YLog(const int level,const QString& logfile,const int type = YLog::OVER):file(logfile),out(&file)
{
Q_ASSERT((this->ERR == level || this->INFO == level || this->DEBUG == level) &&
"Logfile create failed, please check the level(YLog::ERR or YLog::INFO or YLog::DEBUG.");
if (type == this->ADD) {
this->file.open(QFile::WriteOnly | QFile::Append);
} else if (type == this->OVER) {
this->file.open(QFile::WriteOnly | QFile::Truncate);
} else {
Q_ASSERT(0 && "Logfile create failed, please check the type(YLog::OVER or YLog::ADD).");
}
}
~YLog(){
if(this->file.isOpen())
this->file.close();
}
template<typename T> void W(const QString &codefile, const int codeline, const int level, const QString &info, const T &value) {
Q_ASSERT(this->file.isOpen() && "Logfile write failed.");
if (this->minlevel_ <= level)
{
QDateTime dt = QDateTime::currentDateTime();
this->out << dt.toString("yyyy-MM-dd HH:mm:ss.zzz") << " [";
if (this->ERR == level) {
this->out << "ERROR";
} else if (this->INFO == level) {
this->out << "INFO";
} else if (this->DEBUG == level) {
this->out << "DEBUG";
} else {
Q_ASSERT(0 && "Log write failed, please check the level(YLog::ERR or YLog::INFO or YLog::DEBUG.");
}
this->out << "]: [" << codefile << ':' << codeline << "]:" << info << ':' << value<<'\n';
this->out.flush();
}
}
};
YLog mlg(YLog::INFO,"mylog.txt",YLog::ADD);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMap<int,QList<QJsonArray>> mm;
QJsonObject obj;
obj.insert("name","yhj");
obj.insert("age",18);
QJsonObject sec;
sec.insert("name",1.2);
sec.insert("age",19);
QJsonArray arr;
arr.push_back(obj);
arr.push_back(sec);
QJsonArray arr2;
arr2.push_back(obj);
arr2.push_back(sec);
QList<QJsonArray> set;
set.push_back(arr);
set.push_back(arr2);
mm.insert(1,set);
for(int i=0;i<10;++i)
{
mlg.W(__FILE__, __LINE__, YLog::INFO, "测试日志", mm);
}
qDebug()<<arr;
return a.exec();
}
QT日志类,可输出至文件QMap QJsonObject等
最新推荐文章于 2024-07-19 15:18:53 发布