Git Product home page Git Product logo

console_logger's People

Contributors

infocatcher avatar

Watchers

 avatar  avatar  avatar

console_logger's Issues

Use OS.File API to write logs

https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread

Example:

var file = "D:\\test";
appendAsync(file, "1: " + ts() + " Qwerty Йцукен\n", function() {
    appendAsync(file, "2: " + ts() + "\n", function() {
        appendAsync(file, "3: " + ts() + "\n");
    });
});

function appendAsync(file, data, callback) {
    function onFailure(err) {
        Components.utils.reportError(err);
        callback && callback("");
    }
    OS.File.open(file, { write: true, append: true }).then(
        function onSuccess(osFile) {
            var arr = new TextEncoder().encode(data); //~ todo: reuse TextEncoder
            osFile.write(arr).then(
                function onSuccess(bytesCount) {
                    osFile.close().then(
                        function onSuccess() {
                            callback && callback(data);
                        },
                        onFailure
                    ).then(null, onFailure);
                },
                onFailure
            ).then(null, onFailure);
        },
        onFailure
    ).then(null, onFailure);
}
function ts() {
    var d = new Date();
    var ms = d.getMilliseconds();
    return d.toLocaleFormat("%M:%S:") + "000".substr(String(ms).length) + ms;
}

Handle console notifications after small delay (to improve performance)

Looks like all console listeners are called at once:

var halt = 0;
var t = [0, 0, 0, 0];
var o1 = {
    observe: function(msg) {
        Services.tm.mainThread.dispatch({run: function() {
            t[2] = performance.now();
            stats();
        }}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
        t[0] = performance.now();
        if(halt) {
            var stop = performance.now() + halt;
            while(performance.now() < stop);
        }
        Services.console.unregisterListener(this);
        stats();
    }
};
var o2 = {
    observe: function(msg) {
        Services.tm.mainThread.dispatch({run: function() {
            t[3] = performance.now();
            stats();
        }}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
        t[1] = performance.now();
        var stop = performance.now() + 50;
        if(halt) {
            var stop = performance.now() + halt;
            while(performance.now() < stop);
        }
        Services.console.unregisterListener(this);
        stats();
    }
};
Services.console.registerListener(o1);
Services.console.registerListener(o2);
function stats() {
    if(t[0] && t[1] && t[2] && t[3]) {
        var listeners = Math.max(t[0], t[1]);
        alert([
            "Listener #1: " + t[0].toFixed(2),
            "Listener #2: " + t[1].toFixed(2),
            "Delayed #1: " + t[2].toFixed(2) + (t[2] > listeners ? " after listeners!" : ""),
            "Delayed #2: " + t[3].toFixed(2) + (t[3] > listeners ? " after listeners!" : "")
        ].join("\n"));
    }
}
Services.console.logStringMessage("test");

Example output:

Listener #1: 1964481.64
Listener #2: 1964481.59
Delayed #1: 1964483.19 after listeners!
Delayed #2: 1964483.16 after listeners!

Also there is no reasons to handle notification ASAP.

Add support for messages from Console.jsm

See https://developer.mozilla.org/en-US/docs/Tools/Browser_Console#Messages_from_add-ons
resource://gre/modules/devtools/Console.jsm
From 30.0:

/**
 * Send a Console API message. This function will send a console-api-log-event
 * notification through the nsIObserverService.
 *
 * @param {string} aLevel
 *        Message severity level. This is usually the name of the console method
 *        that was called.
 * @param {object} aFrame
 *        The youngest stack frame coming from Components.stack, as formatted by
 *        getStack().
 * @param {array} aArgs
 *        The arguments given to the console method.
 * @param {object} aOptions
 *        Object properties depend on the console method that was invoked:
 *        - timer: for time() and timeEnd(). Holds the timer information.
 *        - groupName: for group(), groupCollapsed() and groupEnd().
 *        - stacktrace: for trace(). Holds the array of stack frames as given by
 *        getStack().
 */
function sendConsoleAPIMessage(aLevel, aFrame, aArgs, aOptions = {})
{
  let consoleEvent = {
    ID: "jsm",
    innerID: aFrame.filename,
    level: aLevel,
    filename: aFrame.filename,
    lineNumber: aFrame.lineNumber,
    functionName: aFrame.functionName,
    timeStamp: Date.now(),
    arguments: aArgs,
  };

  consoleEvent.wrappedJSObject = consoleEvent;

  switch (aLevel) {
    case "trace":
      consoleEvent.stacktrace = aOptions.stacktrace;
      break;
    case "time":
    case "timeEnd":
      consoleEvent.timer = aOptions.timer;
      break;
    case "group":
    case "groupCollapsed":
    case "groupEnd":
      try {
        consoleEvent.groupName = Array.prototype.join.call(aArgs, " ");
      }
      catch (ex) {
        Cu.reportError(ex);
        Cu.reportError(ex.stack);
        return;
      }
      break;
  }

  Services.obs.notifyObservers(consoleEvent, "console-api-log-event", null);
  let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
                            .getService(Ci.nsIConsoleAPIStorage);
  ConsoleAPIStorage.recordEvent("jsm", consoleEvent);
}

Add GUI for options

Special parts:

  • Options: implement export/import functionality (to/from clipboard) #4
  • Options: add search (filtration) #6
  • Options: prevent non-ASCII names #7
  • Options: possible memory leaks for options.xul (and optionsOpener.xul) #8
    (looks like Firefox bug, wontfix)
  • Options: add ability to disable all rules globally #9
  • Options: validate input data #10
  • Options: add ability to open *.log file #11
  • Options: implement export/import functionality (to/from file) #12
  • Options: force sort list of items by name #13
  • Options: add ability to open in tab #14
  • Options: indication of available and updated *.log files #15
  • Options: add ability to remove *.log file #16
  • Options: add localization #5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.