diff options
Diffstat (limited to 'include/logger.h')
-rw-r--r-- | include/logger.h | 294 |
1 files changed, 204 insertions, 90 deletions
diff --git a/include/logger.h b/include/logger.h index 349ca5a55..f0b306c40 100644 --- a/include/logger.h +++ b/include/logger.h @@ -1,41 +1,53 @@ /* + * Anope IRC Services * - * (C) 2003-2016 Anope Team - * Contact us at team@anope.org + * Copyright (C) 2010-2016 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ -#ifndef LOGGER_H -#define LOGGER_H +#pragma once #include "anope.h" #include "defs.h" +#include "language.h" -enum LogType +enum class LogType { + NORMAL, /* Used whenever an administrator uses an administrative comand */ - LOG_ADMIN, + ADMIN, /* Used whenever an administrator overides something, such as adding * access to a channel where they don't have permission to. */ - LOG_OVERRIDE, + OVERRIDE, /* Any other command usage */ - LOG_COMMAND, - LOG_SERVER, - LOG_CHANNEL, - LOG_USER, - LOG_MODULE, - LOG_NORMAL, - LOG_TERMINAL, - LOG_RAWIO, - LOG_DEBUG, - LOG_DEBUG_2, - LOG_DEBUG_3, - LOG_DEBUG_4 + COMMAND, + SERVER, + CHANNEL, + USER, + MODULE +}; + +enum class LogLevel +{ + NORMAL, + TERMINAL, + RAWIO, + DEBUG, + DEBUG_2, + DEBUG_3 }; struct LogFile @@ -48,97 +60,199 @@ struct LogFile const Anope::string &GetName() const; }; -/* Represents a single log message */ -class CoreExport Log +/* Configured in the configuration file, actually does the message logging */ +class CoreExport LogInfo { public: - /* Bot that should log this message */ - BotInfo *bi; - /* For commands, the user executing the command, but might not always exist */ - User *u; - /* For commands, the account executing the command, but will not always exist */ - NickCore *nc; - /* For commands, the command being executed */ - Command *c; - /* For commands, the command source */ - CommandSource *source; - /* Used for LOG_CHANNEL */ - Channel *chan; - /* For commands, the channel the command was executed on, will not always exist */ - const ChannelInfo *ci; - /* For LOG_SERVER */ - Server *s; - /* For LOG_MODULE */ - Module *m; - LogType type; - Anope::string category; + ServiceBot *bot = nullptr; + std::vector<Anope::string> targets; + std::vector<LogFile *> logfiles; + int last_day = 0; + std::vector<Anope::string> sources; + int log_age = 0; + std::vector<Anope::string> admin; + std::vector<Anope::string> override; + std::vector<Anope::string> commands; + std::vector<Anope::string> servers; + std::vector<Anope::string> users; + std::vector<Anope::string> channels; + std::vector<Anope::string> normal; + bool raw_io = false; + bool debug = false; - std::stringstream buf; + LogInfo(int logage, bool rawio, bool debug); - Log(LogType type = LOG_NORMAL, const Anope::string &category = "", BotInfo *bi = NULL); + ~LogInfo(); - /* LOG_COMMAND/OVERRIDE/ADMIN */ - Log(LogType type, CommandSource &source, Command *c, ChannelInfo *ci = NULL); + void OpenLogFiles(); - /* LOG_CHANNEL */ - Log(User *u, Channel *c, const Anope::string &category = ""); + bool HasType(LogType ltype, LogLevel level, const Anope::string &type) const; - /* LOG_USER */ - Log(User *u, const Anope::string &category = "", BotInfo *bi = NULL); + void ProcessMessage(const Logger *l, const Anope::string &message); +}; - /* LOG_SERVER */ - Log(Server *s, const Anope::string &category = "", BotInfo *bi = NULL); +class Logger +{ + friend class LogInfo; - Log(BotInfo *b, const Anope::string &category = ""); + LogType type = LogType::NORMAL; + LogLevel level = LogLevel::NORMAL; - Log(Module *m, const Anope::string &category = "", BotInfo *bi = NULL); + /* Object logger is attached to */ + Module *module = nullptr; + Command *command = nullptr; + ServiceBot *bot = nullptr; + Server *server = nullptr; - ~Log(); + /* Logger category */ + Anope::string category; + /* Non formatted message */ + Anope::string raw_message; - private: - Anope::string FormatSource() const; - Anope::string FormatCommand() const; + /* Sources */ + User *user = nullptr; + NickServ::Account *account = nullptr; + Channel *channel = nullptr; + ChanServ::Channel *ci = nullptr; + CommandSource *source = nullptr; - public: + Anope::string FormatSource() const; Anope::string BuildPrefix() const; + void LogMessage(const Anope::string &message); + void InsertVariables(FormatInfo &fi); + void CheckOverride(); - template<typename T> Log &operator<<(T val) + template<typename... Args> + Anope::string Format(const Anope::string &message, Args&&... args) { - this->buf << val; - return *this; + FormatInfo fi(message, sizeof...(Args)); + fi.AddArgs(std::forward<Args>(args)...); + InsertVariables(fi); + fi.Format(); + return fi.GetFormat(); } -}; -/* Configured in the configuration file, actually does the message logging */ -class CoreExport LogInfo -{ public: - BotInfo *bot; - std::vector<Anope::string> targets; - std::vector<LogFile *> logfiles; - int last_day; - std::vector<Anope::string> sources; - int log_age; - std::vector<Anope::string> admin; - std::vector<Anope::string> override; - std::vector<Anope::string> commands; - std::vector<Anope::string> servers; - std::vector<Anope::string> users; - std::vector<Anope::string> channels; - std::vector<Anope::string> normal; - bool raw_io; - bool debug; + Logger() = default; + Logger(Module *m) : type(LogType::MODULE), module(m) { } + Logger(Command *c) : type(LogType::COMMAND), command(c) { } + Logger(ServiceBot *b) : bot(b) { } + Logger(Channel *c) : type(LogType::CHANNEL), channel(c) { } + Logger(User *u) : type(LogType::USER), user(u) { } + Logger(Server *s) : type(LogType::SERVER), server(s) { } - LogInfo(int logage, bool rawio, bool debug); + LogType GetType() const; + LogLevel GetLevel() const; - ~LogInfo(); + Module *GetModule() const; + Command *GetCommand() const; + ServiceBot *GetBot() const; + Server *GetServer() const; - void OpenLogFiles(); + User *GetUser() const; + void SetUser(User *); + + NickServ::Account *GetAccount() const; + void SetAccount(NickServ::Account *); + + Channel *GetChannel() const; + void SetChannel(Channel *); - bool HasType(LogType ltype, const Anope::string &type) const; + ChanServ::Channel *GetCi() const; + void SetCi(ChanServ::Channel *); - /* Logs the message l if configured to */ - void ProcessMessage(const Log *l); + CommandSource *GetSource() const; + void SetSource(CommandSource *); + + Logger Category(const Anope::string &c) const; + Logger User(class User *u) const; + Logger Channel(class Channel *c) const; + Logger Channel(ChanServ::Channel *c) const; + Logger Source(CommandSource *s) const; + Logger Bot(ServiceBot *bot) const; + Logger Bot(const Anope::string &name) const; + + template<typename... Args> void Log(LogLevel lev, const Anope::string &message, Args&&... args) + { + Logger l = *this; + l.raw_message = message; + l.level = lev; + + Anope::string translated = Language::Translate(message); + l.LogMessage(l.Format(translated, std::forward<Args>(args)...)); + } + + template<typename... Args> void Command(LogType ltype, CommandSource &csource, ChanServ::Channel *chan, const Anope::string &message, Args&&... args) + { + Logger l = *this; + l.type = ltype; + l.SetSource(&csource); + l.SetCi(chan); + + // Override if the source is marked override + l.CheckOverride(); + + Anope::string translated = Language::Translate(message); + l.LogMessage(l.Format(translated, std::forward<Args>(args)...)); + } + + template<typename... Args> void Command(LogType ltype, CommandSource &csource, const Anope::string &message, Args&&... args) + { + Command(ltype, csource, nullptr, message, std::forward<Args>(args)...); + } + + // LOG_COMMAND + + template<typename... Args> void Command(CommandSource &csource, ChanServ::Channel *chan, const Anope::string &message, Args&&... args) + { + Command(LogType::COMMAND, csource, chan, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Command(CommandSource &csource, const Anope::string &message, Args&&... args) + { + Command(LogType::COMMAND, csource, nullptr, message, std::forward<Args>(args)...); + } + + // LOG_ADMIN + + template<typename... Args> void Admin(CommandSource &csource, ChanServ::Channel *chan, const Anope::string &message, Args&&... args) + { + Command(LogType::ADMIN, csource, chan, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Admin(CommandSource &csource, const Anope::string &message, Args&&... args) + { + Command(LogType::ADMIN, csource, nullptr, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Log(const Anope::string &message, Args&&... args) + { + Log(LogLevel::NORMAL, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Terminal(const Anope::string &message, Args&&... args) + { + Log(LogLevel::TERMINAL, message, std::forward<Args>(args)...); + } + + template<typename... Args> void RawIO(const Anope::string &message, Args&&... args) + { + Log(LogLevel::RAWIO, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Debug(const Anope::string &message, Args&&... args) + { + Log(LogLevel::DEBUG, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Debug2(const Anope::string &message, Args&&... args) + { + Log(LogLevel::DEBUG_2, message, std::forward<Args>(args)...); + } + + template<typename... Args> void Debug3(const Anope::string &message, Args&&... args) + { + Log(LogLevel::DEBUG_3, message, std::forward<Args>(args)...); + } }; -#endif // LOGGER_H |