[CalendarServer-changes] [8602] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Jan 29 08:42:56 PST 2012


Revision: 8602
          http://trac.macosforge.org/projects/calendarserver/changeset/8602
Author:   cdaboo at apple.com
Date:     2012-01-29 08:42:54 -0800 (Sun, 29 Jan 2012)
Log Message:
-----------
Added SQL config-based logging options.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tap/util.py
    CalendarServer/trunk/twistedcaldav/stdconfig.py
    CalendarServer/trunk/txdav/common/datastore/sql.py
    CalendarServer/trunk/txdav/common/datastore/test/test_sql.py

Modified: CalendarServer/trunk/calendarserver/tap/util.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/util.py	2012-01-29 02:05:45 UTC (rev 8601)
+++ CalendarServer/trunk/calendarserver/tap/util.py	2012-01-29 16:42:54 UTC (rev 8602)
@@ -1,6 +1,6 @@
 # -*- test-case-name: calendarserver.tap.test.test_caldav -*-
 ##
-# Copyright (c) 2005-2011 Apple Inc. All rights reserved.
+# Copyright (c) 2005-2012 Apple Inc. All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -231,7 +231,10 @@
         return CommonSQLDataStore(
             txnFactory, notifierFactory, FilePath(config.AttachmentsRoot),
             config.EnableCalDAV, config.EnableCardDAV,
-            quota=quota
+            quota=quota,
+            logLabels=config.LogDatabase.LabelsInSQL,
+            logStats=config.LogDatabase.Statistics,
+            logSQL=config.LogDatabase.SQLStatements,
         )
     else:
         return CommonFileDataStore(

Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/stdconfig.py	2012-01-29 02:05:45 UTC (rev 8601)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py	2012-01-29 16:42:54 UTC (rev 8602)
@@ -1,6 +1,6 @@
 # -*- test-case-name: twistedcaldav.test.test_stdconfig -*-
 ##
-# Copyright (c) 2005-2011 Apple Inc. All rights reserved.
+# Copyright (c) 2005-2012 Apple Inc. All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -442,7 +442,13 @@
 
     "GlobalStatsSocket"           : "caldavd-stats.sock", 
     "GlobalStatsLoggingPeriod"    : 60, 
-    "GlobalStatsLoggingFrequency" : 12, 
+    "GlobalStatsLoggingFrequency" : 12,
+    
+    "LogDatabase" : {
+        "LabelsInSQL"   : False,
+        "Statistics"    : False,
+        "SQLStatements" : False,
+    },
 
     #
     # SSL/TLS

Modified: CalendarServer/trunk/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql.py	2012-01-29 02:05:45 UTC (rev 8601)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py	2012-01-29 16:42:54 UTC (rev 8602)
@@ -80,6 +80,7 @@
 from twistedcaldav.dateops import datetimeMktime, parseSQLTimestamp,\
     pyCalendarTodatetime
 
+from cStringIO import StringIO
 from sqlparse import parse
 import collections
 import sys
@@ -132,7 +133,8 @@
 
     def __init__(self, sqlTxnFactory, notifierFactory, attachmentsPath,
                  enableCalendars=True, enableAddressBooks=True,
-                 label="unlabeled", quota=(2 ** 20)):
+                 label="unlabeled", quota=(2 ** 20),
+                 logLabels=False, logStats=False, logSQL=False):
         assert enableCalendars or enableAddressBooks
 
         self.sqlTxnFactory = sqlTxnFactory
@@ -142,6 +144,9 @@
         self.enableAddressBooks = enableAddressBooks
         self.label = label
         self.quota = quota
+        self.logLabels = logLabels
+        self.logStats = logStats
+        self.logSQL = logSQL
         self._migrating = False
         self._enableNotifications = True
 
@@ -269,7 +274,7 @@
         self.dialect = sqlTxn.dialect
         
         # FIXME: want to pass a "debug" option in to enable this via config - off for now
-        self._stats = None #TransactionStatsCollector()
+        self._stats = TransactionStatsCollector() if self._store.logStats else None
 
 
     def store(self):
@@ -535,6 +540,10 @@
         """
         if self._stats:        
             self._stats.startStatement(a[0])
+        if self._store.logLabels:
+            a = ("-- Label: %s\n" % (self._label.replace("%", "%%"),) + a[0],) + a[1:]
+        if self._store.logSQL:
+            log.error("SQL: %r %r" % (a, kw,))
         results = (yield self._sqlTxn.execSQL(*a, **kw))
         if self._stats:        
             self._stats.endStatement()
@@ -567,8 +576,10 @@
                 operation()
             return ignored
 
-        if self._stats:        
-            self._stats.printReport()
+        if self._stats:
+            s = StringIO()
+            self._stats.printReport(s)
+            log.error(s.getvalue())
 
         return self._sqlTxn.commit().addCallback(postCommit)
 

Modified: CalendarServer/trunk/txdav/common/datastore/test/test_sql.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/test_sql.py	2012-01-29 02:05:45 UTC (rev 8601)
+++ CalendarServer/trunk/txdav/common/datastore/test/test_sql.py	2012-01-29 16:42:54 UTC (rev 8602)
@@ -13,17 +13,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 ##
-from txdav.common.datastore.sql_tables import schema
-from twext.enterprise.dal.syntax import Select
-from txdav.common.icommondatastore import AllRetriesFailed
 
 """
 Tests for L{txdav.common.datastore.sql}.
 """
 
+from twext.enterprise.dal.syntax import Select
 from twisted.internet.defer import inlineCallbacks
 from twisted.trial.unittest import TestCase
+from txdav.common.datastore.sql_tables import schema
 from txdav.common.datastore.test.util import CommonCommonTests, buildStore
+from txdav.common.icommondatastore import AllRetriesFailed
 
 
 class SubTransactionTests(CommonCommonTests, TestCase):
@@ -48,6 +48,28 @@
 
 
     @inlineCallbacks
+    def test_logging(self):
+        """
+        txn.execSQL works with all logging options on.
+        """
+        
+        # Patch config to turn on logging then rebuild the store
+        self.patch(self._sqlStore, "logLabels", True)
+        self.patch(self._sqlStore, "logStats", True)
+        self.patch(self._sqlStore, "logSQL", True)
+
+        txn = self.transactionUnderTest()
+        cs = schema.CALENDARSERVER
+        version = (yield Select(
+                [cs.VALUE,],
+                From=cs,
+                Where=cs.NAME == 'VERSION',
+            ).on(txn))
+        self.assertNotEqual(version, None)
+        self.assertEqual(len(version), 1)
+        self.assertEqual(len(version[0]), 1)
+
+    @inlineCallbacks
     def test_subtransactionOK(self):
         """
         txn.subtransaction runs loop once.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120129/ed29b01f/attachment.html>


More information about the calendarserver-changes mailing list