log_utils.dart 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'package:common_utils/common_utils.dart';
  2. import 'package:liftmanager/common/common.dart';
  3. class Log{
  4. static init() {
  5. // LogUtil.debuggable = !Constant.inProduction;
  6. }
  7. static d(String msg, {tag: 'X-LOG'}) {
  8. if (!Constant.inProduction){
  9. LogUtil.v(msg, tag: tag);
  10. }
  11. }
  12. static e(String msg, {tag: 'X-LOG'}) {
  13. if (!Constant.inProduction){
  14. LogUtil.e(msg, tag: tag);
  15. }
  16. }
  17. static json(String msg, {tag: 'X-LOG'}) {
  18. if (!Constant.inProduction){
  19. LogUtil.v(msg, tag: tag);
  20. }
  21. }
  22. /// https://github.com/rhymelph/r_logger
  23. /// json format
  24. ///
  25. /// [s] your json
  26. static String jsonFormat(String s) {
  27. int level = 0;
  28. StringBuffer jsonForMatStr = StringBuffer();
  29. for (int index = 0; index < s.length; index++) {
  30. int c = s.codeUnitAt(index);
  31. if (level > 0 &&
  32. '\n'.codeUnitAt(0) ==
  33. jsonForMatStr.toString().codeUnitAt(jsonForMatStr.length - 1)) {
  34. jsonForMatStr.write(_getLevelStr(level));
  35. }
  36. if ('{'.codeUnitAt(0) == c || '['.codeUnitAt(0) == c) {
  37. jsonForMatStr.write(String.fromCharCode(c) + "\n");
  38. level++;
  39. } else if (','.codeUnitAt(0) == c) {
  40. jsonForMatStr.write(String.fromCharCode(c) + "\n");
  41. } else if ('}'.codeUnitAt(0) == c || ']'.codeUnitAt(0) == c) {
  42. jsonForMatStr.write("\n");
  43. level--;
  44. jsonForMatStr.write(_getLevelStr(level));
  45. jsonForMatStr.writeCharCode(c);
  46. } else {
  47. jsonForMatStr.writeCharCode(c);
  48. }
  49. }
  50. return jsonForMatStr.toString();
  51. }
  52. /// json level ping
  53. ///
  54. /// [level] your level
  55. static String _getLevelStr(int level) {
  56. StringBuffer levelStr = new StringBuffer();
  57. for (int levelI = 0; levelI < level; levelI++) {
  58. List<int> codeUnits = "\t".codeUnits;
  59. codeUnits.forEach((i) {
  60. levelStr.writeCharCode(i);
  61. });
  62. }
  63. return levelStr.toString();
  64. }
  65. }