2007-01-25 07:40:21 +01:00
|
|
|
/*
|
2008-04-03 04:40:54 +02:00
|
|
|
* charybdis: an advanced Internet Relay Chat Daemon(ircd).
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Lee H <lee@leeh.co.uk>
|
|
|
|
* Copyright (C) 2003-2005 ircd-ratbox development team
|
2008-04-03 04:40:54 +02:00
|
|
|
* Copyright (C) 2008 William Pitcock <nenolod@sacredspiral.co.uk>
|
2016-08-26 06:38:15 +02:00
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* 1.Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
2008-04-03 04:40:54 +02:00
|
|
|
*
|
2007-01-25 07:40:21 +01:00
|
|
|
* 2.Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-03 04:40:54 +02:00
|
|
|
*
|
2007-01-25 07:40:21 +01:00
|
|
|
* 3.The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
ConfEntry conf_log_table[] =
|
|
|
|
{
|
|
|
|
{ "file_critical", CF_QSTRING, NULL, PATH_MAX, &fname[CRITICAL] },
|
|
|
|
{ "file_error", CF_QSTRING, NULL, PATH_MAX, &fname[ERROR] },
|
|
|
|
{ "file_warning", CF_QSTRING, NULL, PATH_MAX, &fname[WARNING] },
|
|
|
|
{ "file_notice", CF_QSTRING, NULL, PATH_MAX, &fname[NOTICE] },
|
|
|
|
{ "file_info", CF_QSTRING, NULL, PATH_MAX, &fname[INFO] },
|
|
|
|
{ "file_debug", CF_QSTRING, NULL, PATH_MAX, &fname[DEBUG] },
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
static void check(std::ostream &) noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
static void open(const facility &fac);
|
2017-10-12 02:47:03 +02:00
|
|
|
static void prefix(const facility &fac, const string_view &date);
|
2017-08-28 23:51:22 +02:00
|
|
|
static void suffix(const facility &fac);
|
2017-10-17 09:41:10 +02:00
|
|
|
static std::ostream &compose(std::ostream &s, const string_view &buf, const string_view &name);
|
|
|
|
static void slog(const facility &fac, const std::function<void (std::ostream &)> &closure) noexcept;
|
|
|
|
static void vlog_threadsafe(const facility &fac, const std::string &name, const char *const &fmt, const va_rtti &ap);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
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
|
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
//add_top_conf("log", NULL, NULL, conf_log_table);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
console_err[CRITICAL] = true;
|
|
|
|
console_err[ERROR] = true;
|
|
|
|
console_err[WARNING] = 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
|
|
|
|
2016-09-06 19:09:11 +02:00
|
|
|
file_out[CRITICAL] = true;
|
|
|
|
file_out[ERROR] = true;
|
|
|
|
file_out[WARNING] = true;
|
|
|
|
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;
|
|
|
|
file_flush[WARNING] = true;
|
|
|
|
file_flush[NOTICE] = false;
|
|
|
|
file_flush[INFO] = false;
|
|
|
|
file_flush[DEBUG] = false;
|
|
|
|
|
|
|
|
console_flush[CRITICAL] = true;
|
|
|
|
console_flush[ERROR] = true;
|
|
|
|
console_flush[WARNING] = true;
|
|
|
|
console_flush[NOTICE] = false;
|
|
|
|
console_flush[INFO] = false;
|
|
|
|
console_flush[DEBUG] = true;
|
|
|
|
|
|
|
|
console_ansi[CRITICAL] = "\033[1;5;37;45m";
|
|
|
|
console_ansi[ERROR] = "\033[1;37;41m";
|
|
|
|
console_ansi[WARNING] = "\033[0;30;43m";
|
|
|
|
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
|
|
|
{
|
2016-08-31 12:55:27 +02:00
|
|
|
//remove_top_conf("log");
|
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();
|
|
|
|
});
|
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
|
|
|
{
|
|
|
|
char buf[BUFSIZE];
|
2016-08-26 06:38:15 +02:00
|
|
|
snprintf(buf, sizeof(buf), "!!! Opening log file [%s] failed: %s",
|
|
|
|
fname[fac],
|
|
|
|
e.what());
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
std::cerr << buf << std::endl;
|
|
|
|
throw;
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
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-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));
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::log::log(const std::string &name)
|
2016-08-26 06:38:15 +02:00
|
|
|
:name{name}
|
|
|
|
{
|
|
|
|
}
|
2016-03-29 02:06:31 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::log::log(const std::string &name,
|
2016-08-26 06:38:15 +02:00
|
|
|
const char &snote)
|
2016-11-29 16:23:38 +01:00
|
|
|
:log{name}
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2016-03-29 02:06:31 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::mark(const char *const &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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::mark(const facility &fac,
|
|
|
|
const char *const &msg)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2017-10-12 02:40:49 +02:00
|
|
|
static const auto name{"*"s};
|
2017-10-16 06:27:36 +02:00
|
|
|
vlog(fac, name, "%s", msg);
|
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
|
|
|
|
ircd::log::vlog_threadsafe(const facility &fac,
|
|
|
|
const std::string &name,
|
|
|
|
const char *const &fmt,
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The reference to name has to be safely maintained
|
2017-10-17 09:41:10 +02:00
|
|
|
ircd::post([fac, str(std::move(str)), name(std::move(name))]
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2017-10-17 09:41:10 +02:00
|
|
|
slog(fac, [&str, &name](std::ostream &s)
|
2017-10-12 02:40:49 +02:00
|
|
|
{
|
2017-10-17 09:41:10 +02:00
|
|
|
compose(s, str, name);
|
2017-10-12 02:40:49 +02:00
|
|
|
});
|
2016-08-26 06:38:15 +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::vlog(const facility &fac,
|
|
|
|
const char *const &fmt,
|
|
|
|
const va_rtti &ap)
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2017-10-12 02:40:49 +02:00
|
|
|
static const auto name{"ircd"s};
|
|
|
|
vlog(fac, name, fmt, ap);
|
2016-08-26 06:38:15 +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::vlog(const facility &fac,
|
|
|
|
const std::string &name,
|
|
|
|
const char *const &fmt,
|
|
|
|
const va_rtti &ap)
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2017-10-12 02:40:49 +02:00
|
|
|
if(!is_main_thread())
|
|
|
|
{
|
|
|
|
vlog_threadsafe(fac, name, fmt, ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
char buf[1024];
|
2017-10-17 09:41:10 +02:00
|
|
|
const auto len
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
2017-10-17 09:41:10 +02:00
|
|
|
fmt::vsprintf(buf, fmt, ap)
|
|
|
|
};
|
2017-10-16 06:27:36 +02:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
const string_view msg{buf, size_t(len)};
|
|
|
|
slog(fac, [&msg, &name](std::ostream &s)
|
|
|
|
{
|
|
|
|
compose(s, msg, name);
|
|
|
|
});
|
2017-10-16 06:27:36 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::slog(const facility &fac,
|
|
|
|
const std::function<void (std::ostream &)> &closure)
|
2017-10-17 09:41:10 +02:00
|
|
|
noexcept
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
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
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
// Have to be on the main thread to call slog(). If slog() yields for some
|
|
|
|
// reason it's a problem too. During the composition of this log message,
|
|
|
|
// if another log message is created from calls for normal reasons or from
|
|
|
|
// errors, that's not good either. We can only have one log slog() at a
|
|
|
|
// time for now...
|
2017-10-12 02:40:49 +02:00
|
|
|
assert_main_thread();
|
2017-10-17 09:41:10 +02:00
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
static bool entered;
|
|
|
|
assert(!entered);
|
|
|
|
entered = true;
|
|
|
|
const unwind leaving([&fac]
|
2016-08-26 06:38:15 +02:00
|
|
|
{
|
|
|
|
suffix(fac);
|
2017-10-17 09:41:10 +02:00
|
|
|
entered = false;
|
2016-08-26 06:38:15 +02:00
|
|
|
});
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
//TODO: XXX: Add option toggle for smalldate()
|
|
|
|
char date[64];
|
2017-10-12 02:47:03 +02:00
|
|
|
prefix(fac, microtime(date));
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-26 06:38:15 +02:00
|
|
|
if(console_err[fac])
|
2017-10-17 09:41:10 +02:00
|
|
|
closure(err_console);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
if(console_out[fac])
|
2017-10-17 09:41:10 +02:00
|
|
|
closure(out_console);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
if(file[fac].is_open())
|
|
|
|
closure(file[fac]);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::log::compose(std::ostream &s,
|
|
|
|
const string_view &buf,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
assert(s.good());
|
|
|
|
|
|
|
|
s << std::setw(9)
|
|
|
|
<< std::right
|
|
|
|
<< name
|
|
|
|
<< ' '
|
|
|
|
<< std::setw(8)
|
|
|
|
<< trunc(ctx::name(), 8)
|
|
|
|
<< ' '
|
|
|
|
<< std::setw(6)
|
|
|
|
<< std::right
|
|
|
|
<< ctx::id()
|
|
|
|
<< " :"
|
|
|
|
<< buf;
|
|
|
|
|
|
|
|
assert(s.good());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::prefix(const facility &fac,
|
2017-10-12 02:47:03 +02:00
|
|
|
const string_view &date)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-08-26 06:38:15 +02:00
|
|
|
const auto console_prefix([&fac, &date]
|
|
|
|
(auto &stream)
|
|
|
|
{
|
2017-10-17 09:41:10 +02:00
|
|
|
check(stream);
|
|
|
|
|
|
|
|
stream << date
|
|
|
|
<< ' '
|
|
|
|
<< (console_ansi[fac]? console_ansi[fac] : "");
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
assert(stream.good());
|
2011-10-21 23:21:22 +02:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
stream << std::setw(8)
|
|
|
|
<< std::right
|
|
|
|
<< reflect(fac)
|
|
|
|
<< (console_ansi[fac]? "\033[0m " : " ");
|
2016-06-23 03:23:24 +02:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
assert(stream.good());
|
2016-08-26 06:38:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(console_err[fac])
|
2017-10-17 09:41:10 +02:00
|
|
|
console_prefix(err_console);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
if(console_out[fac])
|
2017-10-17 09:41:10 +02:00
|
|
|
console_prefix(out_console);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
if(file[fac].good())
|
|
|
|
file[fac] << date << ' ' << reflect(fac) << ' ';
|
2016-08-26 06:38:15 +02:00
|
|
|
}
|
2016-06-23 03:23:24 +02:00
|
|
|
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::log::suffix(const facility &fac)
|
2016-06-23 03:23:24 +02:00
|
|
|
{
|
2016-08-26 06:38:15 +02:00
|
|
|
const auto console_newline([&fac]
|
|
|
|
(auto &stream)
|
|
|
|
{
|
2017-10-17 09:41:10 +02:00
|
|
|
assert(stream.good());
|
|
|
|
|
|
|
|
stream << "\r\n";
|
|
|
|
assert(stream.good());
|
|
|
|
|
|
|
|
if(console_flush[fac] || std::current_exception())
|
|
|
|
{
|
|
|
|
std::flush(stream);
|
|
|
|
assert(stream.good());
|
|
|
|
}
|
2016-08-26 06:38:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(console_err[fac])
|
2017-10-17 09:41:10 +02:00
|
|
|
console_newline(err_console);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
|
|
|
if(console_out[fac])
|
2017-10-17 09:41:10 +02:00
|
|
|
console_newline(out_console);
|
2016-08-26 06:38:15 +02:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
if(file[fac].is_open() && file[fac].good() && !std::current_exception())
|
|
|
|
{
|
|
|
|
file[fac] << '\n';
|
2016-06-23 03:23:24 +02:00
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
if(file_flush[fac])
|
|
|
|
file[fac].flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-10-17 09:41:10 +02:00
|
|
|
char buf[128];
|
|
|
|
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);
|
|
|
|
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());
|
|
|
|
std::terminate();
|
2016-08-26 06:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
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)
|
|
|
|
{
|
|
|
|
case facility::DEBUG: return "DEBUG";
|
|
|
|
case facility::INFO: return "INFO";
|
|
|
|
case facility::NOTICE: return "NOTICE";
|
|
|
|
case facility::WARNING: return "WARNING";
|
|
|
|
case facility::ERROR: return "ERROR";
|
|
|
|
case facility::CRITICAL: return "CRITICAL";
|
|
|
|
case facility::_NUM_: break; // Allows -Wswitch to remind developer to add reflection here
|
|
|
|
};
|
|
|
|
|
|
|
|
return "??????";
|
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, <);
|
2016-11-29 16:23:38 +01:00
|
|
|
static 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;
|
|
|
|
}
|