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
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
00032 void log_manager::clear(void)
00033 {
00034 m_new_line = true;
00035 if (m_out.is_open())
00036 m_out.close();
00037 m_initialized = false;
00038 }
00039
00040
00041 void log_manager::init(void)
00042 {
00043 std::string es;
00044 std::string filename;
00045 bool done = false;
00046 int count = 0;
00047
00048 done = false;
00049 while (!done) {
00050 TRY_nomem(filename = config.log_dir());
00051 TRY_nomem(filename += "/");
00052 TRY_nomem(filename += config.timestamp().str());
00053 if (config.action() == configuration_manager::action_archive) {
00054 TRY_nomem(filename += ".log");
00055 }
00056 else if (config.action() == configuration_manager::action_relink) {
00057 TRY_nomem(filename += ".relink");
00058 }
00059 if (count != 0) {
00060 TRY_nomem(filename += ".");
00061 TRY_nomem(filename += estring(count));
00062 }
00063 if (exists(filename))
00064 ++count;
00065 else
00066 done = true;
00067 }
00068 m_out.open(filename.c_str());
00069 if (!m_out.is_open()) {
00070 TRY_nomem(es = "Could not open log file: \"");
00071 TRY_nomem(es += filename);
00072 TRY_nomem(es += "\"");
00073 throw(ERROR(errno,es));
00074 }
00075
00076 m_initialized = true;
00077 }
00078
00079
00080 const bool log_manager::initialized(void) const
00081 {
00082 return(m_initialized);
00083 }
00084
00085
00086 void log_manager::write(
00087 const std::string& a_str,
00088 const uint16 a_indention,
00089 const configuration_manager::logging_type a_logging_level,
00090 const pid_t a_pid
00091 )
00092 {
00093 std::string::const_iterator csi;
00094
00095 if (!initialized())
00096 throw(INTERNAL_ERROR(0,"Log manager is not initialized"));
00097
00098 if (a_logging_level > config.logging_level())
00099 return;
00100
00101 for (csi = a_str.begin(); csi != a_str.end(); ++csi) {
00102 if (m_new_line) {
00103 m_out << stamp(a_pid, a_indention);
00104 m_new_line = false;
00105 }
00106 m_out << *csi;
00107 if (*csi == '\n')
00108 m_new_line = true;
00109 }
00110 m_out.flush();
00111 }
00112
00113
00114
00115
00116 log_manager logger;
00117
00118
00119