blob: 29bfbdaed5e30fdfce5cea3fad65403642a80af7 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2013 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
Mark Salyzynfa3716b2014-02-14 16:05:05 -080017#include <ctype.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080018#include <poll.h>
19#include <sys/socket.h>
20#include <cutils/sockets.h>
21
22#include "LogReader.h"
23#include "FlushCommand.h"
24
25LogReader::LogReader(LogBuffer *logbuf)
26 : SocketListener("logdr", true)
27 , mLogbuf(*logbuf)
28{ }
29
30// When we are notified a new log entry is available, inform
31// all of our listening sockets.
32void LogReader::notifyNewLog() {
33 FlushCommand command(*this);
34 runOnEachSocket(&command);
35}
36
37bool LogReader::onDataAvailable(SocketClient *cli) {
38 char buffer[255];
39
40 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
41 if (len <= 0) {
42 doSocketDelete(cli);
43 return false;
44 }
45 buffer[len] = '\0';
46
47 unsigned long tail = 0;
48 static const char _tail[] = " tail=";
49 char *cp = strstr(buffer, _tail);
50 if (cp) {
51 tail = atol(cp + sizeof(_tail) - 1);
52 }
53
Mark Salyzynfa3716b2014-02-14 16:05:05 -080054 log_time start(log_time::EPOCH);
55 static const char _start[] = " start=";
56 cp = strstr(buffer, _start);
57 if (cp) {
58 // Parse errors will result in current time
59 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
60 }
61
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 unsigned int logMask = -1;
63 static const char _logIds[] = " lids=";
64 cp = strstr(buffer, _logIds);
65 if (cp) {
66 logMask = 0;
67 cp += sizeof(_logIds) - 1;
68 while (*cp && *cp != '\0') {
69 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080070 while (isdigit(*cp)) {
71 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080072 ++cp;
73 }
74 logMask |= 1 << val;
75 if (*cp != ',') {
76 break;
77 }
78 ++cp;
79 }
80 }
81
82 pid_t pid = 0;
83 static const char _pid[] = " pid=";
84 cp = strstr(buffer, _pid);
85 if (cp) {
86 pid = atol(cp + sizeof(_pid) - 1);
87 }
88
89 bool nonBlock = false;
90 if (strncmp(buffer, "dumpAndClose", 12) == 0) {
91 nonBlock = true;
92 }
93
Mark Salyzynfa3716b2014-02-14 16:05:05 -080094 // Convert realtime to monotonic time
95 if (start != log_time::EPOCH) {
96 log_time real(CLOCK_REALTIME);
97 log_time monotonic(CLOCK_MONOTONIC);
98 real -= monotonic; // I know this is not 100% accurate
99 start -= real;
100 }
101 if (start == log_time::EPOCH) {
102 start = LogTimeEntry::EPOCH;
103 }
104
105 FlushCommand command(*this, nonBlock, tail, logMask, pid, start);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800106 command.runSocketCommand(cli);
107 return true;
108}
109
110void LogReader::doSocketDelete(SocketClient *cli) {
111 LastLogTimes &times = mLogbuf.mTimes;
112 LogTimeEntry::lock();
113 LastLogTimes::iterator it = times.begin();
114 while(it != times.end()) {
115 LogTimeEntry *entry = (*it);
116 if (entry->mClient == cli) {
117 times.erase(it);
118 entry->release_Locked();
119 break;
120 }
121 it++;
122 }
123 LogTimeEntry::unlock();
124}