blob: 6e7d302e0ba9e2c5bdebde7f9803f5c2f5f2b238 [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"
Ratan Gupta3e84ec62017-10-06 21:37:01 +053020
21#include <experimental/filesystem>
Ratan Guptacd227862017-10-06 21:27:13 +053022
23namespace phosphor
24{
25namespace events
26{
27
Brad Bishopd1eac882018-03-29 10:34:05 -040028void 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 Guptacd227862017-10-06 21:27:13 +053033{
Ratan Gupta3e84ec62017-10-06 21:37:01 +053034 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 Bishopd1eac882018-03-29 10:34:05 -040055 id++;
Ratan Gupta3e84ec62017-10-06 21:37:01 +053056 }
57
58 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Brad Bishopd1eac882018-03-29 10:34:05 -040059 std::chrono::system_clock::now().time_since_epoch())
60 .count();
Ratan Gupta3e84ec62017-10-06 21:37:01 +053061
Brad Bishopd1eac882018-03-29 10:34:05 -040062 auto objPath =
63 std::string(OBJ_EVENT) + '/' + eventName + '/' + std::to_string(id);
Ratan Gupta3e84ec62017-10-06 21:37:01 +053064
Ratan Gupta8bbf9d22017-10-06 21:45:24 +053065 // 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 Bishopd1eac882018-03-29 10:34:05 -040072 eventQueue.emplace(std::make_unique<Entry>(objPath,
73 ms, // Milliseconds since 1970
74 std::move(msg),
75 std::move(additionalData)));
Ratan Guptacd227862017-10-06 21:27:13 +053076}
77
78Manager& getManager()
79{
80 static Manager mgr;
81 return mgr;
82}
83
84} // namespace events
85} // namespace phosphor