|
| 1 | +// Copyright (c) 2024, Google Inc. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are |
| 6 | +// met: |
| 7 | +// |
| 8 | +// * Redistributions of source code must retain the above copyright |
| 9 | +// notice, this list of conditions and the following disclaimer. |
| 10 | +// * Redistributions in binary form must reproduce the above |
| 11 | +// copyright notice, this list of conditions and the following disclaimer |
| 12 | +// in the documentation and/or other materials provided with the |
| 13 | +// distribution. |
| 14 | +// * Neither the name of Google Inc. nor the names of its |
| 15 | +// contributors may be used to endorse or promote products derived from |
| 16 | +// this software without specific prior written permission. |
| 17 | +// |
| 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +#include "base/commandlineflags.h" |
| 31 | +#include "glog/logging.h" |
| 32 | +#include "glog/raw_logging.h" |
| 33 | +#include "googletest.h" |
| 34 | + |
| 35 | +#ifdef GLOG_USE_GFLAGS |
| 36 | +# include <gflags/gflags.h> |
| 37 | +using namespace GFLAGS_NAMESPACE; |
| 38 | +#endif |
| 39 | + |
| 40 | +#ifdef HAVE_LIB_GMOCK |
| 41 | +# include <gmock/gmock.h> |
| 42 | + |
| 43 | +# include "mock-log.h" |
| 44 | +// Introduce several symbols from gmock. |
| 45 | +using google::glog_testing::ScopedMockLog; |
| 46 | +using testing::_; |
| 47 | +using testing::AllOf; |
| 48 | +using testing::AnyNumber; |
| 49 | +using testing::HasSubstr; |
| 50 | +using testing::InitGoogleMock; |
| 51 | +using testing::StrictMock; |
| 52 | +using testing::StrNe; |
| 53 | +#endif |
| 54 | + |
| 55 | +using namespace google; |
| 56 | + |
| 57 | +TEST(CleanImmediatelyWithFilePathOverride, logging) { |
| 58 | + using namespace std::chrono_literals; |
| 59 | + google::EnableLogCleaner(0h); |
| 60 | + google::SetLogFilenameExtension(".logfileoverride"); |
| 61 | + google::SetLogDestination(GLOG_ERROR, "test_cleanup_error_"); |
| 62 | + google::OverrideLogCleanerFilePathPrefix("test_cleanup_"); |
| 63 | + |
| 64 | + LOG(ERROR) << "cleanup test"; |
| 65 | + |
| 66 | + google::DisableLogCleaner(); |
| 67 | +} |
| 68 | + |
| 69 | +int main(int argc, char** argv) { |
| 70 | + FLAGS_colorlogtostderr = false; |
| 71 | + FLAGS_timestamp_in_logfile_name = true; |
| 72 | +#ifdef GLOG_USE_GFLAGS |
| 73 | + ParseCommandLineFlags(&argc, &argv, true); |
| 74 | +#endif |
| 75 | + // Make sure stderr is not buffered as stderr seems to be buffered |
| 76 | + // on recent windows. |
| 77 | + setbuf(stderr, nullptr); |
| 78 | + |
| 79 | + // Test some basics before InitGoogleLogging: |
| 80 | + CaptureTestStderr(); |
| 81 | + const string early_stderr = GetCapturedTestStderr(); |
| 82 | + |
| 83 | + EXPECT_FALSE(IsGoogleLoggingInitialized()); |
| 84 | + |
| 85 | + InitGoogleLogging(argv[0]); |
| 86 | + |
| 87 | + EXPECT_TRUE(IsGoogleLoggingInitialized()); |
| 88 | + |
| 89 | + InitGoogleTest(&argc, argv); |
| 90 | +#ifdef HAVE_LIB_GMOCK |
| 91 | + InitGoogleMock(&argc, argv); |
| 92 | +#endif |
| 93 | + |
| 94 | + // so that death tests run before we use threads |
| 95 | + CHECK_EQ(RUN_ALL_TESTS(), 0); |
| 96 | +} |
0 commit comments