PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/haxe/haxe/io/log/Logger.hx

http://github.com/tomtera/stax
Haxe | 180 lines | 120 code | 28 blank | 32 comment | 27 complexity | 30d2e3f720be6c0152d3c810ec540f8d MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. HaXe library written by John A. De Goes <john@socialmedia.com>
  3. Contributed by Social Media Networks
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the
  8. distribution.
  9. THIS SOFTWARE IS PROVIDED BY SOCIAL MEDIA NETWORKS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOCIAL MEDIA NETWORKS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  11. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  12. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. */
  14. package haxe.io.log;
  15. import Prelude;
  16. import haxe.PosInfos;
  17. using PreludeExtensions;
  18. typedef LogHandler = LogLevel -> String -> PosInfos -> Void
  19. interface LoggerFacade {
  20. public function trace<T>(t: T, ?p: PosInfos): T;
  21. public function debug(s: String, ?p: PosInfos): Void;
  22. public function info(s: String, ?p: PosInfos): Void;
  23. public function warning(s: String, ?p: PosInfos): Void;
  24. public function error(s: String, ?p: PosInfos): Void;
  25. public function fatal(s: String, ?p: PosInfos): Void;
  26. }
  27. enum LogLevel { All; Debug; Info; Warning; Error; Fatal; None; }
  28. class Logger {
  29. /** The default handlers used for all loggers created using get() */
  30. public static var defaultHandlers: Array<LogHandler> = [];
  31. /** The default filtering level for all loggers created using get() */
  32. public static var defaultLevel: LogLevel = Warning;
  33. /** Convenience function that constructs a logger whose handlers are formed
  34. * from the 'defaultHandlers' field of this class, and whose messages are
  35. * filtered to include only those messages logged at the level of the
  36. * 'defaultLevel' field of this class, or higher.
  37. */
  38. public static function get(): LoggerFacade {
  39. return Logger.create({
  40. level: function() { return defaultLevel; },
  41. handlers: defaultHandlers
  42. });
  43. }
  44. /** Convenience function that constructs a logger with the specified handlers
  45. * and log message cutoff level.
  46. */
  47. public static function create(config: {level: Thunk<LogLevel>, handlers: Array<LogHandler>}): LoggerFacade {
  48. return new LoggerBridge(LogHandlers.filter(LogHandlers.composite(config.handlers), config.level));
  49. }
  50. /** Convenience function that creates a debug logger facade that traces all
  51. * information using HaXe's built-in trace function (and the browser console,
  52. * if the target is JavaScript or Flash).
  53. */
  54. public static function debug(): LoggerFacade {
  55. return Logger.create({
  56. level: Debug.toThunk(),
  57. handlers: [
  58. LogHandlers.Trace
  59. #if (js || flash)
  60. , LogHandlers.Console
  61. #end
  62. ]
  63. });
  64. }
  65. }
  66. /** Converts all the facade methods to log handler invocations */
  67. class LoggerBridge implements LoggerFacade {
  68. var _handler: LogHandler;
  69. public function new(h: LogHandler) {
  70. _handler = h;
  71. }
  72. public function trace<T>(t: T, ?p: PosInfos): T { debug(Std.string(t), p); return t; }
  73. public function debug(s: String, ?p: PosInfos) { _handler(Debug, s, p); }
  74. public function info(s: String, ?p: PosInfos) { _handler(Info, s, p); }
  75. public function warning(s: String, ?p: PosInfos) { _handler(Warning, s, p); }
  76. public function error(s: String, ?p: PosInfos) { _handler(Error, s, p); }
  77. public function fatal(s: String, ?p: PosInfos) { _handler(Fatal, s, p); }
  78. }
  79. /** Standard log handlers */
  80. class LogHandlers {
  81. public static var Trace = function(level: LogLevel, text: String, p: PosInfos): Void {
  82. trace(textLevel(level).toUpperCase() + ": " + format(text, p));
  83. }
  84. #if js
  85. public static var Console = function(level: LogLevel, text: String, p: PosInfos): Void {
  86. (function(text) {
  87. var c = untyped __js__('(typeof console != "undefined") ? console : null');
  88. if (c != null) {
  89. switch (level) {
  90. case All, Debug: if (c.debug != null) c.debug(text);
  91. case Info: if (c.info != null) c.info(text);
  92. case Warning: if (c.warn != null) c.warn(text);
  93. case Error, Fatal, None: if (c.error != null) c.error(text);
  94. }
  95. }
  96. })(format(text, p));
  97. }
  98. #elseif flash
  99. public static var Console = function(level: LogLevel, text: String, p: PosInfos): Void {
  100. (function(text) {
  101. if (flash.external.ExternalInterface.available) {
  102. switch (level) {
  103. case All, Debug: flash.external.ExternalInterface.call('(function(text){if (console != null && console.debug) console.debug(text);})', text);
  104. case Info: flash.external.ExternalInterface.call('(function(text){if (console != null && console.warn) console.warn(text);})', text);
  105. case Warning: flash.external.ExternalInterface.call('(function(text){if (console != null && console.warn) console.warn(text);})', text);
  106. case Error, Fatal, None: flash.external.ExternalInterface.call('(function(text){if (console != null && console.error) console.error(text);})', text);
  107. }
  108. }
  109. })(format(text, p));
  110. }
  111. #end
  112. public static function composite(fns: Array<LogHandler>): LogHandler {
  113. return function(l, t, p) {
  114. for (f in fns) f(l, t, p);
  115. }
  116. }
  117. public static function filter(input: LogHandler, cutoff: Thunk<LogLevel>): LogHandler {
  118. return function(l, t, p) {
  119. if (intLevel(l) >= intLevel(cutoff())) {
  120. input(l, t, p);
  121. }
  122. }
  123. }
  124. private static function format(text: String, p: PosInfos) {
  125. return p.fileName + ":" + p.lineNumber + " (" + p.className + "." + p.methodName + "): " + text;
  126. }
  127. private static function textLevel(level: LogLevel) {
  128. return switch (level) {
  129. case All: "All";
  130. case Debug: "Debug";
  131. case Info: "Info";
  132. case Warning: "Warning";
  133. case Error: "Error";
  134. case Fatal: "Fatal";
  135. case None: "None";
  136. }
  137. }
  138. private static function intLevel(level: LogLevel) {
  139. return switch (level) {
  140. case All: 0;
  141. case Debug: 1;
  142. case Info: 2;
  143. case Warning: 3;
  144. case Error: 4;
  145. case Fatal: 5;
  146. case None: 6;
  147. }
  148. }
  149. }