[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1643: Add support for -logfile and -loglevel command-line argument (in trunk: . src)



Author: edmanm
Date: 2007-02-20 12:33:18 -0500 (Tue, 20 Feb 2007)
New Revision: 1643

Modified:
   trunk/
   trunk/src/main.cpp
   trunk/src/vidalia.cpp
   trunk/src/vidalia.h
Log:
 r1680@adrastea:  edmanm | 2007-02-20 12:17:51 -0500
 Add support for -logfile and -loglevel command-line arguments. Display usage
 information as a message box so even Windows folks can see it, if they'd like
 or if they give Vidalia a bogus argument. Add some basic logging statements in
 main(); more to come later.



Property changes on: trunk
___________________________________________________________________
 svk:merge ticket from /vidalia/local/trunk [r1680] on 54b3572a-7227-0410-958f-53ecd705b71a

Modified: trunk/src/main.cpp
===================================================================
--- trunk/src/main.cpp	2007-02-20 17:33:13 UTC (rev 1642)
+++ trunk/src/main.cpp	2007-02-20 17:33:18 UTC (rev 1643)
@@ -56,21 +56,29 @@
    * that it recognizes in argv, so we'll pass a stringlist of the original
    * list of command-line arguments too. */
   Vidalia vidalia(args, argc, argv);
+  vNotice("Vidalia %1 using Qt %2").arg(Vidalia::version())
+                                   .arg(QT_VERSION_STR);
 
-
-#if !defined(Q_OS_WIN32)
-  /* Validate any command-line arguments. Don't bother doing this on Win32
-   * since they can't see the output anyway. */
+  /* Validate any command-line arguments, or show usage message box, if
+   * necessary. */
   QString errmsg;
-  if (!vidalia.validateArguments(errmsg)) {
-    vidalia.printUsage(errmsg);
-    return -1;
+  if (vidalia.showUsage()) {
+    Vidalia::showUsageMessageBox();
+    return 0;
+  } else if (!vidalia.validateArguments(errmsg)) {
+    vError("Unable to apply command-line arguments: %1").arg(errmsg);
+    VMessageBox::critical(0,
+      vApp->translate("Vidalia",
+        QT_TRANSLATE_NOOP("Vidalia", "Invalid Argument")), errmsg,
+      VMessageBox::Ok);
+    return 1;
   }
-#endif
 
   /* Check if Vidalia is already running. */
   QString pidfile = vidalia.pidFile();
   if (is_vidalia_running(pidfile)) {
+    vWarn("Detected another process with pid %1. Is Vidalia already running?")
+                                                               .arg(get_pid());
     /* Let the user know another Vidalia is running and we are going to exit
      * now. */
     int ret = VMessageBox::critical(0, 
@@ -85,7 +93,8 @@
                 VMessageBox::Continue, VMessageBox::Quit|VMessageBox::Default);
     if (ret != VMessageBox::Continue) {
       /* Don't start a second instance of Vidalia */
-      return 0;
+      vError("Exiting duplicate Vidalia process.");
+      return 1;
     }
   }
   write_pidfile(pidfile);
@@ -104,6 +113,7 @@
 
   /* Vidalia exited, so cleanup our pidfile and return */
   QFile::remove(pidfile);
+  vNotice("Exiting cleanly (return code %1).").arg(ret);
   return ret;
 }
 

Modified: trunk/src/vidalia.cpp
===================================================================
--- trunk/src/vidalia.cpp	2007-02-20 17:33:13 UTC (rev 1642)
+++ trunk/src/vidalia.cpp	2007-02-20 17:33:18 UTC (rev 1643)
@@ -30,23 +30,30 @@
 #include <QStyleFactory>
 #include <util/string.h>
 #include <lang/languagesupport.h>
+#include <gui/common/vmessagebox.h>
+#include <util/html.h>
+#include <stdlib.h>
 
 #include "vidalia.h"
 
 /* Available command-line arguments. */
-#define ARG_LANGUAGE   "lang"    /**< Argument specifying language.    */
-#define ARG_GUISTYLE   "style"   /**< Argument specfying GUI style.    */
-#define ARG_RESET      "reset"   /**< Reset Vidalia's saved settings.  */
-#define ARG_HELP       "help"    /**< Display usage informatino.       */
-#define ARG_DATADIR    "datadir" /**< Directory to use for data files. */
-#define ARG_PIDFILE    "pidfile" /**< Location and name of our pidfile.*/
+#define ARG_LANGUAGE   "lang"     /**< Argument specifying language.    */
+#define ARG_GUISTYLE   "style"    /**< Argument specfying GUI style.    */
+#define ARG_RESET      "reset"    /**< Reset Vidalia's saved settings.  */
+#define ARG_HELP       "help"     /**< Display usage informatino.       */
+#define ARG_DATADIR    "datadir"  /**< Directory to use for data files. */
+#define ARG_PIDFILE    "pidfile"  /**< Location and name of our pidfile.*/
+#define ARG_LOGFILE    "logfile"  /**< Location of our logfile.         */
+#define ARG_LOGLEVEL   "loglevel" /**< Log verbosity.                   */
 
+
 /* Static member variables */
 QMap<QString, QString> Vidalia::_args; /**< List of command-line arguments.  */
 QString Vidalia::_style;               /**< The current GUI style.           */
 QString Vidalia::_language;            /**< The current language.            */
 HelpBrowser*  Vidalia::_help = 0;      /**< Vidalia's help system.           */
 TorControl* Vidalia::_torControl = 0;  /**< Main TorControl object.          */
+Log Vidalia::_log;
 
 
 /** Constructor. Parses the command-line arguments, resets Vidalia's
@@ -59,19 +66,30 @@
   parseArguments(args);
 
   /* Check if we're supposed to reset our config before proceeding. */
-  if (_args.contains(ARG_RESET)) {
+  if (_args.contains(ARG_RESET))
     VidaliaSettings::reset();
+
+  /* Handle the -loglevel and -logfile options. */
+  if (_args.contains(ARG_LOGFILE))
+    _log.open(_args.value(ARG_LOGFILE));
+  if (_args.contains(ARG_LOGLEVEL)) {
+    _log.setLogLevel(Log::stringToLogLevel(
+                      _args.value(ARG_LOGLEVEL)));
+    if (!_args.contains(ARG_LOGFILE))
+      _log.open(stdout);
   }
+  if (!_args.contains(ARG_LOGLEVEL) && 
+      !_args.contains(ARG_LOGFILE))
+    _log.setLogLevel(Log::Off);
 
-  /** Translate the GUI to the appropriate language. */
+  /* Translate the GUI to the appropriate language. */
   setLanguage(_args.value(ARG_LANGUAGE));
-
-  /** Set the GUI style appropriately. */
+  /* Set the GUI style appropriately. */
   setStyle(_args.value(ARG_GUISTYLE));
 
-  /** Creates a TorControl object, used to talk to Tor. */
+  /* Creates a TorControl object, used to talk to Tor. */
   _torControl = new TorControl();
-  /** Create a help browser object, used to dispaly various help topics. */
+  /* Creates a help browser object, used to display various help topics. */
   _help = new HelpBrowser();
 }
 
@@ -95,31 +113,45 @@
 }
 #endif
 
