logger.cc

Go to the documentation of this file.
00001 #include "config.h"
00002 
00003 #ifdef HAVE_UNISTD_H
00004 #include <unistd.h>
00005 #endif
00006 
00007 #include <iostream>
00008 #include <string>
00009 #include <fstream>
00010 
00011 #include "asserts.h"
00012 #include "error.h"
00013 #include "timer.h"
00014 #include "rconfig.h"
00015 #include "estring.h"
00016 
00017 #include "logger.h"
00018 
00019 //-----------------------------------------------------------------------------
00020 
00021 /** C'tor */
00022 log_manager::log_manager()
00023 {
00024         if (this != &logger)
00025                 throw(
00026                         INTERNAL_ERROR(0,"Attempt to allocate multiple log managers")
00027                         );
00028         clear();
00029 }
00030 
00031 /** Clear the log manager */
00032 void log_manager::clear(void)
00033 {
00034         m_new_line = true;
00035         if (m_out.is_open())
00036                 m_out.close();
00037         m_use_error_logging_level = false;
00038         m_initialized = false;
00039 }
00040 
00041 /** Initialize the log manager */
00042 void log_manager::init(void)
00043 {
00044         std::string es;
00045         std::string filename;
00046         bool done = false;
00047         int count = 0;
00048 
00049         done = false;
00050         while (!done) {
00051                 TRY_nomem(filename = config.log_dir());
00052                 TRY_nomem(filename += "/");
00053                 TRY_nomem(filename += config.timestamp().str());
00054                 if (config.action() == configuration_manager::action_archive) {
00055                         TRY_nomem(filename += ".log");
00056                 }
00057                 else if (config.action() == configuration_manager::action_relink) {
00058                         TRY_nomem(filename += ".relink");
00059                 }
00060                 if (count != 0) {
00061                         TRY_nomem(filename += ".");
00062                         TRY_nomem(filename += estring(count));
00063                 }
00064                 if (exists(filename))
00065                         ++count;
00066                 else
00067                         done = true;
00068         }
00069         m_out.open(filename.c_str());
00070         if (!m_out.is_open()) {
00071                 TRY_nomem(es = "Could not open log file: \"");
00072                 TRY_nomem(es += filename);
00073                 TRY_nomem(es += "\"");
00074                 throw(ERROR(errno,es));
00075         }
00076 
00077         m_use_error_logging_level = false;
00078 
00079         m_initialized = true;
00080 }
00081 
00082 /** The initialized state of the log manager */
00083 const bool log_manager::initialized(void) const
00084 {
00085         return(m_initialized);
00086 }
00087 
00088 /** Write a string to the log file */
00089 void log_manager::write(
00090         const std::string& a_str,
00091         const uint16 a_indention,
00092         const configuration_manager::logging_type a_logging_level,
00093         const pid_t a_pid
00094         )
00095 {
00096         std::string::const_iterator csi;
00097 
00098         if (!initialized())
00099                 throw(INTERNAL_ERROR(0,"Log manager is not initialized"));
00100 
00101         if (m_use_error_logging_level) {
00102                 if (a_logging_level > config.error_logging_level())
00103                         return;
00104         }
00105         else {
00106                 if (a_logging_level > config.logging_level())
00107                         return;
00108         }
00109         
00110         for (csi = a_str.begin(); csi != a_str.end(); ++csi) {
00111                 if (m_new_line) {
00112                         m_out << stamp(a_pid, a_indention);
00113                         m_new_line = false;
00114                 }
00115                 m_out << *csi;
00116                 if (*csi == '\n')
00117                         m_new_line = true;
00118         }
00119         m_out.flush();
00120 }
00121 
00122 /** Use error-logging-level instead of logging-level */
00123 void log_manager::set_error_logging(bool a_b)
00124 {
00125         m_use_error_logging_level = a_b;
00126 }
00127 
00128 //-----------------------------------------------------------------------------
00129 
00130 /** The global log manager */
00131 log_manager logger;
00132 
00133 //-----------------------------------------------------------------------------
00134 

Generated on Wed Jun 21 10:50:03 2006 for rvm by  doxygen 1.4.2