Merge lp:~jamesh/bindwood/logging into lp:bindwood
- logging
- Merge into trunk
Proposed by
James Henstridge
Status: | Merged |
---|---|
Approved by: | James Henstridge |
Approved revision: | 31 |
Merged at revision: | 32 |
Proposed branch: | lp:~jamesh/bindwood/logging |
Merge into: | lp:bindwood |
Diff against target: |
921 lines (+182/-144) 3 files modified
modules/bindwood.jsm (+114/-143) modules/logging.jsm (+61/-0) mozmill/tests/test_addbookmark.js (+7/-1) |
To merge this branch: | bzr merge lp:~jamesh/bindwood/logging |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
James Tait (community) | Approve | ||
Zachery Bir (community) | Approve | ||
Review via email: mp+50549@code.launchpad.net |
Commit message
Extract the logging code from the Bindwood object so that it can be reused in other modules.
Description of the change
Extract the logging code from the Bindwood object so that it can be reused in other modules.
To post a comment you must log in.
lp:~jamesh/bindwood/logging
updated
- 31. By James Henstridge
-
Remove unneeded import from bindwood.jsm. Make addbookmark test a
little more reliable (although it might be better to disable it until
more of the rewrite is landed).
Revision history for this message
James Tait (jamestait) wrote : | # |
I am loving this work. Are there any plans to further factor common functionality like this out into a separate XUL extension that could be a prerequisite for other Firefox *and* Thunderbird extensions?
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'modules/bindwood.jsm' |
2 | --- modules/bindwood.jsm 2011-02-04 06:03:15 +0000 |
3 | +++ modules/bindwood.jsm 2011-02-21 06:45:07 +0000 |
4 | @@ -15,13 +15,15 @@ |
5 | */ |
6 | /* Lots and lots of debugging information */ |
7 | |
8 | -Components.utils.import("resource://gre/modules/utils.js"); |
9 | -Components.utils.import("resource://bindwood/desktopcouch.jsm"); |
10 | - |
11 | -var EXPORTED_SYMBOLS = ["Bindwood"]; |
12 | - |
13 | -var Cc = Components.classes; |
14 | -var Ci = Components.interfaces; |
15 | +const EXPORTED_SYMBOLS = ["Bindwood"]; |
16 | + |
17 | +const Cc = Components.classes; |
18 | +const Ci = Components.interfaces; |
19 | +const Cu = Components.utils; |
20 | + |
21 | +Cu.import("resource://gre/modules/utils.js"); |
22 | +Cu.import("resource://bindwood/desktopcouch.jsm"); |
23 | +Cu.import("resource://bindwood/logging.jsm"); |
24 | |
25 | var bookmarksService = Cc["@mozilla.org/browser/nav-bookmarks-service;1"] |
26 | .getService(Ci.nsINavBookmarksService); |
27 | @@ -31,8 +33,6 @@ |
28 | .getService(Ci.nsIUUIDGenerator); |
29 | var annotationService = Cc["@mozilla.org/browser/annotation-service;1"] |
30 | .getService(Ci.nsIAnnotationService); |
31 | -var consoleService = Cc["@mozilla.org/consoleservice;1"] |
32 | - .getService(Ci.nsIConsoleService); |
33 | var historyService = Cc["@mozilla.org/browser/nav-history-service;1"] |
34 | .getService(Ci.nsINavHistoryService); |
35 | var ioService = Cc["@mozilla.org/network/io-service;1"] |
36 | @@ -70,32 +70,6 @@ |
37 | seen_revisions: {}, |
38 | timestamps: {}, |
39 | |
40 | - // Debugging/Console Behavior |
41 | - writeMessage: function(aMessage) { |
42 | - // convenience method for logging. Way better than alert()s. |
43 | - var debug; |
44 | - try { |
45 | - debug = prefsService.getBoolPref('debug'); |
46 | - } catch(e) { |
47 | - debug = false; |
48 | - } |
49 | - if (debug) { |
50 | - consoleService.logStringMessage( |
51 | - "Bindwood: " + aMessage); |
52 | - } |
53 | - }, |
54 | - |
55 | - writeError: function(aMessage, e) { |
56 | - // This should fire whether we're in DEBUG or not |
57 | - consoleService.logStringMessage( |
58 | - "Bindwood: " + aMessage + |
59 | - " message: '" + e.message + |
60 | - "', reason: '" + e.reason + |
61 | - "', description: '" + e.description + |
62 | - "', error: '" + e.error + |
63 | - "', raw error: '" + JSON.stringify(e) + "'"); |
64 | - }, |
65 | - |
66 | noteStartTime: function(key) { |
67 | var now = new Date(); |
68 | Bindwood.timestamps[key] = now.getTime(); |
69 | @@ -104,8 +78,7 @@ |
70 | noteEndTime: function(key) { |
71 | var now = new Date(); |
72 | var diff = now.getTime() - Bindwood.timestamps[key]; |
73 | - Bindwood.writeMessage( |
74 | - key + " took " + diff + " milliseconds"); |
75 | + Log.debug(key + " took " + diff + " milliseconds"); |
76 | delete Bindwood.timestamps[key]; |
77 | }, |
78 | |
79 | @@ -138,11 +111,11 @@ |
80 | // It ensures that we're only running that code on the first window. |
81 | |
82 | if (!Bindwood.running) { |
83 | - Bindwood.writeMessage("Getting started in init."); |
84 | + Log.debug("Getting started in init."); |
85 | Bindwood.currentProfile = Bindwood.extractProfileName( |
86 | directoryService.get('ProfD', Ci.nsIFile).path); |
87 | |
88 | - Bindwood.writeMessage("Got our profile: " + Bindwood.currentProfile); |
89 | + Log.debug("Got our profile: " + Bindwood.currentProfile); |
90 | |
91 | Bindwood.running = true; |
92 | Bindwood.Observer = new BindwoodBookmarkObserver(this); |
93 | @@ -161,13 +134,13 @@ |
94 | db_name, |
95 | function (database) { |
96 | if (database) { |
97 | - Bindwood.writeMessage("Got our db name: " + database.name); |
98 | + Log.debug("Got our db name: " + database.name); |
99 | Bindwood.couch = database; |
100 | |
101 | try { |
102 | Bindwood.startProcess(); |
103 | } catch(e) { |
104 | - Bindwood.writeError( |
105 | + Log.exception( |
106 | "Something wrong with the process, exiting.", e); |
107 | return; |
108 | } |
109 | @@ -176,7 +149,7 @@ |
110 | bookmarksService.removeObserver(Bindwood.Observer); |
111 | } |
112 | }, function (message) { |
113 | - Bindwood.writeMessage(message); |
114 | + Log.debug(message); |
115 | }); |
116 | }, |
117 | |
118 | @@ -212,23 +185,22 @@ |
119 | }, |
120 | |
121 | startProcess: function() { |
122 | - Bindwood.writeMessage("Starting process"); |
123 | + Log.debug("Starting process"); |
124 | Bindwood.last_seq = Bindwood.getLastSequence(); |
125 | - Bindwood.writeMessage( |
126 | - "Got our last known sequence number: " + Bindwood.last_seq); |
127 | + Log.debug("Got our last known sequence number: " + Bindwood.last_seq); |
128 | Bindwood.latest_modified = Bindwood.getLatestModified(); |
129 | - Bindwood.writeMessage( |
130 | + Log.debug( |
131 | "Got our latest known last_modified: " + Bindwood.latest_modified); |
132 | |
133 | - Bindwood.writeMessage("Ensuring the database exisits"); |
134 | + Log.debug("Ensuring the database exisits"); |
135 | Bindwood.ensureDatabase(); |
136 | - Bindwood.writeMessage("Ensuring the views exist"); |
137 | + Log.debug("Ensuring the views exist"); |
138 | Bindwood.ensureViews(); |
139 | - Bindwood.writeMessage("Ensuring our scratch folder exists"); |
140 | + Log.debug("Ensuring our scratch folder exists"); |
141 | Bindwood.scratch_folder = Bindwood.ensureLocalScratchFolder(); |
142 | |
143 | var profile_exists = Bindwood.profileExists(); |
144 | - Bindwood.writeMessage( |
145 | + Log.debug( |
146 | "Determined whether profile already exists: " + profile_exists); |
147 | |
148 | var HAVE_LAST_SEQ = Bindwood.last_seq ? 1 : 0; // 0 or 1 |
149 | @@ -325,7 +297,7 @@ |
150 | try { |
151 | Bindwood.pullChanges(); |
152 | } catch(e) { |
153 | - Bindwood.writeError("Problem pulling changes!", e); |
154 | + Log.exception("Problem pulling changes!", e); |
155 | } |
156 | }, |
157 | |
158 | @@ -337,7 +309,7 @@ |
159 | Bindwood.couch.createDb(); |
160 | } catch (e) { |
161 | if (e.error != 'file_exists') { |
162 | - Bindwood.writeError("Error creating database: ", e); |
163 | + Log.exception("Error creating database: ", e); |
164 | throw(e); |
165 | } |
166 | } |
167 | @@ -367,11 +339,11 @@ |
168 | try { |
169 | Bindwood.couch.save(doc); |
170 | } catch(e) { |
171 | - Bindwood.writeError("Problem saving view: ", e); |
172 | + Log.exception("Problem saving view: ", e); |
173 | } |
174 | } catch(e) { |
175 | // some kind of error fetching the existing design doc |
176 | - Bindwood.writeError("Problem checking for view: ", e); |
177 | + Log.exception("Problem checking for view: ", e); |
178 | } |
179 | }, |
180 | |
181 | @@ -408,7 +380,7 @@ |
182 | }, |
183 | |
184 | migrateOlderBookmarkRecords: function() { |
185 | - Bindwood.writeMessage( |
186 | + Log.debug( |
187 | "We're an older client. " + |
188 | "Let's migrate the remote records and re-sync."); |
189 | |
190 | @@ -424,40 +396,40 @@ |
191 | |
192 | for (var i = 0; i < rows.length; i++) { |
193 | var row = rows[i]; |
194 | - Bindwood.writeMessage("Got a row: " + row); |
195 | + Log.debug("Got a row: " + row); |
196 | var id = row.id; |
197 | - Bindwood.writeMessage("Got an id: " + id); |
198 | + Log.debug("Got an id: " + id); |
199 | var doc = Bindwood.couch.open(id); |
200 | - Bindwood.writeMessage("Got a doc: " + doc); |
201 | + Log.debug("Got a doc: " + doc); |
202 | |
203 | if (doc.record_type_version >= 1) { |
204 | - Bindwood.writeMessage( |
205 | + Log.debug( |
206 | "Record is already migrated Skipping..."); |
207 | continue; |
208 | } |
209 | |
210 | // get the uuid off the bookmark. |
211 | var old_uuid = doc.application_annotations.Firefox.uuid; |
212 | - Bindwood.writeMessage( |
213 | + Log.debug( |
214 | "Got the old uuid from the record: " + old_uuid); |
215 | // look up itemId by uuid |
216 | // XXX: This probably needs to be made more robust |
217 | var itemId = Bindwood.itemIdForUUID(old_uuid); |
218 | - Bindwood.writeMessage( |
219 | + Log.debug( |
220 | "Found its local itemID from the map: " + itemId); |
221 | // annotate the itemId with the document's _id |
222 | Bindwood.annotateItemWithUUID(itemId, id); |
223 | - Bindwood.writeMessage( |
224 | + Log.debug( |
225 | "Annotating the item's uuid with the document's actual id"); |
226 | // delete the uuid field off the record |
227 | delete doc.application_annotations.Firefox.uuid; |
228 | - Bindwood.writeMessage("Deleted the document's uuid annotation"); |
229 | + Log.debug("Deleted the document's uuid annotation"); |
230 | // delete the folder field off the record |
231 | delete doc.application_annotations.Firefox.folder; |
232 | - Bindwood.writeMessage("Deleted the document's folder annotation"); |
233 | + Log.debug("Deleted the document's folder annotation"); |
234 | |
235 | if (doc.deleted) { |
236 | - Bindwood.writeMessage( |
237 | + Log.debug( |
238 | "The document was flagged as deleted, cleaning up."); |
239 | // swap .deleted for conventional .deleted |
240 | if (!doc.application_annotations) { |
241 | @@ -470,28 +442,28 @@ |
242 | doc.application_annotations['Ubuntu One'].private_application_annotations = {}; |
243 | } |
244 | doc.application_annotations['Ubuntu One'].private_application_annotations.deleted = true; |
245 | - Bindwood.writeMessage( |
246 | + Log.debug( |
247 | "Moved deleted flag into private application annotations."); |
248 | delete doc.deleted; |
249 | - Bindwood.writeMessage("Deleted the top-level .deleted flag."); |
250 | + Log.debug("Deleted the top-level .deleted flag."); |
251 | } |
252 | |
253 | // update the document's record_type_version |
254 | doc.record_type_version = 1; |
255 | - Bindwood.writeMessage("Set the schema version to 1"); |
256 | + Log.debug("Set the schema version to 1"); |
257 | |
258 | // Ensure we're dealing with the proper record type on Migrate. |
259 | doc = Bindwood.decorateRecordByType(doc, itemId); |
260 | |
261 | // add to additional |
262 | additional.push(doc); |
263 | - Bindwood.writeMessage( |
264 | + Log.debug( |
265 | "Adding this doc to the stack of addition docs to push back."); |
266 | } |
267 | |
268 | for (var i = 0; i < additional.length; i++) { |
269 | var doc = additional[i]; |
270 | - Bindwood.writeMessage( |
271 | + Log.debug( |
272 | "Preparing to push back " + doc.title || doc.record_type); |
273 | // Ensure that any remote folders have their .children |
274 | // populated, and in particular make sure that we've already |
275 | @@ -500,7 +472,7 @@ |
276 | // XXX: This probably needs to be made more robust |
277 | var itemId = Bindwood.itemIdForUUID(doc._id); |
278 | doc.children = Bindwood.getUUIDsFromFolder(itemId); |
279 | - Bindwood.writeMessage( |
280 | + Log.debug( |
281 | "Folder needed updating, calculated children: " + |
282 | doc.children); |
283 | } |
284 | @@ -511,7 +483,7 @@ |
285 | // pull it later |
286 | Bindwood.seen_revisions[response.rev] = true; |
287 | } catch(e) { |
288 | - Bindwood.writeError( |
289 | + Log.exception( |
290 | "Problem saving record to CouchDB; record is " + |
291 | JSON.stringify(doc + ": ", e)); |
292 | } |
293 | @@ -519,7 +491,7 @@ |
294 | }, |
295 | |
296 | handleSubsequentClientFirstTimeSync: function() { |
297 | - Bindwood.writeMessage( |
298 | + Log.debug( |
299 | "We're a subsequent client. Let's merge the remote and the local."); |
300 | |
301 | // get the remote root from Couch. |
302 | @@ -545,7 +517,7 @@ |
303 | // pull it later |
304 | Bindwood.seen_revisions[response.rev] = true; |
305 | } catch(e) { |
306 | - Bindwood.writeError( |
307 | + Log.exception( |
308 | "Problem saving record to CouchDB; record is " + |
309 | JSON.stringify(records_needing_pushing[i]) + ": ", e); |
310 | } |
311 | @@ -553,7 +525,7 @@ |
312 | }, |
313 | |
314 | syncRemoteAndLocal: function(remote_folder, local_folder, accum) { |
315 | - Bindwood.writeMessage( |
316 | + Log.debug( |
317 | "Syncing remote folder: " + remote_folder + |
318 | " to local folder: " + local_folder); |
319 | var local_needs_pushing = false; |
320 | @@ -565,15 +537,15 @@ |
321 | // walk the remote folder's children, and ask for each one: |
322 | var remote = Bindwood.couch.open(remote_folder); |
323 | var remote_children = remote.children; |
324 | - Bindwood.writeMessage("Beginning to walk remote children"); |
325 | + Log.debug("Beginning to walk remote children"); |
326 | for (var i = 0; i < remote_children.length; i++) { |
327 | - Bindwood.writeMessage( |
328 | + Log.debug( |
329 | "Getting remote child: " + remote_children[i]); |
330 | var remote_child = Bindwood.couch.open(remote_children[i]); |
331 | var local_child; |
332 | var found_local = false; |
333 | |
334 | - Bindwood.writeMessage( |
335 | + Log.debug( |
336 | "Looking for record type: " + remote_child.record_type + |
337 | " identified by " + |
338 | (remote_child.record_type != Bindwood.TYPE_SEPARATOR ? |
339 | @@ -591,7 +563,7 @@ |
340 | (remote_child.record_type == Bindwood.TYPE_SEPARATOR || |
341 | Bindwood.sameTitle(local_child, remote_child))) { |
342 | found_local = true; |
343 | - Bindwood.writeMessage("Found the record."); |
344 | + Log.debug("Found the record."); |
345 | Bindwood.annotateItemWithUUID( |
346 | local_child.itemId, remote_child._id); |
347 | // If we're dealing with a folder, we'll process it |
348 | @@ -604,7 +576,7 @@ |
349 | // If yes, but in a different location, move the |
350 | // local to the proper index within the same |
351 | // folder. |
352 | - Bindwood.writeMessage( |
353 | + Log.debug( |
354 | "Moving local record to same index as remote."); |
355 | Bindwood.makeLocalChangeOnly( |
356 | function() { |
357 | @@ -624,7 +596,7 @@ |
358 | // Add current local folder as one that needs to be |
359 | // pushed back (changing its children) |
360 | |
361 | - Bindwood.writeMessage( |
362 | + Log.debug( |
363 | "Remote record doesn't exist here, recreating it in " + |
364 | local_folder + " at index " + i + "."); |
365 | Bindwood.processCouchRecord(remote_child, local_folder, i); |
366 | @@ -671,8 +643,8 @@ |
367 | var uuid = (seed_uuid ? |
368 | seed_uuid : |
369 | uuidService.generateUUID().toString()); |
370 | - Bindwood.writeMessage("UUID We came up with: " + uuid); |
371 | - Bindwood.writeMessage("Annotating the item now."); |
372 | + Log.debug("UUID We came up with: " + uuid); |
373 | + Log.debug("Annotating the item now."); |
374 | annotationService.setItemAnnotation( |
375 | itemId, |
376 | Bindwood.annotationKey, |
377 | @@ -694,13 +666,13 @@ |
378 | var items = annotationService.getItemsWithAnnotation( |
379 | Bindwood.annotationKey, {}); |
380 | var num_items = items.length; |
381 | - Bindwood.writeMessage( |
382 | + Log.debug( |
383 | "Found " + num_items + " records with the annotation key"); |
384 | for (var i = 0; i < items.length; i++) { |
385 | - Bindwood.writeMessage("Item #" + i + ": ItemId: " + items[i]); |
386 | + Log.debug("Item #" + i + ": ItemId: " + items[i]); |
387 | var anno = annotationService.getItemAnnotation( |
388 | items[i], Bindwood.annotationKey); |
389 | - Bindwood.writeMessage( |
390 | + Log.debug( |
391 | "Annotation on " + items[i] + ": " + anno); |
392 | if (anno == uuid) { |
393 | var itemId = items[i]; |
394 | @@ -709,7 +681,7 @@ |
395 | } |
396 | } |
397 | if (!itemId) { |
398 | - Bindwood.writeMessage( |
399 | + Log.debug( |
400 | "XXX: Still haven't found the right itemId!"); |
401 | } |
402 | } |
403 | @@ -725,7 +697,7 @@ |
404 | itemId, Bindwood.annotationKey); |
405 | Bindwood.uuidItemIdMap[uuid] = itemId; |
406 | } catch(e) { |
407 | - Bindwood.writeError( |
408 | + Log.exception( |
409 | "Couldn't find a UUID for itemId: " + itemId, e); |
410 | uuid = Bindwood.makeLocalChangeOnly( |
411 | function() { return Bindwood.annotateItemWithUUID( |
412 | @@ -825,7 +797,7 @@ |
413 | folderRoot.containerOpen = true; |
414 | |
415 | var folder_record = Bindwood.couchRecordForItemId(folder); |
416 | - Bindwood.writeMessage( |
417 | + Log.debug( |
418 | "Building up a record for " + folder_record.title); |
419 | |
420 | for (var i=0; i<folderRoot.childCount; i++) { |
421 | @@ -833,7 +805,7 @@ |
422 | |
423 | var record = Bindwood.couchRecordForItemId(node.itemId); |
424 | folder_record.children.push(record._id); |
425 | - Bindwood.writeMessage( |
426 | + Log.debug( |
427 | "Added child record " + (record.title || record.record_type)); |
428 | |
429 | // If node is a folder (but not a Livemark or Dynamic container), |
430 | @@ -844,7 +816,7 @@ |
431 | Bindwood.records.push(record); |
432 | } |
433 | } |
434 | - Bindwood.writeMessage( |
435 | + Log.debug( |
436 | "Done collecting children. Folder's children is now: " + |
437 | JSON.stringify(folder_record.children)); |
438 | folderRoot.containerOpen = false; |
439 | @@ -902,10 +874,10 @@ |
440 | |
441 | if (only_latest && |
442 | (ff.last_modified <= Bindwood.latest_modified)) { |
443 | - Bindwood.writeMessage( |
444 | + Log.debug( |
445 | "We've reached records we've already dealt with." + |
446 | " Breaking out of the loop."); |
447 | - Bindwood.writeMessage( |
448 | + Log.debug( |
449 | "Record we've seen: " + record._id); |
450 | break; |
451 | } |
452 | @@ -919,13 +891,13 @@ |
453 | // we pull it later |
454 | Bindwood.seen_revisions[response.rev] = true; |
455 | } catch(e) { |
456 | - Bindwood.writeError( |
457 | + Log.exception( |
458 | "Problem saving record to CouchDB; record is " + |
459 | JSON.stringify(record) + ": ", e); |
460 | } |
461 | } else { |
462 | // record is already in CouchDB, so do nothing |
463 | - Bindwood.writeMessage( |
464 | + Log.debug( |
465 | "This record (" + record._id + |
466 | ") is already in Couch, skipping"); |
467 | } |
468 | @@ -949,7 +921,7 @@ |
469 | var repeater = { |
470 | notify: function(timer) { |
471 | Bindwood.pullRecords(); |
472 | - Bindwood.writeMessage( |
473 | + Log.debug( |
474 | "Successful run, rescheduling ourself"); |
475 | } |
476 | }; |
477 | @@ -961,7 +933,7 @@ |
478 | Bindwood.pull_changes_timer.initWithCallback( |
479 | repeater, 30000, Ci.nsITimer.TYPE_REPEATING_SLACK); |
480 | } catch(e) { |
481 | - Bindwood.writeError("Problem setting up repeater.", e); |
482 | + Log.exception("Problem setting up repeater.", e); |
483 | } |
484 | |
485 | /* Disabling pop-up window for the time being. |
486 | @@ -1002,7 +974,7 @@ |
487 | null |
488 | ); |
489 | } catch(e) { |
490 | - Bindwood.writeError( |
491 | + Log.exception( |
492 | "Problem long polling bookmarks from Couch: ", e); |
493 | } |
494 | var revisions = results.results; |
495 | @@ -1015,7 +987,7 @@ |
496 | // design doc |
497 | if (recordid.indexOf('root_') === 0 || |
498 | recordid.indexOf('_design') === 0) { |
499 | - Bindwood.writeMessage( |
500 | + Log.debug( |
501 | "Root profile or design doc, skipping..."); |
502 | continue; |
503 | } |
504 | @@ -1023,7 +995,7 @@ |
505 | // Skip any revisions we've already seen (because we just |
506 | // put them there) |
507 | if (Bindwood.seen_revisions[revno]) { |
508 | - Bindwood.writeMessage( |
509 | + Log.debug( |
510 | "We've seen this revision (" + revno + |
511 | ") before, when we created it."); |
512 | delete Bindwood.seen_revisions[revno]; |
513 | @@ -1033,7 +1005,7 @@ |
514 | var record = Bindwood.couch.open(recordid); |
515 | |
516 | if (!Bindwood.recordInCurrentProfile(record)) { |
517 | - Bindwood.writeMessage( |
518 | + Log.debug( |
519 | "Record isn't in our current profile. Skipping..."); |
520 | continue; |
521 | } |
522 | @@ -1045,7 +1017,7 @@ |
523 | if (Bindwood.isDeleted(record)) { |
524 | Bindwood.makeLocalChangeOnly( |
525 | function() { |
526 | - Bindwood.writeMessage( |
527 | + Log.debug( |
528 | "Record in Couch marked as deleted;" + |
529 | " attempting to delete local copy."); |
530 | Bindwood.deleteLocalRecord(record); |
531 | @@ -1095,7 +1067,7 @@ |
532 | var aParent = aParent ? aParent : Bindwood.scratch_folder; |
533 | var aIndex = aIndex ? aIndex : -1; |
534 | |
535 | - Bindwood.writeMessage( |
536 | + Log.debug( |
537 | "Processing Couch Record: " + record + " placing it in " + |
538 | aParent + " at location " + aIndex); |
539 | |
540 | @@ -1137,7 +1109,7 @@ |
541 | // Could be an add or change revision. Delete was handled earlier. |
542 | // If it's an addition (we can't resolve its _id to be one of our |
543 | // itemIds), add it to the Desktop Couch folder in unfiled. |
544 | - Bindwood.writeMessage( |
545 | + Log.debug( |
546 | "Processing bookmark record: " + JSON.stringify(record)); |
547 | var itemId = Bindwood.itemIdForUUID(record._id); |
548 | if (itemId) { |
549 | @@ -1162,7 +1134,7 @@ |
550 | // earlier. If it's an addition (we can't resolve its _id to be |
551 | // one of our itemIds), add it to the Desktop Couch folder in |
552 | // unfiled. |
553 | - Bindwood.writeMessage( |
554 | + Log.debug( |
555 | "Processing folder record: " + JSON.stringify(record)); |
556 | var itemId = Bindwood.itemIdForUUID(record._id); |
557 | if (itemId) { |
558 | @@ -1175,7 +1147,7 @@ |
559 | // folder, then move them back in the order of the remote |
560 | // children. |
561 | var local_children = Bindwood.getUUIDsFromFolder(itemId); |
562 | - Bindwood.writeMessage( |
563 | + Log.debug( |
564 | "Moving local children " + JSON.stringify(local_children) + |
565 | " to scratch folder"); |
566 | for (var i = 0; i<local_children.length; i++) { |
567 | @@ -1186,12 +1158,12 @@ |
568 | bookmarksService.moveItem( |
569 | child_itemId, Bindwood.scratch_folder, -1); |
570 | } catch(e) { |
571 | - Bindwood.writeError( |
572 | + Log.exception( |
573 | "Problem moving item to scratch folder: " + |
574 | JSON.stringify(e), e); |
575 | } |
576 | } |
577 | - Bindwood.writeMessage( |
578 | + Log.debug( |
579 | "Moving children identified by record " + |
580 | JSON.stringify(record.children) + " to this folder"); |
581 | for (var j = 0; j<record.children.length; j++) { |
582 | @@ -1201,7 +1173,7 @@ |
583 | try { |
584 | bookmarksService.moveItem(new_child_itemId, itemId, -1); |
585 | } catch(e) { |
586 | - Bindwood.writeError( |
587 | + Log.exception( |
588 | "Problem moving item from scratch folder: " + |
589 | JSON.stringify(e), e); |
590 | } |
591 | @@ -1223,7 +1195,7 @@ |
592 | // earlier. If it's an addition (we can't resolve its _id to be |
593 | // one of our itemIds), add it to the Desktop Couch folder in |
594 | // unfiled. |
595 | - Bindwood.writeMessage( |
596 | + Log.debug( |
597 | "Processing feed record: " + JSON.stringify(record)); |
598 | var itemId = Bindwood.itemIdForUUID(record._id); |
599 | if (itemId) { |
600 | @@ -1251,7 +1223,7 @@ |
601 | // delete was handled earlier. If it's an addition (we can't |
602 | // resolve its _id to be one of our itemIds), add it to the |
603 | // Desktop Couch folder in unfiled. |
604 | - Bindwood.writeMessage( |
605 | + Log.debug( |
606 | "Processing separator record: " + JSON.stringify(record)); |
607 | var itemId = Bindwood.itemIdForUUID(record._id); |
608 | if (!itemId) { |
609 | @@ -1266,7 +1238,7 @@ |
610 | }, |
611 | |
612 | updateDocAndSave: function(uuid, attribute, value, callback) { |
613 | - Bindwood.writeMessage( |
614 | + Log.debug( |
615 | "Updating a document (" + |
616 | uuid + |
617 | ") setting (" + |
618 | @@ -1300,7 +1272,7 @@ |
619 | var response = Bindwood.couch.save(doc); |
620 | Bindwood.seen_revisions[response.rev] = true; |
621 | } catch(e) { |
622 | - Bindwood.writeError("Problem saving document to Couch", e); |
623 | + Log.exception("Problem saving document to Couch", e); |
624 | throw e; |
625 | } |
626 | |
627 | @@ -1318,7 +1290,7 @@ |
628 | var root = 0; |
629 | var parent; |
630 | while (parent != root) { |
631 | - Bindwood.writeMessage("Looking for parent of " + itemId); |
632 | + Log.debug("Looking for parent of " + itemId); |
633 | parent = bookmarksService.getFolderIdForItem(itemId); |
634 | if (parent != root && |
635 | annotationService.itemHasAnnotation( |
636 | @@ -1347,33 +1319,32 @@ |
637 | |
638 | // An nsINavBookmarkObserver |
639 | onItemAdded: function(aItemId, aFolder, aIndex) { |
640 | - this.bw.writeMessage( |
641 | - "onItemAdded: called when push is " + this.bw.push); |
642 | + Log.debug("onItemAdded: called when push is " + this.bw.push); |
643 | // An item has been added, so we create a blank entry |
644 | // in Couch with our local itemId attached. |
645 | if (!this.bw.itemWeCareAbout(aItemId)) { |
646 | - this.bw.writeMessage("Ignoring this add event"); |
647 | + Log.debug("Ignoring this add event"); |
648 | return; |
649 | } |
650 | |
651 | - this.bw.writeMessage( |
652 | + Log.debug( |
653 | "A new item was created. Its id is: " + aItemId + |
654 | " at location: " + aIndex + |
655 | " in folder: " + aFolder ); |
656 | |
657 | switch (this.bw.push) { |
658 | case 'DISABLED': |
659 | - this.bw.writeMessage("Added, but not saving to Couch."); |
660 | + Log.debug("Added, but not saving to Couch."); |
661 | break; |
662 | case 'ENABLED': |
663 | try { |
664 | var doc = this.bw.couchRecordForItemId(aItemId); |
665 | var response = this.bw.couch.save(doc); |
666 | - this.bw.writeMessage("Saved new, bare record to Couch."); |
667 | + Log.debug("Saved new, bare record to Couch."); |
668 | this.bw.seen_revisions[response.rev] = true; |
669 | this.bw.pushFolderChildren(aFolder); |
670 | } catch(e) { |
671 | - this.bw.writeError( |
672 | + Log.exception( |
673 | "Problem saving new bookmark to Couch: ", e); |
674 | } |
675 | break; |
676 | @@ -1386,25 +1357,25 @@ |
677 | }, |
678 | |
679 | onBeforeItemRemoved: function(aItemId) { |
680 | - this.bw.writeMessage( |
681 | + Log.debug( |
682 | "onBeforeItemRemoved: called when push is " + this.bw.push); |
683 | // A bookmark has been removed. This is called before it's |
684 | // been removed locally, though we're passed the itemId, |
685 | // which we use to delete from Couch. |
686 | var folderId = bookmarksService.getFolderIdForItem(aItemId); |
687 | if (!this.bw.itemWeCareAbout(aItemId)) { |
688 | - this.bw.writeMessage("Ignoring this before remove event"); |
689 | + Log.debug("Ignoring this before remove event"); |
690 | return; |
691 | } |
692 | |
693 | - this.bw.writeMessage( |
694 | + Log.debug( |
695 | "Record " + aItemId + " is about to be removed locally."); |
696 | var uuid = this.bw.uuidForItemId(aItemId); |
697 | |
698 | switch (this.bw.push) { |
699 | case 'DISABLED': |
700 | delete this.bw.uuidItemIdMap[uuid]; |
701 | - this.bw.writeMessage( |
702 | + Log.debug( |
703 | "Deleted from local uuid map, but not saving back to Couch."); |
704 | break; |
705 | |
706 | @@ -1427,16 +1398,16 @@ |
707 | var response = this.bw.couch.save(doc); |
708 | this.bw.seen_revisions[response.rev] = true; |
709 | delete this.bw.uuidItemIdMap[uuid]; |
710 | - this.bw.writeMessage( |
711 | + Log.debug( |
712 | "Deleted local reference in the" + |
713 | " uuid-itemId mapping."); |
714 | - this.bw.writeMessage( |
715 | + Log.debug( |
716 | "Saved document back to Couch with deleted flag set."); |
717 | var new_children = this.bw.getUUIDsFromFolder(folderId); |
718 | new_children.splice(new_children.indexOf(uuid), 1); |
719 | this.bw.pushFolderChildren(folderId, new_children); |
720 | } catch(e) { |
721 | - this.bw.writeError( |
722 | + Log.exception( |
723 | "Problem pushing deleted record to Couch: ", e); |
724 | } |
725 | break; |
726 | @@ -1446,11 +1417,11 @@ |
727 | }, |
728 | |
729 | onItemRemoved: function(aItemId, aFolder, aIndex) { |
730 | - this.bw.writeMessage( |
731 | + Log.debug( |
732 | "onItemRemoved: called when push is " + this,bw.push); |
733 | // This only happens locally, so there's never a need to push |
734 | if (!this.bw.itemWeCareAbout(aItemId)) { |
735 | - this.bw.writeMessage("Ignoring this remove event"); |
736 | + Log.debug("Ignoring this remove event"); |
737 | return; |
738 | } |
739 | |
740 | @@ -1458,22 +1429,22 @@ |
741 | function() { |
742 | return annotationService.removeItemAnnotation( |
743 | aItemId, this.bw.annotationKey); }); |
744 | - this.bw.writeMessage( |
745 | + Log.debug( |
746 | "Removed annotations from bookmark identified by: " + aItemId); |
747 | }, |
748 | |
749 | onItemChanged: function(aItemId, aProperty, aIsAnnotationProperty, aValue) { |
750 | - this.bw.writeMessage( |
751 | + Log.debug( |
752 | "onItemChanged: called when push is " + this.bw.push); |
753 | // A property of a bookmark has changed. On multiple |
754 | // property updates, this will be called multiple times, |
755 | // once per property (i.e., for title and URI) |
756 | if (!this.bw.itemWeCareAbout(aItemId)) { |
757 | - this.bw.writeMessage("Ignoring this change event"); |
758 | + Log.debug("Ignoring this change event"); |
759 | return; |
760 | } |
761 | |
762 | - this.bw.writeMessage( |
763 | + Log.debug( |
764 | "A property (" + |
765 | aProperty + |
766 | ") on item id: " + aItemId + |
767 | @@ -1482,21 +1453,21 @@ |
768 | |
769 | switch (this.bw.push) { |
770 | case 'DISABLED': |
771 | - this.bw.writeMessage( |
772 | + Log.debug( |
773 | "Updated, but not saving back to Couch."); |
774 | break; |
775 | case 'ENABLED': |
776 | - this.bw.writeMessage( |
777 | + Log.debug( |
778 | "We will push this change back to Couch."); |
779 | try { |
780 | var result = Bindwood.updateDocAndSave( |
781 | uuid, aProperty.toString(), aValue.toString(), |
782 | function() { |
783 | - this.bw.writeMessage( |
784 | + Log.debug( |
785 | "Saved the document back to Couch"); |
786 | }); |
787 | } catch(e) { |
788 | - this.bw.writeError( |
789 | + Log.exception( |
790 | "Problem saving updated bookmark to Couch: ", e); |
791 | } |
792 | break; |
793 | @@ -1509,16 +1480,16 @@ |
794 | }, |
795 | |
796 | onItemMoved: function(aItemId, aOldParent, aOldIndex, aNewParent, aNewIndex) { |
797 | - this.bw.writeMessage( |
798 | + Log.debug( |
799 | "onItemMoved: called when push is " + this.bw.push); |
800 | - this.bw.writeMessage( |
801 | + Log.debug( |
802 | "The item: " + aItemId + " was moved from (" + |
803 | aOldParent + ", " + aOldIndex + |
804 | ") to (" + aNewParent + ", " + aNewIndex + ")" |
805 | ); |
806 | switch (this.bw.push) { |
807 | case 'DISABLED': |
808 | - this.bw.writeMessage( |
809 | + Log.debug( |
810 | "Moved, but not saving back to Couch."); |
811 | break; |
812 | case 'ENABLED': |
813 | @@ -1531,7 +1502,7 @@ |
814 | var response = this.bw.couch.save(old_parent_doc); |
815 | this.bw.seen_revisions[response.rev] = true; |
816 | } catch(e) { |
817 | - this.bw.writeError( |
818 | + Log.exception( |
819 | "Problem saving updated old parent doc to Couch: ", e); |
820 | } |
821 | if (aOldParent != aNewParent) { |
822 | @@ -1543,7 +1514,7 @@ |
823 | var response = this.bw.couch.save(new_parent_doc); |
824 | this.bw.seen_revisions[response.rev] = true; |
825 | } catch(e) { |
826 | - this.bw.writeError( |
827 | + Log.exception( |
828 | "Problem saving updated new parent doc to Couch: ", |
829 | e); |
830 | } |
831 | |
832 | === added file 'modules/logging.jsm' |
833 | --- modules/logging.jsm 1970-01-01 00:00:00 +0000 |
834 | +++ modules/logging.jsm 2011-02-21 06:45:07 +0000 |
835 | @@ -0,0 +1,61 @@ |
836 | +/* |
837 | + * Copyright 2009-2011 Canonical Ltd. |
838 | + * |
839 | + * This program is free software: you can redistribute it and/or modify it |
840 | + * under the terms of the GNU General Public License version 3, as published |
841 | + * by the Free Software Foundation. |
842 | + * |
843 | + * This program is distributed in the hope that it will be useful, but |
844 | + * WITHOUT ANY WARRANTY; without even the implied warranties of |
845 | + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
846 | + * PURPOSE. See the GNU General Public License for more details. |
847 | + * |
848 | + * You should have received a copy of the GNU General Public License along |
849 | + * with this program. If not, see <http://www.gnu.org/licenses/>. |
850 | + */ |
851 | +/* Logging helpers for use in other Bindwood modules. */ |
852 | + |
853 | +const EXPORTED_SYMBOLS = ["Log"]; |
854 | + |
855 | + |
856 | +const Cc = Components.classes; |
857 | +const Ci = Components.interfaces; |
858 | + |
859 | +var consoleService = Cc["@mozilla.org/consoleservice;1"] |
860 | + .getService(Ci.nsIConsoleService); |
861 | +var prefsBranch = Cc["@mozilla.org/preferences-service;1"] |
862 | + .getService(Ci.nsIPrefService).getBranch("bindwood."); |
863 | + |
864 | + |
865 | +var Log = { |
866 | + _write: function(message) { |
867 | + consoleService.logStringMessage("Bindwood: " + message); |
868 | + /* Uncomment the following to log messages to the console. */ |
869 | + //dump(message + "\n"); |
870 | + }, |
871 | + |
872 | + debug: function(message) { |
873 | + var debug; |
874 | + try { |
875 | + debug = prefsBranch.getBoolPref('debug'); |
876 | + } catch(e) { |
877 | + debug = false; |
878 | + } |
879 | + if (debug) { |
880 | + this._write(message); |
881 | + } |
882 | + }, |
883 | + |
884 | + error: function(message) { |
885 | + this._write(message); |
886 | + }, |
887 | + |
888 | + exception: function(message, error) { |
889 | + this.error(message + |
890 | + " message: '" + error.message + |
891 | + "', reason: '" + error.reason + |
892 | + "', description: '" + error.description + |
893 | + "', error: '" + error.error + |
894 | + "', raw error: '" + JSON.stringify(error) + "'"); |
895 | + }, |
896 | +}; |
897 | |
898 | === modified file 'mozmill/tests/test_addbookmark.js' |
899 | --- mozmill/tests/test_addbookmark.js 2011-02-02 12:39:26 +0000 |
900 | +++ mozmill/tests/test_addbookmark.js 2011-02-21 06:45:07 +0000 |
901 | @@ -1,5 +1,7 @@ |
902 | var bm = require("../shared-modules/bookmarks"); |
903 | |
904 | +const TIMEOUT = 5000; |
905 | + |
906 | const LOCAL_TEST_FOLDER = collector.addHttpResource('../test-files/'); |
907 | const LOCAL_TEST_PAGE = LOCAL_TEST_FOLDER + 'test.html'; |
908 | |
909 | @@ -27,7 +29,11 @@ |
910 | "Bookmark title"); |
911 | |
912 | var uuid = bindwood.Bindwood.uuidForItemId(bookmark_id); |
913 | - var record = bindwood.Bindwood.couch.open(uuid); |
914 | + var record; |
915 | + controller.waitFor(function() { |
916 | + record = bindwood.Bindwood.couch.open(uuid); |
917 | + return record != null; |
918 | + }, "Bookmark synchronised", TIMEOUT); |
919 | jum.assertEquals(record.uri, uri.spec); |
920 | jum.assertEquals(record.title, "Bookmark title"); |
921 | jum.assertEquals(record.record_type, bindwood.Bindwood.TYPE_BOOKMARK); |
Awesome.