HumanReadableLogs.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import LatexLogParser from '../log-parser/latex-log-parser'
  2. import ruleset from './HumanReadableLogsRules'
  3. export default {
  4. parse(rawLog, options) {
  5. let parsedLogEntries
  6. if (typeof rawLog === 'string') {
  7. const latexLogParser = new LatexLogParser(rawLog, options)
  8. parsedLogEntries = latexLogParser.parse()
  9. } else {
  10. parsedLogEntries = rawLog
  11. }
  12. const _getRule = function (logMessage) {
  13. for (const rule of ruleset) {
  14. if (rule.regexToMatch.test(logMessage)) {
  15. return rule
  16. }
  17. }
  18. }
  19. const seenErrorTypes = {} // keep track of types of errors seen
  20. for (const entry of parsedLogEntries.all) {
  21. const ruleDetails = _getRule(entry.message)
  22. if (ruleDetails != null) {
  23. let type
  24. if (ruleDetails.ruleId != null) {
  25. entry.ruleId = ruleDetails.ruleId
  26. } else if (ruleDetails.regexToMatch != null) {
  27. entry.ruleId = `hint_${ruleDetails.regexToMatch
  28. .toString()
  29. .replace(/\s/g, '_')
  30. .slice(1, -1)}`
  31. }
  32. if (ruleDetails.newMessage != null) {
  33. entry.message = entry.message.replace(
  34. ruleDetails.regexToMatch,
  35. ruleDetails.newMessage
  36. )
  37. }
  38. // suppress any entries that are known to cascade from previous error types
  39. if (ruleDetails.cascadesFrom != null) {
  40. for (type of ruleDetails.cascadesFrom) {
  41. if (seenErrorTypes[type]) {
  42. entry.suppressed = true
  43. }
  44. }
  45. }
  46. // record the types of errors seen
  47. if (ruleDetails.types != null) {
  48. for (type of ruleDetails.types) {
  49. seenErrorTypes[type] = true
  50. }
  51. }
  52. if (ruleDetails.humanReadableHint != null) {
  53. entry.humanReadableHint = ruleDetails.humanReadableHint
  54. }
  55. if (ruleDetails.humanReadableHintComponent != null) {
  56. entry.humanReadableHintComponent =
  57. ruleDetails.humanReadableHintComponent
  58. }
  59. if (ruleDetails.extraInfoURL != null) {
  60. entry.extraInfoURL = ruleDetails.extraInfoURL
  61. }
  62. }
  63. }
  64. // filter out the suppressed errors (from the array entries in parsedLogEntries)
  65. for (const key in parsedLogEntries) {
  66. const errors = parsedLogEntries[key]
  67. if (typeof errors === 'object' && errors.length > 0) {
  68. parsedLogEntries[key] = Array.from(errors).filter(
  69. err => !err.suppressed
  70. )
  71. }
  72. }
  73. return parsedLogEntries
  74. },
  75. }