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 1563 2006-12-26 06:06:04Z 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   QDir dir = QFileInfo(filename).absoluteDir();
00053   if (!dir.exists()) {
00054     return dir.mkpath(dir.absolutePath());
00055   }
00056   return true;
00057 }
00058 
00059 /** Opens a log file for writing. */
00060 bool
00061 LogFile::open(QString filename, QString *errmsg)
00062 {
00063   QFile *newLogFile;
00064  
00065   /* If the file is already open, then no need to open it again */
00066   if (_file && _file->isOpen()) {
00067     if (_file->fileName() == filename) {
00068       return true;
00069     }
00070   }
00071 
00072   /* Create the path to the log file, if necessary */
00073   if (!createPathToFile(filename)) {
00074     return err(errmsg, "Unable to create path to log file.");
00075   }
00076  
00077   /* Try to open the new log file */
00078   newLogFile = new QFile(filename);
00079   if (!newLogFile->open(QFile::WriteOnly|QIODevice::Append|QIODevice::Text)) {
00080     delete newLogFile;
00081     return err(errmsg, newLogFile->errorString());
00082   }
00083  
00084   /* Rotate the new log file in place of the old one */
00085   if (_file) {
00086     delete _file;
00087   }
00088   _file = newLogFile;
00089   _stream.setDevice(_file);
00090   return true;
00091 }
00092 
00093 /** Closes an open log file. */
00094 void
00095 LogFile::close()
00096 {
00097   if (_file) {
00098     delete _file;
00099     _file = 0;
00100   }
00101 }
00102 
00103 /** Returns true if the logfile is currently open. */
00104 bool
00105 LogFile::isOpen()
00106 {
00107   return (_file && _file->isOpen());
00108 }
00109 
00110 /** Returns the filename of the current log file. */
00111 QString
00112 LogFile::filename()
00113 {
00114   return (_file ? _file->fileName() : QString());;
00115 }
00116 
00117 /** Overloaded ostream operator. */
00118 LogFile&
00119 LogFile::operator<<(const QString &s)
00120 {
00121   if (_file) {
00122     _stream << s;
00123     _stream.flush();
00124   }
00125   return *this;
00126 }
00127 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3