1 #pragma once
2 
3 #include "file_descriptor.hpp"
4 #include "utils.hpp"
5 #include "xyz/openbmc_project/Logging/Create/server.hpp"
6 
7 #include <nlohmann/json.hpp>
8 
9 #include <cstdint>
10 #include <filesystem>
11 
12 namespace watchdog
13 {
14 namespace dump
15 {
16 
17 namespace fs = std::filesystem;
18 using FFDCFormat =
19     sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat;
20 using FFDCTuple =
21     std::tuple<FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
22 
23 using ::nlohmann::json;
24 /**
25  * @class FFDCFile
26  *
27  * File that contains FFDC (first failure data capture) data in json format.
28  *
29  * This class is used to store FFDC json callout data in an error log.
30  */
31 class FFDCFile
32 {
33   public:
34     // Specify which compiler-generated methods we want
35     FFDCFile() = delete;
36     FFDCFile(const FFDCFile&) = delete;
37     FFDCFile(FFDCFile&&) = default;
38     FFDCFile& operator=(const FFDCFile&) = delete;
39     FFDCFile& operator=(FFDCFile&&) = default;
40     ~FFDCFile();
41 
42     /**
43      * @brief Constructor
44      *
45      * @details Creates the FFDC file by using passed json data.
46      *
47      * Throws an exception if an error occurs.
48      */
49     explicit FFDCFile(const json& calloutData);
50 
51     /**
52      * @brief Returns the file descriptor for the file.
53      *
54      * @details The file is open for both reading and writing.
55      *
56      * @return file descriptor
57      */
getFileDescriptor() const58     int getFileDescriptor() const
59     {
60         // Return the integer file descriptor within the FileDescriptor object
61         return descriptor();
62     }
63 
64     /**
65      * @brief Returns the absolute path to the file.
66      *
67      * @return absolute path
68      */
getPath() const69     const fs::path& getPath() const
70     {
71         return tempFile.getPath();
72     }
73 
74   private:
75     /**
76      * @brief Temporary file where FFDC data is stored.
77      *
78      * @details The TemporaryFile destructor will automatically delete the file
79      * if it was not explicitly deleted using remove().
80      */
81     TemporaryFile tempFile{};
82 
83     /**
84      * @brief File descriptor for reading from/writing to the file.
85      *
86      * @details The FileDescriptor destructor will automatically close the file
87      * if it was not explicitly closed using remove().
88      */
89     FileDescriptor descriptor{};
90 
91     /**
92      * @brief Used to store callout ffdc data from passed json object
93      */
94     std::string calloutData;
95 
96     /**
97      * @brief Creates FFDC file for creating PEL records.
98      */
99     void prepareFFDCFile();
100 };
101 
102 } // namespace dump
103 } // namespace watchdog
104