0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-03 01:09:05 +02:00
construct/include/ircd/logger.h

385 lines
9.3 KiB
C
Raw Normal View History

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.
2016-08-13 05:05:54 +02:00
#pragma once
#define HAVE_IRCD_LOGGER_H
2016-08-26 06:38:15 +02:00
// windows.h #define conflicts with our facility
#ifdef HAVE_WINDOWS_H
#undef ERROR
#endif
namespace ircd
{
const char *smalldate(const time_t &);
}
/// Logging system
namespace ircd::log
{
enum facility :int;
const char *reflect(const facility &);
struct log;
struct vlog;
struct mark;
struct console_quiet;
struct critical;
struct error;
struct derror;
struct warning;
struct dwarning;
struct notice;
struct info;
struct debug;
bool console_enabled(const facility &);
void console_disable(const facility &);
void console_enable(const facility &);
void flush();
void close();
void open();
void init();
void fini();
}
2016-08-26 06:38:15 +02:00
enum ircd::log::facility
:int
2016-08-26 06:38:15 +02:00
{
CRITICAL = 0, ///< Catastrophic/unrecoverable; program is in a compromised state.
ERROR = 1, ///< Things that shouldn't happen; user impacted and should know.
DERROR = 2, ///< An error but only worthy of developers in debug mode.
WARNING = 3, ///< Non-impacting undesirable behavior user should know about.
DWARNING = 4, ///< A warning but only for developers in debug mode.
NOTICE = 5, ///< An infrequent important message with neutral or positive news.
INFO = 6, ///< A more frequent message with good news.
DEBUG = 7, ///< Maximum verbosity for developers.
2016-08-26 06:38:15 +02:00
_NUM_
};
/// A named logger. Create an instance of this to help categorize log messages.
/// All messages sent to this logger will be prefixed with the given name.
/// Admins will use this to create masks to filter log messages. Instances
/// of this class are registered with instance_list for de-confliction and
/// iteration, so the recommended duration of this class is static.
struct ircd::log::log
:instance_list<log>
2016-08-26 06:38:15 +02:00
{
string_view name;
char snote;
2016-08-26 06:38:15 +02:00
public:
2017-03-18 04:24:25 +01:00
template<class... args> void operator()(const facility &, const char *const &fmt, args&&...);
template<class... args> void critical(const char *const &fmt, args&&...);
template<class... args> void error(const char *const &fmt, args&&...);
template<class... args> void warning(const char *const &fmt, args&&...);
template<class... args> void notice(const char *const &fmt, args&&...);
template<class... args> void info(const char *const &fmt, args&&...);
#ifdef RB_DEBUG
template<class... args> void derror(const char *const &fmt, args&&...);
template<class... args> void dwarning(const char *const &fmt, args&&...);
2017-03-18 04:24:25 +01:00
template<class... args> void debug(const char *const &fmt, args&&...);
#else // Required for DCE in gcc 6.3.0 20170519
void derror(const char *const &fmt, ...);
void dwarning(const char *const &fmt, ...);
void debug(const char *const &fmt, ...);
#endif
2016-08-26 06:38:15 +02:00
log(const string_view &name, const char &snote = '\0');
2016-08-26 06:38:15 +02:00
};
struct ircd::log::vlog
{
vlog(const facility &, const string_view &name, const char *const &fmt, const va_rtti &ap);
vlog(const facility &, const char *const &fmt, const va_rtti &ap);
};
struct ircd::log::mark
{
mark(const facility &, const string_view &msg = {});
mark(const string_view &msg = {});
};
struct ircd::log::console_quiet
{
console_quiet(const bool &showmsg = true);
~console_quiet();
};
#ifdef RB_DEBUG
struct ircd::log::debug
2017-03-18 04:24:25 +01:00
{
template<class... args>
debug(const char *const &fmt, args&&... a)
{
vlog(facility::DEBUG, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
debug(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::DEBUG, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
#else
struct ircd::log::debug
{
debug(const char *const &, ...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
debug(const log &, const char *const &, ...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
};
#endif
2016-08-26 06:38:15 +02:00
struct ircd::log::info
2016-08-26 06:38:15 +02:00
{
template<class... args>
info(const char *const &fmt, args&&... a)
{
vlog(facility::INFO, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
info(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::INFO, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
2017-03-18 04:24:25 +01:00
struct ircd::log::notice
2017-03-18 04:24:25 +01:00
{
template<class... args>
notice(const char *const &fmt, args&&... a)
{
vlog(facility::NOTICE, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
notice(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::NOTICE, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
2017-03-18 04:24:25 +01:00
#ifdef RB_DEBUG
struct ircd::log::dwarning
{
template<class... args>
dwarning(const char *const &fmt, args&&... a)
{
vlog(facility::DWARNING, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
dwarning(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::DWARNING, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
#else
struct ircd::log::dwarning
{
dwarning(const char *const &, ...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
dwarning(const log &, const char *const &, ...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
};
#endif
struct ircd::log::warning
2017-03-18 04:24:25 +01:00
{
template<class... args>
warning(const char *const &fmt, args&&... a)
{
vlog(facility::WARNING, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
warning(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::WARNING, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
2016-08-26 06:38:15 +02:00
#ifdef RB_DEBUG
struct ircd::log::derror
{
template<class... args>
derror(const char *const &fmt, args&&... a)
{
vlog(facility::DERROR, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
derror(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::DERROR, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
#else
struct ircd::log::derror
{
derror(const char *const &, ...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
derror(const log &, const char *const &, ...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
};
#endif
struct ircd::log::error
2016-08-26 06:38:15 +02:00
{
template<class... args>
error(const char *const &fmt, args&&... a)
{
vlog(facility::ERROR, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
error(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::ERROR, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
2017-03-18 04:24:25 +01:00
struct ircd::log::critical
2016-08-26 06:38:15 +02:00
{
template<class... args>
critical(const char *const &fmt, args&&... a)
{
vlog(facility::CRITICAL, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
critical(const log &log, const char *const &fmt, args&&... a)
{
vlog(facility::CRITICAL, log.name, fmt, va_rtti{std::forward<args>(a)...});
}
};
2017-03-18 04:24:25 +01:00
#ifdef RB_DEBUG
2017-03-18 04:24:25 +01:00
template<class... args>
void
ircd::log::log::debug(const char *const &fmt,
args&&... a)
2016-08-26 06:38:15 +02:00
{
2017-03-18 04:24:25 +01:00
operator()(facility::DEBUG, fmt, va_rtti{std::forward<args>(a)...});
2016-08-26 06:38:15 +02:00
}
#else
inline void
ircd::log::log::debug(const char *const &fmt,
...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
#endif
2017-03-18 04:24:25 +01:00
template<class... args>
void
ircd::log::log::info(const char *const &fmt,
args&&... a)
2016-08-26 06:38:15 +02:00
{
2017-03-18 04:24:25 +01:00
operator()(facility::INFO, fmt, va_rtti{std::forward<args>(a)...});
2016-08-26 06:38:15 +02:00
}
2017-03-18 04:24:25 +01:00
template<class... args>
void
ircd::log::log::notice(const char *const &fmt,
args&&... a)
2016-08-26 06:38:15 +02:00
{
2017-03-18 04:24:25 +01:00
operator()(facility::NOTICE, fmt, va_rtti{std::forward<args>(a)...});
2016-08-26 06:38:15 +02:00
}
2017-03-18 04:24:25 +01:00
#ifdef RB_DEBUG
template<class... args>
void
ircd::log::log::dwarning(const char *const &fmt,
args&&... a)
{
operator()(facility::DWARNING, fmt, va_rtti{std::forward<args>(a)...});
}
#else
inline void
ircd::log::log::dwarning(const char *const &fmt,
...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
#endif
2017-03-18 04:24:25 +01:00
template<class... args>
void
ircd::log::log::warning(const char *const &fmt,
args&&... a)
2016-08-26 06:38:15 +02:00
{
2017-03-18 04:24:25 +01:00
operator()(facility::WARNING, fmt, va_rtti{std::forward<args>(a)...});
2016-08-26 06:38:15 +02:00
}
2017-03-18 04:24:25 +01:00
#ifdef RB_DEBUG
template<class... args>
void
ircd::log::log::derror(const char *const &fmt,
args&&... a)
{
operator()(facility::DERROR, fmt, va_rtti{std::forward<args>(a)...});
}
#else
inline void
ircd::log::log::derror(const char *const &fmt,
...)
{
// Required in gcc 6.3.0 20170519, template param packs are not DCE'ed
}
#endif
2017-03-18 04:24:25 +01:00
template<class... args>
void
ircd::log::log::error(const char *const &fmt,
args&&... a)
{
2017-03-18 04:24:25 +01:00
operator()(facility::ERROR, fmt, va_rtti{std::forward<args>(a)...});
2016-08-26 06:38:15 +02:00
}
2017-03-18 04:24:25 +01:00
template<class... args>
void
ircd::log::log::critical(const char *const &fmt,
args&&... a)
{
operator()(facility::CRITICAL, fmt, va_rtti{std::forward<args>(a)...});
}
template<class... args>
void
ircd::log::log::operator()(const facility &f,
const char *const &fmt,
args&&... a)
{
vlog(f, name, fmt, va_rtti{std::forward<args>(a)...});
}