2016-04-25 16:26:07 +03:00
|
|
|
#include "logging.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2017-04-12 15:52:49 +03:00
|
|
|
Logger * logger = makeDefaultLogger();
|
2016-04-25 16:26:07 +03:00
|
|
|
|
|
|
|
class SimpleLogger : public Logger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
bool systemd, tty;
|
|
|
|
|
|
|
|
SimpleLogger()
|
|
|
|
{
|
|
|
|
systemd = getEnv("IN_SYSTEMD") == "1";
|
|
|
|
tty = isatty(STDERR_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log(Verbosity lvl, const FormatOrString & fs) override
|
|
|
|
{
|
|
|
|
if (lvl > verbosity) return;
|
|
|
|
|
|
|
|
std::string prefix;
|
|
|
|
|
|
|
|
if (systemd) {
|
|
|
|
char c;
|
|
|
|
switch (lvl) {
|
|
|
|
case lvlError: c = '3'; break;
|
|
|
|
case lvlInfo: c = '5'; break;
|
|
|
|
case lvlTalkative: case lvlChatty: c = '6'; break;
|
|
|
|
default: c = '7';
|
|
|
|
}
|
|
|
|
prefix = std::string("<") + c + ">";
|
|
|
|
}
|
|
|
|
|
|
|
|
writeToStderr(prefix + (tty ? fs.s : filterANSIEscapes(fs.s)) + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void startActivity(Activity & activity, Verbosity lvl, const FormatOrString & fs) override
|
|
|
|
{
|
|
|
|
log(lvl, fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopActivity(Activity & activity) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Verbosity verbosity = lvlInfo;
|
|
|
|
|
|
|
|
void warnOnce(bool & haveWarned, const FormatOrString & fs)
|
|
|
|
{
|
|
|
|
if (!haveWarned) {
|
2016-09-21 17:11:01 +03:00
|
|
|
printError(format("warning: %1%") % fs.s);
|
2016-04-25 16:26:07 +03:00
|
|
|
haveWarned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeToStderr(const string & s)
|
|
|
|
{
|
|
|
|
try {
|
2016-09-16 19:52:42 +03:00
|
|
|
writeFull(STDERR_FILENO, s, false);
|
2016-04-25 16:26:07 +03:00
|
|
|
} catch (SysError & e) {
|
2016-09-16 19:52:42 +03:00
|
|
|
/* Ignore failing writes to stderr. We need to ignore write
|
|
|
|
errors to ensure that cleanup code that logs to stderr runs
|
|
|
|
to completion if the other side of stderr has been closed
|
|
|
|
unexpectedly. */
|
2016-04-25 16:26:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger * makeDefaultLogger()
|
|
|
|
{
|
|
|
|
return new SimpleLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|