logger.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  *
00011  */
00012 
00013 #ifndef LOGGER_H
00014 #define LOGGER_H
00015 
00016 #include "anope.h"
00017 #include "defs.h"
00018 
00019 enum LogType
00020 {
00021         /* Used whenever an administrator uses an administrative comand */
00022         LOG_ADMIN,
00023         /* Used whenever an administrator overides something, such as adding
00024          * access to a channel where they don't have permission to.
00025          */
00026         LOG_OVERRIDE,
00027         /* Any other command usage */
00028         LOG_COMMAND,
00029         LOG_SERVER,
00030         LOG_CHANNEL,
00031         LOG_USER,
00032         LOG_MODULE,
00033         LOG_NORMAL,
00034         LOG_TERMINAL,
00035         LOG_RAWIO,
00036         LOG_DEBUG,
00037         LOG_DEBUG_2,
00038         LOG_DEBUG_3,
00039         LOG_DEBUG_4
00040 };
00041 
00042 struct LogFile
00043 {
00044         Anope::string filename;
00045         std::ofstream stream;
00046 
00047         LogFile(const Anope::string &name);
00048         Anope::string GetName() const;
00049 };
00050 
00051 /* Represents a single log message */
00052 class CoreExport Log
00053 {
00054  public:
00055         /* Bot that should log this message */
00056         const BotInfo *bi;
00057         /* For commands, the user executing the command */
00058         Anope::string nick;
00059         /* For commands, the user executing the command, but might not always exist */
00060         const User *u;
00061         /* For commands, the account executing teh command, but will not always exist */
00062         const NickCore *nc;
00063         /* For commands, the command being executed */
00064         Command *c;
00065         /* Used for LOG_CHANNEL */
00066         Channel *chan;
00067         /* For commands, the channel the command was executed on, will not always exist */
00068         const ChannelInfo *ci;
00069         /* For LOG_SERVER */
00070         Server *s;
00071         /* For LOG_MODULE */
00072         Module *m;
00073         LogType type;
00074         Anope::string category;
00075         std::list<Anope::string> sources;
00076 
00077         std::stringstream buf;
00078 
00079         Log(LogType type = LOG_NORMAL, const Anope::string &category = "", const BotInfo *bi = NULL);
00080 
00081         /* LOG_COMMAND/OVERRIDE/ADMIN */
00082         Log(LogType type, CommandSource &source, Command *c, const ChannelInfo *ci = NULL);
00083 
00084         /* LOG_CHANNEL */
00085         Log(const User *u, Channel *c, const Anope::string &category = "");
00086 
00087         /* LOG_USER */
00088         explicit Log(const User *u, const Anope::string &category = "", const BotInfo *bi = NULL);
00089 
00090         /* LOG_SERVER */
00091         explicit Log(Server *s, const Anope::string &category = "", const BotInfo *bi = NULL);
00092 
00093         explicit Log(const BotInfo *b, const Anope::string &category = "");
00094 
00095         Log(Module *m, const Anope::string &category = "");
00096 
00097         ~Log();
00098 
00099         Anope::string BuildPrefix() const;
00100 
00101         template<typename T> Log &operator<<(T val)
00102         {
00103                 this->buf << val;
00104                 return *this;
00105         }
00106 };
00107 
00108 /* Configured in the configuration file, actually does the message logging */
00109 class CoreExport LogInfo
00110 {
00111  public:
00112         std::list<Anope::string> targets;
00113         std::map<Anope::string, LogFile *> logfiles;
00114         std::list<Anope::string> sources;
00115         int log_age;
00116         std::list<Anope::string> admin;
00117         std::list<Anope::string> override;
00118         std::list<Anope::string> commands;
00119         std::list<Anope::string> servers;
00120         std::list<Anope::string> users;
00121         std::list<Anope::string> channels;
00122         std::list<Anope::string> normal;
00123         bool raw_io;
00124         bool debug;
00125 
00126         LogInfo(int logage, bool rawio, bool debug);
00127 
00128         ~LogInfo();
00129 
00130         void AddType(std::list<Anope::string> &list, const Anope::string &type);
00131 
00132         bool HasType(LogType ltype, const Anope::string &type) const;
00133 
00134         /* Logs the message l if configured to */
00135         void ProcessMessage(const Log *l);
00136 };
00137 
00138 #endif // LOGGER_H
00139