Logging.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /***
  2. MochiKit.Logging 1.4
  3. See <http://mochikit.com/> for documentation, downloads, license, etc.
  4. (c) 2005 Bob Ippolito. All rights Reserved.
  5. ***/
  6. if (typeof(dojo) != 'undefined') {
  7. dojo.provide('MochiKit.Logging');
  8. dojo.require('MochiKit.Base');
  9. }
  10. if (typeof(JSAN) != 'undefined') {
  11. JSAN.use("MochiKit.Base", []);
  12. }
  13. try {
  14. if (typeof(MochiKit.Base) == 'undefined') {
  15. throw "";
  16. }
  17. } catch (e) {
  18. throw "MochiKit.Logging depends on MochiKit.Base!";
  19. }
  20. if (typeof(MochiKit.Logging) == 'undefined') {
  21. MochiKit.Logging = {};
  22. }
  23. MochiKit.Logging.NAME = "MochiKit.Logging";
  24. MochiKit.Logging.VERSION = "1.4";
  25. MochiKit.Logging.__repr__ = function () {
  26. return "[" + this.NAME + " " + this.VERSION + "]";
  27. };
  28. MochiKit.Logging.toString = function () {
  29. return this.__repr__();
  30. };
  31. MochiKit.Logging.EXPORT = [
  32. "LogLevel",
  33. "LogMessage",
  34. "Logger",
  35. "alertListener",
  36. "logger",
  37. "log",
  38. "logError",
  39. "logDebug",
  40. "logFatal",
  41. "logWarning"
  42. ];
  43. MochiKit.Logging.EXPORT_OK = [
  44. "logLevelAtLeast",
  45. "isLogMessage",
  46. "compareLogMessage"
  47. ];
  48. /** @id MochiKit.Logging.LogMessage */
  49. MochiKit.Logging.LogMessage = function (num, level, info) {
  50. this.num = num;
  51. this.level = level;
  52. this.info = info;
  53. this.timestamp = new Date();
  54. };
  55. MochiKit.Logging.LogMessage.prototype = {
  56. /** @id MochiKit.Logging.LogMessage.prototype.repr */
  57. repr: function () {
  58. var m = MochiKit.Base;
  59. return 'LogMessage(' +
  60. m.map(
  61. m.repr,
  62. [this.num, this.level, this.info]
  63. ).join(', ') + ')';
  64. },
  65. /** @id MochiKit.Logging.LogMessage.prototype.toString */
  66. toString: MochiKit.Base.forwardCall("repr")
  67. };
  68. MochiKit.Base.update(MochiKit.Logging, {
  69. /** @id MochiKit.Logging.logLevelAtLeast */
  70. logLevelAtLeast: function (minLevel) {
  71. var self = MochiKit.Logging;
  72. if (typeof(minLevel) == 'string') {
  73. minLevel = self.LogLevel[minLevel];
  74. }
  75. return function (msg) {
  76. var msgLevel = msg.level;
  77. if (typeof(msgLevel) == 'string') {
  78. msgLevel = self.LogLevel[msgLevel];
  79. }
  80. return msgLevel >= minLevel;
  81. };
  82. },
  83. /** @id MochiKit.Logging.isLogMessage */
  84. isLogMessage: function (/* ... */) {
  85. var LogMessage = MochiKit.Logging.LogMessage;
  86. for (var i = 0; i < arguments.length; i++) {
  87. if (!(arguments[i] instanceof LogMessage)) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. },
  93. /** @id MochiKit.Logging.compareLogMessage */
  94. compareLogMessage: function (a, b) {
  95. return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]);
  96. },
  97. /** @id MochiKit.Logging.alertListener */
  98. alertListener: function (msg) {
  99. alert(
  100. "num: " + msg.num +
  101. "\nlevel: " + msg.level +
  102. "\ninfo: " + msg.info.join(" ")
  103. );
  104. }
  105. });
  106. /** @id MochiKit.Logging.Logger */
  107. MochiKit.Logging.Logger = function (/* optional */maxSize) {
  108. this.counter = 0;
  109. if (typeof(maxSize) == 'undefined' || maxSize === null) {
  110. maxSize = -1;
  111. }
  112. this.maxSize = maxSize;
  113. this._messages = [];
  114. this.listeners = {};
  115. this.useNativeConsole = false;
  116. };
  117. MochiKit.Logging.Logger.prototype = {
  118. /** @id MochiKit.Logging.Logger.prototype.clear */
  119. clear: function () {
  120. this._messages.splice(0, this._messages.length);
  121. },
  122. /** @id MochiKit.Logging.Logger.prototype.logToConsole */
  123. logToConsole: function (msg) {
  124. if (typeof(window) != "undefined" && window.console
  125. && window.console.log) {
  126. // Safari and FireBug 0.4
  127. // Percent replacement is a workaround for cute Safari crashing bug
  128. window.console.log(msg.replace(/%/g, '\uFF05'));
  129. } else if (typeof(opera) != "undefined" && opera.postError) {
  130. // Opera
  131. opera.postError(msg);
  132. } else if (typeof(printfire) == "function") {
  133. // FireBug 0.3 and earlier
  134. printfire(msg);
  135. } else if (typeof(Debug) != "undefined" && Debug.writeln) {
  136. // IE Web Development Helper (?)
  137. // http://www.nikhilk.net/Entry.aspx?id=93
  138. Debug.writeln(msg);
  139. } else if (typeof(debug) != "undefined" && debug.trace) {
  140. // Atlas framework (?)
  141. // http://www.nikhilk.net/Entry.aspx?id=93
  142. debug.trace(msg);
  143. }
  144. },
  145. /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */
  146. dispatchListeners: function (msg) {
  147. for (var k in this.listeners) {
  148. var pair = this.listeners[k];
  149. if (pair.ident != k || (pair[0] && !pair[0](msg))) {
  150. continue;
  151. }
  152. pair[1](msg);
  153. }
  154. },
  155. /** @id MochiKit.Logging.Logger.prototype.addListener */
  156. addListener: function (ident, filter, listener) {
  157. if (typeof(filter) == 'string') {
  158. filter = MochiKit.Logging.logLevelAtLeast(filter);
  159. }
  160. var entry = [filter, listener];
  161. entry.ident = ident;
  162. this.listeners[ident] = entry;
  163. },
  164. /** @id MochiKit.Logging.Logger.prototype.removeListener */
  165. removeListener: function (ident) {
  166. delete this.listeners[ident];
  167. },
  168. /** @id MochiKit.Logging.Logger.prototype.baseLog */
  169. baseLog: function (level, message/*, ...*/) {
  170. var msg = new MochiKit.Logging.LogMessage(
  171. this.counter,
  172. level,
  173. MochiKit.Base.extend(null, arguments, 1)
  174. );
  175. this._messages.push(msg);
  176. this.dispatchListeners(msg);
  177. if (this.useNativeConsole) {
  178. this.logToConsole(msg.level + ": " + msg.info.join(" "));
  179. }
  180. this.counter += 1;
  181. while (this.maxSize >= 0 && this._messages.length > this.maxSize) {
  182. this._messages.shift();
  183. }
  184. },
  185. /** @id MochiKit.Logging.Logger.prototype.getMessages */
  186. getMessages: function (howMany) {
  187. var firstMsg = 0;
  188. if (!(typeof(howMany) == 'undefined' || howMany === null)) {
  189. firstMsg = Math.max(0, this._messages.length - howMany);
  190. }
  191. return this._messages.slice(firstMsg);
  192. },
  193. /** @id MochiKit.Logging.Logger.prototype.getMessageText */
  194. getMessageText: function (howMany) {
  195. if (typeof(howMany) == 'undefined' || howMany === null) {
  196. howMany = 30;
  197. }
  198. var messages = this.getMessages(howMany);
  199. if (messages.length) {
  200. var lst = map(function (m) {
  201. return '\n [' + m.num + '] ' + m.level + ': ' + m.info.join(' ');
  202. }, messages);
  203. lst.unshift('LAST ' + messages.length + ' MESSAGES:');
  204. return lst.join('');
  205. }
  206. return '';
  207. },
  208. /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */
  209. debuggingBookmarklet: function (inline) {
  210. if (typeof(MochiKit.LoggingPane) == "undefined") {
  211. alert(this.getMessageText());
  212. } else {
  213. MochiKit.LoggingPane.createLoggingPane(inline || false);
  214. }
  215. }
  216. };
  217. MochiKit.Logging.__new__ = function () {
  218. this.LogLevel = {
  219. ERROR: 40,
  220. FATAL: 50,
  221. WARNING: 30,
  222. INFO: 20,
  223. DEBUG: 10
  224. };
  225. var m = MochiKit.Base;
  226. m.registerComparator("LogMessage",
  227. this.isLogMessage,
  228. this.compareLogMessage
  229. );
  230. var partial = m.partial;
  231. var Logger = this.Logger;
  232. var baseLog = Logger.prototype.baseLog;
  233. m.update(this.Logger.prototype, {
  234. debug: partial(baseLog, 'DEBUG'),
  235. log: partial(baseLog, 'INFO'),
  236. error: partial(baseLog, 'ERROR'),
  237. fatal: partial(baseLog, 'FATAL'),
  238. warning: partial(baseLog, 'WARNING')
  239. });
  240. // indirectly find logger so it can be replaced
  241. var self = this;
  242. var connectLog = function (name) {
  243. return function () {
  244. self.logger[name].apply(self.logger, arguments);
  245. };
  246. };
  247. /** @id MochiKit.Logging.log */
  248. this.log = connectLog('log');
  249. /** @id MochiKit.Logging.logError */
  250. this.logError = connectLog('error');
  251. /** @id MochiKit.Logging.logDebug */
  252. this.logDebug = connectLog('debug');
  253. /** @id MochiKit.Logging.logFatal */
  254. this.logFatal = connectLog('fatal');
  255. /** @id MochiKit.Logging.logWarning */
  256. this.logWarning = connectLog('warning');
  257. this.logger = new Logger();
  258. this.logger.useNativeConsole = true;
  259. this.EXPORT_TAGS = {
  260. ":common": this.EXPORT,
  261. ":all": m.concat(this.EXPORT, this.EXPORT_OK)
  262. };
  263. m.nameFunctions(this);
  264. };
  265. if (typeof(printfire) == "undefined" &&
  266. typeof(document) != "undefined" && document.createEvent &&
  267. typeof(dispatchEvent) != "undefined") {
  268. // FireBug really should be less lame about this global function
  269. printfire = function () {
  270. printfire.args = arguments;
  271. var ev = document.createEvent("Events");
  272. ev.initEvent("printfire", false, true);
  273. dispatchEvent(ev);
  274. };
  275. }
  276. MochiKit.Logging.__new__();
  277. MochiKit.Base._exportSymbols(this, MochiKit.Logging);