Enforce coding style

This commit is contained in:
Jean Flach 2015-02-20 14:14:34 +01:00
parent 7cedb381b8
commit e8e6b187b7
2 changed files with 38 additions and 34 deletions

View File

@ -53,66 +53,68 @@ String TroubleshootCollectCommand::GetShortDescription(void) const
class TroubleshootCollectCommand::InfoLog class TroubleshootCollectCommand::InfoLog
{ {
bool console;
std::ostream *os;
public: public:
InfoLog(const String& path, const bool cons) InfoLog(const String& path, const bool cons)
{ {
console = cons; m_Console = cons;
if (console) { if (m_Console)
os = new std::ostream(std::cout.rdbuf()); m_Stream = new std::ostream(std::cout.rdbuf());
} else { else {
std::ofstream *ofs = new std::ofstream(); std::ofstream *ofs = new std::ofstream();
ofs->open(path.CStr(), std::ios::out | std::ios::trunc); ofs->open(path.CStr(), std::ios::out | std::ios::trunc);
os = ofs; m_Stream = ofs;
} }
}; }
void logLine(const LogSeverity sev, const String& str) void WriteLine(const LogSeverity sev, const String& str)
{ {
if (!console) if (!m_Console)
Log(sev, "troubleshoot", str); Log(sev, "troubleshoot", str);
if (sev == LogCritical || sev == LogWarning) { if (sev == LogCritical || sev == LogWarning) {
*os << std::string(24, '#') << '\n' *m_Stream << std::string(24, '#') << '\n'
<< "# " << str << '\n' << "# " << str << '\n'
<< std::string(24, '#') << '\n'; << std::string(24, '#') << '\n';
} else } else
*os << str << '\n'; *m_Stream << str << '\n';
} }
bool GetStreamHealth() bool GetStreamHealth(void) const
{ {
return *os; return *m_Stream;
} }
private:
bool m_Console;
std::ostream *m_Stream;
}; };
class TroubleshootCollectCommand::InfoLogLine class TroubleshootCollectCommand::InfoLogLine
{ {
public: public:
InfoLogLine(InfoLog& log, LogSeverity sev = LogInformation) InfoLogLine(InfoLog& log, LogSeverity sev = LogInformation)
: log(log), sev(sev) {} : m_Log(log), m_Sev(sev) {}
~InfoLogLine() ~InfoLogLine()
{ {
log.logLine(sev, os.str()); m_Log.WriteLine(m_Sev, m_String.str());
} }
template <typename T> template <typename T>
InfoLogLine& operator<<(const T& info) InfoLogLine& operator<<(const T& info)
{ {
os << info; m_String << info;
return *this; return *this;
} }
private: private:
std::ostringstream os; std::ostringstream m_String;
InfoLog& log; InfoLog& m_Log;
LogSeverity sev; LogSeverity m_Sev;
}; };
bool TroubleshootCollectCommand::GeneralInfo(InfoLog& log, boost::program_options::variables_map vm) bool TroubleshootCollectCommand::GeneralInfo(InfoLog& log, const boost::program_options::variables_map& vm)
{ {
InfoLogLine(log) << '\n' << std::string(14, '=') << " GENERAL INFORMATION " << std::string(14, '=') << '\n'; InfoLogLine(log) << '\n' << std::string(14, '=') << " GENERAL INFORMATION " << std::string(14, '=') << '\n';
@ -133,14 +135,14 @@ bool TroubleshootCollectCommand::GeneralInfo(InfoLog& log, boost::program_option
return true; return true;
} }
bool TroubleshootCollectCommand::FeatureInfo(InfoLog& log, boost::program_options::variables_map vm) bool TroubleshootCollectCommand::FeatureInfo(InfoLog& log, const boost::program_options::variables_map& vm)
{ {
TroubleshootCollectCommand::CheckFeatures(log); TroubleshootCollectCommand::CheckFeatures(log);
//TODO Check whether active faetures are operational. //TODO Check whether active faetures are operational.
return true; return true;
} }
bool TroubleshootCollectCommand::ObjectInfo(InfoLog& log, boost::program_options::variables_map vm, Dictionary::Ptr& logs) bool TroubleshootCollectCommand::ObjectInfo(InfoLog& log, const boost::program_options::variables_map& vm, Dictionary::Ptr& logs)
{ {
InfoLogLine(log) << '\n' << std::string(14, '=') << " OBJECT INFORMATION " << std::string(14, '=') << '\n'; InfoLogLine(log) << '\n' << std::string(14, '=') << " OBJECT INFORMATION " << std::string(14, '=') << '\n';
@ -157,7 +159,7 @@ bool TroubleshootCollectCommand::ObjectInfo(InfoLog& log, boost::program_options
return true; return true;
} }
bool TroubleshootCollectCommand::ReportInfo(InfoLog& log, boost::program_options::variables_map vm, Dictionary::Ptr& logs) bool TroubleshootCollectCommand::ReportInfo(InfoLog& log, const boost::program_options::variables_map& vm, Dictionary::Ptr& logs)
{ {
InfoLogLine(log) << '\n' << std::string(14, '=') << " LOGS AND CRASH REPORTS " << std::string(14, '=') << '\n'; InfoLogLine(log) << '\n' << std::string(14, '=') << " LOGS AND CRASH REPORTS " << std::string(14, '=') << '\n';
PrintLoggers(log, logs); PrintLoggers(log, logs);
@ -166,7 +168,7 @@ bool TroubleshootCollectCommand::ReportInfo(InfoLog& log, boost::program_options
return true; return true;
} }
bool TroubleshootCollectCommand::ConfigInfo(InfoLog& log, boost::program_options::variables_map vm) bool TroubleshootCollectCommand::ConfigInfo(InfoLog& log, const boost::program_options::variables_map& vm)
{ {
InfoLogLine(log) << '\n' << std::string(14, '=') << " CONFIGURATION FILES " << std::string(14, '=') << '\n'; InfoLogLine(log) << '\n' << std::string(14, '=') << " CONFIGURATION FILES " << std::string(14, '=') << '\n';
@ -187,7 +189,7 @@ bool TroubleshootCollectCommand::ConfigInfo(InfoLog& log, boost::program_options
/*Print the last *numLines* of *file* to *os* */ /*Print the last *numLines* of *file* to *os* */
int TroubleshootCollectCommand::tail(const String& file, int numLines, InfoLog& log) int TroubleshootCollectCommand::Tail(const String& file, int numLines, InfoLog& log)
{ {
boost::circular_buffer<std::string> ringBuf(numLines); boost::circular_buffer<std::string> ringBuf(numLines);
std::ifstream text; std::ifstream text;
@ -297,7 +299,7 @@ bool TroubleshootCollectCommand::PrintCrashReports(InfoLog& log)
else { else {
InfoLogLine(log) << "Latest crash report is from " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", Utility::GetTime()) InfoLogLine(log) << "Latest crash report is from " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", Utility::GetTime())
<< "\nFile: " << bestFilename; << "\nFile: " << bestFilename;
tail(bestFilename, 20, log); Tail(bestFilename, 20, log);
} }
return true; return true;
} }
@ -413,7 +415,7 @@ void TroubleshootCollectCommand::PrintLoggers(InfoLog& log, Dictionary::Ptr& log
BOOST_FOREACH(const Dictionary::Pair& kv, logs) BOOST_FOREACH(const Dictionary::Pair& kv, logs)
{ {
InfoLogLine(log) << "\nLogger " << kv.first << " at path: " << kv.second; InfoLogLine(log) << "\nLogger " << kv.first << " at path: " << kv.second;
if (!tail(kv.second, 20, log)) if (!Tail(kv.second, 20, log))
InfoLogLine(log, LogWarning) << kv.second << " either does not exist or is empty"; InfoLogLine(log, LogWarning) << kv.second << " either does not exist or is empty";
} }
} }

View File

@ -45,13 +45,15 @@ namespace icinga
private: private:
class InfoLog; class InfoLog;
class InfoLogLine; class InfoLogLine;
static bool GeneralInfo(InfoLog& log, boost::program_options::variables_map vm); static bool GeneralInfo(InfoLog& log, const boost::program_options::variables_map& vm);
static bool FeatureInfo(InfoLog& log, boost::program_options::variables_map vm); static bool FeatureInfo(InfoLog& log, const boost::program_options::variables_map& vm);
static bool ObjectInfo(InfoLog& log, boost::program_options::variables_map vm, Dictionary::Ptr& logs); static bool ObjectInfo(InfoLog& log, const boost::program_options::variables_map& vm,
static bool ReportInfo(InfoLog& log, boost::program_options::variables_map vm, Dictionary::Ptr& logs); Dictionary::Ptr& logs);
static bool ConfigInfo(InfoLog& log, boost::program_options::variables_map vm); static bool ReportInfo(InfoLog& log, const boost::program_options::variables_map& vm,
Dictionary::Ptr& logs);
static bool ConfigInfo(InfoLog& log, const boost::program_options::variables_map& vm);
static int tail(const String& file, int numLines, InfoLog& log); static int Tail(const String& file, const int numLines, InfoLog& log);
static bool CheckFeatures(InfoLog& log); static bool CheckFeatures(InfoLog& log);
static void GetLatestReport(const String& filename, time_t& bestTimestamp, String& bestFilename); static void GetLatestReport(const String& filename, time_t& bestTimestamp, String& bestFilename);
static bool PrintCrashReports(InfoLog& log); static bool PrintCrashReports(InfoLog& log);