1

I would like to debug what the background script (non persistent) does when it goes to sleep and wakes up (and goes back to sleep again).

Clicking on the "background page" will prevent the background script from going into suspended state, but if I kill the page and open it after a while, only fresh logs are displayed, not the ones that were printed before opening the background page (from the extensions page).

So I am wondering how do we debug the suspended/wake states of an event page?

Edit: Moved the backstory to its own question here

How to prevent/detect race condition between processing and restoring store data when waking up an Event page

Community
  • 1
  • 1
FnuLnu
  • 85
  • 5
  • 1
    Your question is a good question on its own; however, your "background" story is a separate bug (one that I can probably solve) - it would be a good idea to split it off to a separate question, with code. – Xan Jul 14 '15 at 17:52
  • Thanks @Xan, I will spawn off a different question on the background I provided. I added that here just so that I can give a sense of why I needed to ask the main question. Do you recommend I edit the post and delete it? – FnuLnu Jul 14 '15 at 18:01
  • You should probably create a new question, and cross-reference those by having a link in each other. – Xan Jul 14 '15 at 18:04
  • Also, which console output you wish to preserve? Normal `console.log()`s? Errors? – Xan Jul 14 '15 at 18:05
  • The console.log() messages. Would elevating them to errors/warning preserve them across multiple open/close of the background page? – FnuLnu Jul 14 '15 at 18:10

2 Answers2

2

You could log to the console AND non-volatile storage at the same time.

You can either use a custom logging function, or overload console.log:

console._log = console.log;
console.log = function() {
  var args = arguments;
  console._log.apply(console, args);
  chrome.storage.local.get({consoleLog : []}, function(data) {
    data.consoleLog.push(args);
    chrome.storage.local.set({consoleLog: data.consoleLog});
  });
}

Of course, it should probably only be used while you're actively debugging.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Thanks for the succinct code. I did find my issue by trial and error. Turns out my saving of state during suspend was probably problematic, the onSuspend was doing a bit of a calculation and saving other data before saving the state. I reordered the calls to store state first, and the things seem to be working for now. I will do more testing, and use your suggestion to debug better. – FnuLnu Jul 14 '15 at 22:36
  • In `runtime.onSuspend`? Yes, it's too late; Chrome won't wait for async tasks. – Xan Jul 14 '15 at 22:37
0

Try create a pageLog function and send objects to be logged on the page side.

FK-
  • 1,462
  • 1
  • 10
  • 17
  • Thanks, sounds like a really neat idea. Could you please expand a little. When you say "send objects to [...] page side", do you mean modify (some) webpage and add logs there, or do I sendMessage to the conent pages and observe the logs there? – FnuLnu Jul 14 '15 at 18:06
  • sendMessage(data)//backgroun, console.log(data)//page – FK- Jul 14 '15 at 18:09
  • How exactly do you inspect the backgound page? It should not keep it active(not sure about this one). – FK- Jul 14 '15 at 18:12
  • I will try that, a small niggle though. Since I am storing my tab data, and I suspect that I am having a race condition between restore data and processing, not sure I will see all the logs. I will try it out nevertheless. – FnuLnu Jul 14 '15 at 18:15
  • If you want to understand what is going on, I suggest heavy and labeled logging (functions start: ..., function output:...,variables:...). If you want help with your code, show it to us (separate question, of course). – FK- Jul 14 '15 at 18:22
  • @FKasa You seem to misunderstand the question. OP uses an Event page instead of a simple background page; when _not observed_, the page is completely destroyed after a few seconds of inactivity (but Chrome remembers which events should wake it up). If observed (i.e. Dev Tools are open), the page will _never_ unload. If not observed, any console output will be lost when the page unloads. – Xan Jul 14 '15 at 21:56
  • P.S. I downvoted because the answer is incredibly vague. – Xan Jul 14 '15 at 21:57