diff options
Diffstat (limited to 'src/logger.cpp')
-rw-r--r-- | src/logger.cpp | 82 |
1 files changed, 37 insertions, 45 deletions
diff --git a/src/logger.cpp b/src/logger.cpp index 0e54fb778..677258009 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -29,20 +29,15 @@ static Anope::string GetTimeStamp() { char tbuf[256]; - time_t t; - if (time(&t) < 0) - t = Anope::CurTime; - - tm tm = *localtime(&t); + Anope::UpdateTime(); + auto tm = *localtime(&Anope::CurTime); if (Anope::Debug) { char *s; - struct timeval tv; - gettimeofday(&tv, NULL); strftime(tbuf, sizeof(tbuf) - 1, "[%b %d %H:%M:%S", &tm); s = tbuf + strlen(tbuf); - s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06d", static_cast<int>(tv.tv_usec)); + s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06lld", static_cast<long long>(Anope::CurTimeNs / 1000)); strftime(s, sizeof(tbuf) - (s - tbuf) - 1, " %Y]", &tm); } else @@ -57,7 +52,7 @@ static inline Anope::string CreateLogName(const Anope::string &file, time_t t = tm *tm = localtime(&t); strftime(timestamp, sizeof(timestamp), "%Y%m%d", tm); - return Anope::LogDir + "/" + file + "." + timestamp; + return Anope::ExpandLog(file + "." + timestamp); } LogFile::LogFile(const Anope::string &name) : filename(name), stream(name.c_str(), std::ios_base::out | std::ios_base::app) @@ -74,11 +69,11 @@ const Anope::string &LogFile::GetName() const return this->filename; } -Log::Log(LogType t, const Anope::string &cat, BotInfo *b) : bi(b), u(NULL), nc(NULL), c(NULL), source(NULL), chan(NULL), ci(NULL), s(NULL), m(NULL), type(t), category(cat) +Log::Log(LogType t, const Anope::string &cat, BotInfo *b) : bi(b), type(t), category(cat) { } -Log::Log(LogType t, CommandSource &src, Command *_c, ChannelInfo *_ci) : u(src.GetUser()), nc(src.nc), c(_c), source(&src), chan(NULL), ci(_ci), s(NULL), m(NULL), type(t) +Log::Log(LogType t, CommandSource &src, Command *_c, ChannelInfo *_ci) : u(src.GetUser()), nc(src.nc), c(_c), source(&src), ci(_ci), type(t) { if (!c) throw CoreException("Invalid pointers passed to Log::Log"); @@ -87,35 +82,34 @@ Log::Log(LogType t, CommandSource &src, Command *_c, ChannelInfo *_ci) : u(src.G throw CoreException("This constructor does not support this log type"); size_t sl = c->name.find('/'); - this->bi = NULL; if (sl != Anope::string::npos) this->bi = Config->GetClient(c->name.substr(0, sl)); this->category = c->name; } -Log::Log(User *_u, Channel *ch, const Anope::string &cat) : bi(NULL), u(_u), nc(NULL), c(NULL), source(NULL), chan(ch), ci(chan ? *chan->ci : NULL), s(NULL), m(NULL), type(LOG_CHANNEL), category(cat) +Log::Log(User *_u, Channel *ch, const Anope::string &cat) : u(_u), chan(ch), ci(chan ? *chan->ci : nullptr), type(LOG_CHANNEL), category(cat) { if (!chan) throw CoreException("Invalid pointers passed to Log::Log"); } -Log::Log(User *_u, const Anope::string &cat, BotInfo *_bi) : bi(_bi), u(_u), nc(NULL), c(NULL), source(NULL), chan(NULL), ci(NULL), s(NULL), m(NULL), type(LOG_USER), category(cat) +Log::Log(User *_u, const Anope::string &cat, BotInfo *_bi) : bi(_bi), u(_u), type(LOG_USER), category(cat) { if (!u) throw CoreException("Invalid pointers passed to Log::Log"); } -Log::Log(Server *serv, const Anope::string &cat, BotInfo *_bi) : bi(_bi), u(NULL), nc(NULL), c(NULL), source(NULL), chan(NULL), ci(NULL), s(serv), m(NULL), type(LOG_SERVER), category(cat) +Log::Log(Server *serv, const Anope::string &cat, BotInfo *_bi) : bi(_bi), s(serv), type(LOG_SERVER), category(cat) { if (!s) throw CoreException("Invalid pointer passed to Log::Log"); } -Log::Log(BotInfo *b, const Anope::string &cat) : bi(b), u(NULL), nc(NULL), c(NULL), source(NULL), chan(NULL), ci(NULL), s(NULL), m(NULL), type(LOG_NORMAL), category(cat) +Log::Log(BotInfo *b, const Anope::string &cat) : bi(b), type(LOG_NORMAL), category(cat) { } -Log::Log(Module *mod, const Anope::string &cat, BotInfo *_bi) : bi(_bi), u(NULL), nc(NULL), c(NULL), source(NULL), chan(NULL), ci(NULL), s(NULL), m(mod), type(LOG_MODULE), category(cat) +Log::Log(Module *mod, const Anope::string &cat, BotInfo *_bi) : bi(_bi), m(mod), type(LOG_MODULE), category(cat) { } @@ -131,9 +125,13 @@ Log::~Log() FOREACH_MOD(OnLog, (this)); if (Config) - for (unsigned i = 0; i < Config->LogInfos.size(); ++i) - if (Config->LogInfos[i].HasType(this->type, this->category)) - Config->LogInfos[i].ProcessMessage(this); + { + for (auto &li : Config->LogInfos) + { + if (li.HasType(this->type, this->category)) + li.ProcessMessage(this); + } + } } Anope::string Log::FormatSource() const @@ -227,14 +225,14 @@ Anope::string Log::BuildPrefix() const return buffer; } -LogInfo::LogInfo(int la, bool rio, bool ldebug) : bot(NULL), last_day(0), log_age(la), raw_io(rio), debug(ldebug) +LogInfo::LogInfo(int la, bool rio, bool ldebug) : log_age(la), raw_io(rio), debug(ldebug) { } LogInfo::~LogInfo() { - for (unsigned i = 0; i < this->logfiles.size(); ++i) - delete this->logfiles[i]; + for (const auto *logfile : this->logfiles) + delete logfile; this->logfiles.clear(); } @@ -281,16 +279,15 @@ bool LogInfo::HasType(LogType ltype, const Anope::string &type) const if (list == NULL) return false; - for (unsigned i = 0; i < list->size(); ++i) + for (auto value : *list) { - Anope::string cat = list->at(i); bool inverse = false; - if (cat[0] == '~') + if (value[0] == '~') { - cat.erase(cat.begin()); + value.erase(value.begin()); inverse = true; } - if (Anope::Match(type, cat)) + if (Anope::Match(type, value)) { return !inverse; } @@ -301,18 +298,16 @@ bool LogInfo::HasType(LogType ltype, const Anope::string &type) const void LogInfo::OpenLogFiles() { - for (unsigned i = 0; i < this->logfiles.size(); ++i) - delete this->logfiles[i]; + for (const auto *logfile : this->logfiles) + delete logfile; this->logfiles.clear(); - for (unsigned i = 0; i < this->targets.size(); ++i) + for (const auto &target : this->targets) { - const Anope::string &target = this->targets[i]; - if (target.empty() || target[0] == '#' || target == "globops" || target.find(":") != Anope::string::npos) continue; - LogFile *lf = new LogFile(CreateLogName(target)); + auto *lf = new LogFile(CreateLogName(target)); if (!lf->stream.is_open()) { Log() << "Unable to open logfile " << lf->GetName(); @@ -353,10 +348,8 @@ void LogInfo::ProcessMessage(const Log *l) FOREACH_MOD(OnLogMessage, (this, l, buffer)); - for (unsigned i = 0; i < this->targets.size(); ++i) + for (const auto &target : this->targets) { - const Anope::string &target = this->targets[i]; - if (!target.empty() && target[0] == '#') { if (UplinkSock && l->type <= LOG_NORMAL && Me && Me->IsSynced()) @@ -371,7 +364,7 @@ void LogInfo::ProcessMessage(const Log *l) if (!bi) bi = c->WhoSends(); if (bi) - IRCD->SendPrivmsg(bi, c->name, "%s", buffer.c_str()); + IRCD->SendPrivmsg(bi, c->name, buffer); } } else if (target == "globops") @@ -382,7 +375,7 @@ void LogInfo::ProcessMessage(const Log *l) if (!bi) bi = this->bot; if (bi) - IRCD->SendGlobops(bi, "%s", buffer.c_str()); + IRCD->SendGlobops(bi, buffer); } } } @@ -394,11 +387,10 @@ void LogInfo::ProcessMessage(const Log *l) this->OpenLogFiles(); if (this->log_age) - for (unsigned i = 0; i < this->targets.size(); ++i) + { + for (const auto &target : this->targets) { - const Anope::string &target = this->targets[i]; - - if (target.empty() || target[0] == '#' || target == "globops" || target.find(":") != Anope::string::npos) + if (target.empty() || target[0] == '#' || target == "globops" || target.find(":") != Anope::string::npos) continue; Anope::string oldlog = CreateLogName(target, Anope::CurTime - 86400 * this->log_age); @@ -408,11 +400,11 @@ void LogInfo::ProcessMessage(const Log *l) Log(LOG_DEBUG) << "Deleted old logfile " << oldlog; } } + } } - for (unsigned i = 0; i < this->logfiles.size(); ++i) + for (auto *lf : this->logfiles) { - LogFile *lf = this->logfiles[i]; lf->stream << GetTimeStamp() << " " << buffer << std::endl; } } |