00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030 #include <qdatetime.h>
00031 
00032 #include "logger.h"
00033 
00034 Logger::Logger(QObject *parent, const char *name)
00035         : QObject(parent, name),
00036         m_file(Config().Log.Path),
00037         m_log()
00038 {
00039         openFile();
00040 }
00041 
00042 
00043 Logger::~Logger()
00044 {
00045         m_log.unsetDevice();
00046         
00047         m_file.close();
00048 }
00049 
00050 void Logger::print(const QString& text, int type)
00051 {
00052         if(type == Configuration::Normal)
00053         {
00054                 if(Config().Log.UseLog)
00055                 {
00056                         m_log << KGlobal::locale()->formatDateTime(QDateTime::currentDateTime()) << ": " << text << endl << flush;
00057                 }
00058         }
00059         else if(type == Configuration::Debug)
00060         {
00061                 kdDebug() << KGlobal::locale()->formatDateTime(QDateTime::currentDateTime()) << ": " << text << endl << flush;  
00062         }
00063 }
00064 
00065 void Logger::printDebug(const QString& text)
00066 {
00067         print(text, Configuration::Debug);
00068 }
00069 
00070 void Logger::reload()
00071 {
00072         m_log.unsetDevice();
00073         m_file.close();
00074         openFile();
00075 }
00076 
00077 void Logger::openFile()
00078 {
00079         if(!m_file.open(IO_WriteOnly ))
00080         {
00081                 print(i18n("Can't open file '%1' for writing.").arg(Config().Log.Path), Configuration::Debug);
00082                 Config().Log.UseLog = false;
00083         }
00084         else
00085         {
00086                 m_log.setDevice(&m_file);
00087                 print("Opened log file.");
00088         }
00089 }       
00090 
00091 #include "logger.moc"