blob: 0bc42d548b81977bbf7d7e3793557687c14419d0 [file] [log] [blame]
William Luh964428c2015-08-13 10:41:58 -07001/*
2 * Copyright (C) 2015 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 <errno.h>
18#include <string.h>
19
20#include <log/log.h>
21#include <log/logger.h>
22
23#define MAX_EVENT_PAYLOAD 512
24#define MAX_SUBTAG_LEN 32
25
26static inline void copy4LE(uint8_t *buf, size_t pos, int val)
27{
28 buf[pos] = val & 0xFF;
29 buf[pos+1] = (val >> 8) & 0xFF;
30 buf[pos+2] = (val >> 16) & 0xFF;
31 buf[pos+3] = (val >> 24) & 0xFF;
32}
33
34int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
35 uint32_t dataLen)
36{
37 uint8_t buf[MAX_EVENT_PAYLOAD];
38 size_t pos = 0;
39 uint32_t subTagLen = 0;
40 uint32_t roomLeftForData = 0;
41
42 if ((subTag == NULL) || ((data == NULL) && (dataLen != 0))) return -EINVAL;
43
44 subTagLen = strlen(subTag);
45
46 // Truncate subtags that are too long.
47 subTagLen = subTagLen > MAX_SUBTAG_LEN ? MAX_SUBTAG_LEN : subTagLen;
48
49 // Truncate dataLen if it is too long.
50 roomLeftForData = MAX_EVENT_PAYLOAD -
51 (1 + // EVENT_TYPE_LIST
52 1 + // Number of elements in list
53 1 + // EVENT_TYPE_STRING
54 sizeof(subTagLen) +
55 subTagLen +
56 1 + // EVENT_TYPE_INT
57 sizeof(uid) +
58 1 + // EVENT_TYPE_STRING
59 sizeof(dataLen));
60 dataLen = dataLen > roomLeftForData ? roomLeftForData : dataLen;
61
62 buf[pos++] = EVENT_TYPE_LIST;
63 buf[pos++] = 3; // Number of elements in the list (subTag, uid, data)
64
65 // Write sub tag.
66 buf[pos++] = EVENT_TYPE_STRING;
67 copy4LE(buf, pos, subTagLen);
68 pos += 4;
69 memcpy(&buf[pos], subTag, subTagLen);
70 pos += subTagLen;
71
72 // Write UID.
73 buf[pos++] = EVENT_TYPE_INT;
74 copy4LE(buf, pos, uid);
75 pos += 4;
76
77 // Write data.
78 buf[pos++] = EVENT_TYPE_STRING;
79 copy4LE(buf, pos, dataLen);
80 pos += 4;
81 if (dataLen != 0)
82 {
83 memcpy(&buf[pos], data, dataLen);
84 pos += dataLen;
85 }
86
87 return __android_log_bwrite(tag, buf, pos);
88}