Logging.js 7.9 KB

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