xref: /openbmc/phosphor-dbus-monitor/src/event_manager.cpp (revision 8bbf9d2cc5a502fa0df0e70bd4232208a1ab6c01)
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(
29     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()).count();
61 
62     auto objPath =  std::string(OBJ_EVENT) + '/' + eventName + '/' +
63                         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>(
73                        objPath,
74                        ms, // Milliseconds since 1970
75                        std::move(msg),
76                        std::move(additionalData)));
77 }
78 
79 Manager& getManager()
80 {
81     static Manager mgr;
82     return mgr;
83 }
84 
85 } // namespace events
86 } // namespace phosphor
87