myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2961] Allow objects to share log targets with themsel


From: Francesco Pipita
Subject: [myserver-commit] [2961] Allow objects to share log targets with themselves.
Date: Wed, 12 Nov 2008 16:59:08 +0000

Revision: 2961
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2961
Author:   francesco_pipita
Date:     2008-11-12 16:59:08 +0000 (Wed, 12 Nov 2008)

Log Message:
-----------
Allow objects to share log targets with themselves. Before this patch, it was 
not possible
for a virtualhost, to write both its access and warning log messages on the 
same LogStream.

Modified Paths:
--------------
    trunk/myserver/include/log/log_manager.h
    trunk/myserver/src/log/log_manager.cpp
    trunk/myserver/tests/test_log_manager.cpp

Modified: trunk/myserver/include/log/log_manager.h
===================================================================
--- trunk/myserver/include/log/log_manager.h    2008-11-11 23:00:34 UTC (rev 
2960)
+++ trunk/myserver/include/log/log_manager.h    2008-11-12 16:59:08 UTC (rev 
2961)
@@ -82,6 +82,7 @@
   int add (void* owner, string type);
   int add (void* owner, string type, string location, LogStream* ls);
   int computeNewLine ();
+  int logWriteln (string, LoggingLevel);
 private:
   LoggingLevel level;
   Mutex* mutex;

