FesaStream.cpp
Go to the documentation of this file.00001
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 }
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
00037
00038
00039
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
00054 pFileStream = (FesaStream *) ((*itr).second);
00055 }
00056 else
00057 {
00058 pFileStream = new FesaStream(streamName);
00059
00060 fileFactoryMap_[streamName] = pFileStream;
00061 }
00062
00063 return pFileStream;
00064 }
00065
00066
00067
00068 bool FesaStream::update()
00069 {
00070
00071
00072
00073
00074
00075
00076
00077 bool ret;
00078
00079
00080
00081 int32_t oldUmask = umask(0);
00082
00083 try
00084 {
00085 fileStream_.open(fileName_.c_str(), std::ios::out);
00086 }
00087 catch (...)
00088 {
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
00097 fileStream_.clear();
00098 ret = false;
00099 }
00100 else
00101 {
00102
00103 fileStream_ << *fileHeader_;
00104
00105
00106 fileStream_ << str();
00107
00108
00109 fileStream_ << *fileFooter_;
00110 fileStream_.close();
00111 ret = true;
00112 }
00113
00114 umask(oldUmask);
00115 return ret;
00116 }
00117
00118
00119
00120 bool FesaStream::read(std::string& text)
00121 {
00122
00123
00124
00125
00126
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
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