blob: d959ceb46b106a687f91a4d72f9fcb1f39efa51c [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
Mark Salyzynfa3716b2014-02-14 16:05:05 -080027const log_time LogBufferElement::FLUSH_ERROR((uint32_t)0, (uint32_t)0);
Mark Salyzyn0175b072014-02-26 09:50:16 -080028
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080029LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070030 uid_t uid, pid_t pid, pid_t tid,
31 const char *msg, unsigned short len)
Mark Salyzyn0175b072014-02-26 09:50:16 -080032 : mLogId(log_id)
33 , mUid(uid)
34 , mPid(pid)
Mark Salyzynb992d0d2014-03-20 16:09:38 -070035 , mTid(tid)
Mark Salyzyn0175b072014-02-26 09:50:16 -080036 , mMsgLen(len)
37 , mMonotonicTime(CLOCK_MONOTONIC)
38 , mRealTime(realtime) {
39 mMsg = new char[len];
40 memcpy(mMsg, msg, len);
41}
42
43LogBufferElement::~LogBufferElement() {
44 delete [] mMsg;
45}
46
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080047log_time LogBufferElement::flushTo(SocketClient *reader) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080048 struct logger_entry_v3 entry;
49 memset(&entry, 0, sizeof(struct logger_entry_v3));
50 entry.hdr_size = sizeof(struct logger_entry_v3);
51 entry.len = mMsgLen;
52 entry.lid = mLogId;
53 entry.pid = mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -070054 entry.tid = mTid;
Mark Salyzyn0175b072014-02-26 09:50:16 -080055 entry.sec = mRealTime.tv_sec;
56 entry.nsec = mRealTime.tv_nsec;
57
58 struct iovec iovec[2];
59 iovec[0].iov_base = &entry;
60 iovec[0].iov_len = sizeof(struct logger_entry_v3);
61 iovec[1].iov_base = mMsg;
62 iovec[1].iov_len = mMsgLen;
63 if (reader->sendDatav(iovec, 2)) {
64 return FLUSH_ERROR;
65 }
66
67 return mMonotonicTime;
68}