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 <core/exception.h>
23#include <core/threading/mutex.h>
24#include <core/threading/mutex_locker.h>
25#include <webview/access_log.h>
26#include <webview/request.h>
27
28#include <cerrno>
29#include <microhttpd.h>
30#include <stdint.h>
31#include <unistd.h>
32
33namespace fawkes {
34
35/** @class WebviewAccessLog <webview/access_log.h>
36 * Webview access_log writer.
37 * This class can be used to create an access_log using the Apache
38 * common log format.
39 * @author Tim Niemueller
40 */
41
42/** Constructor.
43 * @param filename log file name/path
44 */
46{
47 logfile_ = fopen(filename, "a");
48 if (!logfile_) {
49 throw Exception(errno, "Failed to open access log %s", filename);
50 }
51 mutex_ = new Mutex();
52}
53
54/** Destructor. */
56{
57 fclose(logfile_);
58 delete mutex_;
59}
60
61/** Log a request.
62 * @param request request to log
63 */
64void
66{
67 MutexLocker lock(mutex_);
68 // Apache combined log:
69 //"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"
70 struct tm ltime;
71 time_t timesec = request->time().get_sec();
72 localtime_r(&timesec, &ltime);
73 char timestr[1024];
74 // [day/month/year:hour:minute:second zone]
75 strftime(timestr, sizeof(timestr), "[%d/%b/%Y:%H:%M:%S %z]", &ltime);
76 fprintf(logfile_,
77 "%s - %s %s \"%s %s %s\" %i %zu \"%s\" \"%s\"\n",
78 request->client_addr().c_str(),
79 request->user().length() == 0 ? "-" : request->user().c_str(),
80 timestr,
81 request->method_str(),
82 request->uri().c_str(),
83 request->http_version_str(),
84 request->reply_code(),
85 request->reply_size(),
86 request->has_header(MHD_HTTP_HEADER_REFERER)
87 ? request->header(MHD_HTTP_HEADER_REFERER).c_str()
88 : "",
89 request->has_header(MHD_HTTP_HEADER_USER_AGENT)
90 ? request->header(MHD_HTTP_HEADER_USER_AGENT).c_str()
91 : "");
92
93 fflush(logfile_);
94}
95
96} // end namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex locking helper.
Definition: mutex_locker.h:34
Mutex mutual exclusion lock.
Definition: mutex.h:33
long get_sec() const
Get seconds.
Definition: time.h:117
Web request meta data carrier.
Definition: request.h:42
const char * http_version_str() const
Get HTTP version as string.
Definition: request.cpp:269
bool has_header(std::string key) const
Check if the named header value has been received.
Definition: request.h:256
const char * method_str() const
Get method as string.
Definition: request.cpp:250
std::string header(std::string &key) const
Header specific header value.
Definition: request.h:236
const std::string & client_addr() const
Get client address as string.
Definition: request.h:118
size_t reply_size() const
Get number of bytes actually sent out so far.
Definition: request.cpp:241
const Time & time() const
Get request time.
Definition: request.h:102
const std::string & uri() const
Get URI.
Definition: request.h:76
const std::string & user() const
Get name of authenticated user (basic auth).
Definition: request.h:110
WebReply::Code reply_code() const
Get HTTP code of reply.
Definition: request.cpp:291
void log(const WebRequest *request)
Log a request.
Definition: access_log.cpp:65
WebviewAccessLog(const char *filename)
Constructor.
Definition: access_log.cpp:45
~WebviewAccessLog()
Destructor.
Definition: access_log.cpp:55
Fawkes library namespace.