blob: adb516cf67a9b951c436ce1572638c21d2687c54 [file] [log] [blame]
Philipp Schradere41ed9d2015-03-15 22:57:13 +00001#include <iostream>
2#include <getopt.h>
3
Austin Schuh6b73f0d2019-01-07 17:03:03 -08004#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -07005#include "glog/logging.h"
Philipp Schradere41ed9d2015-03-15 22:57:13 +00006#include "gtest/gtest.h"
Brian Silverman9c72d7b2015-03-30 17:29:04 -04007
Austin Schuh6b73f0d2019-01-07 17:03:03 -08008DEFINE_bool(print_logs, false,
9 "Print the log messages as they are being generated.");
10DEFINE_string(log_file, "",
11 "Print all log messages to FILE instead of standard output.");
12
Brian Silverman9c72d7b2015-03-30 17:29:04 -040013namespace aos {
Austin Schuhcdab6192019-12-29 17:47:46 -080014void SetShmBase(const std::string_view base) __attribute__((weak));
15
Brian Silverman9c72d7b2015-03-30 17:29:04 -040016namespace testing {
17
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050018// Actually declared/defined in //aos/testing:test_logging.
Brian Silverman9c72d7b2015-03-30 17:29:04 -040019void SetLogFileName(const char* filename) __attribute__((weak));
20void ForcePrintLogsDuringTests() __attribute__((weak));
21
22} // namespace testing
Brian Silverman9c72d7b2015-03-30 17:29:04 -040023} // namespace aos
Philipp Schradere41ed9d2015-03-15 22:57:13 +000024
25GTEST_API_ int main(int argc, char **argv) {
Austin Schuh6b73f0d2019-01-07 17:03:03 -080026 ::testing::InitGoogleTest(&argc, argv);
Austin Schuhcb108412019-10-13 16:09:54 -070027 FLAGS_logtostderr = true;
28 google::InitGoogleLogging(argv[0]);
Austin Schuh6b73f0d2019-01-07 17:03:03 -080029 ::gflags::ParseCommandLineFlags(&argc, &argv, false);
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 google::InstallFailureSignalHandler();
Philipp Schradere41ed9d2015-03-15 22:57:13 +000031
Austin Schuh6b73f0d2019-01-07 17:03:03 -080032 if (FLAGS_print_logs) {
33 if (::aos::testing::ForcePrintLogsDuringTests) {
34 ::aos::testing::ForcePrintLogsDuringTests();
Philipp Schradere41ed9d2015-03-15 22:57:13 +000035 }
Austin Schuh6b73f0d2019-01-07 17:03:03 -080036 }
Philipp Schradere41ed9d2015-03-15 22:57:13 +000037
Austin Schuh6b73f0d2019-01-07 17:03:03 -080038 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 Schradere41ed9d2015-03-15 22:57:13 +000044 }
45 }
46
Austin Schuhcdab6192019-12-29 17:47:46 -080047 // 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 Schradere41ed9d2015-03-15 22:57:13 +000056 return RUN_ALL_TESTS();
57}