* Nix now has three different formats for the log information it

writes to stderr:
  
  - `pretty': the old nested style (default)
  - `escapes': uses escape codes to indicate nesting and message
    level; can be processed using `log2xml'
  - `flat': just plain text, no nesting

  These can be set using `--log-type TYPE' or the NIX_LOG_TYPE
  environment variable.
This commit is contained in:
Eelco Dolstra 2004-03-22 20:53:49 +00:00
parent 79bb0008ec
commit 777e13b94b
3 changed files with 52 additions and 7 deletions

View file

@ -18,6 +18,15 @@ void sigintHandler(int signo)
} }
void setLogType(string lt)
{
if (lt == "pretty") logType = ltPretty;
else if (lt == "escapes") logType = ltEscapes;
else if (lt == "flat") logType = ltFlat;
else throw UsageError("unknown log type");
}
/* Initialize and reorder arguments, then call the actual argument /* Initialize and reorder arguments, then call the actual argument
processor. */ processor. */
static void initAndRun(int argc, char * * argv) static void initAndRun(int argc, char * * argv)
@ -44,6 +53,10 @@ static void initAndRun(int argc, char * * argv)
if (sigaction(SIGINT, &act, &oact)) if (sigaction(SIGINT, &act, &oact))
throw SysError("installing handler for SIGINT"); throw SysError("installing handler for SIGINT");
/* Process the NIX_LOG_TYPE environment variable. */
char * lt = getenv("NIX_LOG_TYPE");
if (lt) setLogType(lt);
/* Put the arguments in a vector. */ /* Put the arguments in a vector. */
Strings args, remaining; Strings args, remaining;
while (argc--) args.push_back(*argv++); while (argc--) args.push_back(*argv++);
@ -72,7 +85,11 @@ static void initAndRun(int argc, char * * argv)
string arg = *i; string arg = *i;
if (arg == "--verbose" || arg == "-v") if (arg == "--verbose" || arg == "-v")
verbosity = (Verbosity) ((int) verbosity + 1); verbosity = (Verbosity) ((int) verbosity + 1);
else if (arg == "--build-output" || arg == "-B") else if (arg == "--log-type") {
++i;
if (i == args.end()) throw UsageError("`--log-type' requires an argument");
setLogType(*i);
} else if (arg == "--build-output" || arg == "-B")
buildVerbosity = lvlError; /* lowest */ buildVerbosity = lvlError; /* lowest */
else if (arg == "--help") { else if (arg == "--help") {
printHelp(); printHelp();

View file

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <sstream>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -223,6 +224,7 @@ void writeStringToFile(const Path & path, const string & s)
} }
LogType logType = ltPretty;
Verbosity verbosity = lvlError; Verbosity verbosity = lvlError;
static int nestingLevel = 0; static int nestingLevel = 0;
@ -236,13 +238,28 @@ Nest::Nest()
Nest::~Nest() Nest::~Nest()
{ {
if (nest) nestingLevel--; if (nest) {
nestingLevel--;
if (logType == ltEscapes)
cerr << "\033[q";
}
}
static string escVerbosity(Verbosity level)
{
int l = (int) level;
ostringstream st;
st << l;
return st.str();
} }
void Nest::open(Verbosity level, const format & f) void Nest::open(Verbosity level, const format & f)
{ {
if (level <= verbosity) { if (level <= verbosity) {
if (logType == ltEscapes)
cerr << "\033[" << escVerbosity(level) << "p";
printMsg_(level, f); printMsg_(level, f);
nest = true; nest = true;
nestingLevel++; nestingLevel++;
@ -254,10 +271,13 @@ void printMsg_(Verbosity level, const format & f)
{ {
checkInterrupt(); checkInterrupt();
if (level > verbosity) return; if (level > verbosity) return;
string spaces; string prefix;
for (int i = 0; i < nestingLevel; i++) if (logType == ltPretty)
spaces += "| "; for (int i = 0; i < nestingLevel; i++)
cerr << format("%1%%2%\n") % spaces % f.str(); prefix += "| ";
else if (logType == ltEscapes && level != lvlInfo)
prefix = "\033[" + escVerbosity(level) + "s";
cerr << format("%1%%2%\n") % prefix % f.str();
} }

View file

@ -100,6 +100,13 @@ void writeStringToFile(const Path & path, const string & s);
/* Messages. */ /* Messages. */
typedef enum {
ltPretty, /* nice, nested output */
ltEscapes, /* nesting indicated using escape codes (for log2xml) */
ltFlat /* no nesting */
} LogType;
typedef enum { typedef enum {
lvlError, lvlError,
lvlInfo, lvlInfo,
@ -109,7 +116,8 @@ typedef enum {
lvlVomit lvlVomit
} Verbosity; } Verbosity;
extern Verbosity verbosity; /* supress msgs > this */ extern LogType logType;
extern Verbosity verbosity; /* suppress msgs > this */
class Nest class Nest
{ {