source: trunk/as3/com/jeroenwijering/utils/Logger.as @ 211

Revision 211, 2.4 KB checked in by jeroen, 4 years ago (diff)

added a Logger class for debugging.

Line 
1package com.jeroenwijering.utils {
2
3
4import com.jeroenwijering.utils.Debug;
5
6import flash.external.ExternalInterface;
7
8
9/**
10* <p>Utility class for logging debug messages. it supports the following logging systems:</p>
11* <ul>
12* <li>The tracing sstem built into the debugging players.</li>
13* <li>The standalone Arthropod AIR application.</li>
14* <li>The Console.log function built into Firefox/Firebug.</li>
15* </ul>
16**/
17public class Logger {
18
19
20        /** Constant defining the Arthropod output type. **/
21        public static const ARTHROPOD:String = "arthropod";
22        /** Constant defining the Firefox/Firebug console output type. **/
23        public static const CONSOLE:String = "console";
24        /** Constant defining there's no output. **/
25        public static const NONE:String = "none";
26        /** Constant defining the Flash tracing output type. **/
27        public static const TRACE:String = "trace";
28
29
30        /** Current output system to use for logging. **/
31        public static var output:String = Logger.NONE;
32
33
34        /**
35        * Log a message to the output system.
36        *
37        * @param message        The message to send forward.
38        * @param type           The type of message.
39        **/
40        public static function log(message:*,type:String="log"):void {
41                if(message == undefined) {
42                        send(type.toUpperCase());
43                } else if (message is String) {
44                        send(type.toUpperCase()+' ('+message+')');
45                } else if (message is Number) {
46                        send(type.toUpperCase()+' ('+message.toString()+')');
47                } else if (message is Array) {
48                        Logger.array(message,type);
49                } else {
50                        Logger.object(message,type);
51                }
52        };
53
54
55        /** Format an array for logging. **/
56        private static function array(message:Array,type:String):void {
57                var txt:String = type.toUpperCase()+' ([';
58                for(var i:Number=0; i<message.length; i++) {
59                        txt += message[i]+', ';
60                }
61                txt = txt.substr(0,txt.length-2) +'])';
62                Logger.send(txt);
63        };
64
65
66        /** Format an object for logging. **/
67        private static function object(message:Object,type:String):void {
68                var txt:String = type.toUpperCase()+' ({';
69                for(var s:String in message) {
70                        txt += s+': '+message[s]+', ';
71                }
72                txt = txt.substr(0,txt.length-2);
73                if(s) { txt += '})'; }
74                Logger.send(txt);
75        };
76
77
78        /** Send the messages to the output system. **/
79        private static function send(text:String):void {
80                switch(Logger.output) {
81                        case Logger.ARTHROPOD:
82                                Debug.log(text);
83                                break;
84                        case Logger.CONSOLE:
85                                ExternalInterface.call('console.log',text);
86                                break;
87                        case Logger.TRACE:
88                                trace(text);
89                                break;
90                        default:
91                                break;
92                }
93        };
94
95
96}
97
98
99}
Note: See TracBrowser for help on using the repository browser.