Up UI build
[gitorious:mainline.git] / public / dist / js / logger.js
1 /*global cull, dome, console, gts*/
2 function uinitLogger(app, level) {
3     var params = window.location.href.match(/\?.*components=([^&]+)/) || [];
4     var filters = params[1] && params[1].split(",") || [];
5
6     if (typeof level === "string") {
7         level = uinitLogger.levels.indexOf((level || "info").toLowerCase());
8     }
9
10     if (typeof level !== "number") {
11         level = uinitLogger.DEBUG;
12     }
13
14     if (level <= uinitLogger.INFO) {
15         app.on("init", function () {
16             console.log("===========================");
17             console.log("Attempting to load features");
18             console.log("===========================");
19
20             if (filters.length > 0) {
21                 console.log("=> Only logging events for", filters.join(", "));
22             }
23         });
24     }
25
26     function filtered(fn) {
27         return function (feature) {
28             if (filters.length > 0 && filters.indexOf(feature.name) < 0) {
29                 // Feature is not matched by filter, abort
30                 return;
31             }
32             return fn.apply(this, arguments);
33         };
34     }
35
36     if (level <= uinitLogger.DEBUG) {
37         app.on("loading", filtered(function (feature) {
38             console.log("[Loading:", feature.name + "]");
39         }));
40
41         app.on("reloading", filtered(function (feature) {
42             console.log("[Re-loading:", feature.name + "]");
43         }));
44     }
45
46     if (level <= uinitLogger.INFO) {
47         app.on("pending", filtered(function (feature) {
48             var name = cull.prop("name");
49             var reason, pending = cull.map(name, cull.select(function (f) {
50                 return !f.loaded;
51             }, feature.dependencies()));
52
53             if (pending.length > 0) {
54                 reason = "Waiting for ";
55                 reason += pending.length === 1 ? "dependency" : "dependencies";
56                 reason += " [" + pending.join(", ") + "]";
57             }
58
59             if (!reason && feature.elements) {
60                 if (dome.byClass(feature.elements).length === 0) {
61                     reason = "No matching elements for selector ." +
62                         feature.elements;
63                 }
64             }
65
66             if (!reason && !feature.nullable) {
67                 reason = "Feature produced null/undefined but is not nullable";
68             }
69
70             console.log("[Pending:", feature.name + "]", reason);
71         }));
72     }
73
74     if (level <= uinitLogger.INFO) {
75         app.on("skip", filtered(function (feature) {
76             console.log("[Skip:", feature.name + "]",
77                         "Reload triggered, but input was unchanged");
78         }));
79     }
80
81     if (level <= uinitLogger.INFO) {
82         app.on("loaded", filtered(function (feature, result) {
83             console.log("[Load:", feature.name + "] =>", result);
84         }));
85     }
86
87     if (level <= uinitLogger.ERROR) {
88         app.on("error", filtered(function (feature, err) {
89             console.log("Error while loading", feature.name);
90             console.log("  " + err.message);
91             console.log("  " + err.stack);
92         }));
93     }
94 }
95
96 uinitLogger.levels = ["debug", "info", "warn", "error"];
97
98 cull.doall(function (level, i) {
99     uinitLogger[level.toUpperCase()] = i;
100 }, uinitLogger.levels);
101
102 uinitLogger(this.gts.app, uinitLogger.INFO);