Bug 792546 - Part 3: Move logging functions into standalone module; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Mon, 24 Sep 2012 13:43:02 -0700
changeset 111094 6fc02deb63dad3f73af99b488db40bf2dccb5880
parent 111093 da562c88998fd83cd62eebecbfdd900d159db25e
child 111095 b85f209e6072102cb17c1841de1f956ee9f6a326
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersrnewman
bugs792546
milestone18.0a1
Bug 792546 - Part 3: Move logging functions into standalone module; r=rnewman
services/common/Makefile.in
services/common/modules-testing/logging.js
services/common/tests/unit/head_helpers.js
services/common/tests/unit/test_load_modules.js
--- a/services/common/Makefile.in
+++ b/services/common/Makefile.in
@@ -18,16 +18,17 @@ modules := \
   storageservice.js \
   stringbundle.js \
   tokenserverclient.js \
   utils.js \
   $(NULL)
 
 testing_modules := \
   aitcserver.js \
+  logging.js \
   storageserver.js \
   utils.js \
   $(NULL)
 
 TEST_DIRS += tests
 
 PREF_JS_EXPORTS = $(srcdir)/services-common.js
 
new file mode 100644
--- /dev/null
+++ b/services/common/modules-testing/logging.js
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const EXPORTED_SYMBOLS = [
+  "getTestLogger",
+  "initTestLogging",
+];
+
+const {utils: Cu} = Components;
+
+Cu.import("resource://services-common/log4moz.js");
+
+function initTestLogging(level) {
+  function LogStats() {
+    this.errorsLogged = 0;
+  }
+  LogStats.prototype = {
+    format: function format(message) {
+      if (message.level == Log4Moz.Level.Error) {
+        this.errorsLogged += 1;
+      }
+
+      return message.loggerName + "\t" + message.levelDesc + "\t" +
+        message.message + "\n";
+    }
+  };
+  LogStats.prototype.__proto__ = new Log4Moz.Formatter();
+
+  let log = Log4Moz.repository.rootLogger;
+  let logStats = new LogStats();
+  let appender = new Log4Moz.DumpAppender(logStats);
+
+  if (typeof(level) == "undefined") {
+    level = "Debug";
+  }
+  getTestLogger().level = Log4Moz.Level[level];
+  Log4Moz.repository.getLogger("Services").level = Log4Moz.Level[level];
+
+  log.level = Log4Moz.Level.Trace;
+  appender.level = Log4Moz.Level.Trace;
+  // Overwrite any other appenders (e.g. from previous incarnations)
+  log.ownAppenders = [appender];
+  log.updateAppenders();
+
+  return logStats;
+}
+
+function getTestLogger(component) {
+  return Log4Moz.repository.getLogger("Testing");
+}
+
--- a/services/common/tests/unit/head_helpers.js
+++ b/services/common/tests/unit/head_helpers.js
@@ -1,15 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-Cu.import("resource://testing-common/httpd.js");
 Cu.import("resource://services-common/log4moz.js");
 Cu.import("resource://services-common/utils.js");
+Cu.import("resource://testing-common/httpd.js");
+Cu.import("resource://testing-common/services-common/logging.js");
 
 let btoa = Cu.import("resource://services-common/log4moz.js").btoa;
 let atob = Cu.import("resource://services-common/log4moz.js").atob;
 
 function do_check_empty(obj) {
   do_check_attribute_count(obj, 0);
 }
 
@@ -40,53 +41,16 @@ function do_check_throws(aFunc, aResult,
  *
  * @param [arg0, arg1, arg2, ...]
  *        Any number of arguments to print out
  * @usage _("Hello World") -> prints "Hello World"
  * @usage _(1, 2, 3) -> prints "1 2 3"
  */
 let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" "));
 
-function initTestLogging(level) {
-  function LogStats() {
-    this.errorsLogged = 0;
-  }
-  LogStats.prototype = {
-    format: function BF_format(message) {
-      if (message.level == Log4Moz.Level.Error)
-        this.errorsLogged += 1;
-      return message.loggerName + "\t" + message.levelDesc + "\t" +
-        message.message + "\n";
-    }
-  };
-  LogStats.prototype.__proto__ = new Log4Moz.Formatter();
-
-  let log = Log4Moz.repository.rootLogger;
-  let logStats = new LogStats();
-  let appender = new Log4Moz.DumpAppender(logStats);
-
-  if (typeof(level) == "undefined") {
-    level = "Debug";
-  }
-  getTestLogger().level = Log4Moz.Level[level];
-  Log4Moz.repository.getLogger("Services").level = Log4Moz.Level[level];
-
-  log.level = Log4Moz.Level.Trace;
-  appender.level = Log4Moz.Level.Trace;
-  // Overwrite any other appenders (e.g. from previous incarnations)
-  log.ownAppenders = [appender];
-  log.updateAppenders();
-
-  return logStats;
-}
-
-function getTestLogger(component) {
-  return Log4Moz.repository.getLogger("Testing");
-}
-
 /**
  * Obtain a port number to run a server on.
  *
  * In the ideal world, this would be dynamic so multiple servers could be run
  * in parallel.
  */
 function get_server_port() {
   return 8080;
--- a/services/common/tests/unit/test_load_modules.js
+++ b/services/common/tests/unit/test_load_modules.js
@@ -9,16 +9,17 @@ const modules = [
   "storageservice.js",
   "stringbundle.js",
   "tokenserverclient.js",
   "utils.js",
 ];
 
 const test_modules = [
   "aitcserver.js",
+  "logging.js",
   "storageserver.js",
 ];
 
 function run_test() {
   for each (let m in modules) {
     let resource = "resource://services-common/" + m;
     Components.utils.import(resource, {});
   }