initial commit
[namibia] / public / js / app / debug.js
1 /**
2  * Debugging class.
3  */
4 ;(function(){
5
6         window.tantrum = function(context, task, message)
7         {
8                 this.context    = context;
9                 this.task               = task;
10                 this.message    = message;
11                 bug.error('EXCEPTION on ' + context, task, message);
12         };
13
14         window.bug = {
15
16
17                 /**
18                  * Debug level.
19                  *  0 : No logging
20                  *  1 : Errors only
21                  *  2 : Errors and warnings
22                  *  3 : Errors, warnings and information
23                  *  4 : Errors, warnings, information and dumps
24                  *  5 : Flood mode, hope you can swim
25                  * @type {Number}
26                  */
27                 _debugLevel : 3,
28
29
30                 /**
31                  * Set debug verbosity level.
32                  * @param {Number} level Debug verbosity level
33                  */
34                 setLevel : function(level)
35                 {
36                         bug._debugLevel = level;
37                 },
38
39                 /**
40                  * Start a logging group.
41                  * @param  {String} groupName Name of the group
42                  */
43                 group : function(groupName)
44                 {
45                         console.groupCollapsed(groupName);
46                 },
47
48                 /**
49                  * End the current logging group.
50                  */
51                 ungroup : function()
52                 {
53                         console.groupEnd();
54                 },
55
56                 /**
57                  * Log an error.
58                  * @param  {String}  context Module, class or object context
59                  * @param  {String}  task    The task
60                  * @param  {Unknown} data    The data to log
61                  */
62                 error : function(context, task, data)
63                 {
64                         if (0 < bug._debugLevel)
65                         {
66                                 bug._handle('', 'error', context, task, data);
67                         }
68                 },
69                 
70                 /**
71                  * Log a warning.
72                  * @param  {String}  context Module, class or object context
73                  * @param  {String}  task    The task
74                  * @param  {Unknown} data    The data to log
75                  */
76                 warn : function(context, task, data)
77                 {
78                         if (1 < bug._debugLevel)
79                         {
80                                 bug._handle('', 'warn', context, task, data);
81                         }
82                 },
83                 debug : function(context, task, data)
84                 {
85                         if (1 < bug._debugLevel)
86                         {
87                                 bug._handle('-oO- ', 'warn', context, task, data);
88                         }
89                 },
90                 
91                 /**
92                  * Log some information.
93                  * @param  {String}  context Module, class or object context
94                  * @param  {String}  task    The task
95                  * @param  {Unknown} data    The data to log
96                  */
97                 info : function(context, task, data)
98                 {
99                         if (2 < bug._debugLevel)
100                         {
101                                 bug._handle('(i) ', 'log', context, task, data);
102                         }
103                 },
104                 
105                 /**
106                  * Dump some information, probably lots of it.
107                  * @param  {String}  context Module, class or object context
108                  * @param  {String}  task    The task
109                  * @param  {Unknown} data    The data to log
110                  */
111                 dump : function(context, task, data)
112                 {
113                         if (3 < bug._debugLevel)
114                         {
115                                 bug._handle('<dump> ', 'log', context, task, data);
116                         }
117                 },
118                 
119                 /**
120                  * Drown the developer in tons of information.
121                  * @param  {String}  context Module, class or object context
122                  * @param  {String}  task    The task
123                  * @param  {Unknown} data    The data to log
124                  */
125                 drown : function(context, task, data)
126                 {
127                         if (4 < bug._debugLevel)
128                         {
129                                 bug._handle('..... ', 'log', context, task, data);
130                         }
131                 },
132
133                 /**
134                  * Internal utility to do the actual logging.
135                  * @param  {String}  level   Logging level
136                  * @param  {String}  method  Console method to use
137                  * @param  {String}  context Module, class or object context
138                  * @param  {String}  task    The task
139                  * @param  {Unknown} data    The data to log
140                  */
141                 _handle : function(level, method, context, task, data, wrap)
142                 {
143                         if (undefined == data && undefined == task)
144                         {
145                                 console[method](level, context);
146                         }
147                         else if (undefined == data)
148                         {
149                                 console[method](level + context, task);
150                         }
151                         else
152                         {
153                                 console[method](level + context + ':' + task, data);
154                         }
155                 }
156
157
158         };
159
160 })();