HumanReadableLogs.js 2.4 KB

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