LogBuffer.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (c) 2019 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #ifndef LOG_BUFFER_H
  30. #define LOG_BUFFER_H
  31. #include "SkipList.h"
  32. #include "log_util.h"
  33. #include <loc_cfg.h>
  34. #include <loc_pla.h>
  35. #include <string>
  36. #include <sstream>
  37. #include <ostream>
  38. #include <fstream>
  39. #include <time.h>
  40. #include <mutex>
  41. #include <signal.h>
  42. #include <thread>
  43. #include <functional>
  44. //default error level time depth threshold,
  45. #define TIME_DEPTH_THRESHOLD_MINIMAL_IN_SEC 60
  46. //default maximum log buffer size
  47. #define MAXIMUM_NUM_IN_LIST 50
  48. //file path of dumped log buffer
  49. #define LOG_BUFFER_FILE_PATH "/data/vendor/location/"
  50. namespace loc_util {
  51. class ConfigsInLevel{
  52. public:
  53. uint32_t mTimeDepthThres;
  54. uint32_t mMaxNumThres;
  55. int mCurrentSize;
  56. ConfigsInLevel(uint32_t time, int num, int size):
  57. mTimeDepthThres(time), mMaxNumThres(num), mCurrentSize(size) {}
  58. };
  59. class LogBuffer {
  60. private:
  61. static LogBuffer* mInstance;
  62. static struct sigaction mOriSigAction[NSIG];
  63. static struct sigaction mNewSigAction;
  64. static mutex sLock;
  65. SkipList<pair<uint64_t, string>> mLogList;
  66. vector<ConfigsInLevel> mConfigVec;
  67. mutex mLock;
  68. const vector<string> mLevelMap {"E", "W", "I", "D", "V"};
  69. public:
  70. static LogBuffer* getInstance();
  71. void append(string& data, int level, uint64_t timestamp);
  72. void dump(std::function<void(stringstream&)> log, int level = -1);
  73. void dumpToAdbLogcat();
  74. void dumpToLogFile(string filePath);
  75. void flush();
  76. private:
  77. LogBuffer();
  78. void registerSignalHandler();
  79. static void signalHandler(const int code, siginfo_t *const si, void *const sc);
  80. };
  81. }
  82. #endif