Skip to content

Commit

Permalink
[CB-773] wrapper JSON.stringify calls in loggers
Browse files Browse the repository at this point in the history
Added try/catch wrappers around iOS's console's and
the logger's usage of JSON.stringify().

Added tests for logger; tested console in a Cordova app.

Drive by enhancement, added a grunt file which watches
the source files and runs grunt.  Replacement for "wr"
usage.
  • Loading branch information
pmuellr committed Jun 5, 2012
1 parent ea6b03d commit 29e353b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
75 changes: 75 additions & 0 deletions grunt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// open "http://search.npmjs.org/#/grunt" ; sudo npm -g install grunt

var child_process = require("child_process")

//------------------------------------------------------------------------------
// list of source files to watch
//------------------------------------------------------------------------------
var sourceFiles = [
"build/**/*.js",
"grunt.js",
"Jakefile",
"lib/**/*.js",
"test/**/*.js"
]

//------------------------------------------------------------------------------
var gruntConfig = {
watch: {
jake: {
files: sourceFiles,
tasks: ["jake"]
}
}
}

//------------------------------------------------------------------------------
// run "jake"
//------------------------------------------------------------------------------
function jakeTask(grunt) {
var done = this.async()
var make = child_process.spawn('jake')

make.stdout.on("data", function(data) {
grunt.log.write("" + data)
})

make.stderr.on("data", function(data) {
grunt.log.error("" + data)
})

make.on("exit", function(code) {
if (code === 0) return done(true)

grunt.log.writeln("error running jake", code)
return done(false)
})
}

//------------------------------------------------------------------------------
module.exports = function(grunt) {
grunt.initConfig(gruntConfig)

grunt.registerTask("default", "watch")
grunt.registerTask("jake", "run jake", function(){jakeTask.call(this,grunt)}
)
}
20 changes: 14 additions & 6 deletions lib/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ utils.format = function(formatString /* ,... */) {
utils.vformat = function(formatString, args) {
if (formatString === null || formatString === undefined) return "";
if (arguments.length == 1) return formatString.toString();
if (typeof formatString != "string") return formatString.toString();

var pattern = /(.*?)%(.)(.*)/;
var rest = formatString.toString();
var rest = formatString;
var result = [];

while (args.length) {
Expand Down Expand Up @@ -166,13 +167,20 @@ function UUIDcreatePart(length) {
//------------------------------------------------------------------------------
function formatted(object, formatChar) {

switch(formatChar) {
case 'j':
case 'o': return JSON.stringify(object);
case 'c': return '';
try {
switch(formatChar) {
case 'j':
case 'o': return JSON.stringify(object);
case 'c': return '';
}
}
catch (e) {
return "error JSON.stringify()ing argument: " + e;
}

if (null === object) return Object.prototype.toString.call(object);
if ((object === null) || (object === undefined)) {
return Object.prototype.toString.call(object);
}

return object.toString();
}
7 changes: 6 additions & 1 deletion lib/ios/plugin/ios/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ DebugConsole.prototype.setLevel = function(level) {
var stringify = function(message) {
try {
if (typeof message === "object" && JSON && JSON.stringify) {
return JSON.stringify(message);
try {
return JSON.stringify(message);
}
catch (e) {
return "error JSON.stringify()ing argument: " + e;
}
} else {
return message.toString();
}
Expand Down
20 changes: 20 additions & 0 deletions test/test.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ describe("utils", function () {
it("handles ('1%x2%y3',4,5)", function() {
expect(utils.format('1%x2%y3',4,5)).toBe('14253')
})

var cycler

beforeEach(function(){
cycler = {a: 1}
cycler.cycler = cycler
})

it("handles cyclic objects as format string", function() {
expect(utils.format(cycler)).toBe("[object Object]")
})

it("handles cyclic objects as object arg", function() {
expect(utils.format("%o",cycler)).toMatch(/^error JSON\.stringify\(\)ing argument:/)
})

it("handles cyclic objects as string arg", function() {
expect(utils.format("%s",cycler)).toBe("[object Object]")
})

})

});

0 comments on commit 29e353b

Please sign in to comment.