LogTimes.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2012-2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _LOGD_LOG_TIMES_H__
  17. #define _LOGD_LOG_TIMES_H__
  18. #include <pthread.h>
  19. #include <sys/socket.h>
  20. #include <sys/types.h>
  21. #include <time.h>
  22. #include <list>
  23. #include <memory>
  24. #include <log/log.h>
  25. #include <sysutils/SocketClient.h>
  26. typedef unsigned int log_mask_t;
  27. class LogReader;
  28. class LogBufferElement;
  29. class LogTimeEntry {
  30. static pthread_mutex_t timesLock;
  31. bool mRelease = false;
  32. bool leadingDropped;
  33. pthread_cond_t threadTriggeredCondition;
  34. pthread_t mThread;
  35. LogReader& mReader;
  36. static void* threadStart(void* me);
  37. const log_mask_t mLogMask;
  38. const pid_t mPid;
  39. unsigned int skipAhead[LOG_ID_MAX];
  40. pid_t mLastTid[LOG_ID_MAX];
  41. unsigned long mCount;
  42. unsigned long mTail;
  43. unsigned long mIndex;
  44. public:
  45. LogTimeEntry(LogReader& reader, SocketClient* client, bool nonBlock,
  46. unsigned long tail, log_mask_t logMask, pid_t pid,
  47. log_time start, uint64_t timeout);
  48. SocketClient* mClient;
  49. log_time mStart;
  50. struct timespec mTimeout;
  51. const bool mNonBlock;
  52. const log_time mEnd; // only relevant if mNonBlock
  53. // Protect List manipulations
  54. static void wrlock(void) {
  55. pthread_mutex_lock(&timesLock);
  56. }
  57. static void rdlock(void) {
  58. pthread_mutex_lock(&timesLock);
  59. }
  60. static void unlock(void) {
  61. pthread_mutex_unlock(&timesLock);
  62. }
  63. bool startReader_Locked();
  64. void triggerReader_Locked(void) {
  65. pthread_cond_signal(&threadTriggeredCondition);
  66. }
  67. void triggerSkip_Locked(log_id_t id, unsigned int skip) {
  68. skipAhead[id] = skip;
  69. }
  70. void cleanSkip_Locked(void);
  71. void release_Locked(void) {
  72. // gracefully shut down the socket.
  73. shutdown(mClient->getSocket(), SHUT_RDWR);
  74. mRelease = true;
  75. pthread_cond_signal(&threadTriggeredCondition);
  76. }
  77. bool isWatching(log_id_t id) const {
  78. return mLogMask & (1 << id);
  79. }
  80. bool isWatchingMultiple(log_mask_t logMask) const {
  81. return mLogMask & logMask;
  82. }
  83. // flushTo filter callbacks
  84. static int FilterFirstPass(const LogBufferElement* element, void* me);
  85. static int FilterSecondPass(const LogBufferElement* element, void* me);
  86. };
  87. typedef std::list<std::unique_ptr<LogTimeEntry>> LastLogTimes;
  88. #endif // _LOGD_LOG_TIMES_H__