-/** Display usage information regarding command-line arguments. */
+/** Returns true if the user wants to see usage information. */
+bool
+Vidalia::showUsage()
+{
+  return _args.contains(ARG_HELP);
+}
+
+/** Displays usage information for command-line args. */
 void
-Vidalia::printUsage(QString errmsg)
+Vidalia::showUsageMessageBox()
 {
-  QTextStream out(stdout);
+  QString usage;
+  QTextStream out(&usage);
 
-  /* If there was an error message, print it out. */
-  if (!errmsg.isEmpty()) {
-    out << "** " << errmsg << " **" << endl << endl;
-  }
+  out << "Available Options:" << endl;
+  out << "<table>";
+  out << trow(tcol("-"ARG_HELP) + 
+              tcol(tr("Displays this usage message and exits.")));
+  out << trow(tcol("-"ARG_RESET) +
+              tcol(tr("Resets ALL stored Vidalia settings.")));
+  out << trow(tcol("-"ARG_DATADIR" &lt;dir&gt;") +
+              tcol(tr("Sets the directory Vidalia uses for data files.")));
+  out << trow(tcol("-"ARG_PIDFILE" &lt;file&gt;") +
+              tcol(tr("Sets the name and location of Vidalia's pidfile.")));
+  out << trow(tcol("-"ARG_LOGFILE" &lt;file&gt;") +
+              tcol(tr("Sets the name and location of Vidalia's logfile.")));
+  out << trow(tcol("-"ARG_LOGLEVEL" &lt;level&gt;") +
+              tcol(tr("Sets the verbosity of Vidalia's logging.") +
+                   "<br>[" + Log::logLevels().join("|") +"]"));
+  out << trow(tcol("-"ARG_GUISTYLE" &lt;style&gt;") +
+              tcol(tr("Sets Vidalia's interface style.") +
+                   "<br>[" + QStyleFactory::keys().join("|") + "]"));
+  out << trow(tcol("-"ARG_LANGUAGE" &lt;language&gt;") + 
+              tcol(tr("Sets Vidalia's language.") +
+                   "<br>[" + LanguageSupport::languageCodes().join("|") + "]"));
+  out << "</table>";
 
-  /* Now print the application usage */
-  out << "Usage: " << endl;
-  out << "\t" << qApp->arguments().at(0) << " [options]"    << endl;
-
-  /* And available options */
-  out << endl << "Available Options:"                                   << endl;
-  out << "\t-"ARG_HELP"\t\tDisplays this usage message and exits."      << endl;
-  out << "\t-"ARG_RESET"\t\tResets ALL stored Vidalia settings."        << endl;
-  out << "\t-"ARG_DATADIR"\tSets the directory Vidalia uses for data files"<< endl;
-  out << "\t-"ARG_PIDFILE"\tSets the name and location of Vidalia's pidfile"<< endl;
-  out << "\t-"ARG_GUISTYLE"\t\tSets Vidalia's interface style."         << endl;
-  out << "\t\t\t[" << QStyleFactory::keys().join("|") << "]"            << endl;
-  out << "\t-"ARG_LANGUAGE"\t\tSets Vidalia's language."                << endl;
-  out << "\t\t\t[" << LanguageSupport::languageCodes().join("|") << "]" << endl;
+  VMessageBox::information(0, 
+    tr("Vidalia Usage Information"), usage, VMessageBox::Ok);
 }
 
 /** Returns true if the specified argument expects a value. */
