2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2018-07-01 23:10:20 +02:00
|
|
|
#include <RB_INC_IOSTREAM
|
|
|
|
// <iostream> inclusion here runs std::ios_base::Init() statically as this unit
|
|
|
|
// is initialized (GNU initialization order given in Makefile).
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::log
|
2008-11-16 08:15:28 +01:00
|
|
|
{
|
2017-08-28 23:51:22 +02:00
|
|
|
// Option toggles
|
|
|
|
std::array<bool, num_of<facility>()> file_flush;
|
|
|
|
std::array<bool, num_of<facility>()> console_flush;
|
|
|
|
std::array<const char *, num_of<facility>()> console_ansi;
|
|
|
|
|
|
|
|
// Runtime master switches
|
|
|
|
std::array<bool, num_of<facility>()> file_out;
|
|
|
|
std::array<bool, num_of<facility>()> console_out;
|
|
|
|
std::array<bool, num_of<facility>()> console_err;
|
|
|
|
|
|
|
|
// Suppression state (for struct console_quiet)
|
|
|
|
std::array<bool, num_of<facility>()> quieted_out;
|
|
|
|
std::array<bool, num_of<facility>()> quieted_err;
|
|
|
|
|
|
|
|
// Logfile name and device
|
|
|
|
std::array<const char *, num_of<facility>()> fname;
|
|
|
|
std::array<std::ofstream, num_of<facility>()> file;
|
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
std::ostream &out_console
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &err_console
|
|
|
|
{
|
|
|
|
std::cerr
|
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
static void open(const facility &fac);
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::init()
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
|
|
|
//TODO: XXX: config + cmd control + other fancy stuff
|
|
|
|
|
2018-01-13 01:06:01 +01:00
|
|
|
console_out[CRITICAL] = true;
|
|
|
|
console_out[ERROR] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
console_out[DERROR] = true;
|
2018-01-13 01:06:01 +01:00
|
|
|
console_out[WARNING] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
console_out[DWARNING] = true;
|
2017-10-17 09:41:10 +02:00
|
|
|
console_out[NOTICE] = true;
|
2016-08-26 06:38:15 +02:00
|
|
|
console_out[INFO] = true;
|
2016-09-06 19:09:11 +02:00
|
|
|
console_out[DEBUG] = ircd::debugmode;
|
2016-08-26 06:38:15 +02:00
|
|
|
|
2018-01-13 01:06:01 +01:00
|
|
|
//console_err[CRITICAL] = true;
|
|
|
|
//console_err[ERROR] = true;
|
|
|
|
//console_err[WARNING] = true;
|
|
|
|
|
2016-09-06 19:09:11 +02:00
|
|
|
file_out[CRITICAL] = true;
|
|
|
|
file_out[ERROR] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
file_out[DERROR] = true;
|
2016-09-06 19:09:11 +02:00
|
|
|
file_out[WARNING] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
file_out[DWARNING] = true;
|
2016-09-06 19:09:11 +02:00
|
|
|
file_out[NOTICE] = true;
|
|
|
|
file_out[INFO] = true;
|
|
|
|
file_out[DEBUG] = ircd::debugmode;
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
file_flush[CRITICAL] = true;
|
|
|
|
file_flush[ERROR] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
file_flush[DERROR] = true;
|
2016-08-26 06:38:15 +02:00
|
|
|
file_flush[WARNING] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
file_flush[DWARNING] = true;
|
2016-08-26 06:38:15 +02:00
|
|
|
file_flush[NOTICE] = false;
|
|
|
|
file_flush[INFO] = false;
|
|
|
|
file_flush[DEBUG] = false;
|
|
|
|
|
|
|
|
console_flush[CRITICAL] = true;
|
|
|
|
console_flush[ERROR] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
console_flush[DERROR] = true;
|
2016-08-26 06:38:15 +02:00
|
|
|
console_flush[WARNING] = true;
|
2018-03-15 20:20:21 +01:00
|
|
|
console_flush[DWARNING] = true;
|
|
|
|
console_flush[NOTICE] = true;
|
|
|
|
console_flush[INFO] = true;
|
2016-08-26 06:38:15 +02:00
|
|
|
console_flush[DEBUG] = true;
|
|
|
|
|
|
|
|
console_ansi[CRITICAL] = "\033[1;5;37;45m";
|
|
|
|
console_ansi[ERROR] = "\033[1;37;41m";
|
2018-03-15 20:20:21 +01:00
|
|
|
console_ansi[DERROR] = "\033[0;31;47m";
|
2016-08-26 06:38:15 +02:00
|
|
|
console_ansi[WARNING] = "\033[0;30;43m";
|
2018-03-15 20:20:21 +01:00
|
|
|
console_ansi[DWARNING] = "\033[0;30;47m";
|
2016-08-26 06:38:15 +02:00
|
|
|
console_ansi[NOTICE] = "\033[1;37;46m";
|
|
|
|
console_ansi[INFO] = "\033[1;37;42m";
|
|
|
|
console_ansi[DEBUG] = "\033[1;30;47m";
|
2008-11-16 08:15:28 +01:00
|
|
|
}
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::fini()
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2018-02-07 21:03:56 +01:00
|
|
|
flush();
|
|
|
|
close();
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::open()
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-08-26 06:38:15 +02:00
|
|
|
for_each<facility>([](const facility &fac)
|
|
|
|
{
|
|
|
|
if(!fname[fac])
|
|
|
|
return;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-06 19:09:11 +02:00
|
|
|
if(!file_out[fac])
|
|
|
|
return;
|
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
if(file[fac].is_open())
|
|
|
|
file[fac].close();
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
file[fac].clear();
|
|
|
|
file[fac].exceptions(std::ios::badbit | std::ios::failbit);
|
|
|
|
open(fac);
|
|
|
|
});
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::close()
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
|
|
|
for_each<facility>([](const facility &fac)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-06 19:09:11 +02:00
|
|
|
if(file[fac].is_open())
|
|
|
|
file[fac].close();
|
2016-08-26 06:38:15 +02:00
|
|
|
});
|
2014-03-03 05:25:47 +01:00
|
|
|
}
|
2008-04-20 05:42:30 +02:00
|
|
|
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::flush()
|
2008-04-20 05:42:30 +02:00
|
|
|
{
|
2016-08-26 06:38:15 +02:00
|
|
|
for_each<facility>([](const facility &fac)
|
2008-04-20 05:42:30 +02:00
|
|
|
{
|
2016-08-26 06:38:15 +02:00
|
|
|
file[fac].flush();
|
|
|
|
});
|
2018-02-07 21:03:56 +01:00
|
|
|
|
|
|
|
std::flush(out_console);
|
|
|
|
std::flush(err_console);
|
2008-04-20 05:42:30 +02:00
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::open(const facility &fac)
|
2016-08-26 06:38:15 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &mode(std::ios::app);
|
|
|
|
file[fac].open(fname[fac], mode);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2016-06-23 03:23:24 +02:00
|
|
|
{
|
2018-08-16 09:19:37 +02:00
|
|
|
fprintf(stderr, "!!! Opening log file [%s] failed: %s",
|
|
|
|
fname[fac],
|
|
|
|
e.what());
|
2016-08-26 06:38:15 +02:00
|
|
|
throw;
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2018-12-06 04:00:10 +01:00
|
|
|
void
|
|
|
|
ircd::log::console_enable()
|
|
|
|
{
|
|
|
|
for_each<facility>([](const facility &fac)
|
|
|
|
{
|
|
|
|
console_enable(fac);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::log::console_disable()
|
|
|
|
{
|
|
|
|
for_each<facility>([](const facility &fac)
|
|
|
|
{
|
|
|
|
console_disable(fac);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-22 01:11:24 +01:00
|
|
|
void
|
|
|
|
ircd::log::console_enable(const facility &fac)
|
|
|
|
{
|
|
|
|
console_out[fac] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::log::console_disable(const facility &fac)
|
|
|
|
{
|
|
|
|
console_out[fac] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::log::console_enabled(const facility &fac)
|
|
|
|
{
|
|
|
|
return console_out[fac];
|
|
|
|
}
|
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
void
|
|
|
|
ircd::log::console_mask(const vector_view<string_view> &list)
|
|
|
|
{
|
|
|
|
for(auto *const &log : log::list)
|
|
|
|
log->cmasked = std::find(begin(list), end(list), log->name) != end(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::log::console_unmask(const vector_view<string_view> &list)
|
|
|
|
{
|
|
|
|
for(auto *const &log : log::list)
|
|
|
|
log->cmasked = std::find(begin(list), end(list), log->name) == end(list);
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:14:26 +02:00
|
|
|
//
|
|
|
|
// console quiet
|
|
|
|
//
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::console_quiet::console_quiet(const bool &showmsg)
|
2016-09-07 01:31:29 +02:00
|
|
|
{
|
|
|
|
if(showmsg)
|
|
|
|
notice("Log messages are now quieted at the console");
|
|
|
|
|
|
|
|
std::copy(begin(console_out), end(console_out), begin(quieted_out));
|
|
|
|
std::copy(begin(console_err), end(console_err), begin(quieted_err));
|
|
|
|
std::fill(begin(console_out), end(console_out), false);
|
|
|
|
std::fill(begin(console_err), end(console_err), false);
|
2017-10-25 18:21:08 +02:00
|
|
|
|
|
|
|
// Make a special amend to never suppress CRITICAL messages because
|
|
|
|
// these are usually for a crash or major b0rk where the console
|
|
|
|
// user probably won't be continuing normally anyway...
|
|
|
|
if(quieted_out[CRITICAL]) console_out[CRITICAL] = true;
|
|
|
|
if(quieted_err[CRITICAL]) console_err[CRITICAL] = true;
|
2016-09-07 01:31:29 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::console_quiet::~console_quiet()
|
2016-09-07 01:31:29 +02:00
|
|
|
{
|
|
|
|
std::copy(begin(quieted_out), end(quieted_out), begin(console_out));
|
|
|
|
std::copy(begin(quieted_err), end(quieted_err), begin(console_err));
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:14:26 +02:00
|
|
|
//
|
|
|
|
// mark
|
|
|
|
//
|
2016-03-29 02:06:31 +02:00
|
|
|
|
2018-04-23 23:14:26 +02:00
|
|
|
ircd::log::mark::mark(const string_view &msg)
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
|
|
|
for_each<facility>([&msg]
|
|
|
|
(const auto &fac)
|
|
|
|
{
|
|
|
|
mark(fac, msg);
|
|
|
|
});
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 23:14:26 +02:00
|
|
|
ircd::log::mark::mark(const facility &fac,
|
|
|
|
const string_view &msg)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2018-04-24 00:46:09 +02:00
|
|
|
vlog(star, fac, "%s", msg);
|
2017-10-12 02:40:49 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 23:14:26 +02:00
|
|
|
//
|
|
|
|
// log
|
|
|
|
//
|
|
|
|
|
2018-04-23 23:48:48 +02:00
|
|
|
/// Linkage for list of named loggers.
|
|
|
|
template<>
|
|
|
|
decltype(ircd::instance_list<ircd::log::log>::list)
|
|
|
|
ircd::instance_list<ircd::log::log>::list
|
|
|
|
{};
|
2018-04-23 23:14:26 +02:00
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
ircd::log::log *
|
|
|
|
ircd::log::log::find(const string_view &name)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find_if(begin(list), end(list), [&name]
|
|
|
|
(const auto *const &log)
|
|
|
|
{
|
|
|
|
return log->name == name;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::log::log *
|
|
|
|
ircd::log::log::find(const char &snote)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find_if(begin(list), end(list), [&snote]
|
|
|
|
(const auto *const &log)
|
|
|
|
{
|
|
|
|
return log->snote == snote;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is related to cross-thread logging; its purpose
|
|
|
|
/// is to check if a given named logger still exists after the pointer
|
|
|
|
/// is conveyed to the main thread from some other thread.
|
|
|
|
bool
|
|
|
|
ircd::log::log::exists(const log *const &ptr)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find_if(begin(list), end(list), [&ptr]
|
|
|
|
(const auto *const &log)
|
|
|
|
{
|
|
|
|
return log == ptr;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return it != end(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// log::log
|
|
|
|
//
|
|
|
|
|
2018-04-23 23:14:26 +02:00
|
|
|
ircd::log::log::log(const string_view &name,
|
|
|
|
const char &snote)
|
|
|
|
:name{name}
|
|
|
|
,snote{snote}
|
|
|
|
{
|
2018-04-23 23:48:48 +02:00
|
|
|
for(const auto *const &other : list)
|
|
|
|
{
|
|
|
|
if(other == this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(other->name == name)
|
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"Logger with name '%s' already exists at %p",
|
|
|
|
name,
|
|
|
|
other
|
|
|
|
};
|
|
|
|
|
|
|
|
if(snote != '\0' && other->snote == snote)
|
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"Logger with snote '%c' is '%s' and already exists at %p",
|
|
|
|
snote,
|
2018-04-25 02:56:43 +02:00
|
|
|
other->name,
|
2018-04-23 23:48:48 +02:00
|
|
|
other
|
|
|
|
};
|
|
|
|
}
|
2018-04-23 23:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// vlog
|
|
|
|
//
|
|
|
|
|
2017-10-25 22:42:41 +02:00
|
|
|
namespace ircd::log
|
|
|
|
{
|
|
|
|
static void check(std::ostream &) noexcept;
|
2018-04-24 00:46:09 +02:00
|
|
|
static void slog(const log &, const facility &, const window_buffer::closure &) noexcept;
|
2018-12-10 22:00:33 +01:00
|
|
|
static void vlog_threadsafe(const log &, const facility &, const string_view &fmt, const va_rtti &ap);
|
2017-10-25 22:42:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
decltype(ircd::log::star)
|
|
|
|
ircd::log::star
|
|
|
|
{
|
|
|
|
"*", '*'
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::log::general)
|
|
|
|
ircd::log::general
|
|
|
|
{
|
|
|
|
"ircd", 'G'
|
|
|
|
};
|
|
|
|
|
2017-10-12 02:40:49 +02:00
|
|
|
/// ircd::log is not thread-safe. This internal function is called when the
|
|
|
|
/// normal vlog() detects it's not on the main IRCd thread. It then generates
|
|
|
|
/// the formatted log message on this thread, and posts the message to the
|
|
|
|
/// main IRCd event loop which is running on the main thread.
|
|
|
|
void
|
2018-04-24 00:46:09 +02:00
|
|
|
ircd::log::vlog_threadsafe(const log &log,
|
|
|
|
const facility &fac,
|
2018-12-10 22:00:33 +01:00
|
|
|
const string_view &fmt,
|
2017-10-12 02:40:49 +02:00
|
|
|
const va_rtti &ap)
|
|
|
|
{
|
|
|
|
// Generate the formatted message on this thread first
|
2017-10-17 09:41:10 +02:00
|
|
|
std::string str
|
2017-10-12 02:40:49 +02:00
|
|
|
{
|
|
|
|
fmt::vsnstringf(1024, fmt, ap)
|
|
|
|
};
|
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
// The pointer to the logger is copied to the main thread.
|
|
|
|
auto *const logp{&log};
|
|
|
|
ircd::post([fac, str(std::move(str)), logp]
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2018-04-24 00:46:09 +02:00
|
|
|
// If that named logger was destroyed while this closure was
|
|
|
|
// travelling to the main thread then we just discard this message.
|
|
|
|
if(!log::exists(logp))
|
|
|
|
return;
|
|
|
|
|
|
|
|
slog(*logp, fac, [&str](const mutable_buffer &out) -> size_t
|
2017-10-12 02:40:49 +02:00
|
|
|
{
|
2017-12-23 22:06:12 +01:00
|
|
|
return copy(out, string_view{str});
|
2017-10-12 02:40:49 +02:00
|
|
|
});
|
2016-08-26 06:38:15 +02:00
|
|
|
});
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
ircd::log::vlog::vlog(const log &log,
|
|
|
|
const facility &fac,
|
2018-12-10 22:00:33 +01:00
|
|
|
const string_view &fmt,
|
2018-04-23 23:14:26 +02:00
|
|
|
const va_rtti &ap)
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2017-10-12 02:40:49 +02:00
|
|
|
if(!is_main_thread())
|
|
|
|
{
|
2018-04-24 00:46:09 +02:00
|
|
|
vlog_threadsafe(log, fac, fmt, ap);
|
2017-10-12 02:40:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
slog(log, fac, [&fmt, &ap](const mutable_buffer &out) -> size_t
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2017-12-23 22:06:12 +01:00
|
|
|
return fmt::vsprintf(out, fmt, ap);
|
2017-10-17 09:41:10 +02:00
|
|
|
});
|
2017-10-16 06:27:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-23 07:54:32 +01:00
|
|
|
namespace ircd::log
|
|
|
|
{
|
|
|
|
// linkage for slog() reentrance assertion
|
|
|
|
bool entered;
|
|
|
|
}
|
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
void
|
2018-04-24 00:46:09 +02:00
|
|
|
ircd::log::slog(const log &log,
|
|
|
|
const facility &fac,
|
2018-02-08 06:30:20 +01:00
|
|
|
const window_buffer::closure &closure)
|
2017-10-17 09:41:10 +02:00
|
|
|
noexcept
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2018-04-24 00:46:09 +02:00
|
|
|
// When all of these conditions are true there is no possible log output
|
|
|
|
// so we can bail real quick.
|
2016-08-26 06:38:15 +02:00
|
|
|
if(!file[fac].is_open() && !console_out[fac] && !console_err[fac])
|
|
|
|
return;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2018-04-24 00:46:09 +02:00
|
|
|
// Same for this set of conditions...
|
|
|
|
if((!file[fac].is_open() || !log.fmasked) && !log.cmasked)
|
|
|
|
return;
|
|
|
|
|
2017-12-23 07:54:32 +01:00
|
|
|
// Have to be on the main thread to call slog().
|
2017-10-12 02:40:49 +02:00
|
|
|
assert_main_thread();
|
2017-12-23 07:54:32 +01:00
|
|
|
|
|
|
|
// During the composition of this log message, if another log message
|
|
|
|
// is created from calls for normal reasons or from errors, it's a
|
|
|
|
// problem too. We can only have one log slog() at a time for now...
|
|
|
|
const reentrance_assertion<entered> ra;
|
|
|
|
|
|
|
|
// If slog() yields for some reason that's not good either...
|
2017-10-17 09:41:10 +02:00
|
|
|
const ctx::critical_assertion ca;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2017-12-23 07:54:32 +01:00
|
|
|
// The principal buffer doesn't have to be static but courtesy of all the
|
|
|
|
// above effort we might as well take advantage...
|
2017-12-23 22:06:12 +01:00
|
|
|
static char buf[1024], date[64];
|
2017-11-25 22:19:32 +01:00
|
|
|
|
2017-12-23 22:06:12 +01:00
|
|
|
// Maximum size of log line leaving 2 characters for \r\n
|
|
|
|
const size_t max(sizeof(buf) - 2);
|
|
|
|
|
|
|
|
// Compose the prefix sequence into the buffer through stringstream
|
2017-10-25 22:42:41 +02:00
|
|
|
std::stringstream s;
|
2018-05-04 05:08:24 +02:00
|
|
|
pubsetbuf(s, buf);
|
2017-10-25 22:42:41 +02:00
|
|
|
s << microtime(date)
|
|
|
|
<< ' '
|
|
|
|
<< (console_ansi[fac]? console_ansi[fac] : "")
|
|
|
|
<< std::setw(8)
|
|
|
|
<< std::right
|
|
|
|
<< reflect(fac)
|
2017-12-23 22:06:12 +01:00
|
|
|
<< (console_ansi[fac]? "\033[0m " : " ")
|
2018-04-24 00:46:09 +02:00
|
|
|
// << (log.snote? log.snote : '-')
|
2017-12-23 22:06:12 +01:00
|
|
|
<< std::setw(9)
|
|
|
|
<< std::right
|
2018-04-24 00:46:09 +02:00
|
|
|
<< log.name
|
2017-12-23 22:06:12 +01:00
|
|
|
<< ' '
|
|
|
|
<< std::setw(8)
|
|
|
|
<< trunc(ctx::name(), 8)
|
|
|
|
<< ' '
|
|
|
|
<< std::setw(6)
|
|
|
|
<< std::right
|
|
|
|
<< ctx::id()
|
|
|
|
<< " :";
|
|
|
|
|
|
|
|
// Compose the user message after prefix
|
|
|
|
const size_t pos(s.tellp());
|
|
|
|
const mutable_buffer userspace{buf + pos, max - pos};
|
2018-02-08 06:30:20 +01:00
|
|
|
window_buffer sb{userspace};
|
2017-12-23 22:06:12 +01:00
|
|
|
sb(closure);
|
|
|
|
|
|
|
|
// Compose the newline after user message.
|
|
|
|
size_t len{pos + sb.consumed()};
|
|
|
|
assert(len + 2 <= sizeof(buf));
|
|
|
|
buf[len++] = '\r';
|
|
|
|
buf[len++] = '\n';
|
|
|
|
|
|
|
|
// Closure to copy the message to various places
|
|
|
|
assert(len <= sizeof(buf));
|
|
|
|
const string_view msg{buf, len};
|
|
|
|
const auto write{[&msg](std::ostream &s)
|
2017-10-25 22:42:41 +02:00
|
|
|
{
|
|
|
|
check(s);
|
2017-12-23 22:06:12 +01:00
|
|
|
s.write(data(msg), size(msg));
|
2017-10-25 22:42:41 +02:00
|
|
|
}};
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2017-11-25 22:19:32 +01:00
|
|
|
// copy to std::cerr
|
2018-04-24 00:46:09 +02:00
|
|
|
if(log.cmasked && console_err[fac])
|
2017-10-25 22:42:41 +02:00
|
|
|
{
|
2017-10-27 00:31:50 +02:00
|
|
|
err_console.clear();
|
2017-10-25 22:42:41 +02:00
|
|
|
write(err_console);
|
|
|
|
}
|
2016-08-26 06:38:15 +02:00
|
|
|
|
2017-11-25 22:19:32 +01:00
|
|
|
// copy to std::cout
|
2018-04-24 00:46:09 +02:00
|
|
|
if(log.cmasked && console_out[fac])
|
2017-10-25 22:42:41 +02:00
|
|
|
{
|
2017-10-27 00:31:50 +02:00
|
|
|
out_console.clear();
|
2017-10-25 22:42:41 +02:00
|
|
|
write(out_console);
|
|
|
|
if(console_flush[fac])
|
|
|
|
std::flush(out_console);
|
|
|
|
}
|
2016-08-26 06:38:15 +02:00
|
|
|
|
2017-11-25 22:19:32 +01:00
|
|
|
// copy to file
|
2018-04-24 00:46:09 +02:00
|
|
|
if(log.fmasked && file[fac].is_open())
|
2017-10-25 22:42:41 +02:00
|
|
|
{
|
2017-10-27 00:31:50 +02:00
|
|
|
file[fac].clear();
|
2017-10-25 22:42:41 +02:00
|
|
|
write(file[fac]);
|
|
|
|
if(file_flush[fac])
|
|
|
|
std::flush(file[fac]);
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
void
|
|
|
|
ircd::log::check(std::ostream &s)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
if(likely(s.good()))
|
2016-08-26 06:38:15 +02:00
|
|
|
return;
|
2016-06-23 03:23:24 +02:00
|
|
|
|
2018-08-16 09:19:37 +02:00
|
|
|
thread_local char buf[128];
|
2017-10-17 09:41:10 +02:00
|
|
|
snprintf(buf, sizeof(buf), "fatal: log stream good[%d] bad[%d] fail[%d] eof[%d]",
|
|
|
|
s.good(),
|
|
|
|
s.bad(),
|
|
|
|
s.fail(),
|
|
|
|
s.eof());
|
|
|
|
|
|
|
|
fprintf(stderr, "log stream(%p) fatal: %s\n", (const void *)&s, buf);
|
|
|
|
fprintf(stdout, "log stream(%p) fatal: %s\n", (const void *)&s, buf);
|
2017-10-27 00:31:50 +02:00
|
|
|
fflush(stderr);
|
|
|
|
fflush(stdout);
|
2017-10-17 09:41:10 +02:00
|
|
|
s.exceptions(s.eofbit | s.failbit | s.badbit);
|
|
|
|
throw std::runtime_error(buf);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", e.what());
|
|
|
|
fprintf(stdout, "%s\n", e.what());
|
2017-10-27 00:31:50 +02:00
|
|
|
fflush(stderr);
|
|
|
|
fflush(stdout);
|
2017-11-26 01:20:42 +01:00
|
|
|
ircd::terminate();
|
2016-08-26 06:38:15 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 17:47:13 +01:00
|
|
|
ircd::log::facility
|
|
|
|
ircd::log::reflect(const string_view &f)
|
|
|
|
{
|
|
|
|
if(f == "CRITICAL") return facility::CRITICAL;
|
|
|
|
if(f == "ERROR") return facility::ERROR;
|
|
|
|
if(f == "DERROR") return facility::DERROR;
|
|
|
|
if(f == "DWARNING") return facility::DWARNING;
|
|
|
|
if(f == "WARNING") return facility::WARNING;
|
|
|
|
if(f == "NOTICE") return facility::NOTICE;
|
|
|
|
if(f == "INFO") return facility::INFO;
|
|
|
|
if(f == "DEBUG") return facility::DEBUG;
|
|
|
|
|
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"'%s' is not a recognized log facility", f
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::reflect(const facility &f)
|
2016-06-23 03:23:24 +02:00
|
|
|
{
|
2016-08-26 06:38:15 +02:00
|
|
|
switch(f)
|
|
|
|
{
|
2018-03-15 20:20:21 +01:00
|
|
|
case facility::CRITICAL: return "CRITICAL";
|
|
|
|
case facility::ERROR: return "ERROR";
|
|
|
|
case facility::DERROR: return "ERROR";
|
|
|
|
case facility::WARNING: return "WARNING";
|
|
|
|
case facility::DWARNING: return "WARNING";
|
2016-08-26 06:38:15 +02:00
|
|
|
case facility::INFO: return "INFO";
|
|
|
|
case facility::NOTICE: return "NOTICE";
|
2018-03-15 20:20:21 +01:00
|
|
|
case facility::DEBUG: return "DEBUG";
|
2016-08-26 06:38:15 +02:00
|
|
|
case facility::_NUM_: break; // Allows -Wswitch to remind developer to add reflection here
|
|
|
|
};
|
|
|
|
|
2018-12-12 17:47:13 +01:00
|
|
|
throw assertive
|
|
|
|
{
|
|
|
|
"'%d' is not a recognized log facility", int(f)
|
|
|
|
};
|
2016-06-23 03:23:24 +02:00
|
|
|
}
|
2016-08-13 05:05:54 +02:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
const char *
|
|
|
|
ircd::smalldate(const time_t <ime)
|
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
static const size_t MAX_DATE_STRING
|
|
|
|
{
|
|
|
|
32 // maximum string length for a date string (ircd_defs.h)
|
|
|
|
};
|
2016-08-26 06:38:15 +02:00
|
|
|
|
2017-10-12 02:47:03 +02:00
|
|
|
struct tm lt;
|
|
|
|
localtime_r(<ime, <);
|
2018-10-18 22:23:24 +02:00
|
|
|
thread_local char buf[MAX_DATE_STRING];
|
2016-08-26 06:38:15 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
|
2017-10-12 02:47:03 +02:00
|
|
|
lt.tm_year + 1900,
|
|
|
|
lt.tm_mon + 1,
|
|
|
|
lt.tm_mday,
|
|
|
|
lt.tm_hour,
|
|
|
|
lt.tm_min);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|