console-logger.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. export namespace ConsoleLogger {
  7. export function formatTime(t: number) {
  8. if (isNaN(t)) return 'n/a';
  9. const h = Math.floor(t / (60 * 60 * 1000)),
  10. m = Math.floor(t / (60 * 1000) % 60),
  11. s = Math.floor(t / 1000 % 60);
  12. let ms = Math.floor(t % 1000).toString();
  13. while (ms.length < 3) ms = '0' + ms;
  14. if (h > 0) return `${h}h${m}m${s}.${ms}s`;
  15. if (m > 0) return `${m}m${s}.${ms}s`;
  16. if (s > 0) return `${s}.${ms}s`;
  17. return `${t.toFixed(0)}ms`;
  18. }
  19. export function log(tag: string, msg: string) {
  20. console.log(`[${tag}] ${msg}`);
  21. }
  22. export function logId(guid: string, tag: string, msg: string) {
  23. console.log(`[${guid}][${tag}] ${msg}`);
  24. }
  25. export function error(ctx: string, e: any) {
  26. console.error(`[Error] (${ctx}) ${e}`);
  27. if (e.stack) console.error(e.stack);
  28. }
  29. export function warn(ctx: string, e: any) {
  30. console.error(`[Warn] (${ctx}) ${e}`);
  31. }
  32. export function errorId(guid: string, e: any) {
  33. console.error(`[${guid}][Error] ${e}`);
  34. if (e.stack) console.error(e.stack);
  35. }
  36. }