Squashed 'third_party/google-glog/' changes from 5d46e1bcf..a1b6164ef

a1b6164ef windows: call to FormatMessage() should _IGNORE_INSERTS (#849)
278ed9689 simplify DLOG macro definition in NDEBUG mode (#830)
6ed0c9e58 added emscripten support (#846)
a34226ca9 support ndk r25 (#844)
c515e1ae2 remove unused STDC_HEADERS definition (#835)
79d692c64 Strip only the first leading @
086ce224d bazel: Use glog 0.6.0 in README.
b33e3bad4 Merge pull request #812 from google/release-0.6
54b8bcbbf release 0.6
864ef6492 Merge pull request #801 from skeptic-monkey/master
c640c2f7c Fix namespace resolution issue in LOG_EVERY_T
5addeedc0 Fix "'GLOG_EXPORT' macro redefined" on clang-cl
d153e294b Avoid "GLOG_EXPORT macro redefined" in clang-cl CI
c4ca519a8 Add CI job for clang-cl on Windows
a8e0007e9 Merge pull request #790 from git-hulk/feature/logs-to-stdout
553ddaea8 Merge pull request #794 from Arfrever/tests-TMPDIR
6d3f70a40 Pass EXIT_SUCCESS or EXIT_FAILURE to exit() and _exit()
612a7d28c Tests: Respect TEST_TMPDIR, TMPDIR, TMP environmental variables
180b700dd Add logtostdout and colorlogtostdout flags for logging to stdout
b38ed9d08 cmake_package_config_generate: Fix CMake warnings with CMake 3.23
9f0b7d3bf renamed GOOGLE_GLOG_DLL_DECL to GLOG_EXPORT
aa94e6b9a simplify flags export
17269d649 cmake: enable custom prefix by default
bc1fada1c libglog.pc: Set Libs.private for static linking
18836106d LogCleaner: avoid scanning logs too frequently
b0174b3dd On Solaris, uname() returns non-negative value
696941218 Changed my Email ID
643441014 cmake: require at least gflags 2.2.2
a8cfbe0c0 stdcxx: eliminate excessive use of std::string::c_str()
b3abfaa12 use uint32 for --max-log-size
43fc3bf91 Add log_year_in_prefix flag
42d509b1c revert to int atomics
224f66bb6 cmake: do not search for platform specific unwind
4ffa98388 ensure _Unwind_Backtrace to actually be available
087ad5bf3 eliminated clang warnings
9dc1107f8 run clang-tidy
dfb9ffc32 provide backward compatible send overload
c57511605 ci: removed deprecated msvc runner
d09b999f3 LogCleaner: make overdue_days_ unsigned int
baa7006e6 ci: enable warnings as errors
660352f02 cmake: require at least version 3.16 (fixes #705)
52354b2ad eliminate msvc warnings
ccbda2dfd LogCleaner: Fix relative paths and add a new test
ef36f800e Added an API to get GMT offset from LogMessageTime     * Added API to get GMT offset     * Made LogMessageTime as a memeber of LogMessage     * Refactored LogSink::send() method
ee6faf13b ci: use codecov token
6e9b2f16f cmake: set CMP0074 policy
f4dd77ae6 ci: reworked windows builds
503e3dec8 Add Zhuoran Shen to AUTHORS and CONTRIBUTORS
531659984 Fix log filename format
cd8fbd5fb ci: prefix artifact directories
c8a7f5abe ci: added checkout action
f47582361 ci: install lcov
f773eb9b6 ci: unzip artifacts into separate directories
f42fd3b15 ci: fixed unzipping multiple archives
cb7c19d36 ci: removed non standard --zero option
cf4c4699b ci: unzip artifacts individually
5a3bd74cf ci: replaced forEach by a loop
1da31df93 ci: name unpack step and fix indent
6bd5acfb6 ci: download workflow artifacts
8121c0755 ci: aggregate coverage in separate workflow
8b872216e ci: simplify coveralls job id
11f08e7bb Merge pull request #742 from google/protect-vlog
65199ed0b protect VLOG by use_logging
b79e18b78 ci: updated coveralls job id
4399e5664 ci: explicitly identify coveralls builds
9206460cd Merge pull request #739 from google/cmake-glog-modules-install
64b4e327f cmake: fixed glog-modules.cmake installation
4df5ae746 ci: added cmake package config test
ab5e10929 Revert "cmake: fix incorrect relative path concatenation (#733)"
32522a5f6 ci: notify coveralls on finished parallel builds
d581807e3 ci: run coveralls on completed workflow
6c9f5b2ec ci: added jobs to coveralls workflow
0798a37bb ci: added coveralls workflow
33696664c restore the stream format after writing the log prefix (#731)
68964b8db cmake: fix incorrect relative path concatenation (#733)
17e7679fd Merge pull request #654 from aesophor/fix-path
c27c3a803 make LogCleaner support relative paths
e8e40f76d Merge pull request #732 from google/log-cleaner-tests
3362cc6c7 added log cleaner tests and fixed paths
9cf0eb7c9 Merge pull request #724 from google/mock-log
1398762db added mock log test
ce3533613 Merge pull request #723 from google/coverage-use-templates
06e665108 ci: replace generated headers by templates
56d3facee Merge pull request #722 from google/linux-github-action
4a55b1158 ci: added linux github workflow
396558472 support stack unwind on Android
acda90348 Merge pull request #720 from google/gmock-support
8474b7854 added gmock support
831a6f823 Add -Werror to Bazel presubmits
c34dbe987 Fix `syscall` warning in Bazel Build
56523194b Merge pull request #716 from google/export-os-defines
f7407d4d2 export OS_* defines (fixes #715)
2412db315 `raw_logging.h` depends on type definition in `logging.h`. Resolve https://github.com/google/glog/issues/712
3b6b05d70 Use __EMSCRIPTEN__ instead of __USE_POSIX2
de81bb0aa Fix link error for Emscripten
9733727be Merge pull request #706 from dfreese/tsan_annotation
ffb40018a Change size type in AnnotateBenignRaceSized to size_t from long
6f9e32a79 Merge pull request #700 from Starsss/master
df95749e8 Fix: not implement virtual class when WITH_CUSTOM_PREFIX on
35983c895 Merge pull request #696 from Fettpet/signed_compare
9fce37c46 Add a static cast to compare unsigned with unsigned
a063a04ec Merge pull request #694 from google/more-warnings
92cb3c449 fixed additional warnings
f8c8e99fd Merge pull request #688 from google/eliminate-warnings
98e0e8c65 cmake: bump .so version
0b83bb203 eliminate warnings
370cb741b Merge pull request #674 from StephLin/readme-fix-header-id
3ce6090d0 override header language
36bcd28c6 Merge pull request #687 from google/exception-spec
dfe573088 bump version
717f56983 fixed exception specification mismatch
eca3ddef8 Merge pull request #685 from z-aki/master
4a4331f2f Fix syscall deprecation warning on macOS >= 10.12
a01d739ea Merge pull request #684 from sergiud/cmake-namespaced-gflags
8d3ce1c29 Merge pull request #686 from google/ci-more-targets
c39fcf6e8 ci: added more targets
47ad26d5c readme: fixed inconsistent formatting
5bf7fdea1 cmake: prefer linking against gflags::gflags (fixes #683)
42ce901f2 Merge pull request #669 from darbitman/log-every-time-period
a1374c420 Implement LOG_EVERY_T. Use <chrono> and <atomic> for C++11 or greater. For non-Windows pre-C++11 systems, use <time.h> and built-in atomic operations. For Windows pre-C++11, use the Windows implementations for time and atomic operations.
6e46c125e bazel: add GLOG_CUSTOM_PREFIX_SUPPORT
a41555685 Remove ubuntu1604 from presubmit.yml
a1fa841f3 readme: fix wrong header id
a4a725d54 Merge pull request #671 from sergiud/cmake-define-have-cxx11-atomic
44331dc24 Merge pull request #672 from sergiud/cmake-allow-to-override-fpic
f0bac8852 cmake: allow to override `-fPIC`
764162569 Merge pull request #670 from sergiud/cmake-msvc-snprintf
d25a1b7bc cmake: export `<atomic>` availability (fixes #667)
9c1a25b93 cmake: fixed msvc snprintf detection (fixes #668)
c1499f64a Export COPYING
03f2b3861 Merge pull request #650 from romange/Prod
86fea1ab2 Allow updating vmodule levels after vmodule was parsed, invalidate already cached site flags vmodule flag is relevant for GNUC environments only.
fef48c17e Merge pull request #656 from romange/cygwin
d2fed749a Fix cmake configuration for cygwin environment
0b3d4cb47 Merge pull request #651 from xkszltl/initapi
81e0d616e Expose `IsGoogleLoggingInitialized()` in public API. Usually library does not have control of the process lifespan. Without this function, it is impossible to init/shutdown reliably. It has been one of the major pain points for years when using glog in libraries.
d5c04eec9 bazel: Use glog 0.5.0 in README
8f9ccfe77 Merge pull request #646 from sergiud/release-0.5
36fee386f release 0.5
8d40d7564 Merge pull request #642 from husskade/patch-1
a23e96f04 readme: update CMake source directory option
0efaa59a7 Merge pull request #641 from sergiud/nullptr
230da316c Merge pull request #637 from xiaobfly/master
bda40ec00 Merge pull request #640 from sergiud/gflags-windows-linker-warnings
762a2a7ee eliminated gflags related linker warnings (#569)
7d4eeb14f Merge pull request #554 from santigl/santigl/custom-prefixer
0cbc23529 Add support for setting a custom prefix format
c7656c3ec CHECK support for nullptr (fixes #341)
663bb2696 Merge pull request #620 from sergiud/cmake-allow-to-disable-symbolize
7ec73b509 Merge pull request #639 from sergiud/fix-windows-builds
efc341d76 fixed windows builds
a79416bab Merge pull request #638 from NeroBurner/github_actions
63ec71b58 Add windows builds with GitHub Actions
39e83581d cmake: allow to disable symbolize
635a07818 fix LogCleaner::IsLogFromCurrentProject for filename_extension
e51790bf3 Merge pull request #625 from sergiud/remove-windows-headers
36e6ed910 Merge pull request #635 from google/win-headers
7d60d6e04 removed windows specific headers
1041a221b Use generated config.h for Windows.
82320a8a8 Merge pull request #631 from xiaobfly/master
bf183f44f Merge pull request #632 from sergiud/cmake-default-shared-libs
9ee010d85 cmake: build shared libs by default
4fca1b967 fixed filename extension
30bb9086d bazel: use expand_template instead of genrules
d66e59318 Merge pull request #618 from sergiud/ac_cv___attribute___printf_4_5
d5fdcfefc Merge pull request #623 from sergiud/language-version
7f8a48b3b Merge pull request #595 from r-barnes/richard/ctr_type_fix
5abed00e3 Merge pull request #626 from sergiud/use-c++-headers
bf3e5a80a Change counter type to uint64
01f3e543e use C++ headers
2f7d608a9 Merge pull request #627 from sergiud/cmake-no-c-compiler
3f4df6aee cmake: do not require a C compiler
7a6e743eb Merge pull request #624 from sergiud/qualify-std-string
e27f14119 Merge pull request #622 from sergiud/max_log_size-overflow
0b3c27d04 fully qualify std::string
5c57edfbc readme: document C++ version
20984f973 Merge pull request #621 from sergiud/cmake-use-gtest
3533c22d2 handle --max_log_size overflow
be25d94c2 cmake: optionally use gtest
45fdc922d Merge pull request #619 from sergiud/cmake-proper-symbol-export
c30896d3d cmake: export symbols selectively
b442ef807 build: set ac_cv___attribute___printf_4_5
d4e8ebab7 Add layering_check to Bazel CI.
0863ad797 bazel: declare headers in top-level rule
ce50c57b9 Rename BUILD to BUILD.bazel.
d824eae3c Merge pull request #613 from sergiud/cmake-unwind-patch-match
df3ce126d cmake: fixed unwind patch component match check
246e49ac4 Merge pull request #610 from sergiud/readme-cleanup
9881ea596 Fix race conditions in LOG_EVERY_N (#492)
4cef72332 Merge pull request #611 from sergiud/incorrect-cygwin64-define
544b4a4f5 Merge pull request #609 from sergiud/cmake-unwind-empty-patch-component
b9be6bb5d consolidate readme
6d14ac3ef Fix the symlink creation bug if using bazel
795384957 replaced __CYGWIN32__ and __CYGWIN64__ by __CYGWIN__
e370cd516 bump version
3b21da69e cmake: handle empty unwind version patch component
4fa737b62 Merge pull request #400 from guoyuhong/fixWebmailer
4f09bed0b Merge pull request #263 from kennyyu/tsan_benign_race
7bba6030c Merge pull request #592 from aesophor/fix-logcleaner-file-ext
7a8b66aa1 Merge pull request #593 from aesophor/fix-typo
6d5da2154 Merge pull request #608 from mahiuchun/stl
15d51a17c Make stl_logging_unittest more robust.
fe1287dd5 src/logging.cc: Fix a typo in comment
431d74c80 src/logging.cc: Make LogCleaner aware of filename extension (#589)
c8f8135a5 Merge pull request #502 from aesophor/encapsulate-log-cleaner
d82641f70 Merge branch 'master' into encapsulate-log-cleaner
cec37e1cf Merge pull request #542 from xulongwu4/master
b55437ac1 Fix logmailer problem for dynamic lib.
c5dcae830 Merge pull request #467 from sukill/master
b30a009fa Merge pull request #585 from sergiud/remove-drop-autoconf-support
b539557b3 removed autoconf support
d516278b1 Merge pull request #540 from pwnall/fix-uv-warn
6c0a5fcb5 Fix unused parameter warning on gcc.
f28ae960c Merge pull request #573 from msamoila/msamoila-use-utc-time
fa0d50f9f Merge pull request #570 from jiuqiant/patch-1
fcc9da24a Merge pull request #545 from julianaito/master
40ab82281 Merge pull request #438 from d-uspenskiy/master
4c5a60c3c Merge pull request #551 from aesophor/replace-sprintf
8ee2bb5b3 Merge pull request #553 from huangqinjin/android
024b095d7 Merge pull request #552 from huangqinjin/master
d44246069 Merge pull request #579 from arj-fb/safety_check_at_SymbolizeAndDemangle
5bc1e1177 Merge pull request #580 from Yicong-Huang/patch-1
af4df08e5 Merge pull request #576 from santigl/santigl/streambuf-overflow-override
725818984 Add `override` to `LogStreamBuf::overflow()`
c652590d8 Update CMakeLists.txt
e5ef2728b Added check for if info.dli_sname is NULL
909069ea8 Add FLAGS_log_utc_time; when 'true' the time will be written in log in UTC
64650ef2e Connect glog to Andorid logging API
0a2e5931b Fix build for Emscripten (#546)
0a12a96ce Output to logcat on Android
d8998fe4f Make message text null-terminated
2e87f98fe Replace sprintf() with snprintf() (#536)
7001ab9ec OpenBSD/powerpc: clang does not define _CALL_* use proper ifdef
4a82a856d Use NULL to indicate uninitialized VLOG_IS_ON site.
3ba897659 fix bazel build for freebsd
ceb716388 Add extra information in log file header (application build/version, actual duration time)
28321d895 Merge pull request #503 from alexsmac/fix-custom-logger-deletion
5b4fb63d2 Prepend the year to each glog line (#516) (#530)
7da49d48a Merge pull request #515 from segfaulthunter/doc
940335d82 Add vcpkg installation instructions (#517)
dfc0e441c Merge pull request #523 from edbaunton/cmake-have-symbolize
eb13e4f67 Allow CMake user to disable Symbolize functionality
195d416e3 Add target existence checks to Unwind find module (#518)
74076bebc Explain log levels.
130a3e10d Use file offsets in traces if object has no symbols (#513)
445af7ef7 Merge pull request #508 from xiaosuo/close
a35e612c5 Don't call close(2) more than once
36fa99ba2 src/logging.cc: check the entire filename format
0a6704b43 Fix missing deletion of custom Logger
7764e4a8b src/logging.cc: match logfile with base_filename
3b5f2807f src/logging.cc: encapsulate log cleaner
1b7d54143 Add note about `namespace` on Win32.
95a365579 Remove unnecessary `defines`
c0d2ceade Avoid leaking config.h
925858d99 add windows target to bazel CI
20919b325 add windows support for bazel bazed build
7f732529d apply buildifier on glog.bzl
1863b4228 Merge pull request #368 from UVV-gh/cmake-sdk-fix
c0463b17b added cmake package config unit test
9a43cfb8d install unwind find module alongside glog
19570c69c renamed and cleaned up the unwind find module
58d7f873d Added fixed log name support, take 2 (#489)
0bf00f95a Merge pull request #491 from aesophor/update-doc
0f4975a68 doc: add information about log cleaner
5ef521d4a Merge pull request #487 from Telunsu/fix_wrong_assignment
e58e596f1 src/logging.cc: fix wrong assignment
f811ae9ed Merge pull request #485 from ffontaine/master
3ee89c86d src/symbolize.cc: do not check for HAVE_DLFCN_H for macOS
4d6aff418 Merge pull request #477 from ffontaine/master
742fe94f2 Use libunwind as an imported target
9630e0e84 src/utilities: fix build without pthread
fd3dc2c63 Merge pull request #484 from sergiud/remove-executable-flag
eec1435d5 removed executable flag from .cmake file
e227697b0 Merge pull request #483 from loop0day/enhance
82d0860b7 Support pkg-config
e0a2521e3 Merge pull request #482 from google/revert-19-master
a6f7be14c Add support for automatic removal of old logs (#432)
15fb5ca5b Revert "Added fixed log name support"
5792d60d6 Merge pull request #441 from asekretenko/useconds
44f6079fa Merge pull request #19 from ezegomez/master
242130d30 Merge pull request #350 from TheModMaker/fixIosSim
1a4c47a74 Merge pull request #88 from kekstee/master
f17d11aa8 Merge pull request #161 from mpercy/thread-local-libunwind-2
1227cf4da Merge pull request #312 from ruslo/pr.api21
0f83e53c4 Merge pull request #443 from ammubhave/fix_musl_build
e558d50b0 Merge pull request #370 from hemantbits/patch-1
30f7f3ee9 Merge pull request #479 from neheb/patch-2
6f5699a23 Merge pull request #460 from neheb/patch-1
e24276a35 logging: Fix compilation with uClibc++
1d762fa5b googletest: Switch to nanosleep
1655f4f91 Merge pull request #475 from ffontaine/master
10498b485 src/symbolize.cc: fix build without dlfcn.h
1e6b9f329 declare FLAGS_vmodule
4cc89c9e2 Merge pull request #465 from durswd/support_clang-cl
a8fad525e Changed the order of conditions
00cb9f4e6 I supported clang-cl with BUILD_SHARED_LIBS
ba8a9f695 fix typo: TOOD -> TODO (#448)
0d0c25431 Don't test on Ubuntu 14.04 (#446)
6ca3d3cf5 Fix stacktrace on bazel build. (#347)
834dd780b Fix symbolize_unittest for musl builds
94a54120d Extended the LogSink interface to be able to pass microseconds.
96a2f23dc Merge pull request #419 from shinh/release-0-4-0
5c576f78c Merge pull request #431 from romkatv/disallow_prefix
29dd5da60 Add Roman Perepelitsa to AUTHORS and CONTRIBUTORS
4db063134 Protect macros from user code to the left of them
460ec1f56 Merge branch 'master' into release-0-4-0
41f4bf9cb Merge pull request #425 from plmuon/remove-time-from-raw-logging
64041397b Use `static` to define CheckStackTrace on non-GNU (#429)
7f91846ca Stop using auto_ptr (#428)
e5588de96 remove time from raw logging
7ffca211f fix appveyor link (#424)
ed1ef7c4d Apply #397 to glog/logging.h.in
1431a17f3 Merge pull request #420 from kwaegel/patch-1
1d98d00c7 Update ChangeLog to record release dates at least
342fa2674 Use push/pop macro when supressing warning
406b0e702 glog: release 0.4.0
6d6d31872 Run src/windows/preprocess.sh
41a6a7880 Fix warnings
a606c3187 Allow getpwuid_r to return missing entry.
d064ab8d0 Fix build for Android API < 21
4764ca65f Annotate LOG_EVERY_N macros as a benign race for TSAN
f581614a7 Use thread local for libunwind GetStackTrace()
47af307fa Added fixed log name support
d68736bc1 Respect configured docdir

git-subtree-dir: third_party/google-glog
git-subtree-split: a1b6164ef15cf4be3b1914f2df7125fc7ffc7ca8
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
Change-Id: I2286351db32cb46aa8743a28e191dfc9c62783da
diff --git a/src/logging.cc b/src/logging.cc
index 18a8ec6..1df1034 100644
--- a/src/logging.cc
+++ b/src/logging.cc
@@ -32,7 +32,7 @@
 #include "utilities.h"
 
 #include <algorithm>
-#include <assert.h>
+#include <cassert>
 #include <iomanip>
 #include <string>
 #ifdef HAVE_UNISTD_H
@@ -44,11 +44,12 @@
 #ifdef HAVE_SYS_UTSNAME_H
 # include <sys/utsname.h>  // For uname.
 #endif
+#include <ctime>
 #include <fcntl.h>
 #include <cstdio>
 #include <iostream>
-#include <stdarg.h>
-#include <stdlib.h>
+#include <cstdarg>
+#include <cstdlib>
 #ifdef HAVE_PWD_H
 # include <pwd.h>
 #endif
@@ -56,17 +57,26 @@
 # include <syslog.h>
 #endif
 #include <vector>
-#include <errno.h>                   // for errno
+#include <cerrno>                   // for errno
 #include <sstream>
+#ifdef GLOG_OS_WINDOWS
+#include "windows/dirent.h"
+#else
+#include <dirent.h> // for automatic removal of old logs
+#endif
 #include "base/commandlineflags.h"        // to get the program name
-#include "glog/logging.h"
-#include "glog/raw_logging.h"
+#include <glog/logging.h>
+#include <glog/raw_logging.h>
 #include "base/googleinit.h"
 
 #ifdef HAVE_STACKTRACE
 # include "stacktrace.h"
 #endif
 
+#ifdef __ANDROID__
+#include <android/log.h>
+#endif
+
 using std::string;
 using std::vector;
 using std::setw;
@@ -103,13 +113,20 @@
   return memchr("tTyY1\0", valstr[0], 6) != NULL;
 }
 
+GLOG_DEFINE_bool(timestamp_in_logfile_name,
+                 BoolFromEnv("GOOGLE_TIMESTAMP_IN_LOGFILE_NAME", true),
+                 "put a timestamp at the end of the log file name");
 GLOG_DEFINE_bool(logtostderr, BoolFromEnv("GOOGLE_LOGTOSTDERR", false),
                  "log messages go to stderr instead of logfiles");
 GLOG_DEFINE_bool(alsologtostderr, BoolFromEnv("GOOGLE_ALSOLOGTOSTDERR", false),
                  "log messages go to stderr in addition to logfiles");
 GLOG_DEFINE_bool(colorlogtostderr, false,
                  "color messages logged to stderr (if supported by terminal)");
-#ifdef OS_LINUX
+GLOG_DEFINE_bool(colorlogtostdout, false,
+                 "color messages logged to stdout (if supported by terminal)");
+GLOG_DEFINE_bool(logtostdout, BoolFromEnv("GOOGLE_LOGTOSTDOUT", false),
+                 "log messages go to stdout instead of logfiles");
+#ifdef GLOG_OS_LINUX
 GLOG_DEFINE_bool(drop_log_memory, true, "Drop in-memory buffers of log contents. "
                  "Logs can grow very quickly and they are rarely read before they "
                  "need to be evicted from memory. Instead, drop them from memory "
@@ -131,6 +148,8 @@
                    "in addition to logfiles");
 GLOG_DEFINE_bool(log_prefix, true,
                  "Prepend the log prefix to the start of each log line");
+GLOG_DEFINE_bool(log_year_in_prefix, true,
+                 "Include the year in the log prefix");
 GLOG_DEFINE_int32(minloglevel, 0, "Messages logged at a lower level than this don't "
                   "actually get logged anywhere");
 GLOG_DEFINE_int32(logbuflevel, 0,
@@ -139,11 +158,15 @@
                   " ...)");
 GLOG_DEFINE_int32(logbufsecs, 30,
                   "Buffer log messages for at most this many seconds");
+
+GLOG_DEFINE_int32(logcleansecs, 60 * 5, // every 5 minutes
+                  "Clean overdue logs every this many seconds");
+
 GLOG_DEFINE_int32(logemaillevel, 999,
                   "Email log messages logged at this level or higher"
                   " (0 means email all; 3 means email FATAL only;"
                   " ...)");
-GLOG_DEFINE_string(logmailer, "/bin/mail",
+GLOG_DEFINE_string(logmailer, "",
                    "Mailer used to send logging email");
 
 // Compute the default value for --log_dir
@@ -168,9 +191,9 @@
 GLOG_DEFINE_string(log_link, "", "Put additional links to the log "
                    "files in this directory");
 
-GLOG_DEFINE_int32(max_log_size, 1800,
-                  "approx. maximum log file size (in MB). A value of 0 will "
-                  "be silently overridden to 1.");
+GLOG_DEFINE_uint32(max_log_size, 1800,
+                   "approx. maximum log file size (in MB). A value of 0 will "
+                   "be silently overridden to 1.");
 
 GLOG_DEFINE_bool(stop_logging_if_full_disk, false,
                  "Stop attempting to log to disk if the disk is full.");
@@ -178,11 +201,14 @@
 GLOG_DEFINE_string(log_backtrace_at, "",
                    "Emit a backtrace when logging at file:linenum.");
 
+GLOG_DEFINE_bool(log_utc_time, false,
+    "Use UTC time for logging.");
+
 // TODO(hamaji): consider windows
 #define PATH_SEPARATOR '/'
 
 #ifndef HAVE_PREAD
-#if defined(OS_WINDOWS)
+#if defined(GLOG_OS_WINDOWS)
 #include <basetsd.h>
 #define ssize_t SSIZE_T
 #endif
@@ -220,12 +246,12 @@
 static void GetHostName(string* hostname) {
 #if defined(HAVE_SYS_UTSNAME_H)
   struct utsname buf;
-  if (0 != uname(&buf)) {
+  if (uname(&buf) < 0) {
     // ensure null termination on failure
     *buf.nodename = '\0';
   }
   *hostname = buf.nodename;
-#elif defined(OS_WINDOWS)
+#elif defined(GLOG_OS_WINDOWS)
   char buf[MAX_COMPUTERNAME_LENGTH + 1];
   DWORD len = MAX_COMPUTERNAME_LENGTH + 1;
   if (GetComputerNameA(buf, &len)) {
@@ -242,7 +268,7 @@
 // Returns true iff terminal supports using colors in output.
 static bool TerminalSupportsColor() {
   bool term_supports_color = false;
-#ifdef OS_WINDOWS
+#ifdef GLOG_OS_WINDOWS
   // on Windows TERM variable is usually not set, but the console does
   // support colors.
   term_supports_color = true;
@@ -296,7 +322,7 @@
   return color;
 }
 
-#ifdef OS_WINDOWS
+#ifdef GLOG_OS_WINDOWS
 
 // Returns the character attribute for the given color.
 static WORD GetColorAttribute(GLogColor color) {
@@ -321,11 +347,13 @@
   return NULL; // stop warning about return type.
 }
 
-#endif  // OS_WINDOWS
+#endif  // GLOG_OS_WINDOWS
 
 // Safely get max_log_size, overriding to 1 if it somehow gets defined as 0
-static int32 MaxLogSize() {
-  return (FLAGS_max_log_size > 0 ? FLAGS_max_log_size : 1);
+static uint32 MaxLogSize() {
+  return (FLAGS_max_log_size > 0 && FLAGS_max_log_size < 4096
+              ? FLAGS_max_log_size
+              : 1);
 }
 
 // An arbitrary limit on the length of a single log message.  This
@@ -347,8 +375,6 @@
     std::vector<std::string>* outvec_; // NULL or vector to push message onto
     std::string* message_;             // NULL or string to write message into
   };
-  time_t timestamp_;            // Time of creation of LogMessage
-  struct ::tm tm_time_;         // Time of creation of LogMessage
   size_t num_prefix_chars_;     // # of chars of prefix in this message
   size_t num_chars_to_log_;     // # of chars of msg to send to log
   size_t num_chars_to_syslog_;  // # of chars of msg to send to syslog
@@ -392,6 +418,15 @@
 base::Logger::~Logger() {
 }
 
+#ifdef GLOG_CUSTOM_PREFIX_SUPPORT
+namespace  {
+  // Optional user-configured callback to print custom prefixes.
+  CustomPrefixCallback custom_prefix_callback = NULL;
+  // User-provided data to pass to the callback:
+  void* custom_prefix_callback_data = NULL;
+}
+#endif
+
 namespace {
 
 // Encapsulates all file-system related state
@@ -403,7 +438,7 @@
   virtual void Write(bool force_flush, // Should we force a flush here?
                      time_t timestamp,  // Timestamp for this entry
                      const char* message,
-                     int message_len);
+                     size_t message_len);
 
   // Configuration options
   void SetBasename(const char* basename);
@@ -440,13 +475,50 @@
   uint32 file_length_;
   unsigned int rollover_attempt_;
   int64 next_flush_time_;         // cycle count at which to flush log
+  WallTime start_time_;
 
   // Actually create a logfile using the value of base_filename_ and the
-  // supplied argument time_pid_string
+  // optional argument time_pid_string
   // REQUIRES: lock_ is held
   bool CreateLogfile(const string& time_pid_string);
 };
 
+// Encapsulate all log cleaner related states
+class LogCleaner {
+ public:
+  LogCleaner();
+
+  // Setting overdue_days to 0 days will delete all logs.
+  void Enable(unsigned int overdue_days);
+  void Disable();
+
+  // update next_cleanup_time_
+  void UpdateCleanUpTime();
+
+  void Run(bool base_filename_selected,
+           const string& base_filename,
+           const string& filename_extension);
+
+  bool enabled() const { return enabled_; }
+
+ private:
+  vector<string> GetOverdueLogNames(string log_directory, unsigned int days,
+                                    const string& base_filename,
+                                    const string& filename_extension) const;
+
+  bool IsLogFromCurrentProject(const string& filepath,
+                               const string& base_filename,
+                               const string& filename_extension) const;
+
+  bool IsLogLastModifiedOver(const string& filepath, unsigned int days) const;
+
+  bool enabled_;
+  unsigned int overdue_days_;
+  int64 next_cleanup_time_;         // cycle count at which to clean overdue log
+};
+
+LogCleaner log_cleaner;
+
 }  // namespace
 
 class LogDestination {
@@ -485,12 +557,12 @@
 
  private:
   LogDestination(LogSeverity severity, const char* base_filename);
-  ~LogDestination() { }
+  ~LogDestination();
 
   // Take a log message of a particular severity and log it to stderr
   // iff it's of a high enough severity to deserve it.
   static void MaybeLogToStderr(LogSeverity severity, const char* message,
-			       size_t len);
+			       size_t message_len, size_t prefix_len);
 
   // Take a log message of a particular severity and log it to email
   // iff it's of a high enough severity to deserve it.
@@ -509,12 +581,9 @@
                                const char* message, size_t len);
 
   // Send logging info to all registered sinks.
-  static void LogToSinks(LogSeverity severity,
-                         const char *full_filename,
-                         const char *base_filename,
-                         int line,
-                         const struct ::tm* tm_time,
-                         const char* message,
+  static void LogToSinks(LogSeverity severity, const char* full_filename,
+                         const char* base_filename, int line,
+                         const LogMessageTime& logmsgtime, const char* message,
                          size_t message_len);
 
   // Wait for all registered sinks via WaitTillSent
@@ -571,6 +640,13 @@
     logger_(&fileobject_) {
 }
 
+LogDestination::~LogDestination() {
+  if (logger_ && logger_ != &fileobject_) {
+    // Delete user-specified logger set via SetLogger().
+    delete logger_;
+  }
+}
+
 inline void LogDestination::FlushLogFilesUnsafe(int min_severity) {
   // assume we have the log_mutex or we simply don't care
   // about it
@@ -627,13 +703,7 @@
   MutexLock l(&sink_mutex_);
   // This doesn't keep the sinks in order, but who cares?
   if (sinks_) {
-    for (int i = sinks_->size() - 1; i >= 0; i--) {
-      if ((*sinks_)[i] == destination) {
-        (*sinks_)[i] = (*sinks_)[sinks_->size() - 1];
-        sinks_->pop_back();
-        break;
-      }
-    }
+    sinks_->erase(std::remove(sinks_->begin(), sinks_->end(), destination), sinks_->end());
   }
 }
 
@@ -673,41 +743,61 @@
   LogDestination::addresses_ = addresses;
 }
 
-static void ColoredWriteToStderr(LogSeverity severity,
-                                 const char* message, size_t len) {
-  const GLogColor color =
-      (LogDestination::terminal_supports_color() && FLAGS_colorlogtostderr) ?
-      SeverityToColor(severity) : COLOR_DEFAULT;
+static void ColoredWriteToStderrOrStdout(FILE* output, LogSeverity severity,
+                                         const char* message, size_t len) {
+  bool is_stdout = (output == stdout);
+  const GLogColor color = (LogDestination::terminal_supports_color() &&
+                           ((!is_stdout && FLAGS_colorlogtostderr) ||
+                            (is_stdout && FLAGS_colorlogtostdout)))
+                              ? SeverityToColor(severity)
+                              : COLOR_DEFAULT;
 
   // Avoid using cerr from this module since we may get called during
   // exit code, and cerr may be partially or fully destroyed by then.
   if (COLOR_DEFAULT == color) {
-    fwrite(message, len, 1, stderr);
+    fwrite(message, len, 1, output);
     return;
   }
-#ifdef OS_WINDOWS
-  const HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
+#ifdef GLOG_OS_WINDOWS
+  const HANDLE output_handle =
+      GetStdHandle(is_stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
 
   // Gets the current text color.
   CONSOLE_SCREEN_BUFFER_INFO buffer_info;
-  GetConsoleScreenBufferInfo(stderr_handle, &buffer_info);
+  GetConsoleScreenBufferInfo(output_handle, &buffer_info);
   const WORD old_color_attrs = buffer_info.wAttributes;
 
   // We need to flush the stream buffers into the console before each
   // SetConsoleTextAttribute call lest it affect the text that is already
   // printed but has not yet reached the console.
-  fflush(stderr);
-  SetConsoleTextAttribute(stderr_handle,
+  fflush(output);
+  SetConsoleTextAttribute(output_handle,
                           GetColorAttribute(color) | FOREGROUND_INTENSITY);
-  fwrite(message, len, 1, stderr);
-  fflush(stderr);
+  fwrite(message, len, 1, output);
+  fflush(output);
   // Restores the text color.
-  SetConsoleTextAttribute(stderr_handle, old_color_attrs);
+  SetConsoleTextAttribute(output_handle, old_color_attrs);
 #else
-  fprintf(stderr, "\033[0;3%sm", GetAnsiColorCode(color));
-  fwrite(message, len, 1, stderr);
-  fprintf(stderr, "\033[m");  // Resets the terminal to default.
-#endif  // OS_WINDOWS
+  fprintf(output, "\033[0;3%sm", GetAnsiColorCode(color));
+  fwrite(message, len, 1, output);
+  fprintf(output, "\033[m");  // Resets the terminal to default.
+#endif  // GLOG_OS_WINDOWS
+}
+
+static void ColoredWriteToStdout(LogSeverity severity, const char* message,
+                                 size_t len) {
+  FILE* output = stdout;
+  // We also need to send logs to the stderr when the severity is
+  // higher or equal to the stderr threshold.
+  if (severity >= FLAGS_stderrthreshold) {
+    output = stderr;
+  }
+  ColoredWriteToStderrOrStdout(output, severity, message, len);
+}
+
+static void ColoredWriteToStderr(LogSeverity severity, const char* message,
+                                 size_t len) {
+  ColoredWriteToStderrOrStdout(stderr, severity, message, len);
 }
 
 static void WriteToStderr(const char* message, size_t len) {
@@ -717,12 +807,26 @@
 }
 
 inline void LogDestination::MaybeLogToStderr(LogSeverity severity,
-					     const char* message, size_t len) {
+					     const char* message, size_t message_len, size_t prefix_len) {
   if ((severity >= FLAGS_stderrthreshold) || FLAGS_alsologtostderr) {
-    ColoredWriteToStderr(severity, message, len);
-#ifdef OS_WINDOWS
+    ColoredWriteToStderr(severity, message, message_len);
+#ifdef GLOG_OS_WINDOWS
+    (void) prefix_len;
     // On Windows, also output to the debugger
-    ::OutputDebugStringA(string(message,len).c_str());
+    ::OutputDebugStringA(message);
+#elif defined(__ANDROID__)
+    // On Android, also output to logcat
+    const int android_log_levels[NUM_SEVERITIES] = {
+      ANDROID_LOG_INFO,
+      ANDROID_LOG_WARN,
+      ANDROID_LOG_ERROR,
+      ANDROID_LOG_FATAL,
+    };
+    __android_log_write(android_log_levels[severity],
+                        glog_internal_namespace_::ProgramInvocationShortName(),
+                        message + prefix_len);
+#else
+    (void) prefix_len;
 #endif
   }
 }
@@ -767,27 +871,28 @@
                                              time_t timestamp,
                                              const char* message,
                                              size_t len) {
-
-  if ( FLAGS_logtostderr ) {           // global flag: never log to file
+  if (FLAGS_logtostdout) {  // global flag: never log to file
+    ColoredWriteToStdout(severity, message, len);
+  } else if (FLAGS_logtostderr) {  // global flag: never log to file
     ColoredWriteToStderr(severity, message, len);
   } else {
-    for (int i = severity; i >= 0; --i)
+    for (int i = severity; i >= 0; --i) {
       LogDestination::MaybeLogToLogfile(i, timestamp, message, len);
+    }
   }
 }
 
 inline void LogDestination::LogToSinks(LogSeverity severity,
-                                       const char *full_filename,
-                                       const char *base_filename,
-                                       int line,
-                                       const struct ::tm* tm_time,
+                                       const char* full_filename,
+                                       const char* base_filename, int line,
+                                       const LogMessageTime& logmsgtime,
                                        const char* message,
                                        size_t message_len) {
   ReaderMutexLock l(&sink_mutex_);
   if (sinks_) {
-    for (int i = sinks_->size() - 1; i >= 0; i--) {
+    for (size_t i = sinks_->size(); i-- > 0; ) {
       (*sinks_)[i]->send(severity, full_filename, base_filename,
-                         line, tm_time, message, message_len);
+                         line, logmsgtime, message, message_len);
     }
   }
 }
@@ -795,7 +900,7 @@
 inline void LogDestination::WaitForSinks(LogMessage::LogMessageData* data) {
   ReaderMutexLock l(&sink_mutex_);
   if (sinks_) {
-    for (int i = sinks_->size() - 1; i >= 0; i--) {
+    for (size_t i = sinks_->size(); i-- > 0; ) {
       (*sinks_)[i]->WaitTillSent();
     }
   }
@@ -829,6 +934,35 @@
 
 namespace {
 
+std::string g_application_fingerprint;
+
+} // namespace
+
+void SetApplicationFingerprint(const std::string& fingerprint) {
+  g_application_fingerprint = fingerprint;
+}
+
+namespace {
+
+// Directory delimiter; Windows supports both forward slashes and backslashes
+#ifdef GLOG_OS_WINDOWS
+const char possible_dir_delim[] = {'\\', '/'};
+#else
+const char possible_dir_delim[] = {'/'};
+#endif
+
+string PrettyDuration(int secs) {
+  std::stringstream result;
+  int mins = secs / 60;
+  int hours = mins / 60;
+  mins = mins % 60;
+  secs = secs % 60;
+  result.fill('0');
+  result << hours << ':' << setw(2) << mins << ':' << setw(2) << secs;
+  return result.str();
+}
+
+
 LogFileObject::LogFileObject(LogSeverity severity,
                              const char* base_filename)
   : base_filename_selected_(base_filename != NULL),
@@ -841,7 +975,8 @@
     dropped_mem_length_(0),
     file_length_(0),
     rollover_attempt_(kRolloverAttemptFrequency-1),
-    next_flush_time_(0) {
+    next_flush_time_(0),
+    start_time_(WallTime_Now()) {
   assert(severity >= 0);
   assert(severity < NUM_SEVERITIES);
 }
@@ -903,23 +1038,65 @@
 }
 
 bool LogFileObject::CreateLogfile(const string& time_pid_string) {
-  string string_filename = base_filename_+filename_extension_+
-                           time_pid_string;
+  string string_filename = base_filename_;
+  if (FLAGS_timestamp_in_logfile_name) {
+    string_filename += time_pid_string;
+  }
+  string_filename += filename_extension_;
   const char* filename = string_filename.c_str();
-  int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, FLAGS_logfile_mode);
+  //only write to files, create if non-existant.
+  int flags = O_WRONLY | O_CREAT;
+  if (FLAGS_timestamp_in_logfile_name) {
+    //demand that the file is unique for our timestamp (fail if it exists).
+    flags = flags | O_EXCL;
+  }
+  int fd = open(filename, flags, static_cast<mode_t>(FLAGS_logfile_mode));
   if (fd == -1) return false;
 #ifdef HAVE_FCNTL
   // Mark the file close-on-exec. We don't really care if this fails
   fcntl(fd, F_SETFD, FD_CLOEXEC);
+
+  // Mark the file as exclusive write access to avoid two clients logging to the
+  // same file. This applies particularly when !FLAGS_timestamp_in_logfile_name
+  // (otherwise open would fail because the O_EXCL flag on similar filename).
+  // locks are released on unlock or close() automatically, only after log is
+  // released.
+  // This will work after a fork as it is not inherited (not stored in the fd).
+  // Lock will not be lost because the file is opened with exclusive lock (write)
+  // and we will never read from it inside the process.
+  // TODO windows implementation of this (as flock is not available on mingw).
+  static struct flock w_lock;
+
+  w_lock.l_type = F_WRLCK;
+  w_lock.l_start = 0;
+  w_lock.l_whence = SEEK_SET;
+  w_lock.l_len = 0;
+
+  int wlock_ret = fcntl(fd, F_SETLK, &w_lock);
+  if (wlock_ret == -1) {
+      close(fd); //as we are failing already, do not check errors here
+      return false;
+  }
 #endif
 
+  //fdopen in append mode so if the file exists it will fseek to the end
   file_ = fdopen(fd, "a");  // Make a FILE*.
   if (file_ == NULL) {  // Man, we're screwed!
     close(fd);
-    unlink(filename);  // Erase the half-baked evidence: an unusable log file
+    if (FLAGS_timestamp_in_logfile_name) {
+      unlink(filename);  // Erase the half-baked evidence: an unusable log file, only if we just created it.
+    }
     return false;
   }
-
+#ifdef GLOG_OS_WINDOWS
+  // https://github.com/golang/go/issues/27638 - make sure we seek to the end to append
+  // empirically replicated with wine over mingw build
+  if (!FLAGS_timestamp_in_logfile_name) {
+    if (fseek(file_, 0, SEEK_END) != 0) {
+      return false;
+    }
+  }
+#endif
   // We try to create a symlink called <program_name>.<severity>,
   // which is easier to use.  (Every time we create a new logfile,
   // we destroy the old symlink and create a new one, so it always
@@ -931,11 +1108,11 @@
     const string linkname =
       symlink_basename_ + '.' + LogSeverityNames[severity_];
     string linkpath;
-    if ( slash ) linkpath = string(filename, slash-filename+1);  // get dirname
+    if ( slash ) linkpath = string(filename, static_cast<size_t>(slash-filename+1));  // get dirname
     linkpath += linkname;
     unlink(linkpath.c_str());                    // delete old one if it exists
 
-#if defined(OS_WINDOWS)
+#if defined(GLOG_OS_WINDOWS)
     // TODO(hamaji): Create lnk file on Windows?
 #elif defined(HAVE_UNISTD_H)
     // We must have unistd.h.
@@ -964,7 +1141,7 @@
 void LogFileObject::Write(bool force_flush,
                           time_t timestamp,
                           const char* message,
-                          int message_len) {
+                          size_t message_len) {
   MutexLock l(&lock_);
 
   // We don't log if the base_name_ is "" (which means "don't write")
@@ -972,12 +1149,11 @@
     return;
   }
 
-  if (static_cast<int>(file_length_ >> 20) >= MaxLogSize() ||
-      PidHasChanged()) {
+  if (file_length_ >> 20U >= MaxLogSize() || PidHasChanged()) {
     if (file_ != NULL) fclose(file_);
     file_ = NULL;
     file_length_ = bytes_since_flush_ = dropped_mem_length_ = 0;
-    rollover_attempt_ = kRolloverAttemptFrequency-1;
+    rollover_attempt_ = kRolloverAttemptFrequency - 1;
   }
 
   // If there's no destination file, make one before outputting
@@ -989,7 +1165,11 @@
     rollover_attempt_ = 0;
 
     struct ::tm tm_time;
-    localtime_r(&timestamp, &tm_time);
+    if (FLAGS_log_utc_time) {
+      gmtime_r(&timestamp, &tm_time);
+    } else {
+      localtime_r(&timestamp, &tm_time);
+    }
 
     // The logfile's filename will have the date/time & pid in it
     ostringstream time_pid_stream;
@@ -1073,14 +1253,23 @@
                        << ' '
                        << setw(2) << tm_time.tm_hour << ':'
                        << setw(2) << tm_time.tm_min << ':'
-                       << setw(2) << tm_time.tm_sec << '\n'
+                       << setw(2) << tm_time.tm_sec << (FLAGS_log_utc_time ? " UTC\n" : "\n")
                        << "Running on machine: "
-                       << LogDestination::hostname() << '\n'
-                       << "Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu "
+                       << LogDestination::hostname() << '\n';
+
+    if(!g_application_fingerprint.empty()) {
+      file_header_stream << "Application fingerprint: " << g_application_fingerprint << '\n';
+    }
+    const char* const date_time_format = FLAGS_log_year_in_prefix
+                                             ? "yyyymmdd hh:mm:ss.uuuuuu"
+                                             : "mmdd hh:mm:ss.uuuuuu";
+    file_header_stream << "Running duration (h:mm:ss): "
+                       << PrettyDuration(static_cast<int>(WallTime_Now() - start_time_)) << '\n'
+                       << "Log line format: [IWEF]" << date_time_format << " "
                        << "threadid file:line] msg" << '\n';
     const string& file_header_string = file_header_stream.str();
 
-    const int header_len = file_header_string.size();
+    const size_t header_len = file_header_string.size();
     fwrite(file_header_string.data(), 1, header_len, file_);
     file_length_ += header_len;
     bytes_since_flush_ += header_len;
@@ -1104,8 +1293,9 @@
       bytes_since_flush_ += message_len;
     }
   } else {
-    if ( CycleClock_Now() >= next_flush_time_ )
+    if (CycleClock_Now() >= next_flush_time_) {
       stop_writing = false;  // check to see if disk has free space.
+    }
     return;  // no need to flush
   }
 
@@ -1115,26 +1305,226 @@
        (bytes_since_flush_ >= 1000000) ||
        (CycleClock_Now() >= next_flush_time_) ) {
     FlushUnlocked();
-#ifdef OS_LINUX
+#ifdef GLOG_OS_LINUX
     // Only consider files >= 3MiB
-    if (FLAGS_drop_log_memory && file_length_ >= (3 << 20)) {
+    if (FLAGS_drop_log_memory && file_length_ >= (3U << 20U)) {
       // Don't evict the most recent 1-2MiB so as not to impact a tailer
       // of the log file and to avoid page rounding issue on linux < 4.7
-      uint32 total_drop_length = (file_length_ & ~((1 << 20) - 1)) - (1 << 20);
+      uint32 total_drop_length =
+          (file_length_ & ~((1U << 20U) - 1U)) - (1U << 20U);
       uint32 this_drop_length = total_drop_length - dropped_mem_length_;
-      if (this_drop_length >= (2 << 20)) {
+      if (this_drop_length >= (2U << 20U)) {
         // Only advise when >= 2MiB to drop
-        posix_fadvise(fileno(file_), dropped_mem_length_, this_drop_length,
+# if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
+        // 'posix_fadvise' introduced in API 21:
+        // * https://android.googlesource.com/platform/bionic/+/6880f936173081297be0dc12f687d341b86a4cfa/libc/libc.map.txt#732
+# else
+        posix_fadvise(fileno(file_), static_cast<off_t>(dropped_mem_length_),
+                      static_cast<off_t>(this_drop_length),
                       POSIX_FADV_DONTNEED);
+# endif
         dropped_mem_length_ = total_drop_length;
       }
     }
 #endif
+
+    // Remove old logs
+    if (log_cleaner.enabled()) {
+      log_cleaner.Run(base_filename_selected_,
+                      base_filename_,
+                      filename_extension_);
+    }
   }
 }
 
-}  // namespace
+LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7), next_cleanup_time_(0) {}
 
+void LogCleaner::Enable(unsigned int overdue_days) {
+  enabled_ = true;
+  overdue_days_ = overdue_days;
+}
+
+void LogCleaner::Disable() {
+  enabled_ = false;
+}
+
+void LogCleaner::UpdateCleanUpTime() {
+  const int64 next = (FLAGS_logcleansecs
+                      * 1000000);  // in usec
+  next_cleanup_time_ = CycleClock_Now() + UsecToCycles(next);
+}
+
+void LogCleaner::Run(bool base_filename_selected,
+                     const string& base_filename,
+                     const string& filename_extension) {
+  assert(enabled_);
+  assert(!base_filename_selected || !base_filename.empty());
+
+  // avoid scanning logs too frequently
+  if (CycleClock_Now() < next_cleanup_time_) {
+    return;
+  }
+  UpdateCleanUpTime();
+
+  vector<string> dirs;
+
+  if (!base_filename_selected) {
+    dirs = GetLoggingDirectories();
+  } else {
+    size_t pos = base_filename.find_last_of(possible_dir_delim, string::npos,
+                                            sizeof(possible_dir_delim));
+    if (pos != string::npos) {
+      string dir = base_filename.substr(0, pos + 1);
+      dirs.push_back(dir);
+    } else {
+      dirs.push_back(".");
+    }
+  }
+
+  for (size_t i = 0; i < dirs.size(); i++) {
+    vector<string> logs = GetOverdueLogNames(dirs[i],
+                                             overdue_days_,
+                                             base_filename,
+                                             filename_extension);
+    for (size_t j = 0; j < logs.size(); j++) {
+      static_cast<void>(unlink(logs[j].c_str()));
+    }
+  }
+}
+
+vector<string> LogCleaner::GetOverdueLogNames(
+    string log_directory, unsigned int days, const string& base_filename,
+    const string& filename_extension) const {
+  // The names of overdue logs.
+  vector<string> overdue_log_names;
+
+  // Try to get all files within log_directory.
+  DIR *dir;
+  struct dirent *ent;
+
+  if ((dir = opendir(log_directory.c_str()))) {
+    while ((ent = readdir(dir))) {
+      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
+        continue;
+      }
+
+      string filepath = ent->d_name;
+      const char* const dir_delim_end =
+          possible_dir_delim + sizeof(possible_dir_delim);
+
+      if (!log_directory.empty() &&
+          std::find(possible_dir_delim, dir_delim_end,
+                    log_directory[log_directory.size() - 1]) != dir_delim_end) {
+        filepath = log_directory + filepath;
+      }
+
+      if (IsLogFromCurrentProject(filepath, base_filename, filename_extension) &&
+          IsLogLastModifiedOver(filepath, days)) {
+        overdue_log_names.push_back(filepath);
+      }
+    }
+    closedir(dir);
+  }
+
+  return overdue_log_names;
+}
+
+bool LogCleaner::IsLogFromCurrentProject(const string& filepath,
+                                         const string& base_filename,
+                                         const string& filename_extension) const {
+  // We should remove duplicated delimiters from `base_filename`, e.g.,
+  // before: "/tmp//<base_filename>.<create_time>.<pid>"
+  // after:  "/tmp/<base_filename>.<create_time>.<pid>"
+  string cleaned_base_filename;
+
+  const char* const dir_delim_end =
+      possible_dir_delim + sizeof(possible_dir_delim);
+
+  size_t real_filepath_size = filepath.size();
+  for (size_t i = 0; i < base_filename.size(); ++i) {
+    const char& c = base_filename[i];
+
+    if (cleaned_base_filename.empty()) {
+      cleaned_base_filename += c;
+    } else if (std::find(possible_dir_delim, dir_delim_end, c) ==
+                   dir_delim_end ||
+               (!cleaned_base_filename.empty() &&
+                c != cleaned_base_filename[cleaned_base_filename.size() - 1])) {
+      cleaned_base_filename += c;
+    }
+  }
+
+  // Return early if the filename doesn't start with `cleaned_base_filename`.
+  if (filepath.find(cleaned_base_filename) != 0) {
+    return false;
+  }
+
+  // Check if in the string `filename_extension` is right next to
+  // `cleaned_base_filename` in `filepath` if the user
+  // has set a custom filename extension.
+  if (!filename_extension.empty()) {
+    if (cleaned_base_filename.size() >= real_filepath_size) {
+      return false;
+    }
+    // for origin version, `filename_extension` is middle of the `filepath`.
+    string ext = filepath.substr(cleaned_base_filename.size(), filename_extension.size());
+    if (ext == filename_extension) {
+      cleaned_base_filename += filename_extension;
+    }
+    else {
+      // for new version, `filename_extension` is right of the `filepath`.
+      if (filename_extension.size() >= real_filepath_size) {
+        return false;
+      }
+      real_filepath_size = filepath.size() - filename_extension.size();
+      if (filepath.substr(real_filepath_size) != filename_extension) {
+        return false;
+      }
+    }
+  }
+
+  // The characters after `cleaned_base_filename` should match the format:
+  // YYYYMMDD-HHMMSS.pid
+  for (size_t i = cleaned_base_filename.size(); i < real_filepath_size; i++) {
+    const char& c = filepath[i];
+
+    if (i <= cleaned_base_filename.size() + 7) { // 0 ~ 7 : YYYYMMDD
+      if (c < '0' || c > '9') { return false; }
+
+    } else if (i == cleaned_base_filename.size() + 8) { // 8: -
+      if (c != '-') { return false; }
+
+    } else if (i <= cleaned_base_filename.size() + 14) { // 9 ~ 14: HHMMSS
+      if (c < '0' || c > '9') { return false; }
+
+    } else if (i == cleaned_base_filename.size() + 15) { // 15: .
+      if (c != '.') { return false; }
+
+    } else if (i >= cleaned_base_filename.size() + 16) { // 16+: pid
+      if (c < '0' || c > '9') { return false; }
+    }
+  }
+
+  return true;
+}
+
+bool LogCleaner::IsLogLastModifiedOver(const string& filepath,
+                                       unsigned int days) const {
+  // Try to get the last modified time of this file.
+  struct stat file_stat;
+
+  if (stat(filepath.c_str(), &file_stat) == 0) {
+    const time_t seconds_in_a_day = 60 * 60 * 24;
+    time_t last_modified_time = file_stat.st_mtime;
+    time_t current_time = time(NULL);
+    return difftime(current_time, last_modified_time) > days * seconds_in_a_day;
+  }
+
+  // If failed to get file stat, don't return true!
+  return false;
+}
+
+}  // namespace
 
 // Static log data space to avoid alloc failures in a LOG(FATAL)
 //
@@ -1154,7 +1544,7 @@
 // allocations).
 static GLOG_THREAD_LOCAL_STORAGE bool thread_data_available = true;
 
-#ifdef HAVE_ALIGNED_STORAGE
+#if defined(HAVE_ALIGNED_STORAGE) && __cplusplus >= 201103L
 static GLOG_THREAD_LOCAL_STORAGE
     std::aligned_storage<sizeof(LogMessage::LogMessageData),
                          alignof(LogMessage::LogMessageData)>::type thread_msg_data;
@@ -1169,7 +1559,7 @@
 }
 
 LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
-                       int ctr, void (LogMessage::*send_method)())
+                       int64 ctr, void (LogMessage::*send_method)())
     : allocated_(NULL) {
   Init(file, line, severity, send_method);
   data_->stream_.set_ctr(ctr);
@@ -1255,7 +1645,6 @@
     }
   }
 
-  stream().fill('0');
   data_->preserved_errno_ = errno;
   data_->severity_ = severity;
   data_->line_ = line;
@@ -1263,10 +1652,8 @@
   data_->sink_ = NULL;
   data_->outvec_ = NULL;
   WallTime now = WallTime_Now();
-  data_->timestamp_ = static_cast<time_t>(now);
-  localtime_r(&data_->timestamp_, &data_->tm_time_);
-  int usecs = static_cast<int>((now - data_->timestamp_) * 1000000);
-  RawLog__SetLastTime(data_->tm_time_, usecs);
+  time_t timestamp_now = static_cast<time_t>(now);
+  logmsgtime_ = LogMessageTime(timestamp_now, now);
 
   data_->num_chars_to_log_ = 0;
   data_->num_chars_to_syslog_ = 0;
@@ -1275,23 +1662,45 @@
   data_->has_been_flushed_ = false;
 
   // If specified, prepend a prefix to each line.  For example:
-  //    I1018 160715 f5d4fbb0 logging.cc:1153]
-  //    (log level, GMT month, date, time, thread_id, file basename, line)
+  //    I20201018 160715 f5d4fbb0 logging.cc:1153]
+  //    (log level, GMT year, month, date, time, thread_id, file basename, line)
   // We exclude the thread_id for the default thread.
   if (FLAGS_log_prefix && (line != kNoLogPrefix)) {
-    stream() << LogSeverityNames[severity][0]
-             << setw(2) << 1+data_->tm_time_.tm_mon
-             << setw(2) << data_->tm_time_.tm_mday
-             << ' '
-             << setw(2) << data_->tm_time_.tm_hour  << ':'
-             << setw(2) << data_->tm_time_.tm_min   << ':'
-             << setw(2) << data_->tm_time_.tm_sec   << "."
-             << setw(6) << usecs
-             << ' '
-             << setfill(' ') << setw(5)
-             << static_cast<unsigned int>(GetTID()) << setfill('0')
-             << ' '
-             << data_->basename_ << ':' << data_->line_ << "] ";
+      std::ios saved_fmt(NULL);
+      saved_fmt.copyfmt(stream());
+      stream().fill('0');
+    #ifdef GLOG_CUSTOM_PREFIX_SUPPORT
+      if (custom_prefix_callback == NULL) {
+    #endif
+          stream() << LogSeverityNames[severity][0];
+          if (FLAGS_log_year_in_prefix) {
+            stream() << setw(4) << 1900 + logmsgtime_.year();
+          }
+          stream() << setw(2) << 1 + logmsgtime_.month()
+                   << setw(2) << logmsgtime_.day()
+                   << ' '
+                   << setw(2) << logmsgtime_.hour() << ':'
+                   << setw(2) << logmsgtime_.min() << ':'
+                   << setw(2) << logmsgtime_.sec() << "."
+                   << setw(6) << logmsgtime_.usec()
+                   << ' '
+                   << setfill(' ') << setw(5)
+                   << static_cast<unsigned int>(GetTID()) << setfill('0')
+                   << ' '
+                   << data_->basename_ << ':' << data_->line_ << "] ";
+    #ifdef GLOG_CUSTOM_PREFIX_SUPPORT
+      } else {
+        custom_prefix_callback(
+                stream(),
+                LogMessageInfo(LogSeverityNames[severity],
+                               data_->basename_, data_->line_, GetTID(),
+                               logmsgtime_),
+                custom_prefix_callback_data
+                );
+        stream() << " ";
+      }
+    #endif
+      stream().copyfmt(saved_fmt);
   }
   data_->num_prefix_chars_ = data_->stream_.pcount();
 
@@ -1299,7 +1708,7 @@
     char fileline[128];
     snprintf(fileline, sizeof(fileline), "%s:%d", data_->basename_, line);
 #ifdef HAVE_STACKTRACE
-    if (!strcmp(FLAGS_log_backtrace_at.c_str(), fileline)) {
+    if (FLAGS_log_backtrace_at == fileline) {
       string stacktrace;
       DumpStackTraceToString(&stacktrace);
       stream() << " (stacktrace:\n" << stacktrace << ") ";
@@ -1308,6 +1717,10 @@
   }
 }
 
+const LogMessageTime& LogMessage::getLogMessageTime() const {
+  return logmsgtime_;
+}
+
 LogMessage::~LogMessage() {
   Flush();
 #ifdef GLOG_THREAD_LOCAL_STORAGE
@@ -1334,8 +1747,9 @@
 // Flush buffered message, called by the destructor, or any other function
 // that needs to synchronize the log.
 void LogMessage::Flush() {
-  if (data_->has_been_flushed_ || data_->severity_ < FLAGS_minloglevel)
+  if (data_->has_been_flushed_ || data_->severity_ < FLAGS_minloglevel) {
     return;
+  }
 
   data_->num_chars_to_log_ = data_->stream_.pcount();
   data_->num_chars_to_syslog_ =
@@ -1355,6 +1769,7 @@
     original_final_char = data_->message_text_[data_->num_chars_to_log_];
     data_->message_text_[data_->num_chars_to_log_++] = '\n';
   }
+  data_->message_text_[data_->num_chars_to_log_] = '\0';
 
   // Prevent any subtle race conditions by wrapping a mutex lock around
   // the actual logging action per se.
@@ -1393,7 +1808,7 @@
 
 void ReprintFatalMessage() {
   if (fatal_message[0]) {
-    const int n = strlen(fatal_message);
+    const size_t n = strlen(fatal_message);
     if (!FLAGS_logtostderr) {
       // Also write to stderr (don't color to avoid terminal checks)
       WriteToStderr(fatal_message, n);
@@ -1423,34 +1838,39 @@
   // global flag: never log to file if set.  Also -- don't log to a
   // file if we haven't parsed the command line flags to get the
   // program name.
-  if (FLAGS_logtostderr || !IsGoogleLoggingInitialized()) {
-    ColoredWriteToStderr(data_->severity_,
-                         data_->message_text_, data_->num_chars_to_log_);
+  if (FLAGS_logtostderr || FLAGS_logtostdout || !IsGoogleLoggingInitialized()) {
+    if (FLAGS_logtostdout) {
+      ColoredWriteToStdout(data_->severity_, data_->message_text_,
+                           data_->num_chars_to_log_);
+    } else {
+      ColoredWriteToStderr(data_->severity_, data_->message_text_,
+                           data_->num_chars_to_log_);
+    }
 
     // this could be protected by a flag if necessary.
     LogDestination::LogToSinks(data_->severity_,
                                data_->fullname_, data_->basename_,
-                               data_->line_, &data_->tm_time_,
+                               data_->line_, logmsgtime_,
                                data_->message_text_ + data_->num_prefix_chars_,
                                (data_->num_chars_to_log_ -
-                                data_->num_prefix_chars_ - 1));
+                                data_->num_prefix_chars_ - 1) );
   } else {
-
     // log this message to all log files of severity <= severity_
-    LogDestination::LogToAllLogfiles(data_->severity_, data_->timestamp_,
+    LogDestination::LogToAllLogfiles(data_->severity_, logmsgtime_.timestamp(),
                                      data_->message_text_,
                                      data_->num_chars_to_log_);
 
     LogDestination::MaybeLogToStderr(data_->severity_, data_->message_text_,
-                                     data_->num_chars_to_log_);
+                                     data_->num_chars_to_log_,
+                                     data_->num_prefix_chars_);
     LogDestination::MaybeLogToEmail(data_->severity_, data_->message_text_,
                                     data_->num_chars_to_log_);
     LogDestination::LogToSinks(data_->severity_,
                                data_->fullname_, data_->basename_,
-                               data_->line_, &data_->tm_time_,
+                               data_->line_, logmsgtime_,
                                data_->message_text_ + data_->num_prefix_chars_,
                                (data_->num_chars_to_log_
-                                - data_->num_prefix_chars_ - 1));
+                                - data_->num_prefix_chars_ - 1) );
     // NOTE: -1 removes trailing \n
   }
 
@@ -1465,17 +1885,18 @@
       SetCrashReason(&crash_reason);
 
       // Store shortened fatal message for other logs and GWQ status
-      const int copy = min<int>(data_->num_chars_to_log_,
+      const size_t copy = min(data_->num_chars_to_log_,
                                 sizeof(fatal_message)-1);
       memcpy(fatal_message, data_->message_text_, copy);
       fatal_message[copy] = '\0';
-      fatal_time = data_->timestamp_;
+      fatal_time = logmsgtime_.timestamp();
     }
 
-    if (!FLAGS_logtostderr) {
+    if (!FLAGS_logtostderr && !FLAGS_logtostdout) {
       for (int i = 0; i < NUM_SEVERITIES; ++i) {
-        if ( LogDestination::log_destinations_[i] )
+        if (LogDestination::log_destinations_[i]) {
           LogDestination::log_destinations_[i]->logger_->Write(true, 0, "", 0);
+        }
       }
     }
 
@@ -1491,6 +1912,12 @@
     if (write(STDERR_FILENO, message, strlen(message)) < 0) {
       // Ignore errors.
     }
+#if defined(__ANDROID__)
+    // ANDROID_LOG_FATAL as this message is of FATAL severity.
+    __android_log_write(ANDROID_LOG_FATAL,
+                        glog_internal_namespace_::ProgramInvocationShortName(),
+                        message);
+#endif
     Fail();
   }
 }
@@ -1509,28 +1936,11 @@
 #endif
 }
 
-#ifdef HAVE___ATTRIBUTE__
-# define ATTRIBUTE_NORETURN __attribute__((noreturn))
-#else
-# define ATTRIBUTE_NORETURN
-#endif
+GLOG_EXPORT logging_fail_func_t g_logging_fail_func =
+    reinterpret_cast<logging_fail_func_t>(&abort);
 
-#if defined(OS_WINDOWS)
-__declspec(noreturn)
-#endif
-static void logging_fail() ATTRIBUTE_NORETURN;
-
-static void logging_fail() {
-  abort();
-}
-
-typedef void (*logging_fail_func_t)() ATTRIBUTE_NORETURN;
-
-GOOGLE_GLOG_DLL_DECL
-logging_fail_func_t g_logging_fail_func = &logging_fail;
-
-void InstallFailureFunction(void (*fail_func)()) {
-  g_logging_fail_func = (logging_fail_func_t)fail_func;
+void InstallFailureFunction(logging_fail_func_t fail_func) {
+  g_logging_fail_func = fail_func;
 }
 
 void LogMessage::Fail() {
@@ -1543,10 +1953,10 @@
     RAW_DCHECK(data_->num_chars_to_log_ > 0 &&
                data_->message_text_[data_->num_chars_to_log_-1] == '\n', "");
     data_->sink_->send(data_->severity_, data_->fullname_, data_->basename_,
-                       data_->line_, &data_->tm_time_,
+                       data_->line_, logmsgtime_,
                        data_->message_text_ + data_->num_prefix_chars_,
                        (data_->num_chars_to_log_ -
-                        data_->num_prefix_chars_ - 1));
+                        data_->num_prefix_chars_ - 1) );
   }
 }
 
@@ -1563,7 +1973,7 @@
                data_->message_text_[data_->num_chars_to_log_-1] == '\n', "");
     // Omit prefix of message and trailing newline when recording in outvec_.
     const char *start = data_->message_text_ + data_->num_prefix_chars_;
-    int len = data_->num_chars_to_log_ - data_->num_prefix_chars_ - 1;
+    size_t len = data_->num_chars_to_log_ - data_->num_prefix_chars_ - 1;
     data_->outvec_->push_back(string(start, len));
   } else {
     SendToLog();
@@ -1576,7 +1986,7 @@
                data_->message_text_[data_->num_chars_to_log_-1] == '\n', "");
     // Omit prefix of message and trailing newline when writing to message_.
     const char *start = data_->message_text_ + data_->num_prefix_chars_;
-    int len = data_->num_chars_to_log_ - data_->num_prefix_chars_ - 1;
+    size_t len = data_->num_chars_to_log_ - data_->num_prefix_chars_ - 1;
     data_->message_->assign(start, len);
   }
   SendToLog();
@@ -1636,10 +2046,9 @@
 }
 
 ErrnoLogMessage::ErrnoLogMessage(const char* file, int line,
-                                 LogSeverity severity, int ctr,
+                                 LogSeverity severity, int64 ctr,
                                  void (LogMessage::*send_method)())
-    : LogMessage(file, line, severity, ctr, send_method) {
-}
+    : LogMessage(file, line, severity, ctr, send_method) {}
 
 ErrnoLogMessage::~ErrnoLogMessage() {
   // Don't access errno directly because it may have been altered
@@ -1667,30 +2076,57 @@
 LogSink::~LogSink() {
 }
 
+void LogSink::send(LogSeverity severity, const char* full_filename,
+                   const char* base_filename, int line,
+                   const LogMessageTime& time, const char* message,
+                   size_t message_len) {
+#if defined(__GNUC__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#elif defined(_MSC_VER)
+#pragma warning(push)
+#pragma warning(disable : 4996)
+#endif  // __GNUC__
+  send(severity, full_filename, base_filename, line, &time.tm(), message,
+       message_len);
+#if defined(__GNUC__)
+#pragma GCC diagnostic pop
+#elif defined(_MSC_VER)
+#pragma warning(pop)
+#endif  // __GNUC__
+}
+
+void LogSink::send(LogSeverity severity, const char* full_filename,
+                   const char* base_filename, int line, const std::tm* t,
+                   const char* message, size_t message_len) {
+  (void)severity;
+  (void)full_filename;
+  (void)base_filename;
+  (void)line;
+  (void)t;
+  (void)message;
+  (void)message_len;
+}
+
 void LogSink::WaitTillSent() {
   // noop default
 }
 
 string LogSink::ToString(LogSeverity severity, const char* file, int line,
-                         const struct ::tm* tm_time,
-                         const char* message, size_t message_len) {
+                         const LogMessageTime& logmsgtime, const char* message,
+                         size_t message_len) {
   ostringstream stream(string(message, message_len));
   stream.fill('0');
 
-  // FIXME(jrvb): Updating this to use the correct value for usecs
-  // requires changing the signature for both this method and
-  // LogSink::send().  This change needs to be done in a separate CL
-  // so subclasses of LogSink can be updated at the same time.
-  int usecs = 0;
-
   stream << LogSeverityNames[severity][0]
-         << setw(2) << 1+tm_time->tm_mon
-         << setw(2) << tm_time->tm_mday
+         << setw(4) << 1900 + logmsgtime.year()
+         << setw(2) << 1 + logmsgtime.month()
+         << setw(2) << logmsgtime.day()
          << ' '
-         << setw(2) << tm_time->tm_hour << ':'
-         << setw(2) << tm_time->tm_min << ':'
-         << setw(2) << tm_time->tm_sec << '.'
-         << setw(6) << usecs
+         << setw(2) << logmsgtime.hour() << ':'
+         << setw(2) << logmsgtime.min() << ':'
+         << setw(2) << logmsgtime.sec() << '.'
+         << setw(6) << logmsgtime.usec()
          << ' '
          << setfill(' ') << setw(5) << GetTID() << setfill('0')
          << ' '
@@ -1752,6 +2188,7 @@
 }  // namespace internal
 }  // namespace base
 
+#ifndef GLOG_OS_EMSCRIPTEN
 // Shell-escaping as we need to shell out ot /bin/mail.
 static const char kDontNeedShellEscapeChars[] =
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -1786,13 +2223,14 @@
   }
   return result;
 }
-
+#endif
 
 // use_logging controls whether the logging functions LOG/VLOG are used
 // to log errors.  It should be set to false when the caller holds the
 // log_mutex.
 static bool SendEmailInternal(const char*dest, const char *subject,
                               const char*body, bool use_logging) {
+#ifndef GLOG_OS_EMSCRIPTEN
   if (dest && *dest) {
     if ( use_logging ) {
       VLOG(1) << "Trying to send TITLE:" << subject
@@ -1802,16 +2240,23 @@
               subject, body, dest);
     }
 
+    string logmailer = FLAGS_logmailer;
+    if (logmailer.empty()) {
+        logmailer = "/bin/mail";
+    }
     string cmd =
-        FLAGS_logmailer + " -s" +
+        logmailer + " -s" +
         ShellEscape(subject) + " " + ShellEscape(dest);
-    VLOG(4) << "Mailing command: " << cmd;
+    if (use_logging) {
+        VLOG(4) << "Mailing command: " << cmd;
+    }
 
     FILE* pipe = popen(cmd.c_str(), "w");
     if (pipe != NULL) {
       // Add the body if we have one
-      if (body)
+      if (body) {
         fwrite(body, sizeof(char), strlen(body), pipe);
+      }
       bool ok = pclose(pipe) != -1;
       if ( !ok ) {
         if ( use_logging ) {
@@ -1831,6 +2276,13 @@
       }
     }
   }
+#else
+  (void)dest;
+  (void)subject;
+  (void)body;
+  (void)use_logging;
+  LOG(WARNING) << "Email support not available; not sending message";
+#endif
   return false;
 }
 
@@ -1840,7 +2292,7 @@
 
 static void GetTempDirectories(vector<string>* list) {
   list->clear();
-#ifdef OS_WINDOWS
+#ifdef GLOG_OS_WINDOWS
   // On windows we'll try to find a directory in this order:
   //   C:/Documents & Settings/whomever/TEMP (or whatever GetTempPath() is)
   //   C:/TMP/
@@ -1896,10 +2348,10 @@
 
     if ( !FLAGS_log_dir.empty() ) {
       // A dir was specified, we should use it
-      logging_directories_list->push_back(FLAGS_log_dir.c_str());
+      logging_directories_list->push_back(FLAGS_log_dir);
     } else {
       GetTempDirectories(logging_directories_list);
-#ifdef OS_WINDOWS
+#ifdef GLOG_OS_WINDOWS
       char tmp[MAX_PATH];
       if (GetWindowsDirectoryA(tmp, MAX_PATH))
         logging_directories_list->push_back(tmp);
@@ -1933,16 +2385,16 @@
   }
 }
 
-void TruncateLogFile(const char *path, int64 limit, int64 keep) {
+void TruncateLogFile(const char *path, uint64 limit, uint64 keep) {
 #ifdef HAVE_UNISTD_H
   struct stat statbuf;
   const int kCopyBlockSize = 8 << 10;
   char copybuf[kCopyBlockSize];
-  int64 read_offset, write_offset;
+  off_t read_offset, write_offset;
   // Don't follow symlinks unless they're our own fd symlinks in /proc
   int flags = O_RDWR;
   // TODO(hamaji): Support other environments.
-#ifdef OS_LINUX
+#ifdef GLOG_OS_LINUX
   const char *procfd_prefix = "/proc/self/fd/";
   if (strncmp(procfd_prefix, path, strlen(procfd_prefix))) flags |= O_NOFOLLOW;
 #endif
@@ -1974,18 +2426,18 @@
   // See if the path refers to a regular file bigger than the
   // specified limit
   if (!S_ISREG(statbuf.st_mode)) goto out_close_fd;
-  if (statbuf.st_size <= limit)  goto out_close_fd;
-  if (statbuf.st_size <= keep) goto out_close_fd;
+  if (statbuf.st_size <= static_cast<off_t>(limit))  goto out_close_fd;
+  if (statbuf.st_size <= static_cast<off_t>(keep)) goto out_close_fd;
 
   // This log file is too large - we need to truncate it
   LOG(INFO) << "Truncating " << path << " to " << keep << " bytes";
 
   // Copy the last "keep" bytes of the file to the beginning of the file
-  read_offset = statbuf.st_size - keep;
+  read_offset = statbuf.st_size - static_cast<off_t>(keep);
   write_offset = 0;
-  int bytesin, bytesout;
+  ssize_t bytesin, bytesout;
   while ((bytesin = pread(fd, copybuf, sizeof(copybuf), read_offset)) > 0) {
-    bytesout = pwrite(fd, copybuf, bytesin, write_offset);
+    bytesout = pwrite(fd, copybuf, static_cast<size_t>(bytesin), write_offset);
     if (bytesout == -1) {
       PLOG(ERROR) << "Unable to write to " << path;
       break;
@@ -2009,12 +2461,12 @@
 #else
   LOG(ERROR) << "No log truncation support.";
 #endif
-}
+ }
 
 void TruncateStdoutStderr() {
 #ifdef HAVE_UNISTD_H
-  int64 limit = MaxLogSize() << 20;
-  int64 keep = 1 << 20;
+  uint64 limit = MaxLogSize() << 20U;
+  uint64 keep = 1U << 20U;
   TruncateLogFile("/proc/self/fd/1", limit, keep);
   TruncateLogFile("/proc/self/fd/2", limit, keep);
 #else
@@ -2081,7 +2533,7 @@
       return 0;
     } else {
       buf[0] = '\000';
-#if defined(OS_MACOSX) || defined(OS_FREEBSD) || defined(OS_OPENBSD)
+#if defined(GLOG_OS_MACOSX) || defined(GLOG_OS_FREEBSD) || defined(GLOG_OS_OPENBSD)
       if (reinterpret_cast<intptr_t>(rc) < sys_nerr) {
         // This means an error on MacOSX or FreeBSD.
         return -1;
@@ -2142,7 +2594,7 @@
   if (v >= 32 && v <= 126) {
     (*os) << "'" << v << "'";
   } else {
-    (*os) << "char value " << (short)v;
+    (*os) << "char value " << static_cast<short>(v);
   }
 }
 
@@ -2151,7 +2603,7 @@
   if (v >= 32 && v <= 126) {
     (*os) << "'" << v << "'";
   } else {
-    (*os) << "signed char value " << (short)v;
+    (*os) << "signed char value " << static_cast<short>(v);
   }
 }
 
@@ -2160,14 +2612,31 @@
   if (v >= 32 && v <= 126) {
     (*os) << "'" << v << "'";
   } else {
-    (*os) << "unsigned char value " << (unsigned short)v;
+    (*os) << "unsigned char value " << static_cast<unsigned short>(v);
   }
 }
 
+#if defined(HAVE_CXX11_NULLPTR_T) && __cplusplus >= 201103L
+template <>
+void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& /*v*/) {
+  (*os) << "nullptr";
+}
+#endif // defined(HAVE_CXX11_NULLPTR_T)
+
 void InitGoogleLogging(const char* argv0) {
   glog_internal_namespace_::InitGoogleLoggingUtilities(argv0);
 }
 
+#ifdef GLOG_CUSTOM_PREFIX_SUPPORT
+void InitGoogleLogging(const char* argv0,
+                       CustomPrefixCallback prefix_callback,
+                       void* prefix_callback_data) {
+  custom_prefix_callback = prefix_callback;
+  custom_prefix_callback_data = prefix_callback_data;
+  InitGoogleLogging(argv0);
+}
+#endif
+
 void ShutdownGoogleLogging() {
   glog_internal_namespace_::ShutdownGoogleLoggingUtilities();
   LogDestination::DeleteLogDestinations();
@@ -2175,4 +2644,56 @@
   logging_directories_list = NULL;
 }
 
+void EnableLogCleaner(unsigned int overdue_days) {
+  log_cleaner.Enable(overdue_days);
+}
+
+void DisableLogCleaner() {
+  log_cleaner.Disable();
+}
+
+LogMessageTime::LogMessageTime()
+    : time_struct_(), timestamp_(0), usecs_(0), gmtoffset_(0) {}
+
+LogMessageTime::LogMessageTime(std::tm t) {
+  std::time_t timestamp = std::mktime(&t);
+  init(t, timestamp, 0);
+}
+
+LogMessageTime::LogMessageTime(std::time_t timestamp, WallTime now) {
+  std::tm t;
+  if (FLAGS_log_utc_time)
+    gmtime_r(&timestamp, &t);
+  else
+    localtime_r(&timestamp, &t);
+  init(t, timestamp, now);
+}
+
+void LogMessageTime::init(const std::tm& t, std::time_t timestamp,
+                          WallTime now) {
+  time_struct_ = t;
+  timestamp_ = timestamp;
+  usecs_ = static_cast<int32>((now - timestamp) * 1000000);
+
+  CalcGmtOffset();
+}
+
+void LogMessageTime::CalcGmtOffset() {
+  std::tm gmt_struct;
+  int isDst = 0;
+  if ( FLAGS_log_utc_time ) {
+    localtime_r(&timestamp_, &gmt_struct);
+    isDst = gmt_struct.tm_isdst;
+    gmt_struct = time_struct_;
+  } else {
+    isDst = time_struct_.tm_isdst;
+    gmtime_r(&timestamp_, &gmt_struct);
+  }
+
+  time_t gmt_sec = mktime(&gmt_struct);
+  const long hour_secs = 3600;
+  // If the Daylight Saving Time(isDst) is active subtract an hour from the current timestamp.
+  gmtoffset_ = static_cast<long int>(timestamp_ - gmt_sec + (isDst ? hour_secs : 0) ) ;
+}
+
 _END_GOOGLE_NAMESPACE_