Modified: trunk/myserver/src/log/log_manager.cpp
===================================================================
--- trunk/myserver/src/log/log_manager.cpp      2008-11-11 23:00:34 UTC (rev 
2960)
+++ trunk/myserver/src/log/log_manager.cpp      2008-11-12 16:59:08 UTC (rev 
2961)
@@ -86,8 +86,9 @@
 /*!
  * Add a new LogStream element to the LogManager. The same LogStream can be
  * shared between different owner objects. This means that you can pass
- * multiple times the same string as `location' parameter, but you can't pass
- * more than one time the same <owner, location> pair.
+ * multiple times the same string as `location' parameter, as well as `owner'
+ * and `type', but you can't pass more than one time the same 
+ * <owner, type, location> tuple.
  *
  * \param owner The object that will own the LogStream that is being added.
  * \param type The category which the LogStream that is being added belongs to.
@@ -102,30 +103,42 @@
                  list<string>& filters, u_long cycle)
 {
   mutex->lock ();
-  int success = 1;
+  int failure = 1;
   if (!contains (location))
     {
       LogStream* ls = lsf->create (ff, location, filters, cycle);
       if (ls)
         {
-          success = (add (owner) || add (owner, type) ||
-                     add (owner, type, location, ls));
+          failure = add (owner) || add (owner, type) ||
+            add (owner, type, location, ls);
         }
     }
-  else if (!contains (owner))
+  else
     {
-      success = (add (owner) || add (owner, type) ||
-                 add (owner, type, location, logStreams[location]));
-      if (!success && Server::getInstance ())
+      int oldSize;
+      int newSize;
+      ostringstream oss;
+      
+      oldSize = logStreamOwners[location].size ();
+      if (!contains (owner))
         {
-          string msg ("LogManager: Warning, shared location \'");
-          msg.append (location);
-          msg.append ("\' detected.");
-          Server::getInstance ()->logWriteln (msg.c_str (), 
MYSERVER_LOG_MSG_WARNING);
+          failure = add (owner) || add (owner, type) || 
+            add (owner, type, location, logStreams[location]);
         }
+      else if (!contains (owner, type))
+        {
+          failure = add (owner, type) ||
+            add (owner, type, location, logStreams[location]);
+        }
+      newSize = logStreamOwners[location].size ();
+      if (!failure && newSize > oldSize)
+        {
+          oss << "Warning: \'" << location << "\' shared between " << newSize 
<< " objects.";
+          logWriteln (oss.str (), MYSERVER_LOG_MSG_WARNING);
+        }
     }
   mutex->unlock ();
-  return success;
+  return failure;
 }
 
 /*!
@@ -139,7 +152,7 @@
 int
 LogManager::add (void* owner)
 {
-  int success = 1;
+  int failure = 1;
   if (owner)
     {
       if (!contains (owner))
@@ -147,9 +160,9 @@
           map<string, map<string, LogStream*> > type;
           owners[owner] = type;
         }
-      success = 0;
+      failure = 0;
     }
-  return success;
+  return failure;
 }
 
 /*!
@@ -163,7 +176,7 @@
 int
 LogManager::add (void* owner, string type)
 {
-  int success = 1;
+  int failure = 1;
   if (type.size ())
     {
       if (!contains (owner, type))
@@ -171,9 +184,9 @@
           map<string, LogStream*> target;
           owners[owner][type] = target;
         }
-      success = 0;
+      failure = 0;
     }
-  return success;
+  return failure;
 }
 
 /*!
@@ -190,9 +203,19 @@
 int
 LogManager::add (void* owner, string type, string location, LogStream* ls)
 {
-  logStreams[location] = ls;
-  owners[owner][type][location] = ls;
-  logStreamOwners[location].push_back (owner);
+  if (!contains (location))
+    {
+      logStreams[location] = ls;
+    }
+  if (!contains (owner, type, location))
+    {
+      owners[owner][type][location] = ls;
+    }
+  list<void*>* l = &logStreamOwners[location];
+  if (find (l->begin (), l->end (), owner) == l->end ())
+    {
+      l->push_back (owner);
+    }
   if (contains (location) && contains (owner, type, location))
     {
       return 0;
@@ -215,7 +238,7 @@
 LogManager::remove (void* owner)
 {
   mutex->lock ();
-  int success = 1;
+  int failure = 1;
   if (contains (owner))
     {
       map<string, map<string, LogStream*> >* m = &owners[owner];
@@ -238,10 +261,10 @@
         }
       m->clear ();
       owners.erase (owner);
-      success = 0;
+      failure = 0;
     }
   mutex->unlock ();
-  return success;
+  return failure;
 }
 
 /*!
@@ -262,16 +285,16 @@
 LogManager::notify (void* owner, string type, string location, 
                     LogStreamEvent evt, void* message, void* reply)
 {
-  int success = 1;
+  int failure = 1;
   if (contains (owner, type, location))
     {
-      success = owners[owner][type][location]->update (evt, message, reply);
+      failure = owners[owner][type][location]->update (evt, message, reply);
     }
-  return success;
+  return failure;
 }
 
 /*!
- * Delivery a message to all the LogStream objects belonging to the `type'
+ * Deliver a message to all the LogStream objects belonging to the `type'
  * log category of the `owner' object.
  *
  * \param owner The object which owns the target stream.
@@ -287,22 +310,22 @@
 LogManager::notify (void* owner, string type, LogStreamEvent evt, 
                     void* message, void* reply)
 {
-  int success = 1;
+  int failure = 1;
   if (contains (owner, type))
     {
-      success = 0;
+      failure = 0;
       map<string, LogStream*> m = owners[owner][type];
       map<string, LogStream*>::iterator it;
       for (it = m.begin (); it != m.end (); it++)
         {
-          success |= notify (owner, type, it->first, evt, message, reply);
+          failure |= notify (owner, type, it->first, evt, message, reply);
         }
     }
-  return success;
+  return failure;
 }
 
 /*!
- * Delivery a message to all the LogStream objects owned by `owner'.
+ * Deliver a message to all the LogStream objects owned by `owner'.
  *
  * \param owner The object which owns the target stream.
  * \param evt The event of interest for the target stream. For the list
@@ -316,18 +339,18 @@
 LogManager::notify (void* owner, LogStreamEvent evt, void* message, 
                     void* reply)
 {
-  int success = 1;
+  int failure = 1;
   if (contains (owner))
     {
-      success = 0;
+      failure = 0;
       map<string, map<string, LogStream*> > m = owners[owner];
       map<string, map<string, LogStream*> >::iterator it;
       for (it = m.begin (); it != m.end (); it++)
         {
-          success |= notify (owner, it->first, evt, message, reply);
+          failure |= notify (owner, it->first, evt, message, reply);
         }
     }
-  return success;
+  return failure;
 }
 
 /*!
@@ -404,21 +427,21 @@
 LogManager::log (void* owner, string message, bool appendNL,
                  LoggingLevel level)
 {
-  int success = 1;
+  int failure = 1;
   if (level >= this->level)
     {
-      success = 
+      failure = 
         notify (owner, MYSERVER_LOG_EVT_SET_MODE, static_cast<void*>(&level)) 
||
         notify (owner, MYSERVER_LOG_EVT_LOG, static_cast<void*>(&message));
       if (appendNL)
         {
           LoggingLevel l = MYSERVER_LOG_MSG_PLAIN;
-          success |= 
+          failure |= 
             (notify (owner, MYSERVER_LOG_EVT_SET_MODE, 
(static_cast<void*>(&l))) ||
              notify (owner, MYSERVER_LOG_EVT_LOG, 
(static_cast<void*>(&newline))));
         }
     }
-  return success;
+  return failure;
 }
 
 /*!
@@ -439,25 +462,25 @@
 LogManager::log (void* owner, string type, string message, bool appendNL,
                  LoggingLevel level)
 {
-  int success = 1;
+  int failure = 1;
   if (level >= this->level)
     {
-      success = 
+      failure = 
         notify (owner, type, MYSERVER_LOG_EVT_SET_MODE, 
static_cast<void*>(&level)) ||
         notify (owner, type, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message));
       if (appendNL)
         {
           LoggingLevel l = MYSERVER_LOG_MSG_PLAIN;
-          success |= 
+          failure |= 
             (notify (owner, MYSERVER_LOG_EVT_SET_MODE, 
(static_cast<void*>(&l))) ||
              notify (owner, MYSERVER_LOG_EVT_LOG, 
(static_cast<void*>(&newline))));
         }
     }
-  return success;
+  return failure;
 }
 
 /*!
- * Write a message on a single LogStream, specified by the tuple
+ * Write a message on a single LogStream, specified by the unique tuple
  * <owner, type, location>.
  *
  * \param owner The object that owns the LogStream.
@@ -476,21 +499,21 @@
 LogManager::log (void* owner, string type, string location, string message, 
                  bool appendNL, LoggingLevel level)
 {
-  int success = 1;
+  int failure = 1;
   if (level >= this->level)
     {
-      success = 
+      failure = 
         notify (owner, type, location, MYSERVER_LOG_EVT_SET_MODE, 
static_cast<void*>(&level)) ||
         notify (owner, type, location, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message));
       if (appendNL)
         {
           LoggingLevel l = MYSERVER_LOG_MSG_PLAIN;
-          success |=
+          failure |=
             (notify (owner, MYSERVER_LOG_EVT_SET_MODE, 
(static_cast<void*>(&l))) ||
              notify (owner, MYSERVER_LOG_EVT_LOG, 
(static_cast<void*>(&newline))));
         }
     }
-  return success;
+  return failure;
 }
 
 /*!
@@ -523,7 +546,7 @@
 }
 
 /*!
- * Close the LogStream specified by the tuple <owner, type, location>.
+ * Close the LogStream specified by the unique tuple <owner, type, location>.
  *
  * \param owner The object that owns the LogStream.
  * \param type The log category.
@@ -569,7 +592,7 @@
 }
 
 /*!
- * Set the cycle value for the LogStream specified by the tuple
+ * Set the cycle value for the LogStream specified by the unique tuple
  * <owner, type, location>.
  *
  * \param owner The object that owns the LogStream.
@@ -798,7 +821,7 @@
 
 /*!
  * Query the LogManager to ask it if the `owner' object is actually
- * present in it.
+ * present within it.
  *
  * \param owner The object about which we want to know the existence within
  * the LogManager.
@@ -834,7 +857,7 @@
  * \param type The log category that we are interested to query.
  * \param location The target of the query.
  *
- * \return true if the <owner, type, location> tuple exists.
+ * \return true if the unique <owner, type, location> tuple exists.
  */
 bool
 LogManager::contains (void* owner, string type, string location)
@@ -890,7 +913,7 @@
  * \param location The location string.
  *
  * \return The number of occurrences of `location'. This number should never
- * be greater than 1, since the pair <owner, location> is a key.
+ * be greater than 1, since the tuple <owner, type, location> is a key.
  */
 int
 LogManager::count (void* owner, string type, string location)
@@ -916,3 +939,21 @@
     }
   return 1;
 }
+
+/*!
+ * Write a message on the main MyServer log.
+ *
+ * \param msg The message to write.
+ * \param l The message logging level.
+ *
+ * \return 0 on success, 1 on error.
+ */
+int
+LogManager::logWriteln (string msg, LoggingLevel l)
+{
+  if (Server::getInstance ())
+    {
+      return Server::getInstance ()->logWriteln (msg.c_str (), l);
+    }
+  return 1;
+}

Modified: trunk/myserver/tests/test_log_manager.cpp
===================================================================
--- trunk/myserver/tests/test_log_manager.cpp   2008-11-11 23:00:34 UTC (rev 
2960)
+++ trunk/myserver/tests/test_log_manager.cpp   2008-11-12 16:59:08 UTC (rev 
2961)
@@ -356,6 +356,7 @@
 
     CPPUNIT_ASSERT (!lm->add (this, "test", "file://foo", filters, 0));
     CPPUNIT_ASSERT (!lm->add (&anObject, "test", "file://foo", filters, 0));
+    CPPUNIT_ASSERT (!lm->add (this, "test1", "file://foo", filters, 0));
   }
 
   void testSharedGet ()
@@ -363,11 +364,19 @@
     list<string> filters;
     AnObject anObject;
     list<void*> l;
+    LogStream* ls;
+    LogStream* ls1;
 
     CPPUNIT_ASSERT (!lm->add (this, "test", "file://foo", filters, 0));
     CPPUNIT_ASSERT (!lm->add (&anObject, "test", "file://foo", filters, 0));
     CPPUNIT_ASSERT (!lm->getOwnersList ("file://foo", &l));
     CPPUNIT_ASSERT (l.size () == 2);
+    CPPUNIT_ASSERT (!lm->get (this, "test", "file://foo", &ls));
+    CPPUNIT_ASSERT (!lm->get (this, "test", "file://foo", &ls1));
+    CPPUNIT_ASSERT (ls1 == ls);
+    CPPUNIT_ASSERT (!lm->add (this, "test1", "file://foo", filters, 0));
+    CPPUNIT_ASSERT (!lm->getOwnersList ("file://foo", &l));
+    CPPUNIT_ASSERT (l.size () == 2);
   }
 
   void testSharedRemove ()






reply via email to

[Prev in Thread] Current Thread [Next in Thread]