1 //
2 // This header is used across tests to include all the valid headers
3 //
4
5 #ifndef TEST_HELPERS_H_
6 #define TEST_HELPERS_H_
7
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wundef"
10 #pragma GCC diagnostic ignored "-Wsign-compare"
11 #include <gtest/gtest.h>
12 #pragma GCC diagnostic pop
13
14 #include "easylogging++.h"
15
16 using namespace el;
17 using namespace el::base;
18 using namespace el::base::utils;
19
20 INITIALIZE_EASYLOGGINGPP
21
22 static const char* logfile = "/tmp/logs/el.gtest.log";
23
reconfigureLoggersForTest(void)24 static void reconfigureLoggersForTest(void) {
25 Configurations c;
26 c.setGlobally(ConfigurationType::Format, "%datetime{%a %b %d, %H:%m} %msg");
27 c.setGlobally(ConfigurationType::Filename, "/tmp/logs/el.gtest.log");
28 c.setGlobally(ConfigurationType::MaxLogFileSize, "2097152"); // 2MB
29 c.setGlobally(ConfigurationType::ToStandardOutput, "false");
30 c.setGlobally(ConfigurationType::PerformanceTracking, "true");
31 c.setGlobally(ConfigurationType::LogFlushThreshold, "1");
32 Loggers::setDefaultConfigurations(c, true);
33 // We do not want to reconfgure syslog with date/time
34 Loggers::reconfigureLogger(consts::kSysLogLoggerId, ConfigurationType::Format, "%level: %msg");
35
36 Loggers::addFlag(LoggingFlag::DisableApplicationAbortOnFatalLog);
37 Loggers::addFlag(LoggingFlag::ImmediateFlush);
38 Loggers::addFlag(LoggingFlag::StrictLogFileSizeCheck);
39 Loggers::addFlag(LoggingFlag::DisableVModulesExtensions);
40 }
41
42 static std::string tail(unsigned int n, const char* filename = logfile) {
43 if (n == 0) return std::string();
44 std::ifstream fstr(filename);
45 if (!fstr.is_open()) {
46 return std::string();
47 }
48 fstr.seekg(0, fstr.end);
49 int size = static_cast<int>(fstr.tellg());
50 int ncopy = n + 1;
51 for (int i = (size - 1); i >= 0; --i) {
52 fstr.seekg(i);
53 char c = fstr.get();
54 if (c == '\n' && --ncopy == 0) {
55 break;
56 }
57 if (i == 0) {
58 fstr.seekg(i); // fstr.get() increments buff, so we reset it
59 }
60 }
61 std::stringstream ss;
62 char c = fstr.get();
63 while (fstr.good()) {
64 ss << c;
65 c = fstr.get();
66 }
67 fstr.close();
68 return ss.str();
69 }
70
71 static std::string getDate(const char* format = "%a %b %d, %H:%m") {
72 SubsecondPrecision ssPrec(3);
73 return DateTime::getDateTime(format, &ssPrec);
74 }
75
fileExists(const char * filename)76 static bool fileExists(const char* filename) {
77 el::base::type::fstream_t fstr(filename, el::base::type::fstream_t::in);
78 return fstr.is_open();
79 }
80
81 static void cleanFile(const char* filename = logfile, el::base::type::fstream_t* fs = nullptr) {
82 if (fs != nullptr && fs->is_open()) {
83 fs->close();
84 fs->open(filename, el::base::type::fstream_t::out);
85 } else {
86 el::base::type::fstream_t f(filename, el::base::type::fstream_t::out);
87 if (f.is_open()) {
88 f.close();
89 }
90 ELPP_UNUSED(f);
91 }
92 }
93
94 #undef BUILD_STR
95 #define BUILD_STR(strb) [&]() -> std::string { std::stringstream ssb; ssb << strb; return ssb.str(); }()
96
removeFile(const char * path)97 static void removeFile(const char* path) {
98 (void)(system(BUILD_STR("rm -rf " << path).c_str()) + 1); // (void)(...+1) -> ignore result for gcc 4.6+
99 }
100
101 static const char* kSysLogIdent = "easylogging++ unit test";
102 #endif // TEST_HELPERS_H_
103