00001 // 00002 // logfile.cc - the definition of the LogFile class 00003 // 00004 // ***** BEGIN LICENSE BLOCK ***** 00005 // Version: MPL 1.1/GPL 2.0/LGPL 2.1 00006 // 00007 // The contents of this file are subject to the Mozilla Public License 00008 // Version 1.1 (the "License"); you may not use this file except in 00009 // compliance with the License. You may obtain a copy of the License at 00010 // http://www.mozilla.org/MPL/ 00011 // 00012 // Software distributed under the License is distributed on an "AS IS" 00013 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00014 // the License for the specific language governing rights and limitations 00015 // under the License. 00016 // 00017 // The Original Code is the consmgr network/serial-line monitoring package. 00018 // 00019 // The Initial Developer of the Original Code is Chris P. Ross. 00020 // Portions created by the Initial Developer are Copyright (C) 2000-2008 00021 // the Initial Developer. All Rights Reserved. 00022 // 00023 // Contributor(s): 00024 // 00025 // Alternatively, the contents of this file may be used under the terms of 00026 // either the GNU General Public License Version 2 or later (the "GPL"), or 00027 // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 00028 // in which case the provisions of the GPL or the LGPL are applicable instead 00029 // of those above. If you wish to allow use of your version of this file only 00030 // under the terms of either the GPL or the LGPL, and not to allow others to 00031 // use your version of this file under the terms of the MPL, indicate your 00032 // decision by deleting the provisions above and replace them with the notice 00033 // and other provisions required by the GPL or the LGPL. If you do not delete 00034 // the provisions above, a recipient may use your version of this file under 00035 // the terms of any one of the MPL, the GPL or the LGPL. 00036 // 00037 // ***** END LICENSE BLOCK ***** 00038 00039 extern "C" { 00040 #include <sys/types.h> 00041 #include <fcntl.h> 00042 #include <unistd.h> 00043 } 00044 00045 #include <sstream> 00046 00047 #include "consmgr.h" 00048 #include "logfile.h" 00049 00050 LogFile::LogFile(string fn, mode_t m) 00051 : FDConnector(), path(fn), mode(m) 00052 { 00053 // Set up the connection 00054 connect(); 00055 } 00056 00057 // Identification routines... 00058 string 00059 LogFile::get_id(void) const 00060 { 00061 std::ostringstream id_str; 00062 00063 id_str << "LogFile(" << path << ")"; 00064 00065 return(id_str.str()); 00066 // XXX - Isn't this string deallocated when we return? 00067 } 00068 00069 // Actually connect to the path specified... 00070 void 00071 LogFile::connect() 00072 { 00073 int fd; 00074 00075 // Try to open the file... We'll use libc calls here, rather 00076 // than C++ fstreams, because most of this LogFile object operates 00077 // on raw fd's, not on iostreams. 00078 fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_APPEND, mode); 00079 if (fd < 0) 00080 throw(fireball(21, "Ack. Failed to open output logfile!")); 00081 00082 // Connect up the FDConnector super-class 00083 FDConnector::connect(fd); 00084 00085 // Now, write a note about the logfile being opened... 00086 std::ostringstream logmsg; 00087 00088 logmsg << std::endl << "--- Logfile opened by " << __progname 00089 << "(" << ::getpid() << ") on "; 00090 00091 time_t now = time(NULL); 00092 char buf[26]; 00093 std::string nowstr(ctime_r(&now, buf)); 00094 string::size_type p; 00095 00096 // Remove double-spaces and the terminating EOL in the output string 00097 // XXX - damn ctime_r(). Maybe strftime() would be better? 00098 while ((p = nowstr.find(" ")) != string::npos) 00099 nowstr.replace(p, 2, " "); 00100 logmsg << nowstr.erase(nowstr.find("\n")) << " ---" << std::endl; 00101 00102 Data d(logmsg.str()); 00103 write(d); 00104 00105 return; 00106 } 00107 00109 void 00110 LogFile::disconnect(void) 00111 { 00112 if (connected()) { 00113 std::ostringstream logmsg; 00114 00115 logmsg << std::endl << "--- Logfile closed at "; 00116 00117 time_t now = time(NULL); 00118 char buf[26]; 00119 string nowstr(ctime_r(&now, buf)); 00120 string::size_type p; 00121 00122 // Remove double-spaces in the output string 00123 while ((p = nowstr.find(" ")) != string::npos) 00124 nowstr.replace(p, 2, " "); 00125 logmsg << nowstr.erase(nowstr.find("\n")) << " ---" << std::endl; 00126 00127 Data d(logmsg.str()); 00128 write(d); 00129 } 00130 00131 return; 00132 }