blob: 57a847851dfc27e7840981262c9ba688cb8d2c55 [file] [log] [blame]
Ratan Guptacd227862017-10-06 21:27:13 +05301/**
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 Gupta3e84ec62017-10-06 21:37:01 +053016
17#include "config.h"
Ratan Guptacd227862017-10-06 21:27:13 +053018#include "event.hpp"
19#include "event_manager.hpp"
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050020#include "event_serialize.hpp"
Ratan Gupta3e84ec62017-10-06 21:37:01 +053021
22#include <experimental/filesystem>
Ratan Guptacd227862017-10-06 21:27:13 +053023
24namespace phosphor
25{
26namespace events
27{
28
Brad Bishopd1eac882018-03-29 10:34:05 -040029void 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)
Ratan Guptacd227862017-10-06 21:27:13 +053034{
Ratan Gupta3e84ec62017-10-06 21:37:01 +053035 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()));
Brad Bishopd1eac882018-03-29 10:34:05 -040056 id++;
Ratan Gupta3e84ec62017-10-06 21:37:01 +053057 }
58
59 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Brad Bishopd1eac882018-03-29 10:34:05 -040060 std::chrono::system_clock::now().time_since_epoch())
61 .count();
Ratan Gupta3e84ec62017-10-06 21:37:01 +053062
Brad Bishopd1eac882018-03-29 10:34:05 -040063 auto objPath =
64 std::string(OBJ_EVENT) + '/' + eventName + '/' + std::to_string(id);
Ratan Gupta3e84ec62017-10-06 21:37:01 +053065
Ratan Gupta8bbf9d22017-10-06 21:45:24 +053066 // check for capping of the events,if cap reached then erase the oldest
67 // event.
68 if (eventQueue.size() == MAX_EVENTS)
69 {
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050070 fs::path eventPath(EVENTS_PERSIST_PATH);
71 eventPath /= eventName;
72 eventPath /= std::to_string(eventQueue.front()->timestamp());
Ratan Gupta8bbf9d22017-10-06 21:45:24 +053073 eventQueue.pop();
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050074 std::error_code ec;
75 fs::remove(eventPath, ec);
Ratan Gupta8bbf9d22017-10-06 21:45:24 +053076 }
77
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050078 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
86void 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 }
Ratan Guptacd227862017-10-06 21:27:13 +0530126}
127
128Manager& getManager()
129{
130 static Manager mgr;
131 return mgr;
132}
133
134} // namespace events
135} // namespace phosphor