blob: a6e944cc7c99665c58df79f67fca78b57d6ad2ff [file] [log] [blame]
Philipp Schradere41ed9d2015-03-15 22:57:13 +00001#include <getopt.h>
2
Austin Schuh60e77942022-05-16 17:48:24 -07003#include <iostream>
4
Austin Schuh62288252020-11-18 23:26:04 -08005#include "aos/init.h"
Austin Schuh6b73f0d2019-01-07 17:03:03 -08006#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -07007#include "glog/logging.h"
Philipp Schradere41ed9d2015-03-15 22:57:13 +00008#include "gtest/gtest.h"
Brian Silverman9c72d7b2015-03-30 17:29:04 -04009
Austin Schuh6b73f0d2019-01-07 17:03:03 -080010DEFINE_bool(print_logs, false,
11 "Print the log messages as they are being generated.");
12DEFINE_string(log_file, "",
13 "Print all log messages to FILE instead of standard output.");
14
Brian Silverman9c72d7b2015-03-30 17:29:04 -040015namespace aos {
Austin Schuhcdab6192019-12-29 17:47:46 -080016void SetShmBase(const std::string_view base) __attribute__((weak));
17
Brian Silverman9c72d7b2015-03-30 17:29:04 -040018namespace testing {
19
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050020// Actually declared/defined in //aos/testing:test_logging.
Austin Schuh60e77942022-05-16 17:48:24 -070021void SetLogFileName(const char *filename) __attribute__((weak));
Brian Silverman9c72d7b2015-03-30 17:29:04 -040022void ForcePrintLogsDuringTests() __attribute__((weak));
23
24} // namespace testing
Brian Silverman9c72d7b2015-03-30 17:29:04 -040025} // namespace aos
Philipp Schradere41ed9d2015-03-15 22:57:13 +000026
27GTEST_API_ int main(int argc, char **argv) {
Austin Schuh6b73f0d2019-01-07 17:03:03 -080028 ::testing::InitGoogleTest(&argc, argv);
Austin Schuhcb108412019-10-13 16:09:54 -070029 FLAGS_logtostderr = true;
Austin Schuh62288252020-11-18 23:26:04 -080030
31 aos::InitGoogle(&argc, &argv);
Philipp Schradere41ed9d2015-03-15 22:57:13 +000032
Austin Schuh6b73f0d2019-01-07 17:03:03 -080033 if (FLAGS_print_logs) {
34 if (::aos::testing::ForcePrintLogsDuringTests) {
35 ::aos::testing::ForcePrintLogsDuringTests();
Philipp Schradere41ed9d2015-03-15 22:57:13 +000036 }
Austin Schuh6b73f0d2019-01-07 17:03:03 -080037 }
Philipp Schradere41ed9d2015-03-15 22:57:13 +000038
Austin Schuh6b73f0d2019-01-07 17:03:03 -080039 if (!FLAGS_log_file.empty()) {
40 if (::aos::testing::ForcePrintLogsDuringTests) {
41 ::aos::testing::ForcePrintLogsDuringTests();
42 }
43 if (::aos::testing::SetLogFileName) {
44 ::aos::testing::SetLogFileName(FLAGS_log_file.c_str());
Philipp Schradere41ed9d2015-03-15 22:57:13 +000045 }
46 }
47
Austin Schuhcdab6192019-12-29 17:47:46 -080048 // Point shared memory away from /dev/shm if we are testing. We don't care
49 // about RT in this case, so if it is backed by disk, we are fine.
50 if (::aos::SetShmBase) {
51 const char *tmpdir_c_str = getenv("TEST_TMPDIR");
52 if (tmpdir_c_str != nullptr) {
53 aos::SetShmBase(tmpdir_c_str);
54 }
55 }
56
Philipp Schradere41ed9d2015-03-15 22:57:13 +000057 return RUN_ALL_TESTS();
58}