index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const META = Symbol('proc-log.meta')
  2. module.exports = {
  3. META: META,
  4. output: {
  5. LEVELS: [
  6. 'standard',
  7. 'error',
  8. 'buffer',
  9. 'flush',
  10. ],
  11. KEYS: {
  12. standard: 'standard',
  13. error: 'error',
  14. buffer: 'buffer',
  15. flush: 'flush',
  16. },
  17. standard: function (...args) {
  18. return process.emit('output', 'standard', ...args)
  19. },
  20. error: function (...args) {
  21. return process.emit('output', 'error', ...args)
  22. },
  23. buffer: function (...args) {
  24. return process.emit('output', 'buffer', ...args)
  25. },
  26. flush: function (...args) {
  27. return process.emit('output', 'flush', ...args)
  28. },
  29. },
  30. log: {
  31. LEVELS: [
  32. 'notice',
  33. 'error',
  34. 'warn',
  35. 'info',
  36. 'verbose',
  37. 'http',
  38. 'silly',
  39. 'timing',
  40. 'pause',
  41. 'resume',
  42. ],
  43. KEYS: {
  44. notice: 'notice',
  45. error: 'error',
  46. warn: 'warn',
  47. info: 'info',
  48. verbose: 'verbose',
  49. http: 'http',
  50. silly: 'silly',
  51. timing: 'timing',
  52. pause: 'pause',
  53. resume: 'resume',
  54. },
  55. error: function (...args) {
  56. return process.emit('log', 'error', ...args)
  57. },
  58. notice: function (...args) {
  59. return process.emit('log', 'notice', ...args)
  60. },
  61. warn: function (...args) {
  62. return process.emit('log', 'warn', ...args)
  63. },
  64. info: function (...args) {
  65. return process.emit('log', 'info', ...args)
  66. },
  67. verbose: function (...args) {
  68. return process.emit('log', 'verbose', ...args)
  69. },
  70. http: function (...args) {
  71. return process.emit('log', 'http', ...args)
  72. },
  73. silly: function (...args) {
  74. return process.emit('log', 'silly', ...args)
  75. },
  76. timing: function (...args) {
  77. return process.emit('log', 'timing', ...args)
  78. },
  79. pause: function () {
  80. return process.emit('log', 'pause')
  81. },
  82. resume: function () {
  83. return process.emit('log', 'resume')
  84. },
  85. },
  86. time: {
  87. LEVELS: [
  88. 'start',
  89. 'end',
  90. ],
  91. KEYS: {
  92. start: 'start',
  93. end: 'end',
  94. },
  95. start: function (name, fn) {
  96. process.emit('time', 'start', name)
  97. function end () {
  98. return process.emit('time', 'end', name)
  99. }
  100. if (typeof fn === 'function') {
  101. const res = fn()
  102. if (res && res.finally) {
  103. return res.finally(end)
  104. }
  105. end()
  106. return res
  107. }
  108. return end
  109. },
  110. end: function (name) {
  111. return process.emit('time', 'end', name)
  112. },
  113. },
  114. input: {
  115. LEVELS: [
  116. 'start',
  117. 'end',
  118. 'read',
  119. ],
  120. KEYS: {
  121. start: 'start',
  122. end: 'end',
  123. read: 'read',
  124. },
  125. start: function (fn) {
  126. process.emit('input', 'start')
  127. function end () {
  128. return process.emit('input', 'end')
  129. }
  130. if (typeof fn === 'function') {
  131. const res = fn()
  132. if (res && res.finally) {
  133. return res.finally(end)
  134. }
  135. end()
  136. return res
  137. }
  138. return end
  139. },
  140. end: function () {
  141. return process.emit('input', 'end')
  142. },
  143. read: function (...args) {
  144. let resolve, reject
  145. const promise = new Promise((_resolve, _reject) => {
  146. resolve = _resolve
  147. reject = _reject
  148. })
  149. process.emit('input', 'read', resolve, reject, ...args)
  150. return promise
  151. },
  152. },
  153. }