2020-05-09 03:18:28 +03:00
|
|
|
#include "error.hh"
|
2020-03-22 20:25:47 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2020-03-25 18:52:03 +02:00
|
|
|
#include <optional>
|
2020-04-18 00:07:44 +03:00
|
|
|
#include "serialise.hh"
|
2020-05-14 21:28:18 +03:00
|
|
|
#include <sstream>
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2020-04-29 06:06:08 +03:00
|
|
|
namespace nix {
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2020-04-24 23:57:51 +03:00
|
|
|
const std::string nativeSystem = SYSTEM;
|
|
|
|
|
2022-03-30 18:30:47 +03:00
|
|
|
void BaseError::addTrace(std::optional<ErrPos> e, hintformat hint)
|
2020-06-19 00:25:26 +03:00
|
|
|
{
|
2020-10-07 17:33:19 +03:00
|
|
|
err.traces.push_front(Trace { .pos = e, .hint = hint });
|
2020-06-19 00:25:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:53:19 +03:00
|
|
|
// c++ std::exception descendants must have a 'const char* what()' function.
|
|
|
|
// This stringifies the error and caches it for use by what(), or similarly by msg().
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string & BaseError::calcWhat() const
|
2020-05-14 21:28:18 +03:00
|
|
|
{
|
|
|
|
if (what_.has_value())
|
|
|
|
return *what_;
|
|
|
|
else {
|
|
|
|
std::ostringstream oss;
|
2021-12-28 14:53:21 +02:00
|
|
|
showErrorInfo(oss, err, loggerSettings.showTrace);
|
2020-05-14 21:28:18 +03:00
|
|
|
what_ = oss.str();
|
|
|
|
return *what_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
std::optional<std::string> ErrorInfo::programName = std::nullopt;
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2020-10-07 17:33:19 +03:00
|
|
|
std::ostream & operator<<(std::ostream & os, const hintformat & hf)
|
2020-04-15 19:09:43 +03:00
|
|
|
{
|
|
|
|
return os << hf.str();
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string showErrPos(const ErrPos & errPos)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
2020-05-09 03:18:28 +03:00
|
|
|
if (errPos.line > 0) {
|
|
|
|
if (errPos.column > 0) {
|
2021-01-21 01:55:59 +02:00
|
|
|
return fmt("%d:%d", errPos.line, errPos.column);
|
2020-05-09 03:18:28 +03:00
|
|
|
} else {
|
2021-01-21 01:55:59 +02:00
|
|
|
return fmt("%d", errPos.line);
|
2020-05-09 03:18:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "";
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:33:19 +03:00
|
|
|
std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos)
|
2020-05-21 02:25:02 +03:00
|
|
|
{
|
2020-06-24 17:33:53 +03:00
|
|
|
if (errPos.line <= 0)
|
|
|
|
return std::nullopt;
|
2020-05-21 02:25:02 +03:00
|
|
|
|
2020-06-24 17:33:53 +03:00
|
|
|
if (errPos.origin == foFile) {
|
|
|
|
LinesOfCode loc;
|
2020-05-21 07:18:26 +03:00
|
|
|
try {
|
2021-01-05 15:54:28 +02:00
|
|
|
// FIXME: when running as the daemon, make sure we don't
|
|
|
|
// open a file to which the client doesn't have access.
|
2020-06-24 17:33:53 +03:00
|
|
|
AutoCloseFD fd = open(errPos.file.c_str(), O_RDONLY | O_CLOEXEC);
|
Don't log from inside the logger
This deadlocks ProgressBar, e.g.
# nix run --impure --no-substitute --store '/tmp/nix2?store=/foo' --expr 'derivation { builder = /nix/store/zi90rxslsm4mlr46l2xws1rm94g7pk8p-busybox-1.31.1-x86_64-unknown-linux-musl/bin/busybox; }'
leads to
Thread 1 (Thread 0x7ffff6126e80 (LWP 12250)):
#0 0x00007ffff7215d62 in __lll_lock_wait () from /nix/store/9df65igwjmf2wbw0gbrrgair6piqjgmi-glibc-2.31/lib/libpthread.so.0
#1 0x00007ffff720e721 in pthread_mutex_lock () from /nix/store/9df65igwjmf2wbw0gbrrgair6piqjgmi-glibc-2.31/lib/libpthread.so.0
#2 0x00007ffff7ad17fa in __gthread_mutex_lock (__mutex=0x6c5448) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/gthr-default.h:749
#3 std::mutex::lock (this=0x6c5448) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/std_mutex.h:100
#4 std::unique_lock<std::mutex>::lock (this=0x7fffffff09a8, this=0x7fffffff09a8) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/unique_lock.h:141
#5 std::unique_lock<std::mutex>::unique_lock (__m=..., this=0x7fffffff09a8) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/unique_lock.h:71
#6 nix::Sync<nix::ProgressBar::State, std::mutex>::Lock::Lock (s=0x6c5448, this=0x7fffffff09a0) at src/libutil/sync.hh:45
#7 nix::Sync<nix::ProgressBar::State, std::mutex>::lock (this=0x6c5448) at src/libutil/sync.hh:85
#8 nix::ProgressBar::logEI (this=0x6c5440, ei=...) at src/libmain/progress-bar.cc:131
#9 0x00007ffff7608cfd in nix::Logger::logEI (ei=..., lvl=nix::lvlError, this=0x6c5440) at src/libutil/logging.hh:88
#10 nix::getCodeLines (errPos=...) at src/libutil/error.cc:66
#11 0x00007ffff76073f2 in nix::showErrorInfo (out=..., einfo=..., showTrace=<optimized out>) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/optional:897
#12 0x00007ffff7ad19e7 in nix::ProgressBar::logEI (this=0x6c5440, ei=...) at src/libmain/progress-bar.cc:134
#13 0x00007ffff7ab9d10 in nix::Logger::logEI (ei=..., lvl=nix::lvlError, this=0x6c5440) at src/libutil/logging.hh:88
#14 nix::handleExceptions(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<void ()>) (programName="/home/eelco/Dev/nix/outputs/out/bin/nix", fun=...) at src/libmain/shared.cc:328
#15 0x000000000046226b in main (argc=<optimized out>, argv=<optimized out>) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/ext/new_allocator.h:80
2020-12-22 12:15:29 +02:00
|
|
|
if (!fd) return {};
|
|
|
|
|
|
|
|
// count the newlines.
|
|
|
|
int count = 0;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string line;
|
Don't log from inside the logger
This deadlocks ProgressBar, e.g.
# nix run --impure --no-substitute --store '/tmp/nix2?store=/foo' --expr 'derivation { builder = /nix/store/zi90rxslsm4mlr46l2xws1rm94g7pk8p-busybox-1.31.1-x86_64-unknown-linux-musl/bin/busybox; }'
leads to
Thread 1 (Thread 0x7ffff6126e80 (LWP 12250)):
#0 0x00007ffff7215d62 in __lll_lock_wait () from /nix/store/9df65igwjmf2wbw0gbrrgair6piqjgmi-glibc-2.31/lib/libpthread.so.0
#1 0x00007ffff720e721 in pthread_mutex_lock () from /nix/store/9df65igwjmf2wbw0gbrrgair6piqjgmi-glibc-2.31/lib/libpthread.so.0
#2 0x00007ffff7ad17fa in __gthread_mutex_lock (__mutex=0x6c5448) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/gthr-default.h:749
#3 std::mutex::lock (this=0x6c5448) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/std_mutex.h:100
#4 std::unique_lock<std::mutex>::lock (this=0x7fffffff09a8, this=0x7fffffff09a8) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/unique_lock.h:141
#5 std::unique_lock<std::mutex>::unique_lock (__m=..., this=0x7fffffff09a8) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/unique_lock.h:71
#6 nix::Sync<nix::ProgressBar::State, std::mutex>::Lock::Lock (s=0x6c5448, this=0x7fffffff09a0) at src/libutil/sync.hh:45
#7 nix::Sync<nix::ProgressBar::State, std::mutex>::lock (this=0x6c5448) at src/libutil/sync.hh:85
#8 nix::ProgressBar::logEI (this=0x6c5440, ei=...) at src/libmain/progress-bar.cc:131
#9 0x00007ffff7608cfd in nix::Logger::logEI (ei=..., lvl=nix::lvlError, this=0x6c5440) at src/libutil/logging.hh:88
#10 nix::getCodeLines (errPos=...) at src/libutil/error.cc:66
#11 0x00007ffff76073f2 in nix::showErrorInfo (out=..., einfo=..., showTrace=<optimized out>) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/optional:897
#12 0x00007ffff7ad19e7 in nix::ProgressBar::logEI (this=0x6c5440, ei=...) at src/libmain/progress-bar.cc:134
#13 0x00007ffff7ab9d10 in nix::Logger::logEI (ei=..., lvl=nix::lvlError, this=0x6c5440) at src/libutil/logging.hh:88
#14 nix::handleExceptions(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<void ()>) (programName="/home/eelco/Dev/nix/outputs/out/bin/nix", fun=...) at src/libmain/shared.cc:328
#15 0x000000000046226b in main (argc=<optimized out>, argv=<optimized out>) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/ext/new_allocator.h:80
2020-12-22 12:15:29 +02:00
|
|
|
int pl = errPos.line - 1;
|
|
|
|
do
|
2020-05-21 07:18:26 +03:00
|
|
|
{
|
Don't log from inside the logger
This deadlocks ProgressBar, e.g.
# nix run --impure --no-substitute --store '/tmp/nix2?store=/foo' --expr 'derivation { builder = /nix/store/zi90rxslsm4mlr46l2xws1rm94g7pk8p-busybox-1.31.1-x86_64-unknown-linux-musl/bin/busybox; }'
leads to
Thread 1 (Thread 0x7ffff6126e80 (LWP 12250)):
#0 0x00007ffff7215d62 in __lll_lock_wait () from /nix/store/9df65igwjmf2wbw0gbrrgair6piqjgmi-glibc-2.31/lib/libpthread.so.0
#1 0x00007ffff720e721 in pthread_mutex_lock () from /nix/store/9df65igwjmf2wbw0gbrrgair6piqjgmi-glibc-2.31/lib/libpthread.so.0
#2 0x00007ffff7ad17fa in __gthread_mutex_lock (__mutex=0x6c5448) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/gthr-default.h:749
#3 std::mutex::lock (this=0x6c5448) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/std_mutex.h:100
#4 std::unique_lock<std::mutex>::lock (this=0x7fffffff09a8, this=0x7fffffff09a8) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/unique_lock.h:141
#5 std::unique_lock<std::mutex>::unique_lock (__m=..., this=0x7fffffff09a8) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/unique_lock.h:71
#6 nix::Sync<nix::ProgressBar::State, std::mutex>::Lock::Lock (s=0x6c5448, this=0x7fffffff09a0) at src/libutil/sync.hh:45
#7 nix::Sync<nix::ProgressBar::State, std::mutex>::lock (this=0x6c5448) at src/libutil/sync.hh:85
#8 nix::ProgressBar::logEI (this=0x6c5440, ei=...) at src/libmain/progress-bar.cc:131
#9 0x00007ffff7608cfd in nix::Logger::logEI (ei=..., lvl=nix::lvlError, this=0x6c5440) at src/libutil/logging.hh:88
#10 nix::getCodeLines (errPos=...) at src/libutil/error.cc:66
#11 0x00007ffff76073f2 in nix::showErrorInfo (out=..., einfo=..., showTrace=<optimized out>) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/optional:897
#12 0x00007ffff7ad19e7 in nix::ProgressBar::logEI (this=0x6c5440, ei=...) at src/libmain/progress-bar.cc:134
#13 0x00007ffff7ab9d10 in nix::Logger::logEI (ei=..., lvl=nix::lvlError, this=0x6c5440) at src/libutil/logging.hh:88
#14 nix::handleExceptions(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<void ()>) (programName="/home/eelco/Dev/nix/outputs/out/bin/nix", fun=...) at src/libmain/shared.cc:328
#15 0x000000000046226b in main (argc=<optimized out>, argv=<optimized out>) at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/ext/new_allocator.h:80
2020-12-22 12:15:29 +02:00
|
|
|
line = readLine(fd.get());
|
|
|
|
++count;
|
|
|
|
if (count < pl)
|
|
|
|
;
|
|
|
|
else if (count == pl)
|
|
|
|
loc.prevLineOfCode = line;
|
|
|
|
else if (count == pl + 1)
|
|
|
|
loc.errLineOfCode = line;
|
|
|
|
else if (count == pl + 2) {
|
|
|
|
loc.nextLineOfCode = line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
return loc;
|
2020-05-21 07:18:26 +03:00
|
|
|
}
|
2020-10-07 17:33:19 +03:00
|
|
|
catch (EndOfFile & eof) {
|
2020-07-01 21:02:02 +03:00
|
|
|
if (loc.errLineOfCode.has_value())
|
|
|
|
return loc;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
2020-05-21 07:18:26 +03:00
|
|
|
}
|
2020-10-07 17:33:19 +03:00
|
|
|
catch (std::exception & e) {
|
2020-06-24 17:33:53 +03:00
|
|
|
return std::nullopt;
|
2020-05-21 07:18:26 +03:00
|
|
|
}
|
2020-06-19 00:25:26 +03:00
|
|
|
} else {
|
2020-06-24 17:33:53 +03:00
|
|
|
std::istringstream iss(errPos.file);
|
2020-05-21 02:25:02 +03:00
|
|
|
// count the newlines.
|
|
|
|
int count = 0;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string line;
|
2020-06-24 17:33:53 +03:00
|
|
|
int pl = errPos.line - 1;
|
|
|
|
|
|
|
|
LinesOfCode loc;
|
2020-07-06 20:54:53 +03:00
|
|
|
|
2020-06-19 00:25:26 +03:00
|
|
|
do
|
2020-05-21 02:25:02 +03:00
|
|
|
{
|
2020-05-21 07:18:26 +03:00
|
|
|
std::getline(iss, line);
|
2020-05-21 02:25:02 +03:00
|
|
|
++count;
|
2020-06-19 00:25:26 +03:00
|
|
|
if (count < pl)
|
2020-05-21 02:25:02 +03:00
|
|
|
{
|
2020-06-19 00:25:26 +03:00
|
|
|
;
|
2020-05-21 02:25:02 +03:00
|
|
|
}
|
|
|
|
else if (count == pl) {
|
2020-06-24 17:33:53 +03:00
|
|
|
loc.prevLineOfCode = line;
|
2020-05-21 02:25:02 +03:00
|
|
|
} else if (count == pl + 1) {
|
2020-06-24 17:33:53 +03:00
|
|
|
loc.errLineOfCode = line;
|
2020-05-21 02:25:02 +03:00
|
|
|
} else if (count == pl + 2) {
|
2020-06-24 17:33:53 +03:00
|
|
|
loc.nextLineOfCode = line;
|
2020-05-21 02:25:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-05-21 07:18:26 +03:00
|
|
|
|
|
|
|
if (!iss.good())
|
|
|
|
break;
|
2020-05-21 02:25:02 +03:00
|
|
|
} while (true);
|
2020-06-24 17:33:53 +03:00
|
|
|
|
|
|
|
return loc;
|
2020-05-21 02:25:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 07:05:21 +03:00
|
|
|
// print lines of code to the ostream, indicating the error column.
|
2020-10-07 17:33:19 +03:00
|
|
|
void printCodeLines(std::ostream & out,
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string & prefix,
|
2020-10-07 17:33:19 +03:00
|
|
|
const ErrPos & errPos,
|
|
|
|
const LinesOfCode & loc)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
2020-04-08 18:48:21 +03:00
|
|
|
// previous line of code.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.prevLineOfCode.has_value()) {
|
2020-06-15 15:12:39 +03:00
|
|
|
out << std::endl
|
2020-06-03 23:47:00 +03:00
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
2020-06-24 17:33:53 +03:00
|
|
|
(errPos.line - 1),
|
|
|
|
*loc.prevLineOfCode);
|
2020-04-08 18:48:21 +03:00
|
|
|
}
|
2020-04-03 01:02:40 +03:00
|
|
|
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.errLineOfCode.has_value()) {
|
2020-05-09 03:18:28 +03:00
|
|
|
// line of code containing the error.
|
2020-06-03 23:47:00 +03:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
2020-06-24 17:33:53 +03:00
|
|
|
(errPos.line),
|
|
|
|
*loc.errLineOfCode);
|
2020-05-09 03:18:28 +03:00
|
|
|
// error arrows for the column range.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (errPos.column > 0) {
|
|
|
|
int start = errPos.column;
|
2020-05-09 03:18:28 +03:00
|
|
|
std::string spaces;
|
|
|
|
for (int i = 0; i < start; ++i) {
|
|
|
|
spaces.append(" ");
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2020-05-09 03:18:28 +03:00
|
|
|
std::string arrows("^");
|
2020-04-02 01:20:20 +03:00
|
|
|
|
2020-06-03 23:47:00 +03:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
|
|
|
spaces,
|
|
|
|
arrows);
|
2020-05-09 03:18:28 +03:00
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 18:48:21 +03:00
|
|
|
// next line of code.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.nextLineOfCode.has_value()) {
|
2020-06-03 23:47:00 +03:00
|
|
|
out << std::endl
|
|
|
|
<< fmt("%1% %|2$5d|| %3%",
|
2020-06-19 00:25:26 +03:00
|
|
|
prefix,
|
2020-06-24 17:33:53 +03:00
|
|
|
(errPos.line + 1),
|
|
|
|
*loc.nextLineOfCode);
|
2020-04-08 18:48:21 +03:00
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 01:55:59 +02:00
|
|
|
void printAtPos(const ErrPos & pos, std::ostream & out)
|
2020-06-25 03:31:28 +03:00
|
|
|
{
|
2021-01-21 01:55:59 +02:00
|
|
|
if (pos) {
|
2020-06-25 03:31:28 +03:00
|
|
|
switch (pos.origin) {
|
|
|
|
case foFile: {
|
2021-09-14 11:38:10 +03:00
|
|
|
out << fmt(ANSI_BLUE "at " ANSI_WARNING "%s:%s" ANSI_NORMAL ":", pos.file, showErrPos(pos));
|
2020-06-25 03:31:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case foString: {
|
2021-09-14 11:38:10 +03:00
|
|
|
out << fmt(ANSI_BLUE "at " ANSI_WARNING "«string»:%s" ANSI_NORMAL ":", showErrPos(pos));
|
2020-06-25 03:31:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case foStdin: {
|
2021-09-14 11:38:10 +03:00
|
|
|
out << fmt(ANSI_BLUE "at " ANSI_WARNING "«stdin»:%s" ANSI_NORMAL ":", showErrPos(pos));
|
2020-06-25 03:31:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw Error("invalid FileOrigin in errPos");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
static std::string indent(std::string_view indentFirst, std::string_view indentRest, std::string_view s)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
2021-01-21 01:27:36 +02:00
|
|
|
std::string res;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
while (!s.empty()) {
|
|
|
|
auto end = s.find('\n');
|
|
|
|
if (!first) res += "\n";
|
2021-01-21 01:49:29 +02:00
|
|
|
res += chomp(std::string(first ? indentFirst : indentRest) + std::string(s.substr(0, end)));
|
2021-01-21 01:27:36 +02:00
|
|
|
first = false;
|
|
|
|
if (end == s.npos) break;
|
|
|
|
s = s.substr(end + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace)
|
|
|
|
{
|
|
|
|
std::string prefix;
|
2020-04-03 01:02:40 +03:00
|
|
|
switch (einfo.level) {
|
2020-05-09 03:18:28 +03:00
|
|
|
case Verbosity::lvlError: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_RED "error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlNotice: {
|
|
|
|
prefix = ANSI_RED "note";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlWarn: {
|
2021-09-14 11:38:10 +03:00
|
|
|
prefix = ANSI_WARNING "warning";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlInfo: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "info";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlTalkative: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "talk";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlChatty: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "chat";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlVomit: {
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix = ANSI_GREEN "vomit";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Verbosity::lvlDebug: {
|
2021-09-14 11:38:10 +03:00
|
|
|
prefix = ANSI_WARNING "debug";
|
2020-05-09 03:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-01-21 01:27:36 +02:00
|
|
|
default:
|
|
|
|
assert(false);
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
// FIXME: show the program name as part of the trace?
|
|
|
|
if (einfo.programName && einfo.programName != ErrorInfo::programName)
|
|
|
|
prefix += fmt(" [%s]:" ANSI_NORMAL " ", einfo.programName.value_or(""));
|
2020-04-25 21:05:26 +03:00
|
|
|
else
|
2021-01-21 01:27:36 +02:00
|
|
|
prefix += ":" ANSI_NORMAL " ";
|
2020-03-22 20:25:47 +02:00
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
std::ostringstream oss;
|
2022-11-22 18:45:58 +02:00
|
|
|
|
|
|
|
// traces
|
|
|
|
if (showTrace && !einfo.traces.empty()) {
|
2022-12-09 19:36:25 +02:00
|
|
|
for (const auto & trace : einfo.traces) {
|
|
|
|
oss << "\n" << "… " << trace.hint.str() << "\n";
|
2022-11-22 18:45:58 +02:00
|
|
|
|
2022-12-09 19:36:25 +02:00
|
|
|
if (trace.pos.has_value() && (*trace.pos)) {
|
|
|
|
auto pos = trace.pos.value();
|
2022-11-22 18:45:58 +02:00
|
|
|
oss << "\n";
|
|
|
|
printAtPos(pos, oss);
|
|
|
|
|
|
|
|
auto loc = getCodeLines(pos);
|
|
|
|
if (loc.has_value()) {
|
|
|
|
oss << "\n";
|
|
|
|
printCodeLines(oss, "", pos, *loc);
|
|
|
|
oss << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oss << "\n" << prefix;
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
oss << einfo.msg << "\n";
|
|
|
|
|
|
|
|
if (einfo.errPos.has_value() && *einfo.errPos) {
|
|
|
|
oss << "\n";
|
2021-01-21 01:55:59 +02:00
|
|
|
printAtPos(*einfo.errPos, oss);
|
2020-04-02 01:20:20 +03:00
|
|
|
|
2020-06-24 17:33:53 +03:00
|
|
|
auto loc = getCodeLines(*einfo.errPos);
|
2020-06-19 00:25:26 +03:00
|
|
|
|
2020-05-21 02:25:02 +03:00
|
|
|
// lines of code.
|
2020-06-24 17:33:53 +03:00
|
|
|
if (loc.has_value()) {
|
2021-01-21 01:27:36 +02:00
|
|
|
oss << "\n";
|
|
|
|
printCodeLines(oss, "", *einfo.errPos, *loc);
|
|
|
|
oss << "\n";
|
2020-05-21 02:25:02 +03:00
|
|
|
}
|
2020-05-21 23:28:45 +03:00
|
|
|
}
|
2020-05-21 02:25:02 +03:00
|
|
|
|
2022-03-03 11:50:35 +02:00
|
|
|
auto suggestions = einfo.suggestions.trim();
|
|
|
|
if (! suggestions.suggestions.empty()){
|
|
|
|
oss << "Did you mean " <<
|
2022-03-07 11:04:57 +02:00
|
|
|
suggestions.trim() <<
|
2022-03-03 11:50:35 +02:00
|
|
|
"?" << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:27:36 +02:00
|
|
|
out << indent(prefix, std::string(filterANSIEscapes(prefix, true).size(), ' '), chomp(oss.str()));
|
|
|
|
|
2020-04-18 00:07:44 +03:00
|
|
|
return out;
|
|
|
|
}
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|