FesaStream.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/Diagnostic/FesaStream.h>
00004 
00005 #include <cmw-log/Logger.h>
00006 
00007 #include <sys/stat.h>
00008 
00009 
00010 namespace
00011 {
00012 
00013 CMW::Log::Logger& logger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core.Diagnostic.FesaStream");
00014 
00015 } // namespace
00016 
00017 
00018 namespace fesa
00019 {
00020 
00021 std::string
00022 FesaStream::configHeader_ =
00023     "<?xml version= '1.0' encoding= 'UTF-8'?>\n<Fesa-class-configuration xmlns:xsi= \"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation =\"\" ";
00024 std::string FesaStream::configFooter_ = "</fesa-class-configuration>";
00025 
00026 std::string
00027 FesaStream::stateHeader_ =
00028     "<?xml version= '1.0' encoding= 'UTF-8'?>\n<Fesa-class-state xmlns:xsi= \"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation =\"\">";
00029 std::string FesaStream::stateFooter_ = "</fesa-class-state>";
00030 
00031 std::map<std::string, FesaStream *> FesaStream::fileFactoryMap_;
00032 
00033 
00034 FesaStream::FesaStream(const std::string streamName)
00035 {
00036     /* The configuration file has the following name: "<class><Server|Rt>Config.xml"
00037      * /tmp will be used to store it (volatile).
00038      * This UNIX std. folder is created with rw access and T sticky-bit: force owner write access only.
00039      * umask() can be used to set initial permission at file creation.
00040      */
00041     fileName_ = "/tmp/" + streamName + ".xml";
00042 }
00043 
00044 /* -------------------------------------------------------------------------
00045  */
00046 FesaStream* FesaStream::getFileStream(const std::string streamName)
00047 {
00048 
00049     FesaStream* pFileStream;
00050     std::map<std::string, FesaStream *>::iterator itr = fileFactoryMap_.find(streamName);
00051     if (itr != fileFactoryMap_.end())
00052     {
00053         //This FesaStream has already been defined
00054         pFileStream = (FesaStream *) ((*itr).second);
00055     }
00056     else
00057     {
00058         pFileStream = new FesaStream(streamName);
00059         //register the new instance
00060         fileFactoryMap_[streamName] = pFileStream;
00061     }
00062 
00063     return pFileStream;
00064 }
00065 
00066 /* -------------------------------------------------------------------------
00067  */
00068 bool FesaStream::update()
00069 {
00070 
00071     // This method is used to create the file in one-shot process
00072     // open file, insert header, append with current string buffer and footer
00073     // close file
00074     // file is overwrited each time updateFile is called
00075     // true: means everything is ok
00076     // false: something is wrong: file does not exist ...
00077     bool ret;
00078 
00079     // /tmp will be used to store the configuration file (rw access with T sticky-bit: only owner write access)
00080     // create files and directories with rw public access: disable the sticky-bit protection
00081     int32_t oldUmask = umask(0);
00082 
00083     try
00084     {
00085         fileStream_.open(fileName_.c_str(), std::ios::out);
00086     }
00087     catch (...)
00088     { // Just because an open can throw an exception
00089     }
00090 
00091     if (fileStream_.fail())
00092     {
00093         std::ostringstream errorStrStream;
00094         errorStrStream << "Failed to opene file " << fileName_;
00095         LOG_ERROR_IF(logger, errorStrStream.str());
00096         // clear the state flags
00097         fileStream_.clear();
00098         ret = false;
00099     }
00100     else
00101     {
00102         // Start with Header
00103         fileStream_ << *fileHeader_;
00104 
00105         // Insert current string buffer
00106         fileStream_ << str();
00107 
00108         // End with Footer and close
00109         fileStream_ << *fileFooter_;
00110         fileStream_.close();
00111         ret = true;
00112     }
00113 
00114     umask(oldUmask); //Restore original umask
00115     return ret;
00116 }
00117 
00118 /* -------------------------------------------------------------------------
00119  */
00120 bool FesaStream::read(std::string& text)
00121 {
00122 
00123     // This method is used to read a file
00124     // Complete text is stored in the string parameter
00125     // true: means everything is ok
00126     // false: something is wrong: file does not exist ...
00127     std::ifstream infile(fileName_.c_str());
00128 
00129     if (infile.good())
00130     {
00131         std::string line;
00132         try
00133         {
00134             text.erase();
00135             infile.is_open();
00136             while (getline(infile, line))
00137             {
00138                 text.append(line);
00139                 line.erase();
00140                 if (!infile.eof())
00141                     text.append("\n");
00142             }
00143             return true;
00144         }
00145         catch (...)
00146         {
00147             return false;
00148         }
00149     }
00150     return false;
00151 }
00152 
00153 //instantiate or just retrieve a config stream object (one FesaStream per independent Fesa project).
00154 FesaStream* FesaStream::getConfigStream(const std::string streamName)
00155 {
00156     FesaStream* pFileStream = getFileStream(streamName + "Config");
00157     pFileStream->fileHeader_ = &configHeader_;
00158     pFileStream->fileFooter_ = &configFooter_;
00159     return pFileStream;
00160 }
00161 
00162 FesaStream* FesaStream::getStateStream(const std::string streamName)
00163 {
00164     FesaStream* pFileStream = getFileStream(streamName + "State");
00165     pFileStream->fileHeader_ = &stateHeader_;
00166     pFileStream->fileFooter_ = &stateFooter_;
00167     return pFileStream;
00168 }
00169 
00170 }
00171 

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1