logfile.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file logfile.cpp
00024  * \version $Id: logfile.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Logs messages from Tor to a file
00026  */
00027 
00028 #include <QDir>
00029 #include <util/string.h>
00030 
00031 #include "logfile.h"
00032 
00033 
00034 /** Default constructor. */
00035 LogFile::LogFile()
00036 {
00037   _file = 0;
00038 }
00039 
00040 /** Destructor. */
00041 LogFile::~LogFile()
00042 {
00043   if (_file) {
00044     delete _file;
00045   }
00046 }
00047 
00048 /** Creates a path to the given log file. */
00049 bool
00050 LogFile::createPathToFile(QString filename)
00051 {
00052   QString path = filename.left(filename.lastIndexOf(QDir::separator()));
00053   QDir dir(path);
00054   if (!dir.exists()) {
00055     return dir.mkpath(path);  
00056   }
00057   return true;
00058 }
00059 
00060 /** Opens a log file for writing. */
00061 bool
00062 LogFile::open(QString filename, QString *errmsg)
00063 {
00064   QFile *newLogFile;
00065  
00066   /* If the file is already open, then no need to open it again */
00067   if (_file && _file->isOpen()) {
00068     if (_file->fileName() == filename) {
00069       return true;
00070     }
00071   }
00072 
00073   /* Create the path to the log file, if necessary */
00074   if (!createPathToFile(filename)) {
00075     return err(errmsg, "Unable to create path to log file.");
00076   }
00077  
00078   /* Try to open the new log file */
00079   newLogFile = new QFile(filename);
00080   if (!newLogFile->open(QFile::WriteOnly|QIODevice::Append|QIODevice::Text)) {
00081     delete newLogFile;
00082     return err(errmsg, newLogFile->errorString());
00083   }
00084  
00085   /* Rotate the new log file in place of the old one */
00086   if (_file) {
00087     delete _file;
00088   }
00089   _file = newLogFile;
00090   _stream.setDevice(_file);
00091   return true;
00092 }
00093 
00094 /** Closes an open log file. */
00095 void
00096 LogFile::close()
00097 {
00098   if (_file) {
00099     delete _file;
00100     _file = 0;
00101   }
00102 }
00103 
00104 /** Returns true if the logfile is currently open. */
00105 bool
00106 LogFile::isOpen()
00107 {
00108   return (_file && _file->isOpen());
00109 }
00110 
00111 /** Returns the filename of the current log file. */
00112 QString
00113 LogFile::filename()
00114 {
00115   return (_file ? _file->fileName() : QString());;
00116 }
00117 
00118 /** Overloaded ostream operator. */
00119 LogFile&
00120 LogFile::operator<<(const QString &s)
00121 {
00122   if (_file) {
00123     _stream << s;
00124     _stream.flush();
00125   }
00126   return *this;
00127 }
00128 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0