DebugLogs are now being redirected to the correct files

This commit is contained in:
Felipe Zimmerle
2015-08-27 01:27:34 -03:00
parent 01542e28c3
commit 24b7d72666
14 changed files with 330 additions and 177 deletions

View File

@@ -19,122 +19,60 @@
#include <fstream>
#include "src/debug_log_writer.h"
#include "src/debug_log_writer_agent.h"
namespace ModSecurity {
/**
* @name new_instance
* @brief Create a new instance of the DebugLog.
*
* @return Debug log pointer
* @retval >0 Debug log structure was initialized correctly
* @retval NULL Debug log could not be initialized.
*
*/
DebugLog *DebugLog::new_instance() {
return new DebugLog();
DebugLog::~DebugLog() {
DebugLogWriter::getInstance().close(m_fileName);
}
/**
* @name setOutputFile
* @brief Set an output file where the log will be saved
*
* @param file_path Path to the log file.
*
* @return If the operation successful or not.
* @retval true Operation was successful.
* @retval false Operation failed.
*
*/
bool DebugLog::setOutputFile(const std::string& file_path) {
if (is_open()) {
close();
void DebugLog::setDebugLogFile(const std::string& fileName) {
m_fileName = fileName;
if (isLogFileSet()) {
DebugLogWriter::getInstance().close(m_fileName);
}
open(file_path, std::fstream::out | std::fstream::app);
if (!is_open()) {
return false;
}
return true;
DebugLogWriter::getInstance().open(m_fileName);
}
/**
* @name write_log
* @brief Write a message into the debug log.
*
* @param debug_level Debug level of the given message.
* @param text Message to be written.
*
* @return If the operation successful or not.
* @retval true Operation was successful.
* @retval false Operation failed.
*
*/
bool DebugLog::write_log(int debug_level, const std::string &text) {
std::cout << "?" << std::to_string(is_open()) << ":" << std::to_string(m_debug_level) <<" [" << debug_level << "] " << text << std::endl;
if (!is_open()) {
return false;
}
if (debug_level <= m_debug_level) {
*this << "[" << debug_level << "] " << text << std::endl;
}
return true;
void DebugLog::setDebugLogLevel(int level) {
m_debugLevel = level;
}
/**
* @name setDebugLevel
* @brief Changes the default debug level.
*
* @param level Debug level.
*
* @return If the operation successful or not.
* @retval true Operation was successful.
* @retval false Operation failed.
*
*/
bool DebugLog::setDebugLevel(int level) {
if (level < 0 || level > 9) {
return false;
bool DebugLog::isLogFileSet() {
return m_fileName.empty() == false;
}
bool DebugLog::isLogLevelSet() {
return m_debugLevel != -1;
}
const std::string& DebugLog::getDebugLogFile() {
return m_fileName;
}
int DebugLog::getDebugLogLevel() {
if (m_debugLevel < 0) {
return 0;
}
m_debug_level = level;
return true;
return m_debugLevel;
}
/**
* @name isConfigured
* @brief Returns if debug log is configured or not.
*
* @return If the debug log is configured or not
* @retval true It is configured.
* @retval false It is not configured.
*
*/
bool DebugLog::isConfigured() {
return m_is_configured;
}
void DebugLog::refCountDecreaseAndCheck(void) {
this->m_referenceCount--;
if (this->m_referenceCount == 0) {
delete this;
void DebugLog::write(int level, const std::string &msg) {
if (level <= m_debugLevel) {
DebugLogWriter::getInstance().write(m_fileName, "[" + std::to_string(level) + "] " + msg);
}
}
void DebugLog::refCountIncrease(void) {
this->m_referenceCount++;
}
} // namespace ModSecurity
} // namespace ModSecurity