From 1a7468a57a11288a007c40d50ed28718d757a546 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 24 Jul 2003 08:53:43 +0000 Subject: [PATCH] * Debug levels. Use `--verbose / -v LEVEL' to display only messages up to the given verbosity levels. These currently are: lvlError = 0, lvlNormal = 5, lvlDebug = 10, lvlDebugMore = 15 although only lvlError and lvlDebug are actually used right now. --- src/fix.cc | 10 ++++++++++ src/nix.cc | 16 +++++++++++++--- src/normalise.cc | 6 ++---- src/shared.cc | 23 ++++++++++++++++------- src/store.cc | 2 +- src/test.cc | 3 +-- src/util.cc | 18 +++++++++++++----- src/util.hh | 15 ++++++++++++--- 8 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/fix.cc b/src/fix.cc index afa0167ec..8463c0ddb 100644 --- a/src/fix.cc +++ b/src/fix.cc @@ -310,6 +310,16 @@ void run(Strings args) throw UsageError(format("argument required in `%1%'") % arg); state.searchDirs.push_back(*it++); } + else if (arg == "--verbose" || arg == "-v") { + if (it == args.end()) throw UsageError( + format("`%1%' requires an argument") % arg); + istringstream str(*it++); + int lvl; + str >> lvl; + if (str.fail()) throw UsageError( + format("`%1%' requires an integer argument") % arg); + verbosity = (Verbosity) lvl; + } else if (arg[0] == '-') throw UsageError(format("unknown flag `%1%`") % arg); else diff --git a/src/nix.cc b/src/nix.cc index e08854227..f672c42a8 100644 --- a/src/nix.cc +++ b/src/nix.cc @@ -1,4 +1,5 @@ #include +#include #include "globals.hh" #include "normalise.hh" @@ -278,10 +279,9 @@ void run(Strings args) Strings opFlags, opArgs; Operation op = 0; - for (Strings::iterator it = args.begin(); - it != args.end(); it++) + for (Strings::iterator it = args.begin(); it != args.end(); ) { - string arg = *it; + string arg = *it++; Operation oldOp = op; @@ -307,6 +307,16 @@ void run(Strings args) op = opVerify; else if (arg == "--path" || arg == "-p") pathArgs = true; + else if (arg == "--verbose" || arg == "-v") { + if (it == args.end()) throw UsageError( + format("`%1%' requires an argument") % arg); + istringstream str(*it++); + int lvl; + str >> lvl; + if (str.fail()) throw UsageError( + format("`%1%' requires an integer argument") % arg); + verbosity = (Verbosity) lvl; + } else if (arg[0] == '-') opFlags.push_back(arg); else diff --git a/src/normalise.cc b/src/normalise.cc index f463457e4..6ce73d1ac 100644 --- a/src/normalise.cc +++ b/src/normalise.cc @@ -26,8 +26,7 @@ typedef set FSIdSet; Slice normaliseFState(FSId id, FSIdSet pending) { - debug(format("normalising fstate %1%") % (string) id); - Nest nest(true); + Nest nest(lvlDebug, format("normalising fstate %1%") % (string) id); /* Try to substitute $id$ by any known successors in order to speed up the rewrite process. */ @@ -177,8 +176,7 @@ Slice normaliseFState(FSId id, FSIdSet pending) void realiseSlice(const Slice & slice, FSIdSet pending) { - debug(format("realising slice")); - Nest nest(true); + Nest nest(lvlDebug, format("realising slice")); /* Perhaps all paths already contain the right id? */ diff --git a/src/shared.cc b/src/shared.cc index bfd7498de..75145f6db 100644 --- a/src/shared.cc +++ b/src/shared.cc @@ -1,4 +1,5 @@ #include +#include extern "C" { #include @@ -32,7 +33,12 @@ static void initAndRun(int argc, char * * argv) string arg = *it; if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') { for (unsigned int i = 1; i < arg.length(); i++) - args.insert(it, (string) "-" + arg[i]); + if (isalpha(arg[i])) + args.insert(it, (string) "-" + arg[i]); + else { + args.insert(it, string(arg, i)); + break; + } it = args.erase(it); } else it++; } @@ -50,18 +56,21 @@ int main(int argc, char * * argv) try { initAndRun(argc, argv); } catch (UsageError & e) { - cerr << format( - "error: %1%\n" - "Try `%2% --help' for more information.\n") - % e.what() % programId; + msg(lvlError, + format( + "error: %1%\n" + "Try `%2% --help' for more information.") + % e.what() % programId); return 1; } catch (Error & e) { - cerr << format("error: %1%\n") % e.msg(); + msg(lvlError, format("error: %1%") % e.msg()); return 1; } catch (exception & e) { - cerr << format("error: %1%\n") % e.what(); + msg(lvlError, format("error: %1%") % e.what()); return 1; } return 0; } + + diff --git a/src/store.cc b/src/store.cc index 013bd2e2a..50932d806 100644 --- a/src/store.cc +++ b/src/store.cc @@ -168,7 +168,7 @@ string expandId(const FSId & id, const string & target, const string & prefix, FSIdSet pending) { debug(format("expanding %1%") % (string) id); - Nest nest(true); + Nest nest(lvlDebug, format("expanding %1%") % (string) id); Strings paths; diff --git a/src/test.cc b/src/test.cc index 6b567abe0..a2431273e 100644 --- a/src/test.cc +++ b/src/test.cc @@ -12,8 +12,7 @@ void realise(FSId id) { - debug(format("TEST: realising %1%") % (string) id); - Nest nest(true); + Nest nest(lvlDebug, format("TEST: realising %1%") % (string) id); Slice slice = normaliseFState(id); realiseSlice(slice); } diff --git a/src/util.cc b/src/util.cc index a16643022..8510bb7a6 100644 --- a/src/util.cc +++ b/src/util.cc @@ -130,13 +130,20 @@ void deletePath(string path) } +Verbosity verbosity = lvlNormal; + static int nestingLevel = 0; -Nest::Nest(bool nest) +Nest::Nest(Verbosity level, const format & f) { - this->nest = nest; - if (nest) nestingLevel++; + if (level > verbosity) + nest = false; + else { + msg(level, f); + nest = true; + nestingLevel++; + } } @@ -146,8 +153,9 @@ Nest::~Nest() } -void msg(const format & f) +void msg(Verbosity level, const format & f) { + if (level > verbosity) return; string spaces; for (int i = 0; i < nestingLevel; i++) spaces += "| "; @@ -157,7 +165,7 @@ void msg(const format & f) void debug(const format & f) { - msg(format("debug: %1%") % f.str()); + msg(lvlDebug, format("debug: %1%") % f.str()); } diff --git a/src/util.hh b/src/util.hh index 8b23bee00..6d87898b5 100644 --- a/src/util.hh +++ b/src/util.hh @@ -72,17 +72,26 @@ void deletePath(string path); /* Messages. */ +typedef enum { + lvlError = 0, + lvlNormal = 5, + lvlDebug = 10, + lvlDebugMore = 15 +} Verbosity; + +extern Verbosity verbosity; /* supress msgs > this */ + class Nest { private: bool nest; public: - Nest(bool nest); + Nest(Verbosity level, const format & f); ~Nest(); }; -void msg(const format & f); -void debug(const format & f); +void msg(Verbosity level, const format & f); +void debug(const format & f); /* shorthand */ /* Wrappers arount read()/write() that read/write exactly the