Log.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package common;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. *
  11. * @author Eugenio
  12. */
  13. public class Log implements ErrorReport {
  14. // public static final String ANSI_RESET = "\u001B[0m";
  15. public static final String ANSI_BLACK = "\u001B[30m";
  16. public static final String ANSI_RED = "\u001B[31m";
  17. // public static final String ANSI_GREEN = "\u001B[32m";
  18. public static final String ANSI_YELLOW = "\u001B[33m";
  19. public static final String ANSI_BLUE = "\u001B[34m";
  20. // public static final String ANSI_PURPLE = "\u001B[35m";
  21. // public static final String ANSI_CYAN = "\u001B[36m";
  22. // public static final String ANSI_WHITE = "\u001B[37m";
  23. protected List<Instruction> items = new ArrayList<>();
  24. protected static String format = "%s{%s}[%s:%s|%s\t] : %s %s";
  25. protected int ThrowOnNErrors = 10;
  26. protected int errorCount = 0;
  27. public Log() {
  28. }
  29. // public void Add(int line, int colum, String classe, String msg) {
  30. // items.add(String.format(format, line, colum, classe, msg));
  31. // }
  32. public static void PrintError(String tag, Instruction error) {
  33. Print(tag, error.S("color", ANSI_RED));
  34. }
  35. public static void PrintInfo(String tag, Instruction info) {
  36. Print(tag, info.S("color", ANSI_BLUE));
  37. }
  38. public static void PrintWarning(String tag, Instruction war) {
  39. Print(tag, war.S("color", ANSI_YELLOW));
  40. }
  41. public void AddError(Instruction error) {
  42. Add(error.S("color", ANSI_RED), true);
  43. }
  44. public void AddInfo(Instruction info) {
  45. Add(info.S("color", ANSI_BLUE), false);
  46. }
  47. public void AddWarning(Instruction war) {
  48. Add(war.S("color", ANSI_YELLOW), false);
  49. }
  50. protected static void Print(String tag, Instruction log) {
  51. String f = log.get("format", Log.format);
  52. System.out.println(String.format(f,
  53. log.G("color"),
  54. tag,
  55. log.G("line"),
  56. log.G("col"),
  57. log.G("class"),
  58. log.G("msg"),
  59. ANSI_BLACK
  60. ));
  61. }
  62. protected void Add(Instruction error, boolean inc) {
  63. items.add(error);
  64. if (inc) {
  65. errorCount++;
  66. }
  67. if (errorCount == ThrowOnNErrors) {
  68. PrintErros();
  69. // throw new Exception("Many erros");
  70. }
  71. }
  72. @Override
  73. public void PrintErros() {
  74. for (Instruction error : items) {
  75. Print("", error);
  76. }
  77. }
  78. @Override
  79. public boolean HasError() {
  80. return errorCount > 0;
  81. }
  82. }