forked from lix-project/lix
logError, logWarning; Logger functions; switch to Verbosity enum
This commit is contained in:
parent
12814806ef
commit
3d5b1032a1
|
@ -1,4 +1,4 @@
|
||||||
#include "error.hh"
|
#include "logging.hh"
|
||||||
#include "nixexpr.hh"
|
#include "nixexpr.hh"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -8,24 +8,25 @@ int main()
|
||||||
{
|
{
|
||||||
using namespace nix;
|
using namespace nix;
|
||||||
|
|
||||||
|
std::unique_ptr<Logger> logger(makeDefaultLogger());
|
||||||
|
|
||||||
|
verbosity = lvlError;
|
||||||
|
|
||||||
// In each program where errors occur, this has to be set.
|
// In each program where errors occur, this has to be set.
|
||||||
ErrorInfo::programName = std::optional("error-demo");
|
ErrorInfo::programName = std::optional("error-demo");
|
||||||
|
|
||||||
// Error in a program; no hint and no nix code.
|
// Error in a program; no hint and no nix code.
|
||||||
printErrorInfo(
|
logError(
|
||||||
ErrorInfo { .level = elError,
|
ErrorInfo { .name = "name",
|
||||||
.name = "name",
|
|
||||||
.description = "error description",
|
.description = "error description",
|
||||||
});
|
});
|
||||||
|
|
||||||
// Warning with name, description, and hint.
|
// Warning with name, description, and hint.
|
||||||
// The hintfmt function makes all the substituted text yellow.
|
// The hintfmt function makes all the substituted text yellow.
|
||||||
printErrorInfo(
|
logWarning(
|
||||||
ErrorInfo { .level = elWarning,
|
ErrorInfo { .name = "name",
|
||||||
.name = "name",
|
|
||||||
.description = "error description",
|
.description = "error description",
|
||||||
.hint = std::optional(
|
.hint = hintfmt("there was a %1%", "warning"),
|
||||||
hintfmt("there was a %1%", "warning")),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,12 +35,12 @@ int main()
|
||||||
SymbolTable testTable;
|
SymbolTable testTable;
|
||||||
auto problem_file = testTable.create("myfile.nix");
|
auto problem_file = testTable.create("myfile.nix");
|
||||||
|
|
||||||
printErrorInfo(
|
logWarning(
|
||||||
ErrorInfo{
|
ErrorInfo { .name = "warning name",
|
||||||
.level = elWarning,
|
|
||||||
.name = "warning name",
|
|
||||||
.description = "warning description",
|
.description = "warning description",
|
||||||
.hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
|
.hint = hintfmt("this hint has %1% templated %2%!!",
|
||||||
|
"yellow",
|
||||||
|
"values"),
|
||||||
.nixCode = NixCode {
|
.nixCode = NixCode {
|
||||||
.errPos = Pos(problem_file, 40, 13),
|
.errPos = Pos(problem_file, 40, 13),
|
||||||
.prevLineOfCode = std::nullopt,
|
.prevLineOfCode = std::nullopt,
|
||||||
|
@ -48,12 +49,12 @@ int main()
|
||||||
}});
|
}});
|
||||||
|
|
||||||
// Error with previous and next lines of code.
|
// Error with previous and next lines of code.
|
||||||
printErrorInfo(
|
logError(
|
||||||
ErrorInfo{
|
ErrorInfo { .name = "error name",
|
||||||
.level = elError,
|
|
||||||
.name = "error name",
|
|
||||||
.description = "error description",
|
.description = "error description",
|
||||||
.hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
|
.hint = hintfmt("this hint has %1% templated %2%!!",
|
||||||
|
"yellow",
|
||||||
|
"values"),
|
||||||
.nixCode = NixCode {
|
.nixCode = NixCode {
|
||||||
.errPos = Pos(problem_file, 40, 13),
|
.errPos = Pos(problem_file, 40, 13),
|
||||||
.prevLineOfCode = std::optional("previous line of code"),
|
.prevLineOfCode = std::optional("previous line of code"),
|
||||||
|
|
|
@ -73,6 +73,10 @@ struct TunnelLogger : public Logger
|
||||||
enqueueMsg(*buf.s);
|
enqueueMsg(*buf.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logEI(const ErrorInfo & ei) override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/* startWork() means that we're starting an operation for which we
|
/* startWork() means that we're starting an operation for which we
|
||||||
want to send out stderr to the client. */
|
want to send out stderr to the client. */
|
||||||
void startWork()
|
void startWork()
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include "serialise.hh"
|
||||||
|
|
||||||
namespace nix
|
namespace nix
|
||||||
{
|
{
|
||||||
|
@ -66,25 +67,45 @@ void printCodeLines(const string &prefix, const NixCode &nixCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void printErrorInfo(const ErrorInfo &einfo)
|
std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
|
||||||
{
|
{
|
||||||
int errwidth = 80;
|
int errwidth = 80;
|
||||||
string prefix = " ";
|
string prefix = " ";
|
||||||
|
|
||||||
string levelString;
|
string levelString;
|
||||||
switch (einfo.level) {
|
switch (einfo.level) {
|
||||||
case ErrLevel::elError: {
|
case Verbosity::lvlError: {
|
||||||
levelString = ANSI_RED;
|
levelString = ANSI_RED;
|
||||||
levelString += "error:";
|
levelString += "error:";
|
||||||
levelString += ANSI_NORMAL;
|
levelString += ANSI_NORMAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ErrLevel::elWarning: {
|
case Verbosity::lvlWarn: {
|
||||||
levelString = ANSI_YELLOW;
|
levelString = ANSI_YELLOW;
|
||||||
levelString += "warning:";
|
levelString += "warning:";
|
||||||
levelString += ANSI_NORMAL;
|
levelString += ANSI_NORMAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case Verbosity::lvlInfo: {
|
||||||
|
levelString = ANSI_YELLOW;
|
||||||
|
levelString += "info:";
|
||||||
|
levelString += ANSI_NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Verbosity::lvlTalkative:
|
||||||
|
case Verbosity::lvlChatty:
|
||||||
|
case Verbosity::lvlVomit: {
|
||||||
|
levelString = ANSI_GREEN;
|
||||||
|
levelString += "info:";
|
||||||
|
levelString += ANSI_NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Verbosity::lvlDebug: {
|
||||||
|
levelString = ANSI_YELLOW;
|
||||||
|
levelString += "debug:";
|
||||||
|
levelString += ANSI_NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
levelString = fmt("invalid error level: %1%", einfo.level);
|
levelString = fmt("invalid error level: %1%", einfo.level);
|
||||||
break;
|
break;
|
||||||
|
@ -99,7 +120,7 @@ void printErrorInfo(const ErrorInfo &einfo)
|
||||||
dashes.append("-");
|
dashes.append("-");
|
||||||
|
|
||||||
// divider.
|
// divider.
|
||||||
std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL,
|
out << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL,
|
||||||
prefix,
|
prefix,
|
||||||
levelString,
|
levelString,
|
||||||
"---",
|
"---",
|
||||||
|
@ -115,32 +136,33 @@ void printErrorInfo(const ErrorInfo &einfo)
|
||||||
? string(" ") + showErrPos(einfo.nixCode->errPos)
|
? string(" ") + showErrPos(einfo.nixCode->errPos)
|
||||||
: "";
|
: "";
|
||||||
|
|
||||||
std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL,
|
out << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL,
|
||||||
prefix,
|
prefix,
|
||||||
einfo.nixCode->errPos.nixFile,
|
einfo.nixCode->errPos.nixFile,
|
||||||
eline) << std::endl;
|
eline) << std::endl;
|
||||||
std::cout << prefix << std::endl;
|
out << prefix << std::endl;
|
||||||
} else {
|
} else {
|
||||||
std::cout << fmt("%1%from command line argument", prefix) << std::endl;
|
out << fmt("%1%from command line argument", prefix) << std::endl;
|
||||||
std::cout << prefix << std::endl;
|
out << prefix << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// description
|
// description
|
||||||
std::cout << prefix << einfo.description << std::endl;
|
out << prefix << einfo.description << std::endl;
|
||||||
std::cout << prefix << std::endl;
|
out << prefix << std::endl;
|
||||||
|
|
||||||
// lines of code.
|
// lines of code.
|
||||||
if (einfo.nixCode->errLineOfCode != "") {
|
if (einfo.nixCode->errLineOfCode != "") {
|
||||||
printCodeLines(prefix, *einfo.nixCode);
|
printCodeLines(prefix, *einfo.nixCode);
|
||||||
std::cout << prefix << std::endl;
|
out << prefix << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// hint
|
// hint
|
||||||
if (einfo.hint.has_value()) {
|
if (einfo.hint.has_value()) {
|
||||||
std::cout << prefix << *einfo.hint << std::endl;
|
out << prefix << *einfo.hint << std::endl;
|
||||||
std::cout << prefix << std::endl;
|
out << prefix << std::endl;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,14 @@ namespace nix
|
||||||
{
|
{
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
elWarning,
|
lvlError = 0,
|
||||||
elError
|
lvlWarn,
|
||||||
} ErrLevel;
|
lvlInfo,
|
||||||
|
lvlTalkative,
|
||||||
|
lvlChatty,
|
||||||
|
lvlDebug,
|
||||||
|
lvlVomit
|
||||||
|
} Verbosity;
|
||||||
|
|
||||||
struct ErrPos
|
struct ErrPos
|
||||||
{
|
{
|
||||||
|
@ -101,7 +106,7 @@ inline hintformat hintfmt(const std::string & fs, const Args & ... args)
|
||||||
// ErrorInfo.
|
// ErrorInfo.
|
||||||
struct ErrorInfo
|
struct ErrorInfo
|
||||||
{
|
{
|
||||||
ErrLevel level;
|
Verbosity level;
|
||||||
string name;
|
string name;
|
||||||
string description;
|
string description;
|
||||||
std::optional<hintformat> hint;
|
std::optional<hintformat> hint;
|
||||||
|
@ -110,11 +115,7 @@ struct ErrorInfo
|
||||||
static std::optional<string> programName;
|
static std::optional<string> programName;
|
||||||
};
|
};
|
||||||
|
|
||||||
// --------------------------------------------------------
|
std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo);
|
||||||
// error printing
|
|
||||||
|
|
||||||
// just to cout for now.
|
|
||||||
void printErrorInfo(const ErrorInfo &einfo);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -57,6 +58,14 @@ public:
|
||||||
writeToStderr(prefix + filterANSIEscapes(fs.s, !tty) + "\n");
|
writeToStderr(prefix + filterANSIEscapes(fs.s, !tty) + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logEI(const ErrorInfo & ei) override
|
||||||
|
{
|
||||||
|
std::stringstream oss;
|
||||||
|
oss << ei;
|
||||||
|
|
||||||
|
log(ei.level, oss.str());
|
||||||
|
}
|
||||||
|
|
||||||
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
||||||
const std::string & s, const Fields & fields, ActivityId parent)
|
const std::string & s, const Fields & fields, ActivityId parent)
|
||||||
override
|
override
|
||||||
|
@ -135,6 +144,15 @@ struct JSONLogger : Logger
|
||||||
write(json);
|
write(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logEI(const ErrorInfo & ei) override
|
||||||
|
{
|
||||||
|
// nlohmann::json json;
|
||||||
|
// json["action"] = "msg";
|
||||||
|
// json["level"] = lvl;
|
||||||
|
// json["msg"] = fs.s;
|
||||||
|
// write(json);
|
||||||
|
}
|
||||||
|
|
||||||
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
||||||
const std::string & s, const Fields & fields, ActivityId parent) override
|
const std::string & s, const Fields & fields, ActivityId parent) override
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,19 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "types.hh"
|
#include "types.hh"
|
||||||
|
#include "error.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
lvlError = 0,
|
|
||||||
lvlWarn,
|
|
||||||
lvlInfo,
|
|
||||||
lvlTalkative,
|
|
||||||
lvlChatty,
|
|
||||||
lvlDebug,
|
|
||||||
lvlVomit
|
|
||||||
} Verbosity;
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
actUnknown = 0,
|
actUnknown = 0,
|
||||||
actCopyPath = 100,
|
actCopyPath = 100,
|
||||||
|
@ -70,6 +61,13 @@ public:
|
||||||
log(lvlInfo, fs);
|
log(lvlInfo, fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void logEI(const ErrorInfo &ei) = 0;
|
||||||
|
|
||||||
|
void logEI(Verbosity lvl, ErrorInfo ei) {
|
||||||
|
ei.level = lvl;
|
||||||
|
logEI(ei);
|
||||||
|
}
|
||||||
|
|
||||||
virtual void warn(const std::string & msg);
|
virtual void warn(const std::string & msg);
|
||||||
|
|
||||||
virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
||||||
|
@ -157,6 +155,18 @@ extern Verbosity verbosity; /* suppress msgs > this */
|
||||||
#define debug(args...) printMsg(lvlDebug, args)
|
#define debug(args...) printMsg(lvlDebug, args)
|
||||||
#define vomit(args...) printMsg(lvlVomit, args)
|
#define vomit(args...) printMsg(lvlVomit, args)
|
||||||
|
|
||||||
|
#define logErrorInfo(level, errorInfo...) \
|
||||||
|
do { \
|
||||||
|
if (level <= nix::verbosity) { \
|
||||||
|
logger->logEI(level, errorInfo); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define logError(errorInfo...) logErrorInfo(lvlError, errorInfo)
|
||||||
|
#define logWarning(errorInfo...) logErrorInfo(lvlWarn, errorInfo)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
inline void warn(const std::string & fs, const Args & ... args)
|
inline void warn(const std::string & fs, const Args & ... args)
|
||||||
{
|
{
|
||||||
|
|
|
@ -124,6 +124,12 @@ public:
|
||||||
log(*state, lvl, fs.s);
|
log(*state, lvl, fs.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logEI(const ErrorInfo &ei) override
|
||||||
|
{
|
||||||
|
auto state(state_.lock());
|
||||||
|
// log(*state, lvl, ei.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
void log(State & state, Verbosity lvl, const std::string & s)
|
void log(State & state, Verbosity lvl, const std::string & s)
|
||||||
{
|
{
|
||||||
if (state.active) {
|
if (state.active) {
|
||||||
|
|
Loading…
Reference in a new issue