nix-super/src/libutil/error.hh

122 lines
2.2 KiB
C++
Raw Normal View History

#ifndef error_hh
#define error_hh
2020-03-27 18:55:09 +02:00
#include "ansicolor.hh"
#include <string>
#include <optional>
2020-03-24 19:21:35 +02:00
#include <iostream>
2020-04-03 17:48:20 +03:00
#include "types.hh"
2020-03-27 18:55:09 +02:00
2020-04-03 01:02:40 +03:00
namespace nix
{
2020-04-03 01:02:40 +03:00
typedef enum {
2020-04-02 23:25:43 +03:00
elWarning,
elError
} ErrLevel;
struct ErrPos
2020-04-03 01:02:40 +03:00
{
int lineNumber;
2020-04-07 23:36:32 +03:00
int column;
2020-04-08 18:07:58 +03:00
string nixFile;
2020-03-24 19:21:35 +02:00
2020-04-08 18:07:58 +03:00
template <class P>
ErrPos& operator=(const P &pos)
{
lineNumber = pos.line;
column = pos.column;
2020-04-08 18:48:21 +03:00
nixFile = pos.file;
2020-04-08 18:07:58 +03:00
return *this;
}
2020-04-08 18:48:21 +03:00
template <class P>
ErrPos(const P &p)
{
*this = p;
}
};
struct NixCode
2020-04-08 18:48:21 +03:00
{
ErrPos errPos;
std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
2020-03-24 19:21:35 +02:00
};
2020-04-07 04:43:22 +03:00
// ----------------------------------------------------------------
2020-04-08 18:56:10 +03:00
// format function for hints. same as fmt, except templated values
2020-04-07 04:43:22 +03:00
// are always in yellow.
2020-03-24 19:21:35 +02:00
template <class T>
struct yellowify
2020-04-07 04:43:22 +03:00
{
yellowify(T &s) : value(s) {}
T &value;
};
2020-03-24 19:21:35 +02:00
template <class T>
2020-04-07 04:43:22 +03:00
std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
{
return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
}
2020-03-24 19:21:35 +02:00
2020-04-07 04:43:22 +03:00
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;
}
2020-04-07 04:43:22 +03:00
std::string str() const
{
return fmt.str();
}
template <typename U>
friend class AddHint;
private:
format fmt;
};
2020-04-15 19:09:43 +03:00
std::ostream& operator<<(std::ostream &os, const hintformat &hf);
2020-04-08 18:07:58 +03:00
2020-04-07 04:43:22 +03:00
template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
{
hintformat f(fs);
formatHelper(f, args...);
return f;
}
2020-04-07 04:43:22 +03:00
// -------------------------------------------------
// ErrorInfo.
struct ErrorInfo
2020-04-03 01:02:40 +03:00
{
ErrLevel level;
string name;
string description;
2020-04-08 18:07:58 +03:00
std::optional<hintformat> hint;
2020-04-08 18:48:21 +03:00
std::optional<NixCode> nixCode;
2020-04-03 01:02:40 +03:00
static std::optional<string> programName;
2020-03-24 17:18:23 +02:00
};
2020-03-27 18:03:02 +02:00
// --------------------------------------------------------
// error printing
// just to cout for now.
2020-04-07 04:43:22 +03:00
void printErrorInfo(const ErrorInfo &einfo);
2020-03-27 18:03:02 +02:00
}
#endif