Add the stream mode

The stream mode behaves differently in versus the existing buffer mode.

1. It leverages rsyslog to persist logs;
2. It leverages logrotate to rotate and compress logs;
3. It persists logs as soon as they are collected.

Add configuration options to choose modes at start up time. When stream
mode is disabled, no difference compared to the existing service.

See README.md for details.

This change also adds mock classes for unit test purpose.

Change-Id: Ic7d02e826c7d9372621c096c6e768e6216974150
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
diff --git a/src/stream_service.hpp b/src/stream_service.hpp
new file mode 100644
index 0000000..4aaba41
--- /dev/null
+++ b/src/stream_service.hpp
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright (C) 2021 Google
+
+#pragma once
+
+#include "config.hpp"
+#include "dbus_loop.hpp"
+#include "file_storage.hpp"
+#include "host_console.hpp"
+#include "log_buffer.hpp"
+#include "service.hpp"
+
+#include <sys/un.h>
+
+/**
+ * @class Service
+ * @brief Log service: watches for events and handles them.
+ */
+class StreamService : public Service
+{
+  public:
+    /**
+     * @brief Constructor for stream-only mode. All arguments should outlive
+     * this class.
+     *
+     * @param streamDestination the destination socket to stream logs.
+     * @param dbusLoop the DbusLoop instance.
+     * @param hostConsole the HostConsole instance.
+     */
+    StreamService(const char* streamDestination, DbusLoop& dbusLoop,
+                  HostConsole& hostConsole);
+
+    /**
+     * @brief Destructor; close the file descriptor.
+     */
+    ~StreamService() override;
+
+    /**
+     * @brief Run the service.
+     *
+     * @throw std::exception in case of errors
+     */
+    void run() override;
+
+  protected:
+    /**
+     * @brief Read data from host console and perform actions according to
+     * modes.
+     */
+    virtual void readConsole();
+
+    /**
+     * @brief Stream console data to a datagram unix socket.
+     *
+     * @param data the bytes to stream
+     * @param len the length of the bytes array
+     *
+     * @throw std::exception in case of errors
+     */
+    virtual void streamConsole(const char* data, size_t len);
+
+    /**
+     * @brief Set up stream socket
+     *
+     * @throw std::exception in case of errors
+     */
+    virtual void setStreamSocket();
+
+  private:
+    /** @brief Path to the destination (the rsyslog unix socket) */
+    const char* destinationPath;
+    /** @brief D-Bus event loop. */
+    DbusLoop* dbusLoop;
+    /** @brief Host console connection. */
+    HostConsole* hostConsole;
+    /** @brief File descriptor of the ouput socket */
+    int outputSocketFd;
+    /** @brief Address of the destination (the rsyslog unix socket) */
+    sockaddr_un destination;
+};