Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
| 3 | |
| 4 | #include "config.hpp" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <climits> |
| 8 | #include <cstring> |
| 9 | #include <stdexcept> |
| 10 | #include <string> |
| 11 | |
| 12 | /** |
| 13 | * @brief Set boolean value from environment variable. |
| 14 | * |
| 15 | * @param[in] name name of environment variable |
| 16 | * @param[out] value value to set |
| 17 | * |
| 18 | * @throw std::invalid_argument in case of errors |
| 19 | */ |
| 20 | static void safeSet(const char* name, bool& value) |
| 21 | { |
| 22 | const char* envVal = std::getenv(name); |
| 23 | if (envVal) |
| 24 | { |
| 25 | if (strcmp(envVal, "true") == 0) |
| 26 | { |
| 27 | value = true; |
| 28 | } |
| 29 | else if (strcmp(envVal, "false") == 0) |
| 30 | { |
| 31 | value = false; |
| 32 | } |
| 33 | else |
| 34 | { |
| 35 | std::string err = "Invalid value of environment variable "; |
| 36 | err += name; |
| 37 | err += ": '"; |
| 38 | err += envVal; |
| 39 | err += "', expected 'true' or 'false'"; |
| 40 | throw std::invalid_argument(err); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @brief Set unsigned numeric value from environment variable. |
| 47 | * |
| 48 | * @param[in] name name of environment variable |
| 49 | * @param[out] value value to set |
| 50 | * |
| 51 | * @throw std::invalid_argument in case of errors |
| 52 | */ |
| 53 | static void safeSet(const char* name, size_t& value) |
| 54 | { |
| 55 | const char* envVal = std::getenv(name); |
| 56 | if (envVal) |
| 57 | { |
| 58 | const size_t num = strtoul(envVal, nullptr, 0); |
| 59 | if (std::all_of(envVal, envVal + strlen(envVal), isdigit) && |
| 60 | num != ULONG_MAX) |
| 61 | { |
| 62 | value = num; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | std::string err = "Invalid argument: "; |
| 67 | err += envVal; |
| 68 | err += ", expected unsigned numeric value"; |
| 69 | throw std::invalid_argument(err); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief Set string value from environment variable. |
| 76 | * |
| 77 | * @param[in] name name of environment variable |
| 78 | * @param[out] value value to set |
| 79 | */ |
| 80 | static void safeSet(const char* name, const char*& value) |
| 81 | { |
| 82 | const char* envVal = std::getenv(name); |
| 83 | if (envVal) |
| 84 | { |
| 85 | value = envVal; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | Config::Config() |
| 90 | { |
| 91 | safeSet("SOCKET_ID", socketId); |
| 92 | safeSet("BUF_MAXSIZE", bufMaxSize); |
| 93 | safeSet("BUF_MAXTIME", bufMaxTime); |
| 94 | safeSet("FLUSH_FULL", bufFlushFull); |
| 95 | safeSet("HOST_STATE", hostState); |
| 96 | safeSet("OUT_DIR", outDir); |
| 97 | safeSet("MAX_FILES", maxFiles); |
| 98 | |
| 99 | // Validate parameters |
| 100 | if (bufFlushFull && !bufMaxSize && !bufMaxTime) |
| 101 | { |
| 102 | throw std::invalid_argument( |
| 103 | "Flush policy is set to save the buffer as it fills, but buffer's " |
| 104 | "limits are not defined"); |
| 105 | } |
| 106 | } |