1 | import GLErrors from './GLErrors';
|
2 | import { ExpoWebGLRenderingContext, GLLoggingOption } from './GLView.types';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const MAX_STRING_LENGTH = 20;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export function configureLogging(gl: ExpoWebGLRenderingContext): void {
|
13 |
|
14 | let loggingOption = GLLoggingOption.DISABLED;
|
15 |
|
16 | gl.__expoSetLogging = (option: GLLoggingOption): void => {
|
17 |
|
18 |
|
19 | if (!loggingOption === !option) {
|
20 | loggingOption = option;
|
21 | return;
|
22 | }
|
23 |
|
24 |
|
25 | if (!option || option === GLLoggingOption.DISABLED) {
|
26 | Object.entries(gl).forEach(([key, value]) => {
|
27 | if (typeof value === 'function' && value.original) {
|
28 | gl[key] = value.original;
|
29 | }
|
30 | });
|
31 | loggingOption = option;
|
32 | return;
|
33 | }
|
34 |
|
35 |
|
36 | Object.entries(gl).forEach(([key, originalValue]) => {
|
37 | if (typeof originalValue !== 'function' || key === '__expoSetLogging') {
|
38 | return;
|
39 | }
|
40 |
|
41 | gl[key] = (...args) => {
|
42 | if (loggingOption & GLLoggingOption.METHOD_CALLS) {
|
43 | const params = args.map(arg => {
|
44 |
|
45 |
|
46 |
|
47 | if (loggingOption & GLLoggingOption.RESOLVE_CONSTANTS && typeof arg === 'number') {
|
48 | for (const prop in gl) {
|
49 | if (gl[prop] === arg) {
|
50 | return `${arg} (${prop})`;
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 | if (loggingOption & GLLoggingOption.TRUNCATE_STRINGS && typeof arg === 'string') {
|
58 | if (arg.length > MAX_STRING_LENGTH) {
|
59 | const lastIndex = arg.lastIndexOf(' ', MAX_STRING_LENGTH);
|
60 | return arg.substr(0, lastIndex >= 0 ? lastIndex : MAX_STRING_LENGTH) + '...';
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 | return '' + arg;
|
66 | });
|
67 | console.log(`ExpoGL: ${key}(${params.join(', ')})`);
|
68 | }
|
69 |
|
70 | const result = originalValue.apply(gl, args);
|
71 |
|
72 | if (loggingOption & GLLoggingOption.METHOD_CALLS) {
|
73 | console.log(`ExpoGL: = ${result}`);
|
74 | }
|
75 | if (loggingOption & GLLoggingOption.GET_ERRORS && key !== 'getError') {
|
76 |
|
77 | const error = gl.getError.original.call(gl);
|
78 |
|
79 | if (error && error !== gl.NO_ERROR) {
|
80 |
|
81 | console.log(`\x1b[31mExpoGL: Error ${GLErrors[error]}\x1b[0m`);
|
82 | }
|
83 | }
|
84 | return result;
|
85 | };
|
86 | gl[key].original = originalValue;
|
87 | });
|
88 |
|
89 | loggingOption = option;
|
90 | };
|
91 | }
|