forked from lix-project/lix
Merge remote-tracking branch 'origin/master' into flakes
This commit is contained in:
commit
ef4d3fc111
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -75,6 +75,8 @@ perl/Makefile.config
|
|||
|
||||
/src/nix-copy-closure/nix-copy-closure
|
||||
|
||||
/src/error-demo/error-demo
|
||||
|
||||
/src/build-remote/build-remote
|
||||
|
||||
# /tests/
|
||||
|
|
66
src/error-demo/error-demo.cc
Normal file
66
src/error-demo/error-demo.cc
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include "error.hh"
|
||||
#include "nixexpr.hh"
|
||||
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace nix;
|
||||
|
||||
// In each program where errors occur, this has to be set.
|
||||
ErrorInfo::programName = std::optional("error-demo");
|
||||
|
||||
// Error in a program; no hint and no nix code.
|
||||
printErrorInfo(
|
||||
ErrorInfo { .level = elError,
|
||||
.name = "name",
|
||||
.description = "error description",
|
||||
});
|
||||
|
||||
// Warning with name, description, and hint.
|
||||
// The hintfmt function makes all the substituted text yellow.
|
||||
printErrorInfo(
|
||||
ErrorInfo { .level = elWarning,
|
||||
.name = "name",
|
||||
.description = "error description",
|
||||
.hint = std::optional(
|
||||
hintfmt("there was a %1%", "warning")),
|
||||
});
|
||||
|
||||
|
||||
// Warning with nix file, line number, column, and the lines of
|
||||
// code where a warning occurred.
|
||||
SymbolTable testTable;
|
||||
auto problem_file = testTable.create("myfile.nix");
|
||||
|
||||
printErrorInfo(
|
||||
ErrorInfo{
|
||||
.level = elWarning,
|
||||
.name = "warning name",
|
||||
.description = "warning description",
|
||||
.hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
|
||||
.nixCode = NixCode {
|
||||
.errPos = Pos(problem_file, 40, 13),
|
||||
.prevLineOfCode = std::nullopt,
|
||||
.errLineOfCode = "this is the problem line of code",
|
||||
.nextLineOfCode = std::nullopt
|
||||
}});
|
||||
|
||||
// Error with previous and next lines of code.
|
||||
printErrorInfo(
|
||||
ErrorInfo{
|
||||
.level = elError,
|
||||
.name = "error name",
|
||||
.description = "error description",
|
||||
.hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
|
||||
.nixCode = NixCode {
|
||||
.errPos = Pos(problem_file, 40, 13),
|
||||
.prevLineOfCode = std::optional("previous line of code"),
|
||||
.errLineOfCode = "this is the problem line of code",
|
||||
.nextLineOfCode = std::optional("next line of code"),
|
||||
}});
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
12
src/error-demo/local.mk
Normal file
12
src/error-demo/local.mk
Normal file
|
@ -0,0 +1,12 @@
|
|||
programs += error-demo
|
||||
|
||||
error-demo_DIR := $(d)
|
||||
|
||||
error-demo_SOURCES := \
|
||||
$(wildcard $(d)/*.cc) \
|
||||
|
||||
error-demo_CXXFLAGS += -I src/libutil -I src/libexpr
|
||||
|
||||
error-demo_LIBS = libutil libexpr
|
||||
|
||||
error-demo_LDFLAGS = -pthread $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) -lboost_context -lboost_thread -lboost_system
|
|
@ -57,7 +57,7 @@ inline void EvalState::forceAttrs(Value & v)
|
|||
|
||||
inline void EvalState::forceAttrs(Value & v, const Pos & pos)
|
||||
{
|
||||
forceValue(v);
|
||||
forceValue(v, pos);
|
||||
if (v.type != tAttrs)
|
||||
throwTypeError("value is %1% while a set was expected, at %2%", v, pos);
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ inline void EvalState::forceList(Value & v)
|
|||
|
||||
inline void EvalState::forceList(Value & v, const Pos & pos)
|
||||
{
|
||||
forceValue(v);
|
||||
forceValue(v, pos);
|
||||
if (!v.isList())
|
||||
throwTypeError("value is %1% while a list was expected, at %2%", v, pos);
|
||||
}
|
||||
|
|
|
@ -1551,7 +1551,7 @@ NixFloat EvalState::forceFloat(Value & v, const Pos & pos)
|
|||
|
||||
bool EvalState::forceBool(Value & v, const Pos & pos)
|
||||
{
|
||||
forceValue(v);
|
||||
forceValue(v, pos);
|
||||
if (v.type != tBool)
|
||||
throwTypeError("value is %1% while a Boolean was expected, at %2%", v, pos);
|
||||
return v.boolean;
|
||||
|
@ -1566,7 +1566,7 @@ bool EvalState::isFunctor(Value & fun)
|
|||
|
||||
void EvalState::forceFunction(Value & v, const Pos & pos)
|
||||
{
|
||||
forceValue(v);
|
||||
forceValue(v, pos);
|
||||
if (v.type != tLambda && v.type != tPrimOp && v.type != tPrimOpApp && !isFunctor(v))
|
||||
throwTypeError("value is %1% while a function was expected, at %2%", v, pos);
|
||||
}
|
||||
|
@ -1643,7 +1643,7 @@ std::optional<string> EvalState::tryAttrsToString(const Pos & pos, Value & v,
|
|||
string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context,
|
||||
bool coerceMore, bool copyToStore)
|
||||
{
|
||||
forceValue(v);
|
||||
forceValue(v, pos);
|
||||
|
||||
string s;
|
||||
|
||||
|
|
|
@ -242,7 +242,7 @@ void prim_exec(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|||
/* Return a string representing the type of the expression. */
|
||||
static void prim_typeOf(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
string t;
|
||||
switch (args[0]->type) {
|
||||
case tInt: t = "int"; break;
|
||||
|
@ -270,7 +270,7 @@ static void prim_typeOf(EvalState & state, const Pos & pos, Value * * args, Valu
|
|||
/* Determine whether the argument is the null value. */
|
||||
static void prim_isNull(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tNull);
|
||||
}
|
||||
|
||||
|
@ -278,7 +278,7 @@ static void prim_isNull(EvalState & state, const Pos & pos, Value * * args, Valu
|
|||
/* Determine whether the argument is a function. */
|
||||
static void prim_isFunction(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
bool res;
|
||||
switch (args[0]->type) {
|
||||
case tLambda:
|
||||
|
@ -297,21 +297,21 @@ static void prim_isFunction(EvalState & state, const Pos & pos, Value * * args,
|
|||
/* Determine whether the argument is an integer. */
|
||||
static void prim_isInt(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tInt);
|
||||
}
|
||||
|
||||
/* Determine whether the argument is a float. */
|
||||
static void prim_isFloat(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tFloat);
|
||||
}
|
||||
|
||||
/* Determine whether the argument is a string. */
|
||||
static void prim_isString(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tString);
|
||||
}
|
||||
|
||||
|
@ -319,14 +319,14 @@ static void prim_isString(EvalState & state, const Pos & pos, Value * * args, Va
|
|||
/* Determine whether the argument is a Boolean. */
|
||||
static void prim_isBool(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tBool);
|
||||
}
|
||||
|
||||
/* Determine whether the argument is a path. */
|
||||
static void prim_isPath(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tPath);
|
||||
}
|
||||
|
||||
|
@ -383,7 +383,7 @@ static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * ar
|
|||
args[0]->attrs->find(state.symbols.create("operator"));
|
||||
if (op == args[0]->attrs->end())
|
||||
throw EvalError(format("attribute 'operator' required, at %1%") % pos);
|
||||
state.forceValue(*op->value);
|
||||
state.forceValue(*op->value, pos);
|
||||
|
||||
/* Construct the closure by applying the operator to element of
|
||||
`workSet', adding the result to `workSet', continuing until
|
||||
|
@ -402,7 +402,7 @@ static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * ar
|
|||
e->attrs->find(state.symbols.create("key"));
|
||||
if (key == e->attrs->end())
|
||||
throw EvalError(format("attribute 'key' required, at %1%") % pos);
|
||||
state.forceValue(*key->value);
|
||||
state.forceValue(*key->value, pos);
|
||||
|
||||
if (!doneKeys.insert(key->value).second) continue;
|
||||
res.push_back(e);
|
||||
|
@ -414,7 +414,7 @@ static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * ar
|
|||
|
||||
/* Add the values returned by the operator to the work set. */
|
||||
for (unsigned int n = 0; n < call.listSize(); ++n) {
|
||||
state.forceValue(*call.listElems()[n]);
|
||||
state.forceValue(*call.listElems()[n], pos);
|
||||
workSet.push_back(call.listElems()[n]);
|
||||
}
|
||||
}
|
||||
|
@ -446,7 +446,7 @@ static void prim_throw(EvalState & state, const Pos & pos, Value * * args, Value
|
|||
static void prim_addErrorContext(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
try {
|
||||
state.forceValue(*args[1]);
|
||||
state.forceValue(*args[1], pos);
|
||||
v = *args[1];
|
||||
} catch (Error & e) {
|
||||
PathSet context;
|
||||
|
@ -462,7 +462,7 @@ static void prim_tryEval(EvalState & state, const Pos & pos, Value * * args, Val
|
|||
{
|
||||
state.mkAttrs(v, 2);
|
||||
try {
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
v.attrs->push_back(Attr(state.sValue, args[0]));
|
||||
mkBool(*state.allocAttr(v, state.symbols.create("success")), true);
|
||||
} catch (AssertionError & e) {
|
||||
|
@ -484,8 +484,8 @@ static void prim_getEnv(EvalState & state, const Pos & pos, Value * * args, Valu
|
|||
/* Evaluate the first argument, then return the second argument. */
|
||||
static void prim_seq(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[1]);
|
||||
state.forceValue(*args[0], pos);
|
||||
state.forceValue(*args[1], pos);
|
||||
v = *args[1];
|
||||
}
|
||||
|
||||
|
@ -495,7 +495,7 @@ static void prim_seq(EvalState & state, const Pos & pos, Value * * args, Value &
|
|||
static void prim_deepSeq(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValueDeep(*args[0]);
|
||||
state.forceValue(*args[1]);
|
||||
state.forceValue(*args[1], pos);
|
||||
v = *args[1];
|
||||
}
|
||||
|
||||
|
@ -504,12 +504,12 @@ static void prim_deepSeq(EvalState & state, const Pos & pos, Value * * args, Val
|
|||
return the second expression. Useful for debugging. */
|
||||
static void prim_trace(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
if (args[0]->type == tString)
|
||||
printError(format("trace: %1%") % args[0]->string.s);
|
||||
else
|
||||
printError(format("trace: %1%") % *args[0]);
|
||||
state.forceValue(*args[1]);
|
||||
state.forceValue(*args[1], pos);
|
||||
v = *args[1];
|
||||
}
|
||||
|
||||
|
@ -600,7 +600,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
|
|||
try {
|
||||
|
||||
if (ignoreNulls) {
|
||||
state.forceValue(*i->value);
|
||||
state.forceValue(*i->value, pos);
|
||||
if (i->value->type == tNull) continue;
|
||||
}
|
||||
|
||||
|
@ -1093,7 +1093,7 @@ static void prim_filterSource(EvalState & state, const Pos & pos, Value * * args
|
|||
if (!context.empty())
|
||||
throw EvalError(format("string '%1%' cannot refer to other paths, at %2%") % path % pos);
|
||||
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
if (args[0]->type != tLambda)
|
||||
throw TypeError(format("first argument in call to 'filterSource' is not a function but %1%, at %2%") % showType(*args[0]) % pos);
|
||||
|
||||
|
@ -1119,7 +1119,7 @@ static void prim_path(EvalState & state, const Pos & pos, Value * * args, Value
|
|||
} else if (attr.name == state.sName)
|
||||
name = state.forceStringNoCtx(*attr.value, *attr.pos);
|
||||
else if (n == "filter") {
|
||||
state.forceValue(*attr.value);
|
||||
state.forceValue(*attr.value, pos);
|
||||
filterFun = attr.value;
|
||||
} else if (n == "recursive")
|
||||
recursive = state.forceBool(*attr.value, *attr.pos);
|
||||
|
@ -1190,7 +1190,7 @@ void prim_getAttr(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|||
throw EvalError(format("attribute '%1%' missing, at %2%") % attr % pos);
|
||||
// !!! add to stack trace?
|
||||
if (state.countCalls && i->pos) state.attrSelects[*i->pos]++;
|
||||
state.forceValue(*i->value);
|
||||
state.forceValue(*i->value, pos);
|
||||
v = *i->value;
|
||||
}
|
||||
|
||||
|
@ -1220,7 +1220,7 @@ static void prim_hasAttr(EvalState & state, const Pos & pos, Value * * args, Val
|
|||
/* Determine whether the argument is a set. */
|
||||
static void prim_isAttrs(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->type == tAttrs);
|
||||
}
|
||||
|
||||
|
@ -1346,7 +1346,7 @@ static void prim_catAttrs(EvalState & state, const Pos & pos, Value * * args, Va
|
|||
*/
|
||||
static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
if (args[0]->type != tLambda)
|
||||
throw TypeError(format("'functionArgs' requires a function, at %1%") % pos);
|
||||
|
||||
|
@ -1392,7 +1392,7 @@ static void prim_mapAttrs(EvalState & state, const Pos & pos, Value * * args, Va
|
|||
/* Determine whether the argument is a list. */
|
||||
static void prim_isList(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[0], pos);
|
||||
mkBool(v, args[0]->isList());
|
||||
}
|
||||
|
||||
|
@ -1402,7 +1402,7 @@ static void elemAt(EvalState & state, const Pos & pos, Value & list, int n, Valu
|
|||
state.forceList(list, pos);
|
||||
if (n < 0 || (unsigned int) n >= list.listSize())
|
||||
throw Error(format("list index %1% is out of bounds, at %2%") % n % pos);
|
||||
state.forceValue(*list.listElems()[n]);
|
||||
state.forceValue(*list.listElems()[n], pos);
|
||||
v = *list.listElems()[n];
|
||||
}
|
||||
|
||||
|
@ -1525,9 +1525,9 @@ static void prim_foldlStrict(EvalState & state, const Pos & pos, Value * * args,
|
|||
vCur = n == args[2]->listSize() - 1 ? &v : state.allocValue();
|
||||
state.callFunction(vTmp, *args[2]->listElems()[n], *vCur, pos);
|
||||
}
|
||||
state.forceValue(v);
|
||||
state.forceValue(v, pos);
|
||||
} else {
|
||||
state.forceValue(*args[1]);
|
||||
state.forceValue(*args[1], pos);
|
||||
v = *args[1];
|
||||
}
|
||||
}
|
||||
|
@ -1592,7 +1592,7 @@ static void prim_sort(EvalState & state, const Pos & pos, Value * * args, Value
|
|||
auto len = args[1]->listSize();
|
||||
state.mkList(v, len);
|
||||
for (unsigned int n = 0; n < len; ++n) {
|
||||
state.forceValue(*args[1]->listElems()[n]);
|
||||
state.forceValue(*args[1]->listElems()[n], pos);
|
||||
v.listElems()[n] = args[1]->listElems()[n];
|
||||
}
|
||||
|
||||
|
@ -1627,7 +1627,7 @@ static void prim_partition(EvalState & state, const Pos & pos, Value * * args, V
|
|||
|
||||
for (unsigned int n = 0; n < len; ++n) {
|
||||
auto vElem = args[1]->listElems()[n];
|
||||
state.forceValue(*vElem);
|
||||
state.forceValue(*vElem, pos);
|
||||
Value res;
|
||||
state.callFunction(*args[0], *vElem, res, pos);
|
||||
if (state.forceBool(res, pos))
|
||||
|
@ -1758,8 +1758,8 @@ static void prim_bitXor(EvalState & state, const Pos & pos, Value * * args, Valu
|
|||
|
||||
static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
state.forceValue(*args[0]);
|
||||
state.forceValue(*args[1]);
|
||||
state.forceValue(*args[0], pos);
|
||||
state.forceValue(*args[1], pos);
|
||||
CompareValues comp;
|
||||
mkBool(v, comp(args[0], args[1]));
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
@ -43,6 +42,7 @@
|
|||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <termios.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
@ -4789,8 +4789,7 @@ void Worker::waitForInput()
|
|||
terminated. */
|
||||
|
||||
bool useTimeout = false;
|
||||
struct timeval timeout;
|
||||
timeout.tv_usec = 0;
|
||||
long timeout = 0;
|
||||
auto before = steady_time_point::clock::now();
|
||||
|
||||
/* If we're monitoring for silence on stdout/stderr, or if there
|
||||
|
@ -4808,7 +4807,7 @@ void Worker::waitForInput()
|
|||
nearest = std::min(nearest, i.timeStarted + std::chrono::seconds(settings.buildTimeout));
|
||||
}
|
||||
if (nearest != steady_time_point::max()) {
|
||||
timeout.tv_sec = std::max(1L, (long) std::chrono::duration_cast<std::chrono::seconds>(nearest - before).count());
|
||||
timeout = std::max(1L, (long) std::chrono::duration_cast<std::chrono::seconds>(nearest - before).count());
|
||||
useTimeout = true;
|
||||
}
|
||||
|
||||
|
@ -4819,30 +4818,28 @@ void Worker::waitForInput()
|
|||
if (lastWokenUp == steady_time_point::min())
|
||||
printError("waiting for locks or build slots...");
|
||||
if (lastWokenUp == steady_time_point::min() || lastWokenUp > before) lastWokenUp = before;
|
||||
timeout.tv_sec = std::max(1L,
|
||||
timeout = std::max(1L,
|
||||
(long) std::chrono::duration_cast<std::chrono::seconds>(
|
||||
lastWokenUp + std::chrono::seconds(settings.pollInterval) - before).count());
|
||||
} else lastWokenUp = steady_time_point::min();
|
||||
|
||||
if (useTimeout)
|
||||
vomit("sleeping %d seconds", timeout.tv_sec);
|
||||
vomit("sleeping %d seconds", timeout);
|
||||
|
||||
/* Use select() to wait for the input side of any logger pipe to
|
||||
become `available'. Note that `available' (i.e., non-blocking)
|
||||
includes EOF. */
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
int fdMax = 0;
|
||||
std::vector<struct pollfd> pollStatus;
|
||||
std::map <int, int> fdToPollStatus;
|
||||
for (auto & i : children) {
|
||||
for (auto & j : i.fds) {
|
||||
if (j >= FD_SETSIZE)
|
||||
throw Error("reached FD_SETSIZE limit");
|
||||
FD_SET(j, &fds);
|
||||
if (j >= fdMax) fdMax = j + 1;
|
||||
pollStatus.push_back((struct pollfd) { .fd = j, .events = POLLIN });
|
||||
fdToPollStatus[j] = pollStatus.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (select(fdMax, &fds, 0, 0, useTimeout ? &timeout : 0) == -1) {
|
||||
if (poll(pollStatus.data(), pollStatus.size(),
|
||||
useTimeout ? timeout * 1000 : -1) == -1) {
|
||||
if (errno == EINTR) return;
|
||||
throw SysError("waiting for input");
|
||||
}
|
||||
|
@ -4863,7 +4860,7 @@ void Worker::waitForInput()
|
|||
set<int> fds2(j->fds);
|
||||
std::vector<unsigned char> buffer(4096);
|
||||
for (auto & k : fds2) {
|
||||
if (FD_ISSET(k, &fds)) {
|
||||
if (pollStatus.at(fdToPollStatus.at(k)).revents) {
|
||||
ssize_t rd = read(k, buffer.data(), buffer.size());
|
||||
// FIXME: is there a cleaner way to handle pt close
|
||||
// than EIO? Is this even standard?
|
||||
|
|
13
src/libutil/ansicolor.hh
Normal file
13
src/libutil/ansicolor.hh
Normal file
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
namespace nix
|
||||
{
|
||||
/* Some ANSI escape sequences. */
|
||||
#define ANSI_NORMAL "\e[0m"
|
||||
#define ANSI_BOLD "\e[1m"
|
||||
#define ANSI_FAINT "\e[2m"
|
||||
#define ANSI_RED "\e[31;1m"
|
||||
#define ANSI_GREEN "\e[32;1m"
|
||||
#define ANSI_YELLOW "\e[33;1m"
|
||||
#define ANSI_BLUE "\e[34;1m"
|
||||
}
|
146
src/libutil/error.cc
Normal file
146
src/libutil/error.cc
Normal file
|
@ -0,0 +1,146 @@
|
|||
#include "error.hh"
|
||||
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
namespace nix
|
||||
{
|
||||
|
||||
std::optional<string> ErrorInfo::programName = std::nullopt;
|
||||
|
||||
std::ostream& operator<<(std::ostream &os, const hintformat &hf)
|
||||
{
|
||||
return os << hf.str();
|
||||
}
|
||||
|
||||
string showErrPos(const ErrPos &errPos)
|
||||
{
|
||||
if (errPos.column > 0) {
|
||||
return fmt("(%1%:%2%)", errPos.lineNumber, errPos.column);
|
||||
} else {
|
||||
return fmt("(%1%)", errPos.lineNumber);
|
||||
};
|
||||
}
|
||||
|
||||
void printCodeLines(const string &prefix, const NixCode &nixCode)
|
||||
{
|
||||
// previous line of code.
|
||||
if (nixCode.prevLineOfCode.has_value()) {
|
||||
std::cout << fmt("%1% %|2$5d|| %3%",
|
||||
prefix,
|
||||
(nixCode.errPos.lineNumber - 1),
|
||||
*nixCode.prevLineOfCode)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// line of code containing the error.%2$+5d%
|
||||
std::cout << fmt("%1% %|2$5d|| %3%",
|
||||
prefix,
|
||||
(nixCode.errPos.lineNumber),
|
||||
nixCode.errLineOfCode)
|
||||
<< std::endl;
|
||||
|
||||
// error arrows for the column range.
|
||||
if (nixCode.errPos.column > 0) {
|
||||
int start = nixCode.errPos.column;
|
||||
std::string spaces;
|
||||
for (int i = 0; i < start; ++i) {
|
||||
spaces.append(" ");
|
||||
}
|
||||
|
||||
std::string arrows("^");
|
||||
|
||||
std::cout << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
|
||||
prefix,
|
||||
spaces,
|
||||
arrows) << std::endl;
|
||||
}
|
||||
|
||||
// next line of code.
|
||||
if (nixCode.nextLineOfCode.has_value()) {
|
||||
std::cout << fmt("%1% %|2$5d|| %3%",
|
||||
prefix,
|
||||
(nixCode.errPos.lineNumber + 1),
|
||||
*nixCode.nextLineOfCode)
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void printErrorInfo(const ErrorInfo &einfo)
|
||||
{
|
||||
int errwidth = 80;
|
||||
string prefix = " ";
|
||||
|
||||
string levelString;
|
||||
switch (einfo.level) {
|
||||
case ErrLevel::elError: {
|
||||
levelString = ANSI_RED;
|
||||
levelString += "error:";
|
||||
levelString += ANSI_NORMAL;
|
||||
break;
|
||||
}
|
||||
case ErrLevel::elWarning: {
|
||||
levelString = ANSI_YELLOW;
|
||||
levelString += "warning:";
|
||||
levelString += ANSI_NORMAL;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
levelString = fmt("invalid error level: %1%", einfo.level);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length();
|
||||
int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl;
|
||||
|
||||
string dashes;
|
||||
for (int i = 0; i < dashwidth; ++i)
|
||||
dashes.append("-");
|
||||
|
||||
// divider.
|
||||
std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL,
|
||||
prefix,
|
||||
levelString,
|
||||
"---",
|
||||
einfo.name,
|
||||
dashes,
|
||||
einfo.programName.value_or(""))
|
||||
<< std::endl;
|
||||
|
||||
// filename.
|
||||
if (einfo.nixCode.has_value()) {
|
||||
if (einfo.nixCode->errPos.nixFile != "") {
|
||||
string eline = einfo.nixCode->errLineOfCode != ""
|
||||
? string(" ") + showErrPos(einfo.nixCode->errPos)
|
||||
: "";
|
||||
|
||||
std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL,
|
||||
prefix,
|
||||
einfo.nixCode->errPos.nixFile,
|
||||
eline) << std::endl;
|
||||
std::cout << prefix << std::endl;
|
||||
} else {
|
||||
std::cout << fmt("%1%from command line argument", prefix) << std::endl;
|
||||
std::cout << prefix << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// description
|
||||
std::cout << prefix << einfo.description << std::endl;
|
||||
std::cout << prefix << std::endl;
|
||||
|
||||
// lines of code.
|
||||
if (einfo.nixCode->errLineOfCode != "") {
|
||||
printCodeLines(prefix, *einfo.nixCode);
|
||||
std::cout << prefix << std::endl;
|
||||
}
|
||||
|
||||
// hint
|
||||
if (einfo.hint.has_value()) {
|
||||
std::cout << prefix << *einfo.hint << std::endl;
|
||||
std::cout << prefix << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
121
src/libutil/error.hh
Normal file
121
src/libutil/error.hh
Normal file
|
@ -0,0 +1,121 @@
|
|||
#ifndef error_hh
|
||||
#define error_hh
|
||||
|
||||
#include "ansicolor.hh"
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <iostream>
|
||||
#include "types.hh"
|
||||
|
||||
namespace nix
|
||||
{
|
||||
|
||||
typedef enum {
|
||||
elWarning,
|
||||
elError
|
||||
} ErrLevel;
|
||||
|
||||
struct ErrPos
|
||||
{
|
||||
int lineNumber;
|
||||
int column;
|
||||
string nixFile;
|
||||
|
||||
template <class P>
|
||||
ErrPos& operator=(const P &pos)
|
||||
{
|
||||
lineNumber = pos.line;
|
||||
column = pos.column;
|
||||
nixFile = pos.file;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class P>
|
||||
ErrPos(const P &p)
|
||||
{
|
||||
*this = p;
|
||||
}
|
||||
};
|
||||
|
||||
struct NixCode
|
||||
{
|
||||
ErrPos errPos;
|
||||
std::optional<string> prevLineOfCode;
|
||||
string errLineOfCode;
|
||||
std::optional<string> nextLineOfCode;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// format function for hints. same as fmt, except templated values
|
||||
// are always in yellow.
|
||||
|
||||
template <class T>
|
||||
struct yellowify
|
||||
{
|
||||
yellowify(T &s) : value(s) {}
|
||||
T &value;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
|
||||
{
|
||||
return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
|
||||
}
|
||||
|
||||
class hintformat
|
||||
{
|
||||
public:
|
||||
hintformat(string format) :fmt(format)
|
||||
{
|
||||
fmt.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
|
||||
}
|
||||
template<class T>
|
||||
hintformat& operator%(const T &value)
|
||||
{
|
||||
fmt % yellowify(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string str() const
|
||||
{
|
||||
return fmt.str();
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
friend class AddHint;
|
||||
private:
|
||||
format fmt;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream &os, const hintformat &hf);
|
||||
|
||||
template<typename... Args>
|
||||
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
|
||||
{
|
||||
hintformat f(fs);
|
||||
formatHelper(f, args...);
|
||||
return f;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// ErrorInfo.
|
||||
struct ErrorInfo
|
||||
{
|
||||
ErrLevel level;
|
||||
string name;
|
||||
string description;
|
||||
std::optional<hintformat> hint;
|
||||
std::optional<NixCode> nixCode;
|
||||
|
||||
static std::optional<string> programName;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------
|
||||
// error printing
|
||||
|
||||
// just to cout for now.
|
||||
void printErrorInfo(const ErrorInfo &einfo);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -41,7 +41,8 @@ struct FormatOrString
|
|||
{
|
||||
string s;
|
||||
FormatOrString(const string & s) : s(s) { };
|
||||
FormatOrString(const format & f) : s(f.str()) { };
|
||||
template<class F>
|
||||
FormatOrString(const F & f) : s(f.str()) { };
|
||||
FormatOrString(const char * s) : s(s) { };
|
||||
};
|
||||
|
||||
|
@ -51,12 +52,13 @@ struct FormatOrString
|
|||
... a_n’. However, ‘fmt(s)’ is equivalent to ‘s’ (so no %-expansion
|
||||
takes place). */
|
||||
|
||||
inline void formatHelper(boost::format & f)
|
||||
template<class F>
|
||||
inline void formatHelper(F & f)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
inline void formatHelper(boost::format & f, const T & x, const Args & ... args)
|
||||
template<class F, typename T, typename... Args>
|
||||
inline void formatHelper(F & f, const T & x, const Args & ... args)
|
||||
{
|
||||
formatHelper(f % x, args...);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "types.hh"
|
||||
#include "logging.hh"
|
||||
#include "ansicolor.hh"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -450,15 +451,6 @@ std::string shellEscape(const std::string & s);
|
|||
void ignoreException();
|
||||
|
||||
|
||||
/* Some ANSI escape sequences. */
|
||||
#define ANSI_NORMAL "\e[0m"
|
||||
#define ANSI_BOLD "\e[1m"
|
||||
#define ANSI_FAINT "\e[2m"
|
||||
#define ANSI_RED "\e[31;1m"
|
||||
#define ANSI_GREEN "\e[32;1m"
|
||||
#define ANSI_YELLOW "\e[33;1m"
|
||||
#define ANSI_BLUE "\e[34;1m"
|
||||
|
||||
|
||||
/* Tree formatting. */
|
||||
constexpr char treeConn[] = "├───";
|
||||
|
|
Loading…
Reference in a new issue