1f3633f63SShawn McCarney /** 2f3633f63SShawn McCarney * Copyright © 2020 IBM Corporation 3f3633f63SShawn McCarney * 4f3633f63SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License"); 5f3633f63SShawn McCarney * you may not use this file except in compliance with the License. 6f3633f63SShawn McCarney * You may obtain a copy of the License at 7f3633f63SShawn McCarney * 8f3633f63SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0 9f3633f63SShawn McCarney * 10f3633f63SShawn McCarney * Unless required by applicable law or agreed to in writing, software 11f3633f63SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS, 12f3633f63SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f3633f63SShawn McCarney * See the License for the specific language governing permissions and 14f3633f63SShawn McCarney * limitations under the License. 15f3633f63SShawn McCarney */ 16f3633f63SShawn McCarney #pragma once 17f3633f63SShawn McCarney 18f3633f63SShawn McCarney #include "file_descriptor.hpp" 19f3633f63SShawn McCarney #include "temporary_file.hpp" 20f3633f63SShawn McCarney #include "xyz/openbmc_project/Logging/Create/server.hpp" 21f3633f63SShawn McCarney 22f3633f63SShawn McCarney #include <cstdint> 23f3633f63SShawn McCarney #include <filesystem> 24f3633f63SShawn McCarney 25f3633f63SShawn McCarney namespace phosphor::power::regulators 26f3633f63SShawn McCarney { 27f3633f63SShawn McCarney 28f3633f63SShawn McCarney namespace fs = std::filesystem; 29f3633f63SShawn McCarney using FFDCFormat = 30f3633f63SShawn McCarney sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat; 31f3633f63SShawn McCarney using FileDescriptor = phosphor::power::util::FileDescriptor; 32*5f51444dSShawn McCarney using TemporaryFile = phosphor::power::util::TemporaryFile; 33f3633f63SShawn McCarney 34f3633f63SShawn McCarney /** 35f3633f63SShawn McCarney * @class FFDCFile 36f3633f63SShawn McCarney * 37f3633f63SShawn McCarney * File that contains FFDC (first failure data capture) data. 38f3633f63SShawn McCarney * 39f3633f63SShawn McCarney * This class is used to store FFDC data in an error log. The FFDC data is 40f3633f63SShawn McCarney * passed to the error logging system using a file descriptor. 41f3633f63SShawn McCarney * 42f3633f63SShawn McCarney * The constructor creates the file and opens it for both reading and writing. 43f3633f63SShawn McCarney * 44f3633f63SShawn McCarney * Use getFileDescriptor() to obtain the file descriptor needed to read or write 45f3633f63SShawn McCarney * data to the file. 46f3633f63SShawn McCarney * 47f3633f63SShawn McCarney * Use remove() to delete the file. Otherwise the file will be deleted by the 48f3633f63SShawn McCarney * destructor. 49f3633f63SShawn McCarney * 50f3633f63SShawn McCarney * FFDCFile objects cannot be copied, but they can be moved. This enables them 51f3633f63SShawn McCarney * to be stored in containers like std::vector. 52f3633f63SShawn McCarney */ 53f3633f63SShawn McCarney class FFDCFile 54f3633f63SShawn McCarney { 55f3633f63SShawn McCarney public: 56f3633f63SShawn McCarney // Specify which compiler-generated methods we want 57f3633f63SShawn McCarney FFDCFile() = delete; 58f3633f63SShawn McCarney FFDCFile(const FFDCFile&) = delete; 59f3633f63SShawn McCarney FFDCFile(FFDCFile&&) = default; 60f3633f63SShawn McCarney FFDCFile& operator=(const FFDCFile&) = delete; 61f3633f63SShawn McCarney FFDCFile& operator=(FFDCFile&&) = default; 62f3633f63SShawn McCarney ~FFDCFile() = default; 63f3633f63SShawn McCarney 64f3633f63SShawn McCarney /** 65f3633f63SShawn McCarney * Constructor. 66f3633f63SShawn McCarney * 67f3633f63SShawn McCarney * Creates the file and opens it for both reading and writing. 68f3633f63SShawn McCarney * 69f3633f63SShawn McCarney * Throws an exception if an error occurs. 70f3633f63SShawn McCarney * 71f3633f63SShawn McCarney * @param format format type of the contained data 72f3633f63SShawn McCarney * @param subType format subtype; used for the 'Custom' type 73f3633f63SShawn McCarney * @param version version of the data format; used for the 'Custom' type 74f3633f63SShawn McCarney */ 75f3633f63SShawn McCarney explicit FFDCFile(FFDCFormat format, uint8_t subType = 0, 76f3633f63SShawn McCarney uint8_t version = 0); 77f3633f63SShawn McCarney 78f3633f63SShawn McCarney /** 79f3633f63SShawn McCarney * Returns the file descriptor for the file. 80f3633f63SShawn McCarney * 81f3633f63SShawn McCarney * The file is open for both reading and writing. 82f3633f63SShawn McCarney * 83f3633f63SShawn McCarney * @return file descriptor 84f3633f63SShawn McCarney */ getFileDescriptor()85f3633f63SShawn McCarney int getFileDescriptor() 86f3633f63SShawn McCarney { 87f3633f63SShawn McCarney // Return the integer file descriptor within the FileDescriptor object 88f3633f63SShawn McCarney return descriptor(); 89f3633f63SShawn McCarney } 90f3633f63SShawn McCarney 91f3633f63SShawn McCarney /** 92f3633f63SShawn McCarney * Returns the format type of the contained data. 93f3633f63SShawn McCarney * 94f3633f63SShawn McCarney * @return format type 95f3633f63SShawn McCarney */ getFormat() const96f3633f63SShawn McCarney FFDCFormat getFormat() const 97f3633f63SShawn McCarney { 98f3633f63SShawn McCarney return format; 99f3633f63SShawn McCarney } 100f3633f63SShawn McCarney 101f3633f63SShawn McCarney /** 102f3633f63SShawn McCarney * Returns the absolute path to the file. 103f3633f63SShawn McCarney * 104f3633f63SShawn McCarney * @return absolute path 105f3633f63SShawn McCarney */ getPath() const106f3633f63SShawn McCarney const fs::path& getPath() const 107f3633f63SShawn McCarney { 108f3633f63SShawn McCarney return tempFile.getPath(); 109f3633f63SShawn McCarney } 110f3633f63SShawn McCarney 111f3633f63SShawn McCarney /** 112f3633f63SShawn McCarney * Returns the format subtype. 113f3633f63SShawn McCarney * 114f3633f63SShawn McCarney * @return subtype 115f3633f63SShawn McCarney */ getSubType() const116f3633f63SShawn McCarney uint8_t getSubType() const 117f3633f63SShawn McCarney { 118f3633f63SShawn McCarney return subType; 119f3633f63SShawn McCarney } 120f3633f63SShawn McCarney 121f3633f63SShawn McCarney /** 122f3633f63SShawn McCarney * Returns the version of the data format. 123f3633f63SShawn McCarney * 124f3633f63SShawn McCarney * @return version 125f3633f63SShawn McCarney */ getVersion() const126f3633f63SShawn McCarney uint8_t getVersion() const 127f3633f63SShawn McCarney { 128f3633f63SShawn McCarney return version; 129f3633f63SShawn McCarney } 130f3633f63SShawn McCarney 131f3633f63SShawn McCarney /** 132f3633f63SShawn McCarney * Closes and deletes the file. 133f3633f63SShawn McCarney * 134f3633f63SShawn McCarney * Does nothing if the file has already been removed. 135f3633f63SShawn McCarney * 136f3633f63SShawn McCarney * Throws an exception if an error occurs. 137f3633f63SShawn McCarney */ 138f3633f63SShawn McCarney void remove(); 139f3633f63SShawn McCarney 140f3633f63SShawn McCarney private: 141f3633f63SShawn McCarney /** 142f3633f63SShawn McCarney * Format type of the contained data. 143f3633f63SShawn McCarney */ 144f3633f63SShawn McCarney FFDCFormat format{FFDCFormat::Text}; 145f3633f63SShawn McCarney 146f3633f63SShawn McCarney /** 147f3633f63SShawn McCarney * Format subtype; used for the 'Custom' type. 148f3633f63SShawn McCarney */ 149f3633f63SShawn McCarney uint8_t subType{0}; 150f3633f63SShawn McCarney 151f3633f63SShawn McCarney /** 152f3633f63SShawn McCarney * Version of the data format; used for the 'Custom' type. 153f3633f63SShawn McCarney */ 154f3633f63SShawn McCarney uint8_t version{0}; 155f3633f63SShawn McCarney 156f3633f63SShawn McCarney /** 157f3633f63SShawn McCarney * Temporary file where FFDC data is stored. 158f3633f63SShawn McCarney * 159f3633f63SShawn McCarney * The TemporaryFile destructor will automatically delete the file if it was 160f3633f63SShawn McCarney * not explicitly deleted using remove(). 161f3633f63SShawn McCarney */ 162f3633f63SShawn McCarney TemporaryFile tempFile{}; 163f3633f63SShawn McCarney 164f3633f63SShawn McCarney /** 165f3633f63SShawn McCarney * File descriptor for reading from/writing to the file. 166f3633f63SShawn McCarney * 167f3633f63SShawn McCarney * The FileDescriptor destructor will automatically close the file if it was 168f3633f63SShawn McCarney * not explicitly closed using remove(). 169f3633f63SShawn McCarney */ 170f3633f63SShawn McCarney FileDescriptor descriptor{}; 171f3633f63SShawn McCarney }; 172f3633f63SShawn McCarney 173f3633f63SShawn McCarney } // namespace phosphor::power::regulators 174