Ratan Gupta | cd22786 | 2017-10-06 21:27:13 +0530 | [diff] [blame] | 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 | */ |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 16 | |
| 17 | #include "config.h" |
Ratan Gupta | cd22786 | 2017-10-06 21:27:13 +0530 | [diff] [blame] | 18 | #include "event.hpp" |
| 19 | #include "event_manager.hpp" |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 20 | |
| 21 | #include <experimental/filesystem> |
Ratan Gupta | cd22786 | 2017-10-06 21:27:13 +0530 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace events |
| 26 | { |
| 27 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 28 | void Manager::create(const std::string& eventName, |
| 29 | const std::string& eventMessage, |
| 30 | const std::string& objectPath, |
| 31 | const std::string& propertyName, |
| 32 | const std::string& propertyValue) |
Ratan Gupta | cd22786 | 2017-10-06 21:27:13 +0530 | [diff] [blame] | 33 | { |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 34 | using namespace std::string_literals; |
| 35 | namespace fs = std::experimental::filesystem; |
| 36 | |
| 37 | auto msg = eventMessage; |
| 38 | std::vector<std::string> additionalData; |
| 39 | |
| 40 | auto propVal = propertyName + "=" + propertyValue; |
| 41 | auto path = "path="s + objectPath; |
| 42 | |
| 43 | additionalData.push_back(std::move(path)); |
| 44 | additionalData.push_back(std::move(propVal)); |
| 45 | |
| 46 | auto& eventQueue = eventMap[eventName]; |
| 47 | |
| 48 | // get the last event entry for this event |
| 49 | // to generate the id. |
| 50 | auto id = 0; |
| 51 | if (eventQueue.size() > 0) |
| 52 | { |
| 53 | fs::path path(eventQueue.back()->objectPath); |
| 54 | id = std::stoi(std::string(path.filename().c_str())); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 55 | id++; |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 59 | std::chrono::system_clock::now().time_since_epoch()) |
| 60 | .count(); |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 61 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 62 | auto objPath = |
| 63 | std::string(OBJ_EVENT) + '/' + eventName + '/' + std::to_string(id); |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 64 | |
Ratan Gupta | 8bbf9d2 | 2017-10-06 21:45:24 +0530 | [diff] [blame] | 65 | // check for capping of the events,if cap reached then erase the oldest |
| 66 | // event. |
| 67 | if (eventQueue.size() == MAX_EVENTS) |
| 68 | { |
| 69 | eventQueue.pop(); |
| 70 | } |
| 71 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 72 | eventQueue.emplace(std::make_unique<Entry>(objPath, |
| 73 | ms, // Milliseconds since 1970 |
| 74 | std::move(msg), |
| 75 | std::move(additionalData))); |
Ratan Gupta | cd22786 | 2017-10-06 21:27:13 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Manager& getManager() |
| 79 | { |
| 80 | static Manager mgr; |
| 81 | return mgr; |
| 82 | } |
| 83 | |
| 84 | } // namespace events |
| 85 | } // namespace phosphor |