aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burdette <bburdette@gmail.com>2020-03-25 10:52:03 -0600
committerBen Burdette <bburdette@gmail.com>2020-03-25 10:52:03 -0600
commit3582dc3c88d38d5c46091b4a23601d6f56a4c81e (patch)
treeb973ada9db82355056cd385345bb8879ce9f5cfd
parentfc310eda3afdf97c55a1a5ac6756f8d54a2a5ad6 (diff)
programName as static member var
-rw-r--r--src/libutil/error.cc13
-rw-r--r--src/libutil/error.hh6
-rw-r--r--tests/errors/main.cc4
3 files changed, 18 insertions, 5 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
index f258d6a83..4abee052d 100644
--- a/src/libutil/error.cc
+++ b/src/libutil/error.cc
@@ -1,11 +1,15 @@
#include "error.hh"
#include <iostream>
+#include <optional>
namespace nix {
using std::cout;
using std::endl;
+using std::nullopt;
+
+optional<string> ErrorInfo::programName = nullopt;
// return basic_format?
string showErrLine(ErrLine &errLine)
@@ -98,10 +102,11 @@ void print_error(ErrorInfo &einfo)
{
level_string = "wat:";
break;
- }}
+ }
+ }
- int ndl = level_string.length() + 3 + einfo.name.length() + einfo.program.length();
- int dashwidth = errwidth - 3 ? 3 : 80 - ndl;
+ int ndl = prefix.length() + level_string.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length();
+ int dashwidth = ndl > (errwidth - 3) ? 3 : 80 - ndl;
string dashes;
for (int i = 0; i < dashwidth; ++i)
@@ -114,7 +119,7 @@ void print_error(ErrorInfo &einfo)
% "---"
% einfo.name
% dashes
- % einfo.program
+ % einfo.programName.value_or("")
<< endl;
// filename.
diff --git a/src/libutil/error.hh b/src/libutil/error.hh
index 25f3e8ee8..7260dfd5b 100644
--- a/src/libutil/error.hh
+++ b/src/libutil/error.hh
@@ -80,11 +80,12 @@ class ErrorInfo {
ErrLevel level;
string name;
string description;
- string program;
optional<NixCode> nixCode;
string hint;
ErrorInfo& GetEI() { return *this; }
+ static optional<string> programName;
+
// give these access to the private constructor,
// when they are direct descendants.
friend AddName<ErrorInfo>;
@@ -109,18 +110,21 @@ class ErrorInfo {
};
+// Init as error
class EIError : public ErrorInfo
{
protected:
EIError() : ErrorInfo(elError) {}
};
+// Init as warning
class EIWarning : public ErrorInfo
{
protected:
EIWarning() : ErrorInfo(elWarning) {}
};
+// Builder class definitions.
template <class T>
class AddName : private T
{
diff --git a/tests/errors/main.cc b/tests/errors/main.cc
index 6b024d287..5ac429b46 100644
--- a/tests/errors/main.cc
+++ b/tests/errors/main.cc
@@ -5,11 +5,15 @@
using std::optional;
using std::nullopt;
+using std::cout;
+using std::endl;
int main() {
using namespace nix;
+ ErrorInfo::programName = optional("errorTest");
+
/*
ColumnRange columnRange;
columnRange.start = 24;