Vidalia 0.2.10

LogFile.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file LogFile.cpp
00013 ** \version $Id: LogFile.cpp 3735 2009-04-28 20:28:01Z edmanm $
00014 ** \brief Logs messages from Tor to a file
00015 */
00016 
00017 #include "LogFile.h"
00018 
00019 #include "stringutil.h"
00020 
00021 #include <QDir>
00022 
00023 
00024 /** Default constructor. */
00025 LogFile::LogFile()
00026 {
00027   _file = 0;
00028 }
00029 
00030 /** Destructor. */
00031 LogFile::~LogFile()
00032 {
00033   if (_file) {
00034     delete _file;
00035   }
00036 }
00037 
00038 /** Creates a path to the given log file. */
00039 bool
00040 LogFile::createPathToFile(QString filename)
00041 {
00042   QDir dir = QFileInfo(filename).absoluteDir();
00043   if (!dir.exists()) {
00044     return dir.mkpath(dir.absolutePath());
00045   }
00046   return true;
00047 }
00048 
00049 /** Opens a log file for writing. */
00050 bool
00051 LogFile::open(QString filename, QString *errmsg)
00052 {
00053   QFile *newLogFile;
00054  
00055   /* If the file is already open, then no need to open it again */
00056   if (_file && _file->isOpen()) {
00057     if (_file->fileName() == filename) {
00058       return true;
00059     }
00060   }
00061 
00062   /* Create the path to the log file, if necessary */
00063   if (!createPathToFile(filename)) {
00064     return err(errmsg, "Unable to create path to log file.");
00065   }
00066  
00067   /* Try to open the new log file */
00068   newLogFile = new QFile(filename);
00069   if (!newLogFile->open(QFile::WriteOnly|QIODevice::Append|QIODevice::Text)) {
00070     delete newLogFile;
00071     return err(errmsg, newLogFile->errorString());
00072   }
00073  
00074   /* Rotate the new log file in place of the old one */
00075   if (_file) {
00076     delete _file;
00077   }
00078   _file = newLogFile;
00079   _stream.setDevice(_file);
00080   return true;
00081 }
00082 
00083 /** Closes an open log file. */
00084 void
00085 LogFile::close()
00086 {
00087   if (_file) {
00088     delete _file;
00089     _file = 0;
00090   }
00091 }
00092 
00093 /** Returns true if the logfile is currently open. */
00094 bool
00095 LogFile::isOpen()
00096 {
00097   return (_file && _file->isOpen());
00098 }
00099 
00100 /** Returns the filename of the current log file. */
00101 QString
00102 LogFile::filename()
00103 {
00104   return (_file ? _file->fileName() : QString());;
00105 }
00106 
00107 /** Overloaded ostream operator. */
00108 LogFile&
00109 LogFile::operator<<(const QString &s)
00110 {
00111   if (_file) {
00112     _stream << s;
00113     _stream.flush();
00114   }
00115   return *this;
00116 }
00117