babylon.c2dlogging.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. module BABYLON {
  2. // logging stuffs
  3. export class C2DLogging {
  4. // Set to true to temporary disable logging.
  5. public static snooze = true;
  6. public static logFrameRender(frameCount: number) {
  7. C2DLogging.snooze = true;
  8. C2DLogging._logFramesCount = frameCount;
  9. }
  10. public static setPostMessage(message: () => string) {
  11. if (C2DLoggingInternals.enableLog) {
  12. C2DLoggingInternals.postMessages[C2DLoggingInternals.callDepth-1] = message();
  13. }
  14. }
  15. public static _startFrameRender() {
  16. if (C2DLogging._logFramesCount === 0) {
  17. return;
  18. }
  19. C2DLogging.snooze = false;
  20. }
  21. public static _endFrameRender() {
  22. if (C2DLogging._logFramesCount === 0) {
  23. return;
  24. }
  25. C2DLogging.snooze = true;
  26. --C2DLogging._logFramesCount;
  27. }
  28. private static _logFramesCount = 0;
  29. }
  30. class C2DLoggingInternals {
  31. //-------------FLAG TO CHANGE TO ENABLE/DISABLE LOGGING ACTIVATION--------------
  32. // This flag can't be changed at runtime you must manually change it in the code
  33. public static enableLog = false;
  34. public static callDepth = 0;
  35. public static depths = [
  36. "|-", "|--", "|---", "|----", "|-----", "|------", "|-------", "|--------", "|---------", "|----------",
  37. "|-----------", "|------------", "|-------------", "|--------------", "|---------------", "|----------------", "|-----------------", "|------------------", "|-------------------", "|--------------------"
  38. ];
  39. public static postMessages = [];
  40. public static computeIndent(): string {
  41. // Compute the indent
  42. let indent: string = null;
  43. if (C2DLoggingInternals.callDepth < 20) {
  44. indent = C2DLoggingInternals.depths[C2DLoggingInternals.callDepth];
  45. } else {
  46. indent = "|";
  47. for (let i = 0; i <= C2DLoggingInternals.callDepth; i++) {
  48. indent = indent + "-";
  49. }
  50. }
  51. return indent;
  52. }
  53. public static getFormattedValue(a): string {
  54. if (a instanceof Prim2DBase) {
  55. return a.id;
  56. }
  57. if (a == null) {
  58. return "[null]";
  59. }
  60. return a.toString();
  61. }
  62. }
  63. export function logProp<T>(message: string = "", alsoGet = false, setNoProlog=false, getNoProlog=false): (target: Object, propName: string | symbol, descriptor: TypedPropertyDescriptor<T>) => void {
  64. return (target: Object, propName: string | symbol, descriptor: TypedPropertyDescriptor<T>) => {
  65. if (!C2DLoggingInternals.enableLog) {
  66. return descriptor;
  67. }
  68. let getter = descriptor.get, setter = descriptor.set;
  69. if (getter && alsoGet) {
  70. descriptor.get = function (): T {
  71. if (C2DLogging.snooze) {
  72. return getter.call(this);
  73. } else {
  74. let indent = C2DLoggingInternals.computeIndent();
  75. let id = this.id || "";
  76. if (message !== null && message !== "") {
  77. console.log(message);
  78. }
  79. let isSPP = this instanceof SmartPropertyPrim;
  80. let flags = isSPP ? this._flags : 0;
  81. let depth = C2DLoggingInternals.callDepth;
  82. if (!getNoProlog) {
  83. console.log(`${indent} [${id}] (${depth}) ==> get ${propName} property`);
  84. }
  85. ++C2DLoggingInternals.callDepth;
  86. C2DLogging.setPostMessage(() => "[no msg]");
  87. // Call the initial getter
  88. let r = getter.call(this);
  89. --C2DLoggingInternals.callDepth;
  90. let flagsStr = "";
  91. if (isSPP) {
  92. let nflags = this._flags;
  93. let newFlags = this._getFlagsDebug((nflags & flags) ^ nflags);
  94. let removedFlags = this._getFlagsDebug((nflags & flags) ^ flags);
  95. flagsStr = "";
  96. if (newFlags !== "") {
  97. flagsStr = ` +++[${newFlags}]`;
  98. }
  99. if (removedFlags !== "") {
  100. if (flagsStr !== "") {
  101. flagsStr += ",";
  102. }
  103. flagsStr += ` ---[${removedFlags}]`;
  104. }
  105. }
  106. console.log(`${indent} [${id}] (${depth})${getNoProlog ? "" : " <=="} get ${propName} property => ${C2DLoggingInternals.getFormattedValue(r)}${flagsStr}, ${C2DLoggingInternals.postMessages[C2DLoggingInternals.callDepth]}`);
  107. return r;
  108. }
  109. }
  110. }
  111. // Overload the property setter implementation to add our own logic
  112. if (setter) {
  113. descriptor.set = function (val) {
  114. if (C2DLogging.snooze) {
  115. setter.call(this, val);
  116. } else {
  117. let indent = C2DLoggingInternals.computeIndent();
  118. let id = this.id || "";
  119. if (message !== null && message !== "") {
  120. console.log(message);
  121. }
  122. let isSPP = this instanceof SmartPropertyPrim;
  123. let flags = isSPP ? this._flags : 0;
  124. let depth = C2DLoggingInternals.callDepth;
  125. if (!setNoProlog) {
  126. console.log(`${indent} [${id}] (${depth}) ==> set ${propName} property with ${C2DLoggingInternals.getFormattedValue(val)}`);
  127. }
  128. ++C2DLoggingInternals.callDepth;
  129. C2DLogging.setPostMessage(() => "[no msg]");
  130. // Change the value
  131. setter.call(this, val);
  132. --C2DLoggingInternals.callDepth;
  133. let flagsStr = "";
  134. if (isSPP) {
  135. let nflags = this._flags;
  136. let newFlags = this._getFlagsDebug((nflags & flags) ^ nflags);
  137. let removedFlags = this._getFlagsDebug((nflags & flags) ^ flags);
  138. flagsStr = "";
  139. if (newFlags !== "") {
  140. flagsStr = ` +++[${newFlags}]`;
  141. }
  142. if (removedFlags !== "") {
  143. if (flagsStr !== "") {
  144. flagsStr += ",";
  145. }
  146. flagsStr += ` ---[${removedFlags}]`;
  147. }
  148. }
  149. console.log(`${indent} [${id}] (${depth})${setNoProlog ? "" : " <=="} set ${propName} property, ${C2DLoggingInternals.postMessages[C2DLoggingInternals.callDepth]}${flagsStr}`);
  150. }
  151. }
  152. }
  153. return descriptor;
  154. }
  155. }
  156. export function logMethod(message: string = "", noProlog = false) {
  157. return (target, key, descriptor) => {
  158. if (!C2DLoggingInternals.enableLog) {
  159. return descriptor;
  160. }
  161. if (descriptor === undefined) {
  162. descriptor = Object.getOwnPropertyDescriptor(target, key);
  163. }
  164. var originalMethod = descriptor.value;
  165. //editing the descriptor/value parameter
  166. descriptor.value = function () {
  167. var args = [];
  168. for (var _i = 0; _i < arguments.length; _i++) {
  169. args[_i - 0] = arguments[_i];
  170. }
  171. if (C2DLogging.snooze) {
  172. return originalMethod.apply(this, args);
  173. } else {
  174. var a = args.map(a => C2DLoggingInternals.getFormattedValue(a) + ", ").join();
  175. a = a.slice(0, a.length - 2);
  176. let indent = C2DLoggingInternals.computeIndent();
  177. let id = this.id || "";
  178. if (message !== null && message !== "") {
  179. console.log(message);
  180. }
  181. let isSPP = this instanceof SmartPropertyPrim;
  182. let flags = isSPP ? this._flags : 0;
  183. let depth = C2DLoggingInternals.callDepth;
  184. if (!noProlog) {
  185. console.log(`${indent} [${id}] (${depth}) ==> call: ${key} (${a})`);
  186. }
  187. ++C2DLoggingInternals.callDepth;
  188. C2DLogging.setPostMessage(() => "[no msg]");
  189. // Call the method!
  190. var result = originalMethod.apply(this, args);
  191. --C2DLoggingInternals.callDepth;
  192. let flagsStr = "";
  193. if (isSPP) {
  194. let nflags = this._flags;
  195. let newFlags = this._getFlagsDebug((nflags & flags) ^ nflags);
  196. let removedFlags = this._getFlagsDebug((nflags & flags) ^ flags);
  197. flagsStr = "";
  198. if (newFlags !== "") {
  199. flagsStr = ` +++[${newFlags}]`;
  200. }
  201. if (removedFlags !== "") {
  202. if (flagsStr !== "") {
  203. flagsStr += ",";
  204. }
  205. flagsStr += ` ---[${removedFlags}]`;
  206. }
  207. }
  208. console.log(`${indent} [${id}] (${depth})${noProlog ? "" : " <=="} call: ${key} (${a}) Res: ${C2DLoggingInternals.getFormattedValue(result)}, ${C2DLoggingInternals.postMessages[C2DLoggingInternals.callDepth]}${flagsStr}`);
  209. return result;
  210. }
  211. };
  212. // return edited descriptor as opposed to overwriting the descriptor
  213. return descriptor;
  214. }
  215. }
  216. }