]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/logger.cpp
Per-logstream loglevels.
[user/henk/code/inspircd.git] / src / logger.cpp
index 690c1a5d9597e658da7f2e8ea7b6fd70ef5ecffe..03c02fe09f9659a052e83260076229c19edf26f0 100644 (file)
  *       can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
  * 
  */
+
+void LogManager::CloseLogs()
+{
+       /*
+        * This doesn't remove logstreams from the map/vector etc, because if this is called, shit is hitting the fan
+        * and we're going down anyway - this just provides a "nice" way for logstreams to clean up. -- w
+        */
+       std::map<std::string, std::vector<LogStream *> >::iterator i;
+
+       while (LogStreams.begin() != LogStreams.end())
+       {
+               i = LogStreams.begin();
+
+               while (i->second.begin() != i->second.end())
+               {
+                       std::vector<LogStream *>::iterator it = i->second.begin();
+
+                       delete (*it);
+                       i->second.erase(it);
+               }
+
+               LogStreams.erase(i);
+       }
+}
+
+bool LogManager::AddLogType(const std::string &type, LogStream *l)
+{
+       std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
+
+       if (i != LogStreams.end())
+               i->second.push_back(l);
+       else
+       {
+               std::vector<LogStream *> v;
+               v.push_back(l);
+               LogStreams[type] = v;
+       }
+
+       if (type == "*")
+               GlobalLogStreams.push_back(l);
+
+       return true;
+}
+
+bool LogManager::DelLogType(const std::string &type, LogStream *l)
+{
+       std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
+       std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
+
+       while (gi != GlobalLogStreams.end())
+       {
+               if ((*gi) == l)
+               {
+                       GlobalLogStreams.erase(gi);
+                       break;
+               }
+       }
+
+       if (i != LogStreams.end())
+       {
+               std::vector<LogStream *>::iterator it = i->second.begin();
+
+               while (it != i->second.end())
+               {
+                       if (*it == l)
+                       {
+                               i->second.erase(it);
+
+                               if (i->second.size() == 0)
+                               {
+                                       LogStreams.erase(i);
+                               }
+
+                               delete l;
+                               return true;
+                       }
+
+                       it++;
+               }
+       }
+
+       return false;
+}
+
+void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
+{
+       if (Logging)
+               return;
+
+       va_list a;
+       static char buf[65536];
+
+       va_start(a, fmt);
+       vsnprintf(buf, 65536, fmt, a);
+       va_end(a);
+
+       this->Log(type, loglevel, std::string(buf));
+}
+
+void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
+{
+       if (Logging)
+               return;
+
+       Logging = true;
+
+       std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
+
+       while (gi != GlobalLogStreams.end())
+       {
+               (*gi)->OnLog(loglevel, type, msg);
+               gi++;
+       }
+
+       std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
+
+       if (i != LogStreams.end())
+       {
+               std::vector<LogStream *>::iterator it = i->second.begin();
+
+               while (it != i->second.end())
+               {
+                       (*it)->OnLog(loglevel, type, msg);
+                       it++;
+               }
+       }
+
+       Logging = false;
+}
+
+