Create dedicate crate for php_escaping.rs
[hiphop-php.git] / hphp / runtime / server / access-log.h
blob1cc1d4c0a792d9c6f5e6e7f22386fe7bbdae31ca
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_ACCESS_LOG_H_
17 #define incl_HPHP_ACCESS_LOG_H_
19 #include "hphp/runtime/ext/extension.h"
20 #include "hphp/runtime/server/transport.h"
21 #include "hphp/runtime/server/virtual-host.h"
22 #include "hphp/util/thread-local.h"
23 #include "hphp/util/logger.h"
24 #include "hphp/util/lock.h"
25 #include "hphp/util/cronolog.h"
26 #include "hphp/util/log-file-flusher.h"
29 namespace HPHP {
30 ///////////////////////////////////////////////////////////////////////////////
31 struct LogWriter;
32 enum class LogChannel {CRONOLOG, THREADLOCAL, REGULAR};
34 struct AccessLogFileData {
35 AccessLogFileData() {}
36 AccessLogFileData(const std::string& fil,
37 const std::string& lnk,
38 const std::string& fmt,
39 int mpl);
40 std::string file;
41 std::string symLink;
42 std::string format;
43 int periodMultiplier;
44 // Concrete LogWriter factories to be used depending on the handle
45 using factory_t = std::function<
46 std::unique_ptr<LogWriter>(const AccessLogFileData&, LogChannel)>;
47 // Ask a registered factory for a new LogWriter instance
48 std::unique_ptr<LogWriter> Writer(LogChannel chan) const;
49 static void registerWriter(const std::string& handle, factory_t factory);
50 private:
51 std::string m_logOutputType;
52 static Mutex m_lock;
53 static std::unordered_map<std::string, factory_t> m_factories;
57 struct AccessLog {
58 struct ThreadData {
59 ThreadData() : log(nullptr) {}
60 FILE *log;
61 int64_t startTime;
62 LogFileFlusher flusher;
64 using GetThreadDataFunc = ThreadData* (*)();
65 explicit AccessLog(GetThreadDataFunc f) :
66 m_initialized(false), m_fGetThreadData(f) {}
67 ~AccessLog();
68 void init(const std::string &defaultFormat,
69 std::vector<AccessLogFileData> &files,
70 const std::string &username);
71 void init(const std::string &defaultFormat,
72 std::map<std::string, AccessLogFileData> &files,
73 const std::string &username);
74 void init(const std::string &format, const std::string &symLink,
75 const std::string &file, const std::string &username);
76 void log(Transport *transport, const VirtualHost *vhost);
77 bool setThreadLog(const char *file);
78 void clearThreadLog();
79 void onNewRequest();
80 void flushAllWriters();
81 private:
82 bool m_initialized;
83 Mutex m_lock;
84 GetThreadDataFunc m_fGetThreadData;
85 std::unique_ptr<LogWriter> m_defaultWriter;
86 std::vector<std::shared_ptr<LogWriter>> m_files;
90 struct LogWriter {
91 explicit LogWriter(LogChannel chan)
92 : m_channel(chan)
94 virtual ~LogWriter() {};
95 virtual void init(const std::string& username,
96 AccessLog::GetThreadDataFunc fn) = 0;
97 virtual void write(Transport* transport, const VirtualHost* vhost) = 0;
98 virtual void flush() {}
99 protected:
100 const LogChannel m_channel;
101 FILE* m_filelog{nullptr};
102 std::unique_ptr<Cronolog> m_cronolog;
103 AccessLog::GetThreadDataFunc m_threadDataFn{nullptr};
104 LogFileFlusher m_flusher;
105 protected:
106 FILE* getOutputFile() const;
107 void recordWriteAndMaybeDropCaches(FILE* out, int bytes);
110 ///////////////////////////////////////////////////////////////////////////////
113 #endif // incl_HPHP_ACCESS_LOG_H_