1 /** 2 * Copyright © 2017 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "config.h" 18 #include "event.hpp" 19 #include "event_manager.hpp" 20 #include "event_serialize.hpp" 21 22 #include <experimental/filesystem> 23 24 namespace phosphor 25 { 26 namespace events 27 { 28 29 void Manager::create(const std::string& eventName, 30 const std::string& eventMessage, 31 const std::string& objectPath, 32 const std::string& propertyName, 33 const std::string& propertyValue) 34 { 35 using namespace std::string_literals; 36 namespace fs = std::experimental::filesystem; 37 38 auto msg = eventMessage; 39 std::vector<std::string> additionalData; 40 41 auto propVal = propertyName + "=" + propertyValue; 42 auto path = "path="s + objectPath; 43 44 additionalData.push_back(std::move(path)); 45 additionalData.push_back(std::move(propVal)); 46 47 auto& eventQueue = eventMap[eventName]; 48 49 // get the last event entry for this event 50 // to generate the id. 51 auto id = 0; 52 if (eventQueue.size() > 0) 53 { 54 fs::path path(eventQueue.back()->objectPath); 55 id = std::stoi(std::string(path.filename().c_str())); 56 id++; 57 } 58 59 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( 60 std::chrono::system_clock::now().time_since_epoch()) 61 .count(); 62 63 auto objPath = 64 std::string(OBJ_EVENT) + '/' + eventName + '/' + std::to_string(id); 65 66 // check for capping of the events,if cap reached then erase the oldest 67 // event. 68 if (eventQueue.size() == MAX_EVENTS) 69 { 70 fs::path eventPath(EVENTS_PERSIST_PATH); 71 eventPath /= eventName; 72 eventPath /= std::to_string(eventQueue.front()->timestamp()); 73 eventQueue.pop(); 74 std::error_code ec; 75 fs::remove(eventPath, ec); 76 } 77 78 auto event = 79 std::make_unique<Entry>(objPath, 80 ms, // Milliseconds since 1970 81 std::move(msg), std::move(additionalData)); 82 serialize(*event, eventName); 83 eventQueue.push(std::move(event)); 84 } 85 86 void Manager::restore() 87 { 88 if (!fs::exists(EVENTS_PERSIST_PATH) || fs::is_empty(EVENTS_PERSIST_PATH)) 89 { 90 return; 91 } 92 93 for (auto& eventFile : 94 fs::recursive_directory_iterator(EVENTS_PERSIST_PATH)) 95 { 96 if (!fs::is_regular_file(eventFile)) 97 { 98 continue; 99 } 100 101 EventQueue events; 102 103 auto eventPath = eventFile.path().string(); 104 auto pos1 = eventPath.rfind("/"); 105 auto pos2 = eventPath.rfind("/", pos1 - 1) + 1; 106 auto eventName = eventPath.substr(pos2, (pos1 - pos2)); 107 auto validEvent = false; 108 auto timestamp = eventFile.path().filename().string(); 109 auto tsNum = std::stoll(timestamp); 110 auto objPath = 111 std::string(OBJ_EVENT) + '/' + eventName + '/' + timestamp; 112 113 auto event = std::make_unique<Entry>(objPath, tsNum); 114 if (deserialize(eventFile.path(), *event)) 115 { 116 event->emit_object_added(); 117 events.push(std::move(event)); 118 validEvent = true; 119 } 120 121 if (validEvent) 122 { 123 eventMap[eventName] = std::move(events); 124 } 125 } 126 } 127 128 Manager& getManager() 129 { 130 static Manager mgr; 131 return mgr; 132 } 133 134 } // namespace events 135 } // namespace phosphor 136