You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
933 B

  1. const maxSmallIntegerV8 = 2 ** 30; // Max number that can be stored in V8's smis (small integers)
  2. const scopes = new Map<number, LogScope>();
  3. let scopeCounter = 0;
  4. export interface LogScope {
  5. readonly scopeId?: number;
  6. readonly prefix: string;
  7. exitDetails?: string;
  8. }
  9. export function clearLogScope(scopeId: number) {
  10. scopes.delete(scopeId);
  11. }
  12. export function getLogScope(): LogScope | undefined {
  13. return scopes.get(scopeCounter);
  14. }
  15. export function getNewLogScope(prefix: string): LogScope {
  16. const scopeId = getNextLogScopeId();
  17. return {
  18. scopeId: scopeId,
  19. prefix: `[${String(scopeId).padStart(5)}] ${prefix}`,
  20. };
  21. }
  22. export function getLogScopeId(): number {
  23. return scopeCounter;
  24. }
  25. export function getNextLogScopeId(): number {
  26. if (scopeCounter === maxSmallIntegerV8) {
  27. scopeCounter = 0;
  28. }
  29. return ++scopeCounter;
  30. }
  31. export function setLogScope(scopeId: number, scope: LogScope) {
  32. scopes.set(scopeId, scope);
  33. }