From 7c11cc1addc0a00e60884f4b3495815d9414c06a Mon Sep 17 00:00:00 2001 From: Rodolfo Granata Date: Thu, 4 Feb 2016 01:28:54 -0800 Subject: [PATCH] Add interface to trigger log-writers to flush their buffers. Summary: Add interface to trigger log-writers to flush their buffers. Reviewed By: markw65 Differential Revision: D2895713 fb-gh-sync-id: eeba9121a954665c522717efd23fb6e3fedc9dbe --- hphp/runtime/base/crash-reporter.cpp | 6 +++++- hphp/runtime/server/access-log.cpp | 6 ++++++ hphp/runtime/server/access-log.h | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hphp/runtime/base/crash-reporter.cpp b/hphp/runtime/base/crash-reporter.cpp index 8ce3fd84220..6817bc4d064 100644 --- a/hphp/runtime/base/crash-reporter.cpp +++ b/hphp/runtime/base/crash-reporter.cpp @@ -22,6 +22,7 @@ #include "hphp/runtime/base/execution-context.h" #include "hphp/runtime/ext/std/ext_std_errorfunc.h" #include "hphp/runtime/debugger/debugger.h" +#include "hphp/runtime/server/http-request-handler.h" #include "hphp/runtime/vm/ringbuffer-print.h" #include "hphp/runtime/vm/jit/mc-generator.h" #include "hphp/runtime/vm/jit/translator.h" @@ -45,7 +46,7 @@ static void bt_handler(int sig) { } } - // In case we crash again in the signal hander or something + // In case we crash again in the signal handler or something signal(sig, SIG_DFL); IsCrashing = true; @@ -121,6 +122,9 @@ static void bt_handler(int sig) { Logger::Error("Core dumped: %s", strsignal(sig)); Logger::Error("Stack trace in %s", RuntimeOption::StackTraceFilename.c_str()); + // Flush whatever access logs are still pending + HttpRequestHandler::GetAccessLog().flushAllWriters(); + // Give the debugger a chance to do extra logging if there are any attached // debugger clients. Eval::Debugger::LogShutdown(Eval::Debugger::ShutdownKind::Abnormal); diff --git a/hphp/runtime/server/access-log.cpp b/hphp/runtime/server/access-log.cpp index 6b7a1a14d46..543ab19c773 100644 --- a/hphp/runtime/server/access-log.cpp +++ b/hphp/runtime/server/access-log.cpp @@ -154,6 +154,12 @@ void AccessLog::onNewRequest() { threadData->startTime = TimeStamp::Current(); } +void AccessLog::flushAllWriters() { + if (!m_initialized) return; + m_defaultWriter->flush(); + for (auto& file : m_files) file->flush(); +} + bool AccessLog::setThreadLog(const char *file) { return (m_fGetThreadData()->log = fopen(file, "a")) != nullptr; } diff --git a/hphp/runtime/server/access-log.h b/hphp/runtime/server/access-log.h index 486c35ab37d..601a1b39f8d 100644 --- a/hphp/runtime/server/access-log.h +++ b/hphp/runtime/server/access-log.h @@ -78,8 +78,8 @@ public: bool setThreadLog(const char *file); void clearThreadLog(); void onNewRequest(); + void flushAllWriters(); private: - bool m_initialized; Mutex m_lock; GetThreadDataFunc m_fGetThreadData; @@ -97,6 +97,7 @@ public: virtual void init(const std::string& username, AccessLog::GetThreadDataFunc fn) = 0; virtual void write(Transport* transport, const VirtualHost* vhost) = 0; + virtual void flush() {} protected: const LogChannel m_channel; FILE* m_filelog{nullptr}; -- 2.11.4.GIT