Home | History | Annotate | Download | only in logd
      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 #ifndef _LOGD_LOG_BUFFER_ELEMENT_H__
     18 #define _LOGD_LOG_BUFFER_ELEMENT_H__
     19 
     20 #include <stdatomic.h>
     21 #include <stdlib.h>
     22 #include <sys/types.h>
     23 
     24 #include <sysutils/SocketClient.h>
     25 #include <log/log.h>
     26 #include <log/log_read.h>
     27 
     28 class LogBuffer;
     29 
     30 #define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve
     31                                  // non-chatty UIDs less than this age in hours
     32 #define EXPIRE_THRESHOLD 10      // A smaller expire count is considered too
     33                                  // chatty for the temporal expire messages
     34 #define EXPIRE_RATELIMIT 10      // maximum rate in seconds to report expiration
     35 
     36 class LogBufferElement {
     37 
     38     friend LogBuffer;
     39 
     40     const log_id_t mLogId;
     41     const uid_t mUid;
     42     const pid_t mPid;
     43     const pid_t mTid;
     44     char *mMsg;
     45     union {
     46         const unsigned short mMsgLen; // mMSg != NULL
     47         unsigned short mDropped;      // mMsg == NULL
     48     };
     49     const uint64_t mSequence;
     50     log_time mRealTime;
     51     static atomic_int_fast64_t sequence;
     52 
     53     // assumption: mMsg == NULL
     54     size_t populateDroppedMessage(char *&buffer,
     55                                   LogBuffer *parent);
     56 
     57 public:
     58     LogBufferElement(log_id_t log_id, log_time realtime,
     59                      uid_t uid, pid_t pid, pid_t tid,
     60                      const char *msg, unsigned short len);
     61     virtual ~LogBufferElement();
     62 
     63     log_id_t getLogId() const { return mLogId; }
     64     uid_t getUid(void) const { return mUid; }
     65     pid_t getPid(void) const { return mPid; }
     66     pid_t getTid(void) const { return mTid; }
     67     unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
     68     unsigned short setDropped(unsigned short value) {
     69         if (mMsg) {
     70             delete [] mMsg;
     71             mMsg = NULL;
     72         }
     73         return mDropped = value;
     74     }
     75     unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
     76     uint64_t getSequence(void) const { return mSequence; }
     77     static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
     78     log_time getRealTime(void) const { return mRealTime; }
     79 
     80     uint32_t getTag(void) const;
     81 
     82     static const uint64_t FLUSH_ERROR;
     83     uint64_t flushTo(SocketClient *writer, LogBuffer *parent, bool privileged);
     84 };
     85 
     86 #endif
     87