1 #include "config.h"
2 
3 #include "dump_manager_faultlog.hpp"
4 
5 #include "faultlog_dump_entry.hpp"
6 
7 #include <fmt/core.h>
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <phosphor-logging/elog.hpp>
11 #include <xyz/openbmc_project/Common/File/error.hpp>
12 #include <xyz/openbmc_project/Common/error.hpp>
13 
14 #include <filesystem>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 
19 namespace phosphor
20 {
21 namespace dump
22 {
23 namespace faultlog
24 {
25 
26 using namespace phosphor::logging;
27 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
28 using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
29 using ErrnoOpen = xyz::openbmc_project::Common::File::Open::ERRNO;
30 using PathOpen = xyz::openbmc_project::Common::File::Open::PATH;
31 
32 sdbusplus::message::object_path
33     Manager::createDump(phosphor::dump::DumpCreateParams params)
34 {
35     log<level::INFO>("In dump_manager_fault.cpp createDump");
36 
37     // Currently we ignore the parameters.
38     // TODO phosphor-debug-collector/issues/22: Check parameter values and
39     // exit early if we don't receive the expected parameters
40     if (params.empty())
41     {
42         log<level::INFO>("No additional parameters received");
43     }
44     else
45     {
46         log<level::INFO>("Got additional parameters");
47     }
48 
49     // Get the id
50     auto id = lastEntryId + 1;
51     auto idString = std::to_string(id);
52     auto objPath = std::filesystem::path(baseEntryPath) / idString;
53 
54     std::filesystem::path faultLogFilePath(std::string(FAULTLOG_DUMP_PATH) +
55                                            idString);
56     std::ofstream faultLogFile;
57 
58     errno = 0;
59 
60     faultLogFile.open(faultLogFilePath,
61                       std::ofstream::out | std::fstream::trunc);
62 
63     if (faultLogFile.is_open())
64     {
65         log<level::INFO>("faultLogFile is open");
66 
67         faultLogFile << "This is faultlog file #" << idString << " at "
68                      << std::string(FAULTLOG_DUMP_PATH) + idString << std::endl;
69 
70         faultLogFile.close();
71     }
72     else
73     {
74         log<level::ERR>(fmt::format("Failed to open fault log file at {}, "
75                                     "errno({}), strerror(\"{}\"), "
76                                     "OBJECTPATH({}), ID({})",
77                                     faultLogFilePath.c_str(), errno,
78                                     strerror(errno), objPath.c_str(), id)
79                             .c_str());
80         elog<Open>(ErrnoOpen(errno), PathOpen(objPath.c_str()));
81     }
82 
83     try
84     {
85         log<level::INFO>("dump_manager_faultlog.cpp: add faultlog entry");
86 
87         uint64_t timestamp =
88             std::chrono::duration_cast<std::chrono::microseconds>(
89                 std::chrono::system_clock::now().time_since_epoch())
90                 .count();
91 
92         entries.insert(std::make_pair(
93             id,
94             std::make_unique<faultlog::Entry>(
95                 bus, objPath.c_str(), id, timestamp,
96                 std::filesystem::file_size(faultLogFilePath), faultLogFilePath,
97                 phosphor::dump::OperationStatus::Completed, *this)));
98     }
99     catch (const std::invalid_argument& e)
100     {
101         log<level::ERR>(fmt::format("Error in creating dump entry, "
102                                     "errormsg({}), OBJECTPATH({}), ID({})",
103                                     e.what(), objPath.c_str(), id)
104                             .c_str());
105         elog<InternalFailure>();
106     }
107 
108     lastEntryId++;
109 
110     log<level::INFO>("End of dump_manager_faultlog.cpp createDump");
111     return objPath.string();
112 }
113 
114 } // namespace faultlog
115 } // namespace dump
116 } // namespace phosphor
117