diff options
Diffstat (limited to 'sources/core/log.js')
-rw-r--r-- | sources/core/log.js | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/sources/core/log.js b/sources/core/log.js new file mode 100644 index 0000000..6981612 --- /dev/null +++ b/sources/core/log.js | |||
@@ -0,0 +1,127 @@ | |||
1 | /** | ||
2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | ||
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | ||
4 | */ | ||
5 | |||
6 | /** | ||
7 | * @fileOverview Defines {@link CKEDITOR#verbosity} and binary flags {@link CKEDITOR#VERBOSITY_WARN} and | ||
8 | * {@link CKEDITOR#VERBOSITY_ERROR}. Defines also the {@link CKEDITOR#error} and {@link CKEDITOR#warn} functions | ||
9 | * and the default handler for the {@link CKEDITOR#log} event. | ||
10 | */ | ||
11 | |||
12 | /* global console */ | ||
13 | |||
14 | 'use strict'; | ||
15 | |||
16 | /** | ||
17 | * Warning reporting verbosity level. When {@link CKEDITOR#verbosity} is set to this value, only {@link CKEDITOR#warn} | ||
18 | * messages will be output to the console. It is a binary flag so it might be combined with | ||
19 | * the {@link CKEDITOR#VERBOSITY_ERROR} flag. | ||
20 | * | ||
21 | * @since 4.5.4 | ||
22 | * @readonly | ||
23 | * @property {Number} [=1] | ||
24 | * @member CKEDITOR | ||
25 | */ | ||
26 | CKEDITOR.VERBOSITY_WARN = 1; | ||
27 | |||
28 | /** | ||
29 | * Error reporting verbosity level. When {@link CKEDITOR#verbosity} is set to this value, only {@link CKEDITOR#error} | ||
30 | * messages will be output to the console. It is a binary flag so it might be combined with | ||
31 | * the {@link CKEDITOR#VERBOSITY_WARN} flag. | ||
32 | * | ||
33 | * @since 4.5.4 | ||
34 | * @readonly | ||
35 | * @property {Number} [=2] | ||
36 | * @member CKEDITOR | ||
37 | */ | ||
38 | CKEDITOR.VERBOSITY_ERROR = 2; | ||
39 | |||
40 | /** | ||
41 | * Verbosity of {@link CKEDITOR#error} and {@link CKEDITOR#warn} methods. Accepts binary flags | ||
42 | * {@link CKEDITOR#VERBOSITY_WARN} and {@link CKEDITOR#VERBOSITY_ERROR}. | ||
43 | * | ||
44 | * CKEDITOR.verbosity = 0; // No console output after CKEDITOR.warn and CKEDITOR.error methods. | ||
45 | * CKEDITOR.verbosity = CKEDITOR.VERBOSITY_WARN; // Console output after CKEDITOR.warn only. | ||
46 | * CKEDITOR.verbosity = CKEDITOR.VERBOSITY_ERROR; // Console output after CKEDITOR.error only. | ||
47 | * CKEDITOR.verbosity = CKEDITOR.VERBOSITY_WARN | CKEDITOR.VERBOSITY_ERROR; // Console output after both methods. | ||
48 | * | ||
49 | * Default value enables both {@link CKEDITOR#VERBOSITY_WARN} and {@link CKEDITOR#VERBOSITY_ERROR}. | ||
50 | * | ||
51 | * @since 4.5.4 | ||
52 | * @member CKEDITOR | ||
53 | * @type {Number} | ||
54 | */ | ||
55 | CKEDITOR.verbosity = CKEDITOR.VERBOSITY_WARN | CKEDITOR.VERBOSITY_ERROR; | ||
56 | |||
57 | /** | ||
58 | * Warning reporting function. When {@link CKEDITOR#verbosity} has the {@link CKEDITOR#VERBOSITY_WARN} flag set, it fires | ||
59 | * the {@link CKEDITOR#log} event with type set to `warn`. Fired event contains also provided `errorCode` and `additionalData`. | ||
60 | * | ||
61 | * @since 4.5.4 | ||
62 | * @member CKEDITOR | ||
63 | * @param {String} errorCode Error code describing reported problem. | ||
64 | * @param {Object} [additionalData] Additional data associated with reported problem. | ||
65 | */ | ||
66 | CKEDITOR.warn = function( errorCode, additionalData ) { | ||
67 | if ( CKEDITOR.verbosity & CKEDITOR.VERBOSITY_WARN ) { | ||
68 | CKEDITOR.fire( 'log', { type: 'warn', errorCode: errorCode, additionalData: additionalData } ); | ||
69 | } | ||
70 | }; | ||
71 | |||
72 | /** | ||
73 | * Error reporting function. When {@link CKEDITOR#verbosity} has {@link CKEDITOR#VERBOSITY_ERROR} flag set, it fires | ||
74 | * {@link CKEDITOR#log} event with the type set to `error`. The fired event also contains the provided `errorCode` and | ||
75 | * `additionalData`. | ||
76 | * | ||
77 | * @since 4.5.4 | ||
78 | * @member CKEDITOR | ||
79 | * @param {String} errorCode Error code describing the reported problem. | ||
80 | * @param {Object} [additionalData] Additional data associated with the reported problem. | ||
81 | */ | ||
82 | CKEDITOR.error = function( errorCode, additionalData ) { | ||
83 | if ( CKEDITOR.verbosity & CKEDITOR.VERBOSITY_ERROR ) { | ||
84 | CKEDITOR.fire( 'log', { type: 'error', errorCode: errorCode, additionalData: additionalData } ); | ||
85 | } | ||
86 | }; | ||
87 | |||
88 | /** | ||
89 | * Fired by {@link CKEDITOR#warn} and {@link CKEDITOR#error} methods. | ||
90 | * Default listener logs provided information to the console. | ||
91 | * | ||
92 | * This event can be used to provide a custom error/warning handler: | ||
93 | * | ||
94 | * CKEDTIOR.on( 'log', function( evt ) { | ||
95 | * // Cancel default listener. | ||
96 | * evt.cancel(); | ||
97 | * // Log event data. | ||
98 | * console.log( evt.data.type, evt.data.errorCode, evt.data.additionalData ); | ||
99 | * } ); | ||
100 | * | ||
101 | * @since 4.5.4 | ||
102 | * @event log | ||
103 | * @member CKEDITOR | ||
104 | * @param data | ||
105 | * @param {String} data.type Log type. Can be `error` or `warn`. | ||
106 | * @param {String} data.errorCode Error code describing the reported problem. | ||
107 | * @param {Object} [data.additionalData] Additional data associated with this log event. | ||
108 | */ | ||
109 | CKEDITOR.on( 'log', function( evt ) { | ||
110 | if ( !window.console || !window.console.log ) { | ||
111 | return; | ||
112 | } | ||
113 | |||
114 | var type = console[ evt.data.type ] ? evt.data.type : 'log', | ||
115 | errorCode = evt.data.errorCode, | ||
116 | additionalData = evt.data.additionalData, | ||
117 | prefix = '[CKEDITOR] ', | ||
118 | errorCodeLabel = 'Error code: '; | ||
119 | |||
120 | if ( additionalData ) { | ||
121 | console[ type ]( prefix + errorCodeLabel + errorCode + '.', additionalData ); | ||
122 | } else { | ||
123 | console[ type ]( prefix + errorCodeLabel + errorCode + '.' ); | ||
124 | } | ||
125 | |||
126 | console[ type ]( prefix + 'For more information about this error go to http://docs.ckeditor.com/#!/guide/dev_errors-section-' + errorCode ); | ||
127 | }, null, null, 999 ); | ||