Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1a12ae3a authored by Tom Cherry's avatar Tom Cherry
Browse files

logd: decouple LogTags from LogBuffer

LogBuffer needs a pointer to LogTags, but it should not own the
instance.  It should not provide accessors into LogTags either.

Also, clean up CommandListener a bit.

Test: logging unit tests
Change-Id: Ic0c86a2bac0c4dd80262278588b9fdc2326dbe5b
parent 2a2e9e0a
Loading
Loading
Loading
Loading
+21 −73
Original line number Diff line number Diff line
@@ -38,37 +38,20 @@
#include "LogCommand.h"
#include "LogUtils.h"

CommandListener::CommandListener(LogBuffer* buf, LogReader* /*reader*/,
                                 LogListener* /*swl*/)
    : FrameworkListener(getLogSocket()) {
    // registerCmd(new ShutdownCmd(buf, writer, swl));
    registerCmd(new ClearCmd(buf));
    registerCmd(new GetBufSizeCmd(buf));
    registerCmd(new SetBufSizeCmd(buf));
    registerCmd(new GetBufSizeUsedCmd(buf));
    registerCmd(new GetStatisticsCmd(buf));
    registerCmd(new SetPruneListCmd(buf));
    registerCmd(new GetPruneListCmd(buf));
    registerCmd(new GetEventTagCmd(buf));
    registerCmd(new ReinitCmd());
CommandListener::CommandListener(LogBuffer* buf, LogTags* tags)
    : FrameworkListener(getLogSocket()), buf_(buf), tags_(tags) {
    registerCmd(new ClearCmd(this));
    registerCmd(new GetBufSizeCmd(this));
    registerCmd(new SetBufSizeCmd(this));
    registerCmd(new GetBufSizeUsedCmd(this));
    registerCmd(new GetStatisticsCmd(this));
    registerCmd(new SetPruneListCmd(this));
    registerCmd(new GetPruneListCmd(this));
    registerCmd(new GetEventTagCmd(this));
    registerCmd(new ReinitCmd(this));
    registerCmd(new ExitCmd(this));
}

CommandListener::ShutdownCmd::ShutdownCmd(LogReader* reader, LogListener* swl)
    : LogCommand("shutdown"), mReader(*reader), mSwl(*swl) {
}

int CommandListener::ShutdownCmd::runCommand(SocketClient* /*cli*/,
                                             int /*argc*/, char** /*argv*/) {
    mSwl.stopListener();
    mReader.stopListener();
    exit(0);
}

CommandListener::ClearCmd::ClearCmd(LogBuffer* buf)
    : LogCommand("clear"), mBuf(*buf) {
}

static void setname() {
    static bool name_set;
    if (!name_set) {
@@ -96,14 +79,10 @@ int CommandListener::ClearCmd::runCommand(SocketClient* cli, int argc,
        return 0;
    }

    cli->sendMsg(mBuf.clear((log_id_t)id, uid) ? "busy" : "success");
    cli->sendMsg(buf()->clear((log_id_t)id, uid) ? "busy" : "success");
    return 0;
}

CommandListener::GetBufSizeCmd::GetBufSizeCmd(LogBuffer* buf)
    : LogCommand("getLogSize"), mBuf(*buf) {
}

int CommandListener::GetBufSizeCmd::runCommand(SocketClient* cli, int argc,
                                               char** argv) {
    setname();
@@ -118,17 +97,13 @@ int CommandListener::GetBufSizeCmd::runCommand(SocketClient* cli, int argc,
        return 0;
    }

    unsigned long size = mBuf.getSize((log_id_t)id);
    unsigned long size = buf()->getSize((log_id_t)id);
    char buf[512];
    snprintf(buf, sizeof(buf), "%lu", size);
    cli->sendMsg(buf);
    return 0;
}

CommandListener::SetBufSizeCmd::SetBufSizeCmd(LogBuffer* buf)
    : LogCommand("setLogSize"), mBuf(*buf) {
}

int CommandListener::SetBufSizeCmd::runCommand(SocketClient* cli, int argc,
                                               char** argv) {
    setname();
@@ -149,7 +124,7 @@ int CommandListener::SetBufSizeCmd::runCommand(SocketClient* cli, int argc,
    }

    unsigned long size = atol(argv[2]);
    if (mBuf.setSize((log_id_t)id, size)) {
    if (buf()->setSize((log_id_t)id, size)) {
        cli->sendMsg("Range Error");
        return 0;
    }
@@ -158,10 +133,6 @@ int CommandListener::SetBufSizeCmd::runCommand(SocketClient* cli, int argc,
    return 0;
}

CommandListener::GetBufSizeUsedCmd::GetBufSizeUsedCmd(LogBuffer* buf)
    : LogCommand("getLogSizeUsed"), mBuf(*buf) {
}

int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient* cli, int argc,
                                                   char** argv) {
    setname();
@@ -176,17 +147,13 @@ int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient* cli, int argc,
        return 0;
    }

    unsigned long size = mBuf.getSizeUsed((log_id_t)id);
    unsigned long size = buf()->getSizeUsed((log_id_t)id);
    char buf[512];
    snprintf(buf, sizeof(buf), "%lu", size);
    cli->sendMsg(buf);
    return 0;
}

CommandListener::GetStatisticsCmd::GetStatisticsCmd(LogBuffer* buf)
    : LogCommand("getStatistics"), mBuf(*buf) {
}

// This returns a string with a length prefix with the format <length>\n<data>\n\f.  The length
// prefix includes the length of the prefix itself.
static std::string PackageString(const std::string& str) {
@@ -241,25 +208,17 @@ int CommandListener::GetStatisticsCmd::runCommand(SocketClient* cli, int argc,
        }
    }

    cli->sendMsg(PackageString(mBuf.formatStatistics(uid, pid, logMask)).c_str());
    cli->sendMsg(PackageString(buf()->formatStatistics(uid, pid, logMask)).c_str());
    return 0;
}

CommandListener::GetPruneListCmd::GetPruneListCmd(LogBuffer* buf)
    : LogCommand("getPruneList"), mBuf(*buf) {
}

int CommandListener::GetPruneListCmd::runCommand(SocketClient* cli,
                                                 int /*argc*/, char** /*argv*/) {
    setname();
    cli->sendMsg(PackageString(mBuf.formatPrune()).c_str());
    cli->sendMsg(PackageString(buf()->formatPrune()).c_str());
    return 0;
}

CommandListener::SetPruneListCmd::SetPruneListCmd(LogBuffer* buf)
    : LogCommand("setPruneList"), mBuf(*buf) {
}

int CommandListener::SetPruneListCmd::runCommand(SocketClient* cli, int argc,
                                                 char** argv) {
    setname();
@@ -276,7 +235,7 @@ int CommandListener::SetPruneListCmd::runCommand(SocketClient* cli, int argc,
        str += argv[i];
    }

    int ret = mBuf.initPrune(str.c_str());
    int ret = buf()->initPrune(str.c_str());

    if (ret) {
        cli->sendMsg("Invalid");
@@ -288,10 +247,6 @@ int CommandListener::SetPruneListCmd::runCommand(SocketClient* cli, int argc,
    return 0;
}

CommandListener::GetEventTagCmd::GetEventTagCmd(LogBuffer* buf)
    : LogCommand("getEventTag"), mBuf(*buf) {
}

int CommandListener::GetEventTagCmd::runCommand(SocketClient* cli, int argc,
                                                char** argv) {
    setname();
@@ -328,18 +283,15 @@ int CommandListener::GetEventTagCmd::runCommand(SocketClient* cli, int argc,
            cli->sendMsg("can not mix id= with either format= or name=");
            return 0;
        }
        cli->sendMsg(PackageString(mBuf.formatEntry(atoi(id), uid)).c_str());
        cli->sendMsg(PackageString(tags()->formatEntry(atoi(id), uid)).c_str());
        return 0;
    }

    cli->sendMsg(PackageString(mBuf.formatGetEventTag(uid, name, format)).c_str());
    cli->sendMsg(PackageString(tags()->formatGetEventTag(uid, name, format)).c_str());

    return 0;
}

CommandListener::ReinitCmd::ReinitCmd() : LogCommand("reinit") {
}

int CommandListener::ReinitCmd::runCommand(SocketClient* cli, int /*argc*/,
                                           char** /*argv*/) {
    setname();
@@ -351,16 +303,12 @@ int CommandListener::ReinitCmd::runCommand(SocketClient* cli, int /*argc*/,
    return 0;
}

CommandListener::ExitCmd::ExitCmd(CommandListener* parent)
    : LogCommand("EXIT"), mParent(*parent) {
}

int CommandListener::ExitCmd::runCommand(SocketClient* cli, int /*argc*/,
                                         char** /*argv*/) {
    setname();

    cli->sendMsg("success");
    release(cli);
    parent_->release(cli);

    return 0;
}
+29 −61
Original line number Diff line number Diff line
@@ -14,85 +14,53 @@
 * limitations under the License.
 */

#ifndef _COMMANDLISTENER_H__
#define _COMMANDLISTENER_H__
#pragma once

#include <sysutils/FrameworkListener.h>

#include "LogBuffer.h"
#include "LogCommand.h"
#include "LogListener.h"
#include "LogReader.h"
#include "LogTags.h"

// See main.cpp for implementation
void reinit_signal_handler(int /*signal*/);

class CommandListener : public FrameworkListener {
  public:
    CommandListener(LogBuffer* buf, LogReader* reader, LogListener* swl);
    virtual ~CommandListener() {
    }
    CommandListener(LogBuffer* buf, LogTags* tags);
    virtual ~CommandListener() {}

  private:
    static int getLogSocket();

    class ShutdownCmd : public LogCommand {
        LogReader& mReader;
        LogListener& mSwl;

       public:
        ShutdownCmd(LogReader* reader, LogListener* swl);
        virtual ~ShutdownCmd() {
        }
        int runCommand(SocketClient* c, int argc, char** argv);
    };

#define LogBufferCmd(name)                                      \
    class name##Cmd : public LogCommand {                       \
        LogBuffer& mBuf;                                        \
                                                                \
       public:                                                  \
        explicit name##Cmd(LogBuffer* buf);                     \
        virtual ~name##Cmd() {                                  \
        }                                                       \
        int runCommand(SocketClient* c, int argc, char** argv); \
    }

    LogBufferCmd(Clear);
    LogBufferCmd(GetBufSize);
    LogBufferCmd(SetBufSize);
    LogBufferCmd(GetBufSizeUsed);
    LogBufferCmd(GetStatistics);
    LogBufferCmd(GetPruneList);
    LogBufferCmd(SetPruneList);
    LogBufferCmd(GetEventTag);
    LogBuffer* buf_;
    LogTags* tags_;

#define LogCmd(name)                                            \
#define LogCmd(name, command_string)                            \
    class name##Cmd : public LogCommand {                       \
      public:                                                   \
        name##Cmd();                                            \
        virtual ~name##Cmd() {                                  \
        }                                                       \
        explicit name##Cmd(CommandListener* parent)             \
            : LogCommand(#command_string), parent_(parent) {}   \
        virtual ~name##Cmd() {}                                 \
        int runCommand(SocketClient* c, int argc, char** argv); \
    }

    LogCmd(Reinit);

#define LogParentCmd(name)                                      \
    class name##Cmd : public LogCommand {                       \
        CommandListener& mParent;                               \
                                                                \
       public:                                                  \
        name##Cmd();                                            \
        explicit name##Cmd(CommandListener* parent);            \
        virtual ~name##Cmd() {                                  \
        }                                                       \
        int runCommand(SocketClient* c, int argc, char** argv); \
        void release(SocketClient* c) {                         \
            mParent.release(c);                                 \
        }                                                       \
      private:                                                  \
        LogBuffer* buf() const { return parent_->buf_; }        \
        LogTags* tags() const { return parent_->tags_; }        \
        CommandListener* parent_;                               \
    }

    LogParentCmd(Exit);
    LogCmd(Clear, clear);
    LogCmd(GetBufSize, getLogSize);
    LogCmd(SetBufSize, setLogSize);
    LogCmd(GetBufSizeUsed, getLogSizeUsed);
    LogCmd(GetStatistics, getStatistics);
    LogCmd(GetPruneList, getPruneList);
    LogCmd(SetPruneList, setPruneList);
    LogCmd(GetEventTag, getEventTag);
    LogCmd(Reinit, reinit);
    LogCmd(Exit, EXIT);
#undef LogCmd
};

#endif
+3 −3
Original line number Diff line number Diff line
@@ -109,8 +109,8 @@ void LogBuffer::init() {
    LogTimeEntry::unlock();
}

LogBuffer::LogBuffer(LastLogTimes* times)
    : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
LogBuffer::LogBuffer(LastLogTimes* times, LogTags* tags)
    : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times), tags_(tags) {
    pthread_rwlock_init(&mLogElementsLock, nullptr);

    log_id_for_each(i) {
@@ -232,7 +232,7 @@ int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
    const char* tag = nullptr;
    size_t tag_len = 0;
    if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
        tag = tagToName(elem->getTag());
        tag = tags_->tagToName(elem->getTag());
        if (tag) {
            tag_len = strlen(tag);
        }
+4 −18
Original line number Diff line number Diff line
@@ -14,8 +14,7 @@
 * limitations under the License.
 */

#ifndef _LOGD_LOG_BUFFER_H__
#define _LOGD_LOG_BUFFER_H__
#pragma once

#include <sys/types.h>

@@ -98,8 +97,6 @@ class LogBuffer {

    bool monotonic;

    LogTags tags;

    LogBufferElement* lastLoggedElements[LOG_ID_MAX];
    LogBufferElement* droppedElements[LOG_ID_MAX];
    void log(LogBufferElement* elem);
@@ -107,7 +104,7 @@ class LogBuffer {
   public:
    LastLogTimes& mTimes;

    explicit LogBuffer(LastLogTimes* times);
    LogBuffer(LastLogTimes* times, LogTags* tags);
    ~LogBuffer();
    void init();
    bool isMonotonic() {
@@ -143,17 +140,6 @@ class LogBuffer {
        return mPrune.format();
    }

    std::string formatGetEventTag(uid_t uid, const char* name,
                                  const char* format) {
        return tags.formatGetEventTag(uid, name, format);
    }
    std::string formatEntry(uint32_t tag, uid_t uid) {
        return tags.formatEntry(tag, uid);
    }
    const char* tagToName(uint32_t tag) {
        return tags.tagToName(tag);
    }

    // helper must be protected directly or implicitly by wrlock()/unlock()
    const char* pidToName(pid_t pid) {
        return stats.pidToName(pid);
@@ -186,6 +172,6 @@ class LogBuffer {
    // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
    // there are no logs for the given log type. Requires mLogElementsLock to be held.
    LogBufferElementCollection::iterator GetOldest(log_id_t log_id);
};

#endif  // _LOGD_LOG_BUFFER_H__
    LogTags* tags_;
};
+2 −1
Original line number Diff line number Diff line
@@ -94,7 +94,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    }

    LastLogTimes times;
    LogBuffer log_buffer(&times);
    LogTags tags;
    LogBuffer log_buffer(&times, &tags);
    size_t data_left = size;
    const uint8_t** pdata = &data;

Loading