2 +----------------------------------------------------------------------+
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"
30 ///////////////////////////////////////////////////////////////////////////////
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
,
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
);
51 std::string m_logOutputType
;
53 static std::unordered_map
<std::string
, factory_t
> m_factories
;
59 ThreadData() : log(nullptr) {}
62 LogFileFlusher flusher
;
64 using GetThreadDataFunc
= ThreadData
* (*)();
65 explicit AccessLog(GetThreadDataFunc f
) :
66 m_initialized(false), m_fGetThreadData(f
) {}
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();
80 void flushAllWriters();
84 GetThreadDataFunc m_fGetThreadData
;
85 std::unique_ptr
<LogWriter
> m_defaultWriter
;
86 std::vector
<std::shared_ptr
<LogWriter
>> m_files
;
91 explicit LogWriter(LogChannel 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() {}
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
;
106 FILE* getOutputFile() const;
107 void recordWriteAndMaybeDropCaches(FILE* out
, int bytes
);
110 ///////////////////////////////////////////////////////////////////////////////
113 #endif // incl_HPHP_ACCESS_LOG_H_