daidaibg
2022-10-10 a44105de2e57b676280f175237e3f2e2c017db6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
 * Possible log levels
 */
export enum LogLevels {
  VERBOSE = "VERBOSE",
  INFO = "INFO",
  WARN = "WARN",
  ERROR = "ERROR",
}
 
/**
 * Custom logger
 *
 * @param {boolean} labeled — if true,  label is shown
 * @param {string} msg  - message
 * @param {string} type - logging type 'log'|'warn'|'error'|'info'
 * @param {*} [args]      - argument to log with a message
 * @param {string} style  - additional styling to message
 */
 
function _log(
  labeled: boolean,
  msg: string,
  type = "log",
 
  args?: any,
  style = "color: inherit",
): void {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  if (!("console" in window) || !window.console[type])
    return;
 
  const isSimpleType = ["info", "log", "warn", "error"].includes(type);
  const argsToPass = [];
 
  switch (_log.logLevel) {
    case LogLevels.ERROR:
      if (type !== "error")
        return;
 
      break;
 
    case LogLevels.WARN:
      if (!["error", "warn"].includes(type))
        return;
 
      break;
 
    case LogLevels.INFO:
      if (!isSimpleType || labeled)
        return;
 
      break;
  }
 
  if (args)
    argsToPass.push(args);
 
  const editorLabelText = "ViteBoot";
  const editorLabelStyle = `line-height: 1em;
            color: #006FEA;
            display: inline-block;
            font-size: 11px;
            line-height: 1em;
            background-color: #fff;
            padding: 4px 9px;
            border-radius: 30px;
            border: 1px solid rgba(56, 138, 229, 0.16);
            margin: 4px 5px 4px 0;`;
 
  if (labeled) {
    if (isSimpleType) {
      argsToPass.unshift(editorLabelStyle, style);
      msg = `%c${editorLabelText}%c ${msg}`;
    }
    else {
      msg = `( ${editorLabelText} )${msg}`;
    }
  }
 
  try {
    if (!isSimpleType) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-expect-error
      // eslint-disable-next-line no-console
      console[type](msg);
    }
    else if (args) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-expect-error
      // eslint-disable-next-line no-console
      console[type](`${msg} %o`, ...argsToPass);
    }
    else {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-expect-error
      // eslint-disable-next-line no-console
      console[type](msg, ...argsToPass);
    }
  }
  catch (ignored) {}
}
 
/**
 * Current log level
 */
_log.logLevel = LogLevels.VERBOSE;
 
/**
 * Set current log level
 *
 * @param {LogLevels} logLevel - log level to set
 */
export function setLogLevel(logLevel: LogLevels): void {
  _log.logLevel = logLevel;
}
 
/**
 * _log method proxy without log label
 */
export const log = _log.bind(window, false);
 
/**
 * _log method proxy with log label
 */
export const logLabeled = _log.bind(window, true);