@@ -129,7 +161,9 @@
   return (argName == ARG_GUISTYLE ||
           argName == ARG_LANGUAGE ||
           argName == ARG_DATADIR  ||
-          argName == ARG_PIDFILE);
+          argName == ARG_PIDFILE  ||
+          argName == ARG_LOGFILE  ||
+          argName == ARG_LOGLEVEL);
 }
 
 /** Parses the list of command-line arguments for their argument names and
@@ -162,10 +196,6 @@
 bool
 Vidalia::validateArguments(QString &errmsg)
 {
-  /* If they want help, just return false now */
-  if (_args.contains(ARG_HELP)) {
-    return false;
-  }
   /* Check for a language that Vidalia recognizes. */
   if (_args.contains(ARG_LANGUAGE) &&
       !LanguageSupport::isValidLanguageCode(_args.value(ARG_LANGUAGE))) {
@@ -179,6 +209,19 @@
     errmsg = tr("Invalid GUI style specified: ") + _args.value(ARG_GUISTYLE);
     return false;
   }
+  /* Check for a valid log level */
+  if (_args.contains(ARG_LOGLEVEL) &&
+      !Log::logLevels().contains(_args.value(ARG_LOGLEVEL))) {
+    errmsg = tr("Invalid log level specified: ") + _args.value(ARG_LOGLEVEL);
+    return false;
+  }
+  /* Check for a writable log file */
+  if (_args.contains(ARG_LOGFILE) && !_log.isOpen()) {
+    errmsg = tr("Unable to open log file '%1': %2")
+                           .arg(_args.value(ARG_LOGFILE))
+                           .arg(_log.errorString());
+    return false;
+  }
   return true;
 }
 
@@ -261,3 +304,9 @@
   return QDir::convertSeparators(dataDirectory() + "/vidalia.pid");
 }
 
+Log::LogMessage
+Vidalia::log(Log::LogLevel level, QString msg)
+{
+  return _log.log(level, msg);
+}
+

Modified: trunk/src/vidalia.h
===================================================================
--- trunk/src/vidalia.h	2007-02-20 17:33:13 UTC (rev 1642)
+++ trunk/src/vidalia.h	2007-02-20 17:33:18 UTC (rev 1643)
@@ -37,6 +37,7 @@
 #include <QMap>
 #include <QString>
 
+#include <util/log.h>
 #include <gui/help/browser/helpbrowser.h>
 #include <config/vidaliasettings.h>
 #include <control/torcontrol.h>
@@ -47,7 +48,13 @@
 /** Pointer to this Vidalia application instance. */
 #define vApp  ((Vidalia *)qApp)
 
+#define vDebug(fmt)   (vApp->log(Log::Debug, (fmt)))
+#define vInfo(fmt)    (vApp->log(Log::Info, (fmt)))
+#define vNotice(fmt)  (vApp->log(Log::Notice, (fmt)))
+#define vWarn(fmt)    (vApp->log(Log::Warn, (fmt)))
+#define vError(fmt)   (vApp->log(Log::Error, (fmt)))
 
+
 class Vidalia : public QApplication
 {
   Q_OBJECT
@@ -61,10 +68,12 @@
   /** Return the map of command-line arguments and values. */
   static QMap<QString, QString> arguments() { return _args; }
   /** Validates that all arguments were well-formed. */
-  bool validateArguments(QString &errmsg);
-  /** Prints usage information to the given text stream. */
-  void printUsage(QString errmsg = QString());
-
+  static bool validateArguments(QString &errmsg);
+  /** Displays usage information for command-line args. */
+  static void showUsageMessageBox();
+  /** Returns true if the user wants to see usage information. */
+  static bool showUsage();
+  
   /** Sets the current language. */
   static bool setLanguage(QString languageCode = QString());
   /** Sets the current GUI style. */
@@ -87,7 +96,10 @@
   
   /** Returns the location of Vidalia's pid file. */
   static QString pidFile();
- 
+
+  /** Writes <b>msg</b> with severity <b>level</b> to Vidalia's log. */
+  static Log::LogMessage log(Log::LogLevel level, QString msg);
+
 public slots:
   /** Shows the specified help topic, or the default if empty. */
   static void help(QString topic = QString());
@@ -114,6 +126,8 @@
 
   static TorControl* _torControl;      /**< Vidalia's main TorControl object.*/
   static HelpBrowser* _help;           /**< Vidalia's configurable settings. */
+  
+  static Log _log; /**< Logs debugging messages to file or stdout. */
 };
 
 #endif