Hi All,
I have created one class LogTask to store logs in certain file.
My problem is it's blocking GUI thread when I call Log(QString("")) .
Here is the code.
Can anyone help me with this???
Thanks in Advance
---
[CENTER][B]Task.cpp[/B][/CENTER]
typedef QList<QEvent*> Pocket;
Pocket mypocket;
Task
::Task():QThread() //constructor for task{
taskmanager::addTask(this); //adding task to the tasklist
stopsignal = false;
}
void Task::run(){
while (!stopsignal)
{
mymutex.lock();
mycondition.wait(&mymutex);
// got a signal..now process
processIncomingEvents();
mymutex.unlock();
}
}
void Task::processIncomingEvents()
{
while (e){
handleEvent(e);
mymutex.lock();
mypocket.remove(e);
mymutex.unlock();
e = mypocket.first();
}
//mypocket.clear();
}
[CENTER][B]Task.cpp[/B][/CENTER]
typedef QList<QEvent*> Pocket;
Pocket mypocket;
Task::Task():QThread() //constructor for task
{
taskmanager::addTask(this); //adding task to the tasklist
stopsignal = false;
}
void Task::run(){
while (!stopsignal)
{
mymutex.lock();
mycondition.wait(&mymutex);
// got a signal..now process
processIncomingEvents();
mymutex.unlock();
}
}
void Task::processIncomingEvents()
{
QEvent* e = mypocket.first();
while (e){
handleEvent(e);
mymutex.lock();
mypocket.remove(e);
mymutex.unlock();
e = mypocket.first();
}
//mypocket.clear();
}
To copy to clipboard, switch view to plain text mode
[CENTER][B]LogTask.h[/B][/CENTER]
#define FILE_PATH "d:/Aer2.log"
#define Log(x) LogTask::OutputDebugLog(QString(x+" in %1 in %2 at %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__))
class LogTask : public Task
{
public:
LogTask();
~LogTask();
bool Initialize();
bool Finalize();
virtual void handleEvent
( QEvent* ev
);
static void OutputDebugLog
( const QString & );
};
//##################################################
[CENTER][B]LogTask.cpp[/B][/CENTER]
LogTask* currentlogtask=0;
LogTask::LogTask()
{
currentlogtask = this;
}
LogTask::~LogTask()
{
qDebug("Logtask ended");
currentlogtask = 0;
}
bool LogTask::Initialize()
{
/*
file.setName(FILE_NAME);
if (!file.open(IO_WriteOnly | IO_Append))
return -1;
ts.setDevice(&file);*/
return true;
}
bool LogTask::Finalize()
{
//ts.flush();
//ts.close();
//file.close();
return true;
}
void LogTask
::OutputDebugLog(const QString &str
) {
if (currentlogtask)
currentlogtask
->acceptEvent
( new HonCustomEvent
(ETLOG,
0,
0,
new QString(str
)));
}
void LogTask
::handleEvent( QEvent* ev
) {
HonCustomEvent* rev = (HonCustomEvent*) ev;
if (rev->eventtype == ETLOG)
{
if(fileinfo.size() >= 5000000)
{ ///If file is 5MB in size change the file to output everything
if(tmpinfo.exists())
{
QDir dir
= tmpinfo.
dir();
dir.remove(newpath);
}
QDir newdir
= newinfo.
dir();
newdir.rename(path,newpath);
}
if(!file.open(IO_WriteOnly | IO_Append))
{
return;
}
ts << (*str)+"\n";
file.close();
delete str;
}
return;
}
[CENTER][B]LogTask.h[/B][/CENTER]
#define FILE_PATH "d:/Aer2.log"
#define Log(x) LogTask::OutputDebugLog(QString(x+" in %1 in %2 at %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__))
class LogTask : public Task
{
public:
LogTask();
~LogTask();
bool Initialize();
bool Finalize();
virtual void handleEvent( QEvent* ev );
static void OutputDebugLog( const QString & );
};
//##################################################
[CENTER][B]LogTask.cpp[/B][/CENTER]
LogTask* currentlogtask=0;
LogTask::LogTask()
{
currentlogtask = this;
}
LogTask::~LogTask()
{
qDebug("Logtask ended");
currentlogtask = 0;
}
bool LogTask::Initialize()
{
/*
file.setName(FILE_NAME);
if (!file.open(IO_WriteOnly | IO_Append))
return -1;
ts.setDevice(&file);*/
return true;
}
bool LogTask::Finalize()
{
//ts.flush();
//ts.close();
//file.close();
return true;
}
void LogTask::OutputDebugLog(const QString &str)
{
if (currentlogtask)
currentlogtask->acceptEvent( new HonCustomEvent(ETLOG, 0, 0, new QString(str)));
}
void LogTask::handleEvent( QEvent* ev )
{
HonCustomEvent* rev = (HonCustomEvent*) ev;
if (rev->eventtype == ETLOG)
{
QString path = FILE_PATH;
QString* str = (QString* )rev->ptr;
QFileInfo fileinfo(path);
if(fileinfo.size() >= 5000000)
{ ///If file is 5MB in size change the file to output everything
QString newpath = QString(FILE_PATH)+"."+"0";
QFileInfo tmpinfo(newpath);
if(tmpinfo.exists())
{
QDir dir = tmpinfo.dir();
dir.remove(newpath);
}
QFileInfo newinfo(path);
QDir newdir = newinfo.dir();
newdir.rename(path,newpath);
}
QFile file(path);
if(!file.open(IO_WriteOnly | IO_Append))
{
return;
}
QTextStream ts(&file);
ts << (*str)+"\n";
file.close();
delete str;
}
return;
}
To copy to clipboard, switch view to plain text mode
[CENTER][B]taskmanager.cpp[/B][/CENTER]
#include "taskmanager.h"
TaskList tasklist;
void taskmanager :: stopAllTasks()
{
Task *t = tasklist.first();
while(t) {
t->stopnow();
tasklist.remove(t);
t = tasklist.first();
}
tasklist.clear();
}
void taskmanager :: addTask(Task *t)
{
// Task *t = tasklist.getlast();
//add task to the list
tasklist.append( t );
}
int taskmanager :: getCountTasks()
{
return tasklist.count();
}
[CENTER][B]taskmanager.cpp[/B][/CENTER]
#include "taskmanager.h"
TaskList tasklist;
void taskmanager :: stopAllTasks()
{
Task *t = tasklist.first();
while(t) {
t->stopnow();
tasklist.remove(t);
t = tasklist.first();
}
tasklist.clear();
}
void taskmanager :: addTask(Task *t)
{
// Task *t = tasklist.getlast();
//add task to the list
tasklist.append( t );
}
int taskmanager :: getCountTasks()
{
return tasklist.count();
}
To copy to clipboard, switch view to plain text mode
Bookmarks