xref: /openbmc/phosphor-dbus-monitor/src/event_manager.cpp (revision d1eac88d18ae7dd11033dba87b6aebb220da9064)
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 
21 #include <experimental/filesystem>
22 
23 namespace phosphor
24 {
25 namespace events
26 {
27 
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)
33 {
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()));
55         id++;
56     }
57 
58     auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
59                   std::chrono::system_clock::now().time_since_epoch())
60                   .count();
61 
62     auto objPath =
63         std::string(OBJ_EVENT) + '/' + eventName + '/' + std::to_string(id);
64 
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 
72     eventQueue.emplace(std::make_unique<Entry>(objPath,
73                                                ms, // Milliseconds since 1970
74                                                std::move(msg),
75                                                std::move(additionalData)));
76 }
77 
78 Manager& getManager()
79 {
80     static Manager mgr;
81     return mgr;
82 }
83 
84 } // namespace events
85 } // namespace phosphor
86