Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00022
00023 #include <QtCore/QByteArray>
00024 #include <QtCore/QStringList>
00025 #include <QtSql/QSqlError>
00026 #include <QtSql/QSqlQuery>
00027
00028 #include <iostream>
00029
00030 #include "UdpLoggingServer.h"
00031
00032 UdpLoggingServer::UdpLoggingServer(const QString& db_filename)
00033 {
00034
00035 m_udp_socket = new QUdpSocket(this);
00036 m_udp_socket->bind(60000, QUdpSocket::ShareAddress);
00037
00038 connect(m_udp_socket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
00039
00040
00041 m_db = QSqlDatabase::addDatabase("QSQLITE");
00042 m_db.setDatabaseName(db_filename);
00043 m_db.open();
00044
00045 bool log_entries_exists = false;
00046 foreach(QString table, m_db.tables(QSql::AllTables))
00047 {
00048 if (table == "log_entries")
00049 {
00050 log_entries_exists = true;
00051 }
00052 }
00053 if (!log_entries_exists)
00054 {
00055 m_db.exec("CREATE TABLE log_entries ("
00056 "system_name TEXT,"
00057 "timestamp TEXT, timestamp_ns INTEGER, "
00058 "log_level TEXT, log_stream TEXT, "
00059 "file TEXT, line INTEGER, "
00060 "class TEXT, object TEXT, function TEXT, "
00061 "message TEXT)");
00062 }
00063 }
00064
00065 UdpLoggingServer::~UdpLoggingServer()
00066 {
00067 }
00068
00069 void UdpLoggingServer::processPendingDatagrams()
00070 {
00071 while (m_udp_socket->hasPendingDatagrams())
00072 {
00073 QByteArray datagram;
00074 datagram.resize(m_udp_socket->pendingDatagramSize());
00075 m_udp_socket->readDatagram(datagram.data(), datagram.size());
00076
00077 QString sql = "INSERT INTO log_entries VALUES (";
00078 sql.append(datagram.data());
00079 sql.append(")");
00080 m_db.exec(sql);
00081 }
00082 }