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
---
Qt Code:
  1. [CENTER][B]Task.cpp[/B][/CENTER]
  2.  
  3. typedef QList<QEvent*> Pocket;
  4. Pocket mypocket;
  5. Task::Task():QThread() //constructor for task
  6. {
  7. taskmanager::addTask(this); //adding task to the tasklist
  8. stopsignal = false;
  9.  
  10. }
  11.  
  12. void Task::run(){
  13. while (!stopsignal)
  14. {
  15. mymutex.lock();
  16. mycondition.wait(&mymutex);
  17.  
  18. // got a signal..now process
  19. processIncomingEvents();
  20. mymutex.unlock();
  21.  
  22. }
  23. }
  24.  
  25. void Task::processIncomingEvents()
  26. {
  27. QEvent* e = mypocket.first();
  28. while (e){
  29. handleEvent(e);
  30. mymutex.lock();
  31. mypocket.remove(e);
  32. mymutex.unlock();
  33. e = mypocket.first();
  34. }
  35. //mypocket.clear();
  36. }
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. [CENTER][B]LogTask.h[/B][/CENTER]
  2.  
  3. #define FILE_PATH "d:/Aer2.log"
  4. #define Log(x) LogTask::OutputDebugLog(QString(x+" in %1 in %2 at %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__))
  5.  
  6. class LogTask : public Task
  7. {
  8. public:
  9. LogTask();
  10. ~LogTask();
  11.  
  12. bool Initialize();
  13. bool Finalize();
  14. virtual void handleEvent( QEvent* ev );
  15. static void OutputDebugLog( const QString & );
  16.  
  17. };
  18.  
  19. //##################################################
  20.  
  21. [CENTER][B]LogTask.cpp[/B][/CENTER]
  22.  
  23. LogTask* currentlogtask=0;
  24.  
  25. LogTask::LogTask()
  26. {
  27. currentlogtask = this;
  28. }
  29.  
  30. LogTask::~LogTask()
  31. {
  32. qDebug("Logtask ended");
  33. currentlogtask = 0;
  34. }
  35.  
  36. bool LogTask::Initialize()
  37. {
  38.  
  39. /*
  40. file.setName(FILE_NAME);
  41. if (!file.open(IO_WriteOnly | IO_Append))
  42. return -1;
  43. ts.setDevice(&file);*/
  44.  
  45. return true;
  46. }
  47.  
  48. bool LogTask::Finalize()
  49. {
  50. //ts.flush();
  51. //ts.close();
  52. //file.close();
  53. return true;
  54. }
  55.  
  56. void LogTask::OutputDebugLog(const QString &str)
  57. {
  58.  
  59. if (currentlogtask)
  60. currentlogtask->acceptEvent( new HonCustomEvent(ETLOG, 0, 0, new QString(str)));
  61. }
  62.  
  63.  
  64.  
  65. void LogTask::handleEvent( QEvent* ev )
  66. {
  67. HonCustomEvent* rev = (HonCustomEvent*) ev;
  68. if (rev->eventtype == ETLOG)
  69. {
  70. QString path = FILE_PATH;
  71. QString* str = (QString* )rev->ptr;
  72. QFileInfo fileinfo(path);
  73.  
  74. if(fileinfo.size() >= 5000000)
  75. { ///If file is 5MB in size change the file to output everything
  76. QString newpath = QString(FILE_PATH)+"."+"0";
  77. QFileInfo tmpinfo(newpath);
  78. if(tmpinfo.exists())
  79. {
  80. QDir dir = tmpinfo.dir();
  81. dir.remove(newpath);
  82. }
  83. QFileInfo newinfo(path);
  84. QDir newdir = newinfo.dir();
  85. newdir.rename(path,newpath);
  86. }
  87.  
  88. QFile file(path);
  89.  
  90. if(!file.open(IO_WriteOnly | IO_Append))
  91. {
  92. return;
  93. }
  94. QTextStream ts(&file);
  95.  
  96. ts << (*str)+"\n";
  97. file.close();
  98. delete str;
  99. }
  100.  
  101. return;
  102. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. [CENTER][B]taskmanager.cpp[/B][/CENTER]
  2.  
  3. #include "taskmanager.h"
  4.  
  5.  
  6. TaskList tasklist;
  7.  
  8. void taskmanager :: stopAllTasks()
  9. {
  10. Task *t = tasklist.first();
  11. while(t) {
  12. t->stopnow();
  13. tasklist.remove(t);
  14. t = tasklist.first();
  15. }
  16. tasklist.clear();
  17.  
  18. }
  19.  
  20. void taskmanager :: addTask(Task *t)
  21. {
  22.  
  23. // Task *t = tasklist.getlast();
  24.  
  25. //add task to the list
  26. tasklist.append( t );
  27.  
  28. }
  29.  
  30. int taskmanager :: getCountTasks()
  31. {
  32. return tasklist.count();
  33. }
To copy to clipboard, switch view to plain text mode