Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 1 | /** |
| 2 | * @brief Log storage. |
| 3 | * |
| 4 | * This file is part of HostLogger project. |
| 5 | * |
| 6 | * Copyright (c) 2018 YADRO |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 21 | #include "log_storage.hpp" |
| 22 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 23 | #include "config.hpp" |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 24 | #include "log_file.hpp" |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 25 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 26 | #include <fcntl.h> |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 27 | #include <string.h> |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <unistd.h> |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 30 | |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 31 | #include <exception> |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 32 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 33 | void LogStorage::parse(const char* data, size_t len) |
| 34 | { |
| 35 | // Split log stream to separate messages. |
| 36 | // Stream may not be ended with EOL, so we handle this situation by |
| 37 | // last_complete_ flag. |
| 38 | size_t pos = 0; |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 39 | while (pos < len) |
| 40 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 41 | // Search for EOL ('\n') |
| 42 | size_t eol = pos; |
| 43 | while (eol < len && data[eol] != '\n') |
| 44 | ++eol; |
| 45 | const bool eolFound = eol < len; |
| 46 | const char* msgText = data + pos; |
| 47 | size_t msgLen = (eolFound ? eol : len) - pos; |
| 48 | |
| 49 | // Remove '\r' from the end of message |
| 50 | while (msgLen && msgText[msgLen - 1] == '\r') |
| 51 | --msgLen; |
| 52 | |
| 53 | // Append message to store |
| 54 | if (msgLen) |
| 55 | append(msgText, msgLen); |
| 56 | |
| 57 | pos = eol + 1; // Skip '\n' |
| 58 | last_complete_ = eolFound; |
| 59 | } |
| 60 | } |
| 61 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 62 | void LogStorage::append(const char* msg, size_t len) |
| 63 | { |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 64 | if (!last_complete_) |
| 65 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 66 | // The last message is incomplete, add msg as part of it |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 67 | if (!messages_.empty()) |
| 68 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 69 | Message& last_msg = *messages_.rbegin(); |
| 70 | last_msg.text.append(msg, len); |
| 71 | } |
| 72 | } |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 73 | else |
| 74 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 75 | Message new_msg; |
| 76 | time(&new_msg.timeStamp); |
| 77 | new_msg.text.assign(msg, len); |
| 78 | messages_.push_back(new_msg); |
| 79 | shrink(); |
| 80 | } |
| 81 | } |
| 82 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 83 | void LogStorage::clear() |
| 84 | { |
| 85 | messages_.clear(); |
| 86 | last_complete_ = true; |
| 87 | } |
| 88 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 89 | bool LogStorage::empty() const |
| 90 | { |
| 91 | return messages_.empty(); |
| 92 | } |
| 93 | |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 94 | int LogStorage::save(const char* fileName) const |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 95 | { |
| 96 | int rc = 0; |
| 97 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 98 | if (empty()) |
| 99 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 100 | printf("No messages to write\n"); |
| 101 | return 0; |
| 102 | } |
| 103 | |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 104 | try |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 105 | { |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 106 | LogFile log(fileName); |
| 107 | |
| 108 | // Write full datetime stamp as the first record |
| 109 | const time_t& tmStart = messages_.begin()->timeStamp; |
| 110 | tm tmLocal; |
| 111 | localtime_r(&tmStart, &tmLocal); |
| 112 | char tmText[20]; // size of "%F %T" asciiz (YYYY-MM-DD HH:MM:SS) |
| 113 | strftime(tmText, sizeof(tmText), "%F %T", &tmLocal); |
| 114 | std::string titleMsg = ">>> Log collection started at "; |
| 115 | titleMsg += tmText; |
| 116 | log.write(tmStart, titleMsg); |
| 117 | |
| 118 | // Write messages |
| 119 | for (auto it = messages_.begin(); it != messages_.end(); ++it) |
| 120 | log.write(it->timeStamp, it->text); |
| 121 | |
| 122 | log.close(); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 123 | } |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 124 | catch (std::exception& e) |
| 125 | { |
| 126 | rc = EIO; |
| 127 | fprintf(stderr, "%s\n", e.what()); |
| 128 | } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 129 | |
| 130 | return rc; |
| 131 | } |
| 132 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 133 | void LogStorage::shrink() |
| 134 | { |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 135 | if (loggerConfig.storageSizeLimit) |
| 136 | { |
| 137 | while (messages_.size() > |
| 138 | static_cast<size_t>(loggerConfig.storageSizeLimit)) |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 139 | messages_.pop_front(); |
| 140 | } |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 141 | if (loggerConfig.storageTimeLimit) |
| 142 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 143 | // Get time for N hours ago |
| 144 | time_t oldestTimeStamp; |
| 145 | time(&oldestTimeStamp); |
| 146 | oldestTimeStamp -= loggerConfig.storageTimeLimit * 60 * 60; |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 147 | while (!messages_.empty() && |
| 148 | messages_.begin()->timeStamp < oldestTimeStamp) |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 149 | messages_.pop_front(); |
| 150 | } |
| 151 | } |