Fawkes API  Fawkes Development Version
access_log.cpp
1 
2 /***************************************************************************
3  * access_log.cpp - Web server access logger
4  *
5  * Created: Fr Feb 14 22:23:45 2014
6  * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include <webview/access_log.h>
23 #include <webview/request.h>
24 
25 #include <core/exception.h>
26 #include <core/threading/mutex.h>
27 #include <core/threading/mutex_locker.h>
28 
29 #include <cerrno>
30 #include <unistd.h>
31 #include <stdint.h>
32 #include <microhttpd.h>
33 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 /** @class WebviewAccessLog <webview/access_log.h>
40  * Webview access_log writer.
41  * This class can be used to create an access_log using the Apache
42  * common log format.
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor.
47  * @param filename log file name/path
48  */
50 {
51  logfile_ = fopen(filename, "a");
52  if (! logfile_) {
53  throw Exception(errno, "Failed to open access log %s", filename);
54  }
55  mutex_ = new Mutex();
56 }
57 
58 /** Destructor. */
60 {
61  fclose(logfile_);
62  delete mutex_;
63 }
64 
65 
66 /** Log a request.
67  * @param request request to log
68  */
69 void
71 {
72  MutexLocker lock(mutex_);
73  // Apache combined log:
74  //"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"
75  struct tm ltime;
76  time_t timesec = request->time().get_sec();
77  localtime_r(&timesec, &ltime);
78  char timestr[1024];
79  // [day/month/year:hour:minute:second zone]
80  strftime(timestr, sizeof(timestr), "[%d/%b/%Y:%H:%M:%S %z]", &ltime);
81  fprintf(logfile_, "%s - %s %s \"%s %s %s\" %i %zu \"%s\" \"%s\"\n",
82  request->client_addr().c_str(),
83  request->user().length() == 0 ? "-" : request->user().c_str(),
84  timestr,
85  request->method_str(), request->uri().c_str(), request->http_version_str(),
86  request->reply_code(), request->reply_size(),
87  request->has_header(MHD_HTTP_HEADER_REFERER)
88  ? request->header(MHD_HTTP_HEADER_REFERER).c_str() : "",
89  request->has_header(MHD_HTTP_HEADER_USER_AGENT)
90  ? request->header(MHD_HTTP_HEADER_USER_AGENT).c_str() : "");
91 
92  fflush(logfile_);
93 }
94 
95 } // end namespace fawkes
std::string header(std::string &key) const
Header specific header value.
Definition: request.h:180
const std::string & client_addr() const
Get client address as string.
Definition: request.h:95
Fawkes library namespace.
~WebviewAccessLog()
Destructor.
Definition: access_log.cpp:59
const Time & time() const
Get request time.
Definition: request.h:87
Mutex locking helper.
Definition: mutex_locker.h:33
WebviewAccessLog(const char *filename)
Constructor.
Definition: access_log.cpp:49
size_t reply_size() const
Get number of bytes actually sent out so far.
Definition: request.cpp:205
const std::string & uri() const
Get URI.
Definition: request.h:73
bool has_header(std::string key) const
Check if the named header value has been received.
Definition: request.h:198
Base class for exceptions in Fawkes.
Definition: exception.h:36
WebReply::Code reply_code() const
Get HTTP code of reply.
Definition: request.cpp:259
const char * http_version_str() const
Get HTTP version as string.
Definition: request.cpp:235
Web request meta data carrier.
Definition: request.h:42
const char * method_str() const
Get method as string.
Definition: request.cpp:215
long get_sec() const
Get seconds.
Definition: time.h:110
Mutex mutual exclusion lock.
Definition: mutex.h:32
const std::string & user() const
Get name of authenticated user (basic auth).
Definition: request.h:91
void log(const WebRequest *request)
Log a request.
Definition: access_log.cpp:70