Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <getopt.h> |
| 3 | |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 4 | #include "gflags/gflags.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 5 | #include "glog/logging.h" |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
Brian Silverman | 9c72d7b | 2015-03-30 17:29:04 -0400 | [diff] [blame] | 7 | |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 8 | DEFINE_bool(print_logs, false, |
| 9 | "Print the log messages as they are being generated."); |
| 10 | DEFINE_string(log_file, "", |
| 11 | "Print all log messages to FILE instead of standard output."); |
| 12 | |
Brian Silverman | 9c72d7b | 2015-03-30 17:29:04 -0400 | [diff] [blame] | 13 | namespace aos { |
Austin Schuh | cdab619 | 2019-12-29 17:47:46 -0800 | [diff] [blame] | 14 | void SetShmBase(const std::string_view base) __attribute__((weak)); |
| 15 | |
Brian Silverman | 9c72d7b | 2015-03-30 17:29:04 -0400 | [diff] [blame] | 16 | namespace testing { |
| 17 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 18 | // Actually declared/defined in //aos/testing:test_logging. |
Brian Silverman | 9c72d7b | 2015-03-30 17:29:04 -0400 | [diff] [blame] | 19 | void SetLogFileName(const char* filename) __attribute__((weak)); |
| 20 | void ForcePrintLogsDuringTests() __attribute__((weak)); |
| 21 | |
| 22 | } // namespace testing |
Brian Silverman | 9c72d7b | 2015-03-30 17:29:04 -0400 | [diff] [blame] | 23 | } // namespace aos |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 24 | |
| 25 | GTEST_API_ int main(int argc, char **argv) { |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 26 | ::testing::InitGoogleTest(&argc, argv); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 27 | FLAGS_logtostderr = true; |
| 28 | google::InitGoogleLogging(argv[0]); |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 29 | ::gflags::ParseCommandLineFlags(&argc, &argv, false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | google::InstallFailureSignalHandler(); |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 31 | |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 32 | if (FLAGS_print_logs) { |
| 33 | if (::aos::testing::ForcePrintLogsDuringTests) { |
| 34 | ::aos::testing::ForcePrintLogsDuringTests(); |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 35 | } |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 36 | } |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 37 | |
Austin Schuh | 6b73f0d | 2019-01-07 17:03:03 -0800 | [diff] [blame] | 38 | if (!FLAGS_log_file.empty()) { |
| 39 | if (::aos::testing::ForcePrintLogsDuringTests) { |
| 40 | ::aos::testing::ForcePrintLogsDuringTests(); |
| 41 | } |
| 42 | if (::aos::testing::SetLogFileName) { |
| 43 | ::aos::testing::SetLogFileName(FLAGS_log_file.c_str()); |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Austin Schuh | cdab619 | 2019-12-29 17:47:46 -0800 | [diff] [blame] | 47 | // Point shared memory away from /dev/shm if we are testing. We don't care |
| 48 | // about RT in this case, so if it is backed by disk, we are fine. |
| 49 | if (::aos::SetShmBase) { |
| 50 | const char *tmpdir_c_str = getenv("TEST_TMPDIR"); |
| 51 | if (tmpdir_c_str != nullptr) { |
| 52 | aos::SetShmBase(tmpdir_c_str); |
| 53 | } |
| 54 | } |
| 55 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 56 | return RUN_ALL_TESTS(); |
| 57 | } |