Guest User

Untitled

a guest
Dec 11th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. // main.cpp
  2.  
  3. void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
  4. {
  5. QStringList data;
  6. data.append(context.category);
  7. data.append(context.file);
  8. data.append(context.function);
  9. data.append(QString(context.line));
  10. data.append(QString(context.version));
  11. Logger::get_obj()->myMessageOutput(type, data, msg);
  12. }
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. QApplication a(argc, argv);
  17. a.setApplicationName("MyApp");
  18. a.setWindowIcon(QIcon(":/Icons/MyApp.png"));
  19.  
  20. Logger log;
  21. qInstallMessageHandler(myMessageOutput);
  22. log.turnOnDebug();
  23.  
  24. MySplashScreen splash;
  25. splash.show();
  26. a.processEvents();
  27.  
  28. ...
  29.  
  30. splash.close();
  31. MainWindow *w = new MainWindow();
  32.  
  33. int errorCode = a.exec();
  34. delete w;
  35. session.destruct();
  36. qDebug() << "n*** Application exited with error code " << errorCode << " ***";
  37. return errorCode;
  38. }
  39.  
  40. // logger.h
  41.  
  42. #ifndef LOGGER_H
  43. #define LOGGER_H
  44.  
  45. #include <QObject>
  46. #include <QFile>
  47. #include <QTextStream>
  48. #include <QMutex>
  49.  
  50. class Logger : public QObject
  51. {
  52. Q_OBJECT
  53. public:
  54. void myMessageOutput(QtMsgType type, QStringList context, const QString msg);
  55. explicit Logger(QObject *parent = 0);
  56. ~Logger();
  57. static Logger *get_obj() {return ptr;}
  58. static void turnOnDebug() {get_obj()->debugMode = true;}
  59. static void turnOffDebug() {get_obj()->debugMode = false;}
  60. static void moveLogToSession(QString sessionName);
  61.  
  62. private:
  63. QFile *logFile;
  64. QTextStream *stream;
  65. QMutex *mutex;
  66. bool debugMode;
  67. bool errorMsg;
  68. static Logger *ptr;
  69.  
  70. void write(QString str);
  71.  
  72. signals:
  73. void SaveSession();
  74. void FatalSaveSession();
  75.  
  76. public slots:
  77.  
  78. };
  79.  
  80. #endif // LOGGER_H
  81.  
  82. // logger.cpp
  83.  
  84. #include <QDir>
  85. #include <QFile>
  86. #include <QMessageBox>
  87. #include <QApplication>
  88. #include "logger.h"
  89. #include "systemvariables.h"
  90.  
  91. Logger *Logger::ptr = NULL;
  92.  
  93. Logger::Logger(QObject *parent) : QObject(parent)
  94. {
  95. logFile = NULL;
  96. stream = NULL;
  97. mutex = NULL;
  98. debugMode = false;
  99. errorMsg = false;
  100.  
  101. ptr = this;
  102. QString path = SystemVariables::getAppData();
  103. QDir dir = QDir(path);
  104. if(dir.exists())
  105. {
  106. path.append("/MyApp/Logs");
  107. dir = QDir(path);
  108. if(!dir.exists())
  109. dir.mkpath(path);
  110.  
  111. logFile = new QFile(path + "/Session.log");
  112. if(logFile->exists())
  113. logFile->remove();
  114.  
  115. if(!logFile->open(QFile::WriteOnly | QFile::Text))
  116. {
  117. qFatal("Could not create log file.");
  118. }
  119.  
  120. stream = new QTextStream(logFile);
  121. stream->setRealNumberNotation(QTextStream::SmartNotation);
  122. stream->setRealNumberPrecision(15);
  123. *stream << "*** MyApp Session Begins ***n";
  124. }
  125. else
  126. qFatal("Could not create log file.");
  127.  
  128. mutex = new QMutex();
  129. }
  130.  
  131. void Logger::myMessageOutput(QtMsgType type, QStringList context, const QString msg)
  132. {
  133. mutex->lock();
  134. QString str = "";
  135. switch (type)
  136. {
  137. case QtDebugMsg:
  138. if(!debugMode)
  139. {
  140. mutex->unlock();
  141. return;
  142. }
  143. write(msg);
  144. break;
  145.  
  146. case QtInfoMsg:
  147. str.append("n*** Information ***n");
  148. str.append(msg + "n");
  149. str.append("Category: " + context.at(0) + "n");
  150. str.append("File: " + context.at(1) + "n");
  151. str.append("Function: " + context.at(2) + "n");
  152. str.append("Line: " + context.at(3) + "n");
  153. str.append("Version: " + context.at(4));
  154. str.append("n*** Information Complete ***n");
  155. write(str);
  156. break;
  157.  
  158. case QtWarningMsg:
  159. if(!(context.at(2).contains("setGeometry")))
  160. {
  161. str.append("n*** Warning ***n");
  162. ...
  163. str.append("n*** Warning Complete ***n");
  164. write(str);
  165. errorMsg = true;
  166. emit Logger::ptr->SaveSession();
  167. }
  168. break;
  169.  
  170. case QtCriticalMsg:
  171. str.append("n*** Critical ***n");
  172. ...
  173. str.append("n*** Critical Complete ***n");
  174. write(str);
  175. errorMsg = true;
  176. emit Logger::ptr->SaveSession();
  177. break;
  178.  
  179. case QtFatalMsg:
  180. str.append("n*** Fatal ***n");
  181. ...
  182. str.append("n*** Fatal Complete ***n");
  183. write(str);
  184. errorMsg = false;
  185. emit Logger::ptr->FatalSaveSession();
  186. QApplication::exit(-2);
  187. }
  188. Logger::mutex->unlock();
  189. }
  190.  
  191. void Logger::write(QString str)
  192. {
  193. if(!stream)
  194. return;
  195.  
  196. if(str.isEmpty())
  197. return;
  198.  
  199. (*stream) << str; //!!!!!!!!!!!!CRASHES HERE***********
  200. stream->flush();
  201. }
  202.  
  203. void Logger::moveLogToSession(QString sessionName)
  204. {
  205. QMutex *myMutex = get_obj()->mutex;
  206. myMutex->lock();
  207.  
  208. QTextStream *myStream = get_obj()->stream;
  209. myStream->flush();
  210. QFile *myLogFile = get_obj()->logFile;
  211. myLogFile->flush();
  212. myLogFile->close();
  213. delete myStream;
  214.  
  215. QString path = SystemVariables::getAppData() + "/Smovault/Logs";
  216. if(!myLogFile->open(QFile::ReadOnly | QFile::Text))
  217. {
  218. QString errtitle = "FATAL ERROR!";
  219. QString errtxt = myLogFile->errorString() + "nCould not open the log file: ";
  220. errtxt.append(path + "/Session.log");
  221. errtxt.append(".nApplication Abort!");
  222. QMessageBox *mb = new QMessageBox(QMessageBox::Critical, errtitle, errtxt);
  223. mb->exec();
  224. delete mb;
  225. QApplication::exit(-1);
  226. }
  227. QTextStream in(myLogFile);
  228. QString data = in.readAll();
  229. myLogFile->close();
  230. myLogFile->remove();
  231. delete myLogFile;
  232.  
  233. path = SystemVariables::getAppData() + "/Smovault/Sessions/" + sessionName;
  234. myLogFile = new QFile(path + "/Session.log");
  235. if(!myLogFile->open(QFile::WriteOnly | QFile::Text))
  236. {
  237. QString errtitle = "FATAL ERROR!";
  238. QString errtxt = myLogFile->errorString() + "nCould not write to the log file: ";
  239. errtxt.append(path + "/Session.log");
  240. errtxt.append(".nApplication Abort!");
  241. QMessageBox *mb = new QMessageBox(QMessageBox::Critical, errtitle, errtxt);
  242. mb->exec();
  243. delete mb;
  244. QApplication::exit(-1);
  245. }
  246. QTextStream out(myLogFile);
  247. out << data;
  248. out.flush();
  249. myLogFile->flush();
  250.  
  251. myStream = new QTextStream(myLogFile);
  252. myStream->setRealNumberNotation(QTextStream::SmartNotation);
  253. myStream->setRealNumberPrecision(15);
  254.  
  255. myMutex->unlock();
  256. }
  257.  
  258. QTextStream *myStream = get_obj()->stream;
  259. myStream->flush();
  260. ...
  261. delete myStream; // here
  262.  
  263. myStream = new QTextStream(myLogFile);
  264.  
  265. get_obj()->stream = myStream; // you need THIS
Add Comment
Please, Sign In to add comment