blob: 2c09f35a7639dd4f1934dbf820996e35bd81908a [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
28void Manager::create(
29 const std::string& eventName,
Ratan Gupta3e84ec62017-10-06 21:37:01 +053030 const std::string& eventMessage,
Ratan Guptacd227862017-10-06 21:27:13 +053031 const std::string& objectPath,
32 const std::string& propertyName,
Ratan Gupta3e84ec62017-10-06 21:37:01 +053033 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()));
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
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
Ratan Gupta3e84ec62017-10-06 21:37:01 +053072 eventQueue.emplace(std::make_unique<Entry>(
73 objPath,
74 ms, // Milliseconds since 1970
75 std::move(msg),
76 std::move(additionalData)));
Ratan Guptacd227862017-10-06 21:27:13 +053077}
78
79Manager& getManager()
80{
81 static Manager mgr;
82 return mgr;
83}
84
85} // namespace events
86} // namespace phosphor