diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fix.cc | 10 | ||||
-rw-r--r-- | src/nix.cc | 16 | ||||
-rw-r--r-- | src/normalise.cc | 6 | ||||
-rw-r--r-- | src/shared.cc | 23 | ||||
-rw-r--r-- | src/store.cc | 2 | ||||
-rw-r--r-- | src/test.cc | 3 | ||||
-rw-r--r-- | src/util.cc | 18 | ||||
-rw-r--r-- | src/util.hh | 15 |
8 files changed, 68 insertions, 25 deletions
diff --git a/src/fix.cc b/src/fix.cc index afa0167ecdc0..8463c0ddb15d 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 e0885422762f..f672c42a809d 100644 --- a/src/nix.cc +++ b/src/nix.cc @@ -1,4 +1,5 @@ #include <iostream> +#include <sstream> #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 f463457e4ab5..6ce73d1acdad 100644 --- a/src/normalise.cc +++ b/src/normalise.cc @@ -26,8 +26,7 @@ typedef set<FSId> 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 bfd7498de16d..75145f6db2fa 100644 --- a/src/shared.cc +++ b/src/shared.cc @@ -1,4 +1,5 @@ #include <iostream> +#include <cctype> extern "C" { #include <aterm2.h> @@ -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 013bd2e2a7bc..50932d806292 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 6b567abe030f..a2431273e71a 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 a16643022a9d..8510bb7a6355 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 8b23bee00b76..6d87898b5db2 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 |