myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [2959] Added the possibility to allow shared LogStream


From: Francesco Pipita
Subject: [myserver-commit] [2959] Added the possibility to allow shared LogStream objects within the LogManager .
Date: Tue, 11 Nov 2008 16:48:45 +0000

Revision: 2959
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2959
Author:   francesco_pipita
Date:     2008-11-11 16:48:45 +0000 (Tue, 11 Nov 2008)

Log Message:
-----------
Added the possibility to allow shared LogStream objects within the LogManager.

Modified Paths:
--------------
    trunk/myserver/documentation/log_management.texi
    trunk/myserver/include/log/log_manager.h
    trunk/myserver/src/log/log_manager.cpp
    trunk/myserver/src/log/stream/log_stream.cpp
    trunk/myserver/tests/test_log_manager.cpp

Modified: trunk/myserver/documentation/log_management.texi
===================================================================
--- trunk/myserver/documentation/log_management.texi    2008-11-10 20:02:16 UTC 
(rev 2958)
+++ trunk/myserver/documentation/log_management.texi    2008-11-11 16:48:45 UTC 
(rev 2959)
@@ -56,7 +56,7 @@
 configuration file, are respectively @code{ACCESSLOG} and
 @code{WARNINGLOG}. Each of these  targets, can in turn write its
 messages over different streams. To tell MyServer about all that, you
-can edit the @file{virtualhosts.xml} file, and apply the informations
+can edit the @file{virtualhosts.xml} file, and apply the information
 found in this section, to each @code{VHOST} entry of that file. As we
 can see in the following example, the @code{ACCESSLOG} root entry for
 the accesses log configuration, contains some @code{STREAM} nodes, and
@@ -90,6 +90,5 @@
   <STREAM location="file://MyServerHTTP.err"/>
 </WARNINGLOG>
 @end example
-It is important to notice that no duplicate location strings are
-admitted. If any, only the first one will be added. Also, if a wrong
-filter is specified, its relative @code{STREAM} willn't be added.
+If a wrong filter is specified, its relative @code{STREAM} willn't 
+be added.

Modified: trunk/myserver/include/log/log_manager.h
===================================================================
--- trunk/myserver/include/log/log_manager.h    2008-11-10 20:02:16 UTC (rev 
2958)
+++ trunk/myserver/include/log/log_manager.h    2008-11-11 16:48:45 UTC (rev 
2959)
@@ -19,6 +19,7 @@
 #ifndef LOG_MANAGER_H
 #define LOG_MANAGER_H
 
+#include <algorithm>
 #include <map>
 #include <string>
 
@@ -70,6 +71,7 @@
   int count (void* owner, string type);
   int count (void* owner, string type, string location);
   int clear ();
+  int getOwnersList (string, list<void*>*);
 protected:
   int notify (void* owner, string type, string location, LogStreamEvent evt, 
               void* msg = 0, void* reply = 0);
@@ -85,8 +87,27 @@
   Mutex* mutex;
   LogStreamFactory* lsf;
   FiltersFactory* ff;
+
+  /*!
+   * Store all the LogStream objects, each identified through its location 
string.
+   * There can't be two LogStream objects pointing to the same location,
+   * anyway it is possible to share the same LogStream between more owners.
+   */
   map<string, LogStream*> logStreams;
+  
+  /*!
+   * For each LogStream, store the list of objects that use it.
+   */
+  map<string, list<void*> > logStreamOwners;
+
+  /*!
+   * For each owner, store the LogStream objects that it owns.
+   */
   map<void*, map<string, map<string, LogStream*> > > owners;
+
+  /*!
+   * Store the newline string for the host operating system.
+   */
   string newline;
 };
 

Modified: trunk/myserver/src/log/log_manager.cpp
===================================================================
--- trunk/myserver/src/log/log_manager.cpp      2008-11-10 20:02:16 UTC (rev 
2958)
+++ trunk/myserver/src/log/log_manager.cpp      2008-11-11 16:48:45 UTC (rev 
2959)
@@ -16,7 +16,14 @@
 */
 
 #include <include/log/log_manager.h>
+#include <include/server/server.h>
 
+/*!
+ * The constructor.
+ *
+ * \param ff The FiltersFactory object.
+ * \param level The default level of logging.
+ */
 LogManager::LogManager (FiltersFactory* ff,
                         LoggingLevel level) : level (level)
 {
@@ -27,6 +34,9 @@
   computeNewLine ();
 }
 
+/*!
+ * The destructor. Deallocates all the LogStream objects.
+ */
 LogManager::~LogManager ()
 {
   if (!empty ())
@@ -37,6 +47,7 @@
 
 /*!
  * Precalculate the newline string for the host operating system.
+ *
  * \return 0 on success, 1 on error.
  */
 int
@@ -51,6 +62,11 @@
   return 1;
 }
 
+/*!
+ * Empty the LogManager.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::clear ()
 {
@@ -62,10 +78,25 @@
     }
   logStreams.clear ();
   owners.clear ();
+  logStreamOwners.clear ();
   mutex->unlock ();
   return !empty ();
 }
 
+/*!
+ * 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.
+ *
+ * \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.
+ * \param location The location string for the new LogStream.
+ * \param filters A list of strings, each representing a valid filter name.
+ * \param cycle The cycle value for the LogStream.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::add (void* owner, string type, string location, 
                  list<string>& filters, u_long cycle)
@@ -81,10 +112,30 @@
                      add (owner, type, location, ls));
         }
     }
+  else if (!contains (owner))
+    {
+      success = (add (owner) || add (owner, type) ||
+                 add (owner, type, location, logStreams[location]));
+      if (!success && Server::getInstance ())
+        {
+          string msg ("LogManager: Warning, shared location \'");
+          msg.append (location);
+          msg.append ("\' detected.");
+          Server::getInstance ()->logWriteln (msg.c_str (), 
MYSERVER_LOG_MSG_WARNING);
+        }
+    }
   mutex->unlock ();
   return success;
 }
 
+/*!
+ * Helper method that inserts a new owner within the LogManager
+ * data structures.
+ *
+ * \param owner The new owner.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::add (void* owner)
 {
@@ -101,6 +152,14 @@
   return success;
 }
 
+/*!
+ * Helper method that associates a new log category to an object.
+ *
+ * \param owner The owner of the new log category.
+ * \param type The name of the new log category.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::add (void* owner, string type)
 {
@@ -117,11 +176,23 @@
   return success;
 }
 
+/*!
+ * Helper method, that physically adds a LogStream to the
+ * LogManager data structures.
+ *
+ * \param owner The owner object.
+ * \param type The log category.
+ * \param location The location string.
+ * \param ls The LogStream object to add.
+ *
+ * \return 0 on success, 1 on error.
+ */
 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) && contains (owner, type, location))
     {
       return 0;
@@ -132,7 +203,13 @@
 /*
  * Remove all the logs owned by `owner' and `owner' as well.
  * After the call to this method, a call to contains (owner)
- * must return false.
+ * must return false. If `owner' owns some LogStream shared with
+ * another object, that LogStream will be left within the LogManager
+ * until all its owners will be removed.
+ *
+ * \param owner The object whose LogStream objects will be removed.
+ *
+ * \return 0 on success, 1 on error.
  */
 int 
 LogManager::remove (void* owner)
@@ -141,7 +218,7 @@
   int success = 1;
   if (contains (owner))
     {
-      map<string, map<string, LogStream*> > *m = &owners[owner];
+      map<string, map<string, LogStream*> >* m = &owners[owner];
       map<string, map<string, LogStream*> >::iterator it_1;
       for (it_1 = m->begin (); it_1 != m->end (); it_1++)
         {
@@ -149,8 +226,13 @@
           map<string, LogStream*>::iterator it_2;
           for (it_2 = t->begin (); it_2 != t->end (); it_2++)
             {
-              delete it_2->second;
-              logStreams.erase (it_2->first);
+              logStreamOwners[it_2->first].remove (owner);
+              if (!logStreamOwners[it_2->first].size ())
+                {
+                  delete it_2->second;
+                  logStreams.erase (it_2->first);
+                  logStreamOwners.erase (it_2->first);
+                }
             }
           t->clear ();
         }
@@ -162,6 +244,20 @@
   return success;
 }
 
+/*!
+ * Deliver a message to a single LogStream object specified by the tuple
+ * <owner, type, location>.
+ *
+ * \param owner The object which owns the target stream.
+ * \param type The log category which the target stream belongs.
+ * \param location The target stream.
+ * \param evt The event of interest for the target stream. For the list
+ * of all events that can be notified, check the `log_stream.h' header.
+ * \param message The message to deliver.
+ * \param reply The recipient reply.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::notify (void* owner, string type, string location, 
                     LogStreamEvent evt, void* message, void* reply)
@@ -174,6 +270,19 @@
   return success;
 }
 
+/*!
+ * Delivery 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.
+ * \param type The log category which the target stream belongs.
+ * \param evt The event of interest for the target stream. For the list
+ * of all events that can be notified, check the `log_stream.h' header.
+ * \param message The message to deliver.
+ * \param reply The recipient reply.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::notify (void* owner, string type, LogStreamEvent evt, 
                     void* message, void* reply)
@@ -192,6 +301,17 @@
   return success;
 }
 
+/*!
+ * Delivery 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
+ * of all events that can be notified, check the `log_stream.h' header.
+ * \param message The message to deliver.
+ * \param reply The recipient reply.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::notify (void* owner, LogStreamEvent evt, void* message, 
                     void* reply)
@@ -210,6 +330,15 @@
   return success;
 }
 
+/*!
+ * Change the owner for all the LogStream objects owned by `owner'.
+ *
+ * \param owner The object which owns the target stream.
+ * \param uid The new uid for the stream.
+ * \param gid The new gid for the stream.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::chown (void* owner, int uid, int gid)
 {
@@ -219,6 +348,17 @@
   return notify (owner, MYSERVER_LOG_EVT_CHOWN, static_cast<void*>(message));
 }
 
+/*!
+ * Change the owner for a group of LogStream objects specified by the tuple
+ * <owner, type>.
+ *
+ * \param owner The object which owns the target stream.
+ * \param type The category where the target stream can be found.
+ * \param uid The new uid for the stream.
+ * \param gid The new gid for the stream.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::chown (void* owner, string type, int uid, int gid)
 {
@@ -228,6 +368,17 @@
   return notify (owner, MYSERVER_LOG_EVT_CHOWN, static_cast<void*>(message));
 }
 
+/*!
+ * Change the owner for a LogStream specified by the tuple <owner, type, 
location>.
+ *
+ * \param owner The object which owns the target stream.
+ * \param type The category where the target stream can be found.
+ * \param location The target stream.
+ * \param uid The new uid for the stream.
+ * \param gid The new gid for the stream.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::chown (void* owner, string type, string location, int uid, int gid)
 {
@@ -237,6 +388,18 @@
   return notify (owner, type, location, MYSERVER_LOG_EVT_CHOWN, 
static_cast<void*>(message));
 }
 
+/*!
+ * Write a message on all the LogStream objects owned by the `owner' object.
+ *
+ * \param owner The object that owns the category.
+ * \param message The message to write.
+ * \param appendNL If set, tells the LogManager to append a newline sequence
+ * to the message.
+ * \param level If less than the LogManager's logging level, the message
+ * will be discarded.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::log (void* owner, string message, bool appendNL,
                  LoggingLevel level)
@@ -258,6 +421,20 @@
   return success;
 }
 
+/*!
+ * Write a message on all the LogStream objects belonging to the `type'
+ * category of the `owner' object.
+ *
+ * \param owner The object that owns the category.
+ * \param type The log category where to write.
+ * \param message The message to write.
+ * \param appendNL If set, tells the LogManager to append a newline sequence
+ * to the message.
+ * \param level If less than the LogManager's logging level, the message
+ * will be discarded.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::log (void* owner, string type, string message, bool appendNL,
                  LoggingLevel level)
@@ -279,6 +456,22 @@
   return success;
 }
 
+/*!
+ * Write a message on a single LogStream, specified by the tuple
+ * <owner, type, location>.
+ *
+ * \param owner The object that owns the LogStream.
+ * \param type The log category where we want to write.
+ * \param location The target LogStream.
+ * \param message The message we want to write.
+ * \param appendNL a flag that, if set, tells the LogManager to append
+ * a new line sequence to the message, according to the host operating system
+ * convention.
+ * \param level The level of logging of this message. If it is less than
+ * the LogManager's level of logging, the message will be discarded.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::log (void* owner, string type, string location, string message, 
                  bool appendNL, LoggingLevel level)
@@ -300,36 +493,92 @@
   return success;
 }
 
+/*!
+ * Close all the LogStream objects owned by `owner'.
+ *
+ * \param owner The object about which we want to close all the LogStream
+ * objects.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::close (void* owner)
 {
   return notify (owner, MYSERVER_LOG_EVT_CLOSE);
 }
 
+/*!
+ * Close all the LogStream objects belonging to the `type'
+ * category of the `owner' object.
+ *
+ * \param owner The object that owns the `type' category.
+ * \param type The log category to close.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::close (void* owner, string type)
 {
   return notify (owner, type, MYSERVER_LOG_EVT_CLOSE);
 }
 
+/*!
+ * Close the LogStream specified by the tuple <owner, type, location>.
+ *
+ * \param owner The object that owns the LogStream.
+ * \param type The log category.
+ * \param location The LogStream location.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::close (void* owner, string type, string location)
 {
   return notify (owner, type, location, MYSERVER_LOG_EVT_CLOSE);
 }
 
+/*!
+ * Set the cycle value for all the LogStream objects owned by `owner'.
+ *
+ * \param owner The object that wants to modify the cycle value for its 
+ * LogStream objects.
+ * \param cycle The new cycle value.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::setCycle (void* owner, u_long cycle)
 {
   return notify (owner, MYSERVER_LOG_EVT_SET_CYCLE, 
static_cast<void*>(&cycle));
 }
 
+/*!
+ * Set the cycle value for all the LogStream objects belonging to the
+ * log category `type' of the `owner' object.
+ *
+ * \param owner The object which owns the `type' category.
+ * \param type The log category to modify.
+ * \param cycle The new cycle value.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::setCycle (void* owner, string type, u_long cycle)
 {
   return notify (owner, type, MYSERVER_LOG_EVT_SET_CYCLE, 
static_cast<void*>(&cycle));
 }
 
+/*!
+ * Set the cycle value for the LogStream specified by the tuple
+ * <owner, type, location>.
+ *
+ * \param owner The object that owns the LogStream.
+ * \param type The log category which the LogStream belongs.
+ * \param location The LogStream location.
+ * \param cycle The new cycle value.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::setCycle (void* owner, string type, string location, u_long cycle)
 {
@@ -337,6 +586,16 @@
                  static_cast<void*>(&cycle));
 }
 
+/*!
+ * Return the cycle value for the `location' LogStream.
+ *
+ * \param location The LogStream about which we want to retrieve the cycle 
+ * value.
+ * \param cycle On a successful method execution, the cycle value will be 
+ * placed here.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::getCycle (string location, u_long* cycle)
 {
@@ -348,6 +607,16 @@
   return 1;
 }
 
+/*!
+ * Retrieve a list of strings each representing a LogStream location
+ * of all the LogStreams owned by the owner object.
+ *
+ * \param owner The object about which we want to get all LogStream
+ * object.
+ * \param l The list that will be filled on successful method execution.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::get (void* owner, list<string>* l)
 {
@@ -370,6 +639,16 @@
   return 1;
 }
 
+/*!
+ * Retrieve a list of strings each representing the location of
+ * a LogStream belonging to the `type' category of the `owner' object.
+ *
+ * \param owner The object that should own the wanted information.
+ * \param type The log category where the wanted information can be found.
+ * \param l The list that will be filled on a successful method execution.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::get (void* owner, string type, list<string>* l)
 {
@@ -387,6 +666,19 @@
   return 1;
 }
 
+/*!
+ * Get a LogStream object by its location, the log category which it belongs
+ * and its owner.
+ *
+ * \param owner The object that should own the LogStream.
+ * \param type The log category which the LogStream that will be retrieved 
should
+ * belong.
+ * \param location The LogStream location.
+ * \param ls On a successful method execution, the LogStream object will be
+ * placed here.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::get (void* owner, string type, string location, LogStream** ls)
 {
@@ -398,6 +690,15 @@
   return 1;
 }
 
+/*!
+ * Retrieve the filters list used by the `location' LogStream.
+ *
+ * \param location The LogStream about which we want to retrieve the
+ * filters list.
+ * \param l A list that will be filled by a successful method execution.
+ *
+ * \return 0 on success, 1 on error.
+ */
 int
 LogManager::getFilters (string location, list<string>* l)
 {
@@ -409,6 +710,15 @@
   return 1;
 }
 
+/*!
+ * Set the logging level for the LogManager. Check
+ * the `log_stream.h' header file for more information about
+ * the LoggingLevel enumeration.
+ *
+ * \param level the new level of logging.
+ *
+ * \return the old level of logging.
+ */
 LoggingLevel
 LogManager::setLevel (LoggingLevel level)
 {
@@ -419,12 +729,24 @@
   return oldLevel;
 }
 
+/*!
+ * Return the level of logging used by the LogManager. Check
+ * the `log_stream.h' header file for more information about
+ * the LoggingLevel enumeration.
+ *
+ * \return the level of logging used by the LogManager
+ */
 LoggingLevel
 LogManager::getLevel ()
 {
   return level;
 }
 
+/*!
+ * Set the FiltersFactory used by the LogManager.
+ *
+ * \param ff The FiltersFactory object that will be used by the LogManager.
+ */
 void
 LogManager::setFiltersFactory (FiltersFactory* ff)
 {
@@ -433,42 +755,104 @@
   mutex->unlock ();
 }
 
+/*!
+ * Get the FiltersFactory used by the LogManager.
+ *
+ * \return the FiltersFactory object used by the LogManager.
+ */
 FiltersFactory* 
 LogManager::getFiltersFactory ()
 {
   return ff;
 }
 
+/*!
+ * Check if the LogManager is empty.
+ *
+ * \return true if the LogManager is empty.
+ */
 bool
 LogManager::empty ()
 {
-  return logStreams.size () == 0 && owners.size () == 0;
+  return 
+    logStreams.size () == 0 &&
+    owners.size () == 0 &&
+    logStreamOwners.size () == 0;
 }
 
+/*!
+ * Check if a LogStream which points to `location' is already
+ * present in the LogManager.
+ *
+ * \param location The `location' about which we want to know the
+ * existence.
+ *
+ * \return true if `location' is already present within the LogManager.
+ */
 bool
 LogManager::contains (string location)
 {
-  return logStreams.count (location) > 0;
+  return logStreams.count (location) > 0 && 
+    logStreamOwners[location].size ();
 }
 
+/*!
+ * Query the LogManager to ask it if the `owner' object is actually
+ * present in it.
+ *
+ * \param owner The object about which we want to know the existence within
+ * the LogManager.
+ *
+ * \return true if `owner' is actually present within the LogManager.
+ */
 bool
 LogManager::contains (void* owner)
 {
   return owners.count (owner) > 0;
 }
 
+/*!
+ * Query the LogManager to ask if the category `type' belongs to
+ * the `owner' object.
+ *
+ * \param owner An object that may own some log category.
+ * \param type The category we are interested in.
+ *
+ * \return true if the tupe <owner, type> exists.
+ */
 bool
 LogManager::contains (void* owner, string type)
 {
   return owners[owner].count (type) > 0;
 }
 
+/*!
+ * Query the LogManager to ask it if `location' belongs to the `type'
+ * log category of the `owner' object.
+ *
+ * \param owner An object that may own some LogStream object.
+ * \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.
+ */
 bool
 LogManager::contains (void* owner, string type, string location)
 {
-  return owners[owner][type].count (location) > 0;
+  return owners[owner][type].count (location) > 0 &&
+    (find (logStreamOwners[location].begin (), logStreamOwners[location].end 
(), owner) != 
+     logStreamOwners[location].end ());
 }
 
+/*!
+ * Given an owner object, get the number of LogStream objects that
+ * belong to all its log categories.
+ *
+ * \param owner A pointer to an object that may own some LogStream.
+ *
+ * \return The total number of LogStream objects belonging to all
+ * its log categories.
+ */
 int 
 LogManager::count (void* owner)
 {
@@ -482,14 +866,53 @@
   return size;
 }
 
+/*!
+ * Given an owner and one of its log categories, retrieve the
+ * number of LogStream objects which belongs to that category.
+ *
+ * \param owner A pointer to an object which may own some LogStream.
+ * \param type A log category owned by `owner'.
+ *
+ * \return The number of LogStream objects that belong to `type'.
+ */
 int
 LogManager::count (void* owner, string type)
 {
   return owners[owner][type].size ();
 }
 
+/*!
+ * Given an owner object, one of its log categories and 
+ * a location, returns the number of occurrences of `location'.
+ *
+ * \param owner A pointer to an object that may own `location'.
+ * \param type The category of logs which `location' should belong.
+ * \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.
+ */
 int
 LogManager::count (void* owner, string type, string location)
 {
   return owners[owner][type].count (location);
 }
+
+/*!
+ * Retrieve a list of objects that are currently using `location'.
+ * 
+ * \param location We want to know which objects are using it.
+ * \param l The location owners will be inserted here.
+ *
+ * \return 0 if location is owned at least by an object, 1 else.
+ */
+int
+LogManager::getOwnersList (string location, list<void*>* l)
+{
+  if (contains (location))
+    {
+      *l = logStreamOwners[location];
+      return 0;
+    }
+  return 1;
+}

Modified: trunk/myserver/src/log/stream/log_stream.cpp
===================================================================
--- trunk/myserver/src/log/stream/log_stream.cpp        2008-11-10 20:02:16 UTC 
(rev 2958)
+++ trunk/myserver/src/log/stream/log_stream.cpp        2008-11-11 16:48:45 UTC 
(rev 2959)
@@ -112,12 +112,10 @@
 {
   mutex->lock ();
   int success = 1;
-  if (isOpened)
+  isOpened = fc->flush (&nbw) || out->close ();
+  if (!isOpened)
     {
-      if (!(isOpened = (fc->flush (&nbw) || out->close ())))
-        {
-          success = 0;
-        }
+      success = 0;
     }
   mutex->unlock ();
   return success;
@@ -130,33 +128,33 @@
     {
     case MYSERVER_LOG_EVT_SET_CYCLE:
       {
-        return setCycle (*static_cast<u_long*>(message));
+        return !isOpened || setCycle (*static_cast<u_long*>(message));
       }
       break;
     case MYSERVER_LOG_EVT_LOG:
       {
-        return log (*static_cast<string*>(message));
+        return !isOpened || log (*static_cast<string*>(message));
       }
       break;
     case MYSERVER_LOG_EVT_CLOSE:
       {
-        return close ();
+        return !isOpened || close ();
       }
       break;
     case MYSERVER_LOG_EVT_ADD_FILTER:
       {
-        return addFilter (static_cast<Filter*>(message));
+        return !isOpened || addFilter (static_cast<Filter*>(message));
       }
       break;
     case MYSERVER_LOG_EVT_CHOWN:
       {
-        return chown (static_cast<int*>(message)[0], 
-                      static_cast<int*>(message)[1]);
+        return !isOpened || chown (static_cast<int*>(message)[0], 
+                                   static_cast<int*>(message)[1]);
       }
       break;
     case MYSERVER_LOG_EVT_SET_MODE:
       {
-        return setMode (*static_cast<LoggingLevel*>(message));
+        return !isOpened || setMode (*static_cast<LoggingLevel*>(message));
       }
     default:
       return 1;

Modified: trunk/myserver/tests/test_log_manager.cpp
===================================================================
--- trunk/myserver/tests/test_log_manager.cpp   2008-11-10 20:02:16 UTC (rev 
2958)
+++ trunk/myserver/tests/test_log_manager.cpp   2008-11-11 16:48:45 UTC (rev 
2959)
@@ -18,6 +18,11 @@
 #include <list>
 #include <string>
 
+#ifdef NOT_WIN
+#include <unistd.h>
+#include <sys/types.h>
+#endif
+
 #include <cppunit/CompilerOutputter.h>
 #include <cppunit/extensions/TestFactoryRegistry.h>
 #include <cppunit/ui/text/TestRunner.h>
@@ -36,7 +41,7 @@
   CPPUNIT_TEST_SUITE (TestLogManager);
   CPPUNIT_TEST (testEmpty);
   CPPUNIT_TEST (testContains);
-  CPPUNIT_TEST (testAdd);
+  CPPUNIT_TEST (testSuccessfulAdd);
   CPPUNIT_TEST (testRemove);
   CPPUNIT_TEST (testLog);
   CPPUNIT_TEST (testClose);
@@ -48,8 +53,19 @@
   CPPUNIT_TEST (testCount);
   CPPUNIT_TEST (testGet);
   CPPUNIT_TEST (testReOpen);
-  CPPUNIT_TEST (testAddWithNotExistingFilter);
+  CPPUNIT_TEST (testNotExistingFilterAdd);
+  CPPUNIT_TEST (testFailureAdd);
+  CPPUNIT_TEST (testSharedAdd);
+  CPPUNIT_TEST (testSharedGet);
+  CPPUNIT_TEST (testSharedRemove);
+  CPPUNIT_TEST (testFailureLog);
+  CPPUNIT_TEST (testChown);
   CPPUNIT_TEST_SUITE_END ();
+  
+  class AnObject
+  {
+  };
+
 public:
   void setUp ()
   {
@@ -70,18 +86,17 @@
     CPPUNIT_ASSERT (!lm->contains ("foo"));
   }
 
-  void testAdd ()
+  void testSuccessfulAdd ()
   {
     list<string> filters;
-    CPPUNIT_ASSERT (lm->add (this, "test", "foo", filters, 0));
-    CPPUNIT_ASSERT (lm->add (this, "test", "file", filters, 0));
+
     CPPUNIT_ASSERT (!lm->add (this, "test", "file://foo", filters, 0));
-    CPPUNIT_ASSERT (lm->add (this, "test", "file://foo", filters, 0));
   }
 
   void testRemove ()
   {
     list<string> filters;
+
     CPPUNIT_ASSERT (lm->remove (this));
     lm->add (this, "test", "file://foo", filters, 0);
     CPPUNIT_ASSERT (!lm->remove (this));
@@ -92,13 +107,14 @@
   {
     string message ("A message");
     list<string> filters;
+    File f;
+    char buf[message.size () + 1];
+    u_long nbr;
+
     lm->add (this, "test", "file://foo", filters, 0);
     lm->log (this, "test", "file://foo", message);
     lm->close (this);
-    File f;
     f.openFile ("foo", FileStream::defaultFileMask);
-    char buf[message.size () + 1];
-    u_long nbr;
     f.read (buf, message.size () + 1, &nbr);
     buf[nbr] = '\0';
     CPPUNIT_ASSERT (!message.compare (buf));
@@ -108,6 +124,7 @@
   void testClose ()
   {
     list<string> filters;
+
     lm->add (this, "test", "file://foo", filters, 0);
     CPPUNIT_ASSERT (lm->close (this, "test", "file://bar"));
     CPPUNIT_ASSERT (!lm->close (this, "test", "file://foo"));
@@ -119,22 +136,24 @@
     string message ("A message\n");
     string message1 ("Another message\n");
     list<string> filters;
+    File f;
+    char buf[64];
+    u_long nbr;
+    LogStream* ls;
+    list<string> cs;
+    list<string>::iterator it;
+
     lm->add (this, "test", "file://foo", filters, 10);
     lm->log (this, "test", "file://foo", message);
     CPPUNIT_ASSERT (!lm->log (this, "test", "file://foo", message1));
     lm->close (this, "test", "file://foo");
-    File f;
     f.openFile ("foo", FileStream::defaultFileMask);
-    char buf[64];
-    u_long nbr;
     f.read (buf, 64, &nbr);
     buf[nbr] = '\0';
     f.close ();
     CPPUNIT_ASSERT (!message1.compare (buf));
-    LogStream* ls;
     CPPUNIT_ASSERT (!lm->get (this, "test", "file://foo", &ls));
-    list<string> cs = ls->getCycledStreams ();
-    list<string>::iterator it;
+    cs = ls->getCycledStreams ();
     for (it = cs.begin (); it != cs.end (); it++)
       {
         f.openFile (*it, FileStream::defaultFileMask);
@@ -149,6 +168,7 @@
   void testLevel ()
   {
     list<string> filters;
+
     lm->add (this, "test", "file://foo", filters, 0);
     lm->setLevel (MYSERVER_LOG_MSG_WARNING);
     CPPUNIT_ASSERT (lm->log (this, "test", "a message", false, 
MYSERVER_LOG_MSG_INFO));
@@ -161,6 +181,7 @@
   void testClear ()
   {
     list<string> filters;
+
     lm->add (this, "test", "file://foo", filters, 0);
     lm->add (this, "test", "console://stdout", filters, 0);
     CPPUNIT_ASSERT (!lm->empty ());
@@ -172,18 +193,19 @@
   {
     list<string> filters;
     string message ("a message");
+    File f;
+    char gzipChainComp[64];
+    char gzipChainDecomp[64];
+    u_long nbr;
+    GzipDecompress gzdc;
+
     filters.push_back ("gzip");
     lm->add (this, "test", "file://foo", filters, 0);
     lm->log (this, "test", "file://foo", message);
     lm->close (this, "test", "file://foo");
-    File f;
     f.openFile ("foo", FileStream::defaultFileMask);
-    char gzipChainComp[64];
-    char gzipChainDecomp[64];
-    u_long nbr;
     f.read (gzipChainComp, 64, &nbr);
     f.close ();
-    GzipDecompress gzdc;
     gzdc.decompress (&gzipChainComp[gzdc.headerSize ()], 
                      nbr - gzdc.headerSize (),
                      gzipChainDecomp, 64);
@@ -204,6 +226,7 @@
     u_long nbr = 0;
     list<string> cs;
     list<string>::iterator it;
+    LogStream* ls;
     
     filters.push_back ("gzip");
     lm->add (this, "test", "file://foo", filters, cycleLog);
@@ -217,7 +240,6 @@
                      gzipDecomp, 128);
     gzipDecomp[message1.size ()] = '\0';
     CPPUNIT_ASSERT (!message1.compare (gzipDecomp));
-    LogStream* ls;
     CPPUNIT_ASSERT (!lm->get (this, "test", "file://foo", &ls));
     cs = ls->getCycledStreams ();
     for (it = cs.begin (); it != cs.end (); it++)
@@ -237,6 +259,7 @@
   void testCount ()
   {
     list<string> filters;
+
     lm->add (this, "test", "console://stdout", filters, 0);
     lm->add (this, "test_1", "console://stderr", filters, 0);
     lm->add (this, "test_1", "file://foo", filters, 0);
@@ -257,6 +280,7 @@
     list<string> filters;
     list<string> tmp;
     list<string> l;
+    LogStream* ls;
     
     CPPUNIT_ASSERT (lm->get (this, &l));
     CPPUNIT_ASSERT (lm->get (this, "test", &l));
@@ -275,7 +299,6 @@
     tmp.push_back ("console://stdout");
     tmp.sort (); l.sort ();
     CPPUNIT_ASSERT (tmp == l);
-    LogStream* ls;
     CPPUNIT_ASSERT (!lm->get (this, "test", "console://stdout", &ls));
   }
 
@@ -293,7 +316,6 @@
     message1.assign (oss.str ());
     oss << "message2" << endl;
     message2.assign (oss.str ());
-
     lm->add (this, "test", "file://foo", filters, 0);
     lm->log (this, "test", "file://foo", message1);
     lm->clear ();
@@ -307,7 +329,7 @@
     CPPUNIT_ASSERT (!string (buf).compare (message1.append (message2)));
   }
 
-  void testAddWithNotExistingFilter ()
+  void testNotExistingFilterAdd ()
   {
     list<string> filters;
 
@@ -317,6 +339,78 @@
     CPPUNIT_ASSERT (lm->add (this, "test", "console://stdout", filters, 0));
   }
 
+  void testFailureAdd ()
+  {
+    list<string> filters;
+
+    CPPUNIT_ASSERT (lm->add (this, "test", "foo", filters, 0));
+    CPPUNIT_ASSERT (lm->add (this, "test", "file", filters, 0));
+    CPPUNIT_ASSERT (!lm->add (this, "test", "file://foo", filters, 0));
+    CPPUNIT_ASSERT (lm->add (this, "test", "file://foo", filters, 0));
+  }
+
+  void testSharedAdd ()
+  {
+    list<string> filters;
+    AnObject anObject;
+
+    CPPUNIT_ASSERT (!lm->add (this, "test", "file://foo", filters, 0));
+    CPPUNIT_ASSERT (!lm->add (&anObject, "test", "file://foo", filters, 0));
+  }
+
+  void testSharedGet ()
+  {
+    list<string> filters;
+    AnObject anObject;
+    list<void*> l;
+
+    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);
+  }
+
+  void testSharedRemove ()
+  {
+    list<string> filters;
+    AnObject anObject;
+    AnObject anotherObject;
+    list<void*> l;
+
+    CPPUNIT_ASSERT (!lm->add (this, "test", "file://foo", filters, 0));
+    CPPUNIT_ASSERT (!lm->add (&anObject, "test1", "file://foo", filters, 0));
+    CPPUNIT_ASSERT (!lm->add (&anotherObject, "test2", "file://foo", filters, 
0));
+    lm->remove (this);
+    CPPUNIT_ASSERT (lm->contains ("file://foo"));
+    lm->getOwnersList ("file://foo", &l);
+    CPPUNIT_ASSERT (l.size () == 2);
+    lm->remove (&anObject);
+    CPPUNIT_ASSERT (lm->contains ("file://foo"));
+    lm->getOwnersList ("file://foo", &l);
+    CPPUNIT_ASSERT (l.size () == 1);
+    lm->remove (&anotherObject);
+    CPPUNIT_ASSERT (lm->getOwnersList ("file://foo", &l));
+  }
+  
+  void testFailureLog ()
+  {
+    list<string> filters;
+
+    lm->add (this, "test", "file://foo", filters, 0);
+    lm->close (this, "test", "file://foo");
+    CPPUNIT_ASSERT (lm->log (this, "test", "file://foo", "message"));
+  }
+
+  void testChown ()
+  {
+#ifdef NOT_WIN
+    list<string> filters;
+
+    lm->add (this, "test", "file://foo", filters, 0);
+    CPPUNIT_ASSERT (!lm->chown (this, "test", "file://foo", getuid (), getgid 
()));
+#endif
+  }
+
   void tearDown ()
   {
     delete lm;






reply via email to

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