blob: 1c55623c39c534f35d60620dd89dce8c931a2e49 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 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 <string.h>
19#include <time.h>
20#include <unistd.h>
21
22#include <log/logger.h>
23
24#include "LogBufferElement.h"
25#include "LogReader.h"
26
27const struct timespec LogBufferElement::FLUSH_ERROR = { 0, 0 };
28
29LogBufferElement::LogBufferElement(log_id_t log_id, struct timespec realtime, uid_t uid, pid_t pid, const char *msg, unsigned short len)
30 : mLogId(log_id)
31 , mUid(uid)
32 , mPid(pid)
33 , mMsgLen(len)
34 , mMonotonicTime(CLOCK_MONOTONIC)
35 , mRealTime(realtime) {
36 mMsg = new char[len];
37 memcpy(mMsg, msg, len);
38}
39
40LogBufferElement::~LogBufferElement() {
41 delete [] mMsg;
42}
43
44struct timespec LogBufferElement::flushTo(SocketClient *reader) {
45 struct logger_entry_v3 entry;
46 memset(&entry, 0, sizeof(struct logger_entry_v3));
47 entry.hdr_size = sizeof(struct logger_entry_v3);
48 entry.len = mMsgLen;
49 entry.lid = mLogId;
50 entry.pid = mPid;
51 entry.sec = mRealTime.tv_sec;
52 entry.nsec = mRealTime.tv_nsec;
53
54 struct iovec iovec[2];
55 iovec[0].iov_base = &entry;
56 iovec[0].iov_len = sizeof(struct logger_entry_v3);
57 iovec[1].iov_base = mMsg;
58 iovec[1].iov_len = mMsgLen;
59 if (reader->sendDatav(iovec, 2)) {
60 return FLUSH_ERROR;
61 }
62
63 return mMonotonicTime;
64}