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/config.cpp b/src/config.cpp
index d7f2ee1..9a6f44c 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -3,12 +3,20 @@
 
 #include "config.hpp"
 
+#include <sys/un.h>
+
 #include <algorithm>
 #include <climits>
 #include <cstring>
 #include <stdexcept>
 #include <string>
 
+namespace
+{
+constexpr char bufferModeStr[] = "buffer";
+constexpr char streamModeStr[] = "stream";
+} // namespace
+
 /**
  * @brief Set boolean value from environment variable.
  *
@@ -89,18 +97,46 @@
 Config::Config()
 {
     safeSet("SOCKET_ID", socketId);
-    safeSet("BUF_MAXSIZE", bufMaxSize);
-    safeSet("BUF_MAXTIME", bufMaxTime);
-    safeSet("FLUSH_FULL", bufFlushFull);
-    safeSet("HOST_STATE", hostState);
-    safeSet("OUT_DIR", outDir);
-    safeSet("MAX_FILES", maxFiles);
-
-    // Validate parameters
-    if (bufFlushFull && !bufMaxSize && !bufMaxTime)
+    const char* mode_str = bufferModeStr;
+    safeSet("MODE", mode_str);
+    if (strcmp(mode_str, bufferModeStr) == 0)
+    {
+        mode = Mode::bufferMode;
+    }
+    else if (strcmp(mode_str, streamModeStr) == 0)
+    {
+        mode = Mode::streamMode;
+    }
+    else
     {
         throw std::invalid_argument(
-            "Flush policy is set to save the buffer as it fills, but buffer's "
-            "limits are not defined");
+            "Invalid value for mode; expect either 'stream' or 'buffer'");
+    }
+
+    if (mode == Mode::bufferMode)
+    {
+        safeSet("BUF_MAXSIZE", bufMaxSize);
+        safeSet("BUF_MAXTIME", bufMaxTime);
+        safeSet("FLUSH_FULL", bufFlushFull);
+        safeSet("HOST_STATE", hostState);
+        safeSet("OUT_DIR", outDir);
+        safeSet("MAX_FILES", maxFiles);
+        // Validate parameters
+        if (bufFlushFull && !bufMaxSize && !bufMaxTime)
+        {
+            throw std::invalid_argument("Flush policy is set to save the "
+                                        "buffer as it fills, but buffer's "
+                                        "limits are not defined");
+        }
+    }
+    else
+    {
+        // mode == Mode::streamMode
+        safeSet("STREAM_DST", streamDestination);
+        // We need an extra +1 for null terminator.
+        if (strlen(streamDestination) + 1 > sizeof(sockaddr_un::sun_path))
+        {
+            throw std::invalid_argument("Invalid STREAM_DST: too long");
+        }
     }
 }