| 1 | /* |
| 2 | suPHP - (c)2002-2005 Sebastian Marsching <sebastian@marsching.com> |
| 3 | |
| 4 | This file is part of suPHP. |
| 5 | |
| 6 | suPHP is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | suPHP is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with suPHP; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <iostream> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "API_Linux_Logger.hpp" |
| 31 | |
| 32 | using namespace suPHP; |
| 33 | |
| 34 | suPHP::API_Linux_Logger::API_Linux_Logger() { |
| 35 | this->ready = false; |
| 36 | this->logFd = -1; |
| 37 | } |
| 38 | |
| 39 | void suPHP::API_Linux_Logger::log(const std::string& classification, |
| 40 | const std::string& message) { |
| 41 | ::time_t ts; |
| 42 | struct ::tm *now; |
| 43 | char timestr[64] = {0}; |
| 44 | std::string logline; |
| 45 | |
| 46 | ts = ::time(NULL); |
| 47 | now = ::localtime(&ts); |
| 48 | |
| 49 | // Do not check for error when running strftime - |
| 50 | // we couldn't handle it anyway :-) |
| 51 | ::strftime(timestr, 64, "[%a %b %d %H:%M:%S %Y]", now); |
| 52 | |
| 53 | // Construct logline |
| 54 | logline = std::string(timestr) + " [" + classification + "] " |
| 55 | + message + "\n"; |
| 56 | |
| 57 | // Check wheter we have an uninitialized logfile |
| 58 | // or write to the logfile failed |
| 59 | if (!this->isInitialized() |
| 60 | || (write(this->logFd, logline.c_str(), logline.size()) == -1)) { |
| 61 | // Print message to stderr |
| 62 | std::cerr << "Could not write to logfile:" << std::endl |
| 63 | // << ::strerror(::errno) << std::endl |
| 64 | << "Printing message to stderr:" << std::endl |
| 65 | << logline << std::endl; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void suPHP::API_Linux_Logger::init(const Configuration& config) |
| 70 | throw (IOException) { |
| 71 | // Open logfile in append mode, create if not existing. |
| 72 | this->logFd = ::open(config.getLogfile().c_str(), |
| 73 | O_WRONLY|O_CREAT|O_APPEND|O_NOCTTY, |
| 74 | S_IRUSR|S_IWUSR); |
| 75 | |
| 76 | // Check wheter something failed |
| 77 | if (this->logFd == -1) { |
| 78 | throw IOException("Could not open logfile " + config.getLogfile(), |
| 79 | __FILE__, __LINE__); |
| 80 | } |
| 81 | |
| 82 | // Set close-on-exec flag, because we do not want |
| 83 | // the user to write to our logfile |
| 84 | if (::fcntl(this->logFd, F_SETFD, FD_CLOEXEC)) { |
| 85 | // Ooops, something went wrong |
| 86 | throw IOException("Could not set close-on-exec flag on logfile", |
| 87 | __FILE__, __LINE__); |
| 88 | } |
| 89 | |
| 90 | // Get log level from configuration |
| 91 | this->setLogLevel(config.getLogLevel()); |
| 92 | |
| 93 | // We got here, so nothing failed |
| 94 | this->ready = true; |
| 95 | } |
| 96 | |
| 97 | bool suPHP::API_Linux_Logger::isInitialized() { |
| 98 | return this->ready; |
| 99 | } |