2 suPHP - (c)2002-2005 Sebastian Marsching <sebastian@marsching.com>
4 This file is part of suPHP.
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.
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.
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
23 #include <sys/types.h>
30 #include "API_Linux_Logger.hpp"
32 using namespace suPHP
;
34 suPHP::API_Linux_Logger::API_Linux_Logger() {
39 void suPHP::API_Linux_Logger::log(const std::string
& classification
,
40 const std::string
& message
) {
43 char timestr
[64] = {0};
47 now
= ::localtime(&ts
);
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
);
54 logline
= std::string(timestr
) + " [" + classification
+ "] "
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
;
69 void suPHP::API_Linux_Logger::init(const Configuration
& config
)
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
,
76 // Check wheter something failed
77 if (this->logFd
== -1) {
78 throw IOException("Could not open logfile " + config
.getLogfile(),
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",
90 // Get log level from configuration
91 this->setLogLevel(config
.getLogLevel());
93 // We got here, so nothing failed
97 bool suPHP::API_Linux_Logger::isInitialized() {