logger.ts 1.1 KB

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