/engine/src/main/java/org/advin/engine/sys/SysTools.java

https://github.com/avega/Advin · Java · 117 lines · 88 code · 19 blank · 10 comment · 16 complexity · d791ea838ff6bd2bfcaddcb2f3b39bd4 MD5 · raw file

  1. package org.advin.engine.sys;
  2. import org.apache.commons.cli.CommandLine;
  3. import org.apache.commons.cli.CommandLineParser;
  4. import org.apache.commons.cli.GnuParser;
  5. import org.apache.commons.cli.Options;
  6. import org.apache.commons.cli.ParseException;
  7. import sun.reflect.Reflection;
  8. import java.io.IOException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.logging.*;
  11. public class SysTools
  12. {
  13. public static Logger logger;
  14. public static final String LOG_NAME = updateFileSeporator(SysInfo.getUserDir() + SysInfo.getFileSeparator() + "advin.log");
  15. public static final String LOG_FOR = "org.advin.engine";
  16. public static final String LOG_DATETIME_FORMAT = "[dd.MM.yyyy HH:mm:ss]";
  17. public static final String LOG_MSG_FORMAT = "%1$20s [%2$s]: %3$s";
  18. public static final String LOG_EMPTY_PLACER = "NONE";
  19. static
  20. {
  21. try
  22. {
  23. boolean append = true;
  24. FileHandler fh = new FileHandler(LOG_NAME, append);
  25. fh.setFormatter(new Formatter()
  26. {
  27. public String format(LogRecord rec)
  28. {
  29. StringBuilder buf = new StringBuilder(1000);
  30. buf.append((new SimpleDateFormat(LOG_DATETIME_FORMAT)).format(new java.util.Date()));
  31. buf.append(' ');
  32. buf.append(rec.getMessage());
  33. buf.append('\n');
  34. return buf.toString();
  35. };
  36. });
  37. logger = Logger.getLogger(LOG_FOR);
  38. logger.addHandler(fh);
  39. logger.setUseParentHandlers(false);
  40. logger.setLevel(Level.FINEST);
  41. }
  42. catch (IOException e) { e.printStackTrace(); };
  43. };
  44. /**
  45. * Output formated log messages to console and lg file
  46. * @param info message info
  47. * @param message the message for output
  48. */
  49. public void logMsg(String info, String message)
  50. {
  51. String caller = LOG_EMPTY_PLACER;
  52. if (Reflection.getCallerClass(3) != null)
  53. { caller = Reflection.getCallerClass(3).getSimpleName(); }
  54. else if (Reflection.getCallerClass(2) != null)
  55. { caller = Reflection.getCallerClass(2).getSimpleName(); }
  56. else if (Reflection.getCallerClass(1) != null)
  57. { caller = Reflection.getCallerClass(1).getSimpleName(); };
  58. if (info == null) { info = LOG_EMPTY_PLACER; };
  59. if (message == null) { message = LOG_EMPTY_PLACER; };
  60. String msg = String.format(LOG_MSG_FORMAT, caller, info, message);
  61. System.out.println(msg);
  62. logger.info(msg);
  63. };
  64. /**
  65. * Change all file path separators to system specific char
  66. * @param aPath file path that may contain wrong separator char
  67. * @return changed file path
  68. */
  69. public static String updateFileSeporator(String aPath)
  70. {
  71. String fromExp = "/";
  72. String toExp = "\\\\";
  73. if (SysInfo.getFileSeparator().equals("/")) { fromExp = "\\\\"; toExp = "/"; };
  74. return aPath.replaceAll(fromExp, toExp);
  75. };
  76. public String getArgValue(String[] args, String param)
  77. {
  78. String result = "";
  79. Options options = new Options();
  80. options.addOption(param, true, "");
  81. CommandLineParser parser = new GnuParser();
  82. CommandLine cl = null;
  83. try
  84. {
  85. cl = parser.parse(options, args);
  86. }
  87. catch (ParseException e) { logMsg("E", " Arguments parse error: "+ e.getMessage()); };
  88. if ((cl != null) && (cl.hasOption(param)))
  89. {
  90. result = cl.getOptionValue(param);
  91. };
  92. return result;
  93. };
  94. public String getCP()
  95. {
  96. return System.getProperties().getProperty("java.class.path", null);
  97. };
  98. };