| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 27 | const uint64_t LogBufferElement::FLUSH_ERROR(0); | 
|  | 28 | atomic_int_fast64_t LogBufferElement::sequence; | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 |  | 
| Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 30 | LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, | 
| Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 31 | uid_t uid, pid_t pid, pid_t tid, | 
|  | 32 | const char *msg, unsigned short len) | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | : mLogId(log_id) | 
|  | 34 | , mUid(uid) | 
|  | 35 | , mPid(pid) | 
| Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 36 | , mTid(tid) | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 37 | , mMsgLen(len) | 
| Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 38 | , mSequence(sequence.fetch_add(1, memory_order_relaxed)) | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 39 | , mRealTime(realtime) { | 
|  | 40 | mMsg = new char[len]; | 
|  | 41 | memcpy(mMsg, msg, len); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | LogBufferElement::~LogBufferElement() { | 
|  | 45 | delete [] mMsg; | 
|  | 46 | } | 
|  | 47 |  | 
| Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 48 | uint64_t LogBufferElement::flushTo(SocketClient *reader) { | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 49 | struct logger_entry_v3 entry; | 
|  | 50 | memset(&entry, 0, sizeof(struct logger_entry_v3)); | 
|  | 51 | entry.hdr_size = sizeof(struct logger_entry_v3); | 
|  | 52 | entry.len = mMsgLen; | 
|  | 53 | entry.lid = mLogId; | 
|  | 54 | entry.pid = mPid; | 
| Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 55 | entry.tid = mTid; | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 56 | entry.sec = mRealTime.tv_sec; | 
|  | 57 | entry.nsec = mRealTime.tv_nsec; | 
|  | 58 |  | 
|  | 59 | struct iovec iovec[2]; | 
|  | 60 | iovec[0].iov_base = &entry; | 
|  | 61 | iovec[0].iov_len = sizeof(struct logger_entry_v3); | 
|  | 62 | iovec[1].iov_base = mMsg; | 
|  | 63 | iovec[1].iov_len = mMsgLen; | 
|  | 64 | if (reader->sendDatav(iovec, 2)) { | 
|  | 65 | return FLUSH_ERROR; | 
|  | 66 | } | 
|  | 67 |  | 
| Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 68 | return mSequence; | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 69 | } |