EventLog.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <log/log.h>
  19. #include <utils/String8.h>
  20. #include "EventLog.h"
  21. // ---------------------------------------------------------------------------
  22. namespace android {
  23. // ---------------------------------------------------------------------------
  24. ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)
  25. EventLog::EventLog() {
  26. }
  27. void EventLog::doLogFrameDurations(const String8& window,
  28. const int32_t* durations, size_t numDurations) {
  29. EventLog::TagBuffer buffer(LOGTAG_SF_FRAME_DUR);
  30. buffer.startList(1 + numDurations);
  31. buffer.writeString8(window);
  32. for (size_t i = 0; i < numDurations; i++) {
  33. buffer.writeInt32(durations[i]);
  34. }
  35. buffer.endList();
  36. buffer.log();
  37. }
  38. void EventLog::logFrameDurations(const String8& window,
  39. const int32_t* durations, size_t numDurations) {
  40. EventLog::getInstance().doLogFrameDurations(window, durations,
  41. numDurations);
  42. }
  43. // ---------------------------------------------------------------------------
  44. EventLog::TagBuffer::TagBuffer(int32_t tag)
  45. : mPos(0), mTag(tag), mOverflow(false) {
  46. }
  47. void EventLog::TagBuffer::log() {
  48. if (mOverflow) {
  49. ALOGW("couldn't log to binary event log: overflow.");
  50. } else if (android_bWriteLog(mTag, mStorage, mPos) < 0) {
  51. ALOGE("couldn't log to EventLog: %s", strerror(errno));
  52. }
  53. // purge the buffer
  54. mPos = 0;
  55. mOverflow = false;
  56. }
  57. void EventLog::TagBuffer::startList(int8_t count) {
  58. if (mOverflow) return;
  59. const size_t needed = 1 + sizeof(count);
  60. if (mPos + needed > STORAGE_MAX_SIZE) {
  61. mOverflow = true;
  62. return;
  63. }
  64. mStorage[mPos + 0] = EVENT_TYPE_LIST;
  65. mStorage[mPos + 1] = count;
  66. mPos += needed;
  67. }
  68. void EventLog::TagBuffer::endList() {
  69. if (mOverflow) return;
  70. const size_t needed = 1;
  71. if (mPos + needed > STORAGE_MAX_SIZE) {
  72. mOverflow = true;
  73. return;
  74. }
  75. mStorage[mPos + 0] = '\n';
  76. mPos += needed;
  77. }
  78. void EventLog::TagBuffer::writeInt32(int32_t value) {
  79. if (mOverflow) return;
  80. const size_t needed = 1 + sizeof(value);
  81. if (mPos + needed > STORAGE_MAX_SIZE) {
  82. mOverflow = true;
  83. return;
  84. }
  85. mStorage[mPos + 0] = EVENT_TYPE_INT;
  86. memcpy(&mStorage[mPos + 1], &value, sizeof(value));
  87. mPos += needed;
  88. }
  89. void EventLog::TagBuffer::writeInt64(int64_t value) {
  90. if (mOverflow) return;
  91. const size_t needed = 1 + sizeof(value);
  92. if (mPos + needed > STORAGE_MAX_SIZE) {
  93. mOverflow = true;
  94. return;
  95. }
  96. mStorage[mPos + 0] = EVENT_TYPE_LONG;
  97. memcpy(&mStorage[mPos + 1], &value, sizeof(value));
  98. mPos += needed;
  99. }
  100. void EventLog::TagBuffer::writeString8(const String8& value) {
  101. if (mOverflow) return;
  102. const int32_t stringLen = value.length();
  103. const size_t needed = 1 + sizeof(int32_t) + stringLen;
  104. if (mPos + needed > STORAGE_MAX_SIZE) {
  105. mOverflow = true;
  106. return;
  107. }
  108. mStorage[mPos + 0] = EVENT_TYPE_STRING;
  109. memcpy(&mStorage[mPos + 1], &stringLen, sizeof(int32_t));
  110. memcpy(&mStorage[mPos + 5], value.string(), stringLen);
  111. mPos += needed;
  112. }
  113. // ---------------------------------------------------------------------------
  114. }// namespace android
  115. // ---------------------------------------------------------------------------