00001 /**************************************************************************** 00002 00003 This file is part of the GLC-lib library. 00004 Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net) 00005 http://glc-lib.sourceforge.net 00006 00007 GLC-lib is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 GLC-lib is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with GLC-lib; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 *****************************************************************************/ 00022 00023 #include "glc_tracelog.h" 00024 00025 #include <QDir> 00026 #include "glc_global.h" 00027 #include <QMutexLocker> 00028 00029 GLC_TraceLog* GLC_TraceLog::m_pTraceLog= NULL; 00030 QMutex GLC_TraceLog::m_Mutex; 00031 bool GLC_TraceLog::m_IsEnable= false; 00032 00033 GLC_TraceLog::GLC_TraceLog(const QString& fullLogFileName) 00034 : GLC_Log(fullLogFileName) 00035 { 00036 00037 } 00038 00039 GLC_TraceLog::~GLC_TraceLog() 00040 { 00041 00042 } 00043 00044 GLC_TraceLog* GLC_TraceLog::instance(QString baseName) 00045 { 00046 if (NULL == m_pTraceLog) 00047 { 00048 if (baseName.isEmpty()) 00049 { 00050 QString fileName(QApplication::applicationName()); 00051 if (fileName.isEmpty()) 00052 { 00053 baseName= "GLC_lib_TraceLog"; 00054 } 00055 else 00056 { 00057 baseName= fileName + "_TraceLog"; 00058 } 00059 } 00060 QString logFileName(QDir::tempPath() + QDir::separator() + baseName); 00061 m_pTraceLog= new GLC_TraceLog(logFileName); 00062 m_pTraceLog->writeHeader(); 00063 } 00064 return m_pTraceLog; 00065 } 00066 00067 bool GLC_TraceLog::isEmpty() 00068 { 00069 return (NULL == m_pTraceLog); 00070 } 00071 00072 bool GLC_TraceLog::isEnable() 00073 { 00074 return m_IsEnable; 00075 } 00076 00077 void GLC_TraceLog::addTrace(const QStringList& traceDescription) 00078 { 00079 if (m_IsEnable) 00080 { 00081 QMutexLocker locker(&m_Mutex); 00082 GLC_TraceLog::instance()->addSeparator(); 00083 GLC_TraceLog::instance()->addCurrentTime(); 00084 const int size= traceDescription.size(); 00085 for (int i= 0; i < size; ++i) 00086 { 00087 GLC_TraceLog::instance()->add(traceDescription.at(i)); 00088 } 00089 } 00090 } 00091 void GLC_TraceLog::close() 00092 { 00093 QMutexLocker locker(&m_Mutex); 00094 delete m_pTraceLog; 00095 m_pTraceLog= NULL; 00096 } 00097 00098 void GLC_TraceLog::setEnabled(bool enable) 00099 { 00100 m_IsEnable= enable; 00101 } 00102 00103 void GLC_TraceLog::writeHeader() 00104 { 00105 QString currentLine; 00106 currentLine= "Trace Log file"; 00107 GLC_Log::m_TextStream << currentLine << '\n'; 00108 currentLine= "Application " + QCoreApplication::applicationName(); 00109 GLC_Log::m_TextStream << currentLine << '\n'; 00110 currentLine= QDate::currentDate().toString(Qt::ISODate); 00111 GLC_Log::m_TextStream << currentLine << '\n'; 00112 GLC_Log::m_TextStream.flush(); 00113 }