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