1 | import GLErrors from './GLErrors';
|
2 | import { GLLoggingOption } from './GLView.types';
|
3 | /**
|
4 | * Maximum length of the strings printed to the console.
|
5 | */
|
6 | const MAX_STRING_LENGTH = 20;
|
7 | /**
|
8 | * Sets up `__expoSetLogging` method providing some logging options useful when debugging GL calls.
|
9 | */
|
10 | export function configureLogging(gl) {
|
11 | // Enable/disable logging of all GL function calls
|
12 | let loggingOption = GLLoggingOption.DISABLED;
|
13 | gl.__expoSetLogging = (option) => {
|
14 | // If boolean values are the same, just change the internal value,
|
15 | // there is no need to wrap/unwrap functions in this case.
|
16 | if (!loggingOption === !option) {
|
17 | loggingOption = option;
|
18 | return;
|
19 | }
|
20 | // Turn off logging.
|
21 | if (!option || option === GLLoggingOption.DISABLED) {
|
22 | Object.entries(gl).forEach(([key, value]) => {
|
23 | if (typeof value === 'function' && value.original) {
|
24 | gl[key] = value.original;
|
25 | }
|
26 | });
|
27 | loggingOption = option;
|
28 | return;
|
29 | }
|
30 | // Turn on logging.
|
31 | Object.entries(gl).forEach(([key, originalValue]) => {
|
32 | if (typeof originalValue !== 'function' || key === '__expoSetLogging') {
|
33 | return;
|
34 | }
|
35 | gl[key] = (...args) => {
|
36 | if (loggingOption & GLLoggingOption.METHOD_CALLS) {
|
37 | const params = args.map(arg => {
|
38 | // If the type is `number`, then try to find name of the constant that has such value,
|
39 | // so it's easier to read these logs. In some cases it might be misleading
|
40 | // if the parameter is for example a width or height, so the number is still logged.
|
41 | if (loggingOption & GLLoggingOption.RESOLVE_CONSTANTS && typeof arg === 'number') {
|
42 | for (const prop in gl) {
|
43 | if (gl[prop] === arg) {
|
44 | return `${arg} (${prop})`;
|
45 | }
|
46 | }
|
47 | }
|
48 | // Truncate strings so they don't produce too much output and don't block the bridge.
|
49 | // It mostly applies to shaders which might be very long...
|
50 | if (loggingOption & GLLoggingOption.TRUNCATE_STRINGS && typeof arg === 'string') {
|
51 | if (arg.length > MAX_STRING_LENGTH) {
|
52 | const lastIndex = arg.lastIndexOf(' ', MAX_STRING_LENGTH);
|
53 | return arg.substr(0, lastIndex >= 0 ? lastIndex : MAX_STRING_LENGTH) + '...';
|
54 | }
|
55 | }
|
56 | // Just return the parameter as a string.
|
57 | return '' + arg;
|
58 | });
|
59 | console.log(`ExpoGL: ${key}(${params.join(', ')})`);
|
60 | }
|
61 | const result = originalValue.apply(gl, args);
|
62 | if (loggingOption & GLLoggingOption.METHOD_CALLS) {
|
63 | console.log(`ExpoGL: = ${result}`);
|
64 | }
|
65 | if (loggingOption & GLLoggingOption.GET_ERRORS && key !== 'getError') {
|
66 | // @ts-ignore We need to call into the original `getError`.
|
67 | const error = gl.getError.original.call(gl);
|
68 | if (error && error !== gl.NO_ERROR) {
|
69 | // `console.error` would cause a red screen, so let's just log with red color.
|
70 | console.log(`\x1b[31mExpoGL: Error ${GLErrors[error]}\x1b[0m`);
|
71 | }
|
72 | }
|
73 | return result;
|
74 | };
|
75 | gl[key].original = originalValue;
|
76 | });
|
77 | loggingOption = option;
|
78 | };
|
79 | }
|
80 | //# sourceMappingURL=GLUtils.js.map |
\ | No newline at end of file |