32 #include <gtest/gtest.h>
52 using std::stringstream;
55 using std::shared_ptr;
56 using std::system_error;
57 using std::make_shared;
58 using std::istreambuf_iterator;
63 struct LoggerTest :
public testing::Test
65 shared_ptr<stringstream> strings;
67 shared_ptr<ofstream> files;
75 fs::remove_all(
"sandbox", &err);
79 strings.reset(
new stringstream());
80 fname =
"ofstream_test";
81 files.reset(
new ofstream(fname));
91 fs::remove_all(
"sandbox", &err);
94 std::string read_file()
100 istreambuf_iterator<char> it{f}, end;
105 void validate(
const string& logs)
107 ASSERT_EQ(strings->str(), logs);
108 ASSERT_EQ(read_file(), logs);
112 TEST(LoggerTestBasic, move_and_construct)
116 auto s = make_shared<stringstream>();
122 log <<
"Original 1" << endl;
123 ver <<
"Original 1" << endl;
126 cc <<
"Copy constructed" << endl;
127 ver <<
"Copy constructed" << endl;
128 log <<
"Original 2" << endl;
129 ver <<
"Original 2" << endl;
132 ca <<
"Copy assigned" << endl;
133 ver <<
"Copy assigned" << endl;
134 log <<
"Original 3" << endl;
135 ver <<
"Original 3" << endl;
138 mc <<
"Move constructed" << endl;
139 ver <<
"Move constructed" << endl;
140 cc <<
"Original 4 (won't see this)" << endl;
142 Logger ma = std::move(ca);
143 ma <<
"Move assigned" << endl;
144 ver <<
"Move assigned" << endl;
145 ca <<
"Original 5 (won't see this)" << endl;
147 cout <<
"** start readback from log stringstream" << endl;
149 cout <<
"** end readback from log stringstream" << endl;
151 cout <<
"** start readback from verify stringstream" << endl;
153 cout <<
"** end readback from verify stringstream" << endl;
155 ASSERT_EQ(s->str(), ver.str());
161 log <<
"line" << endl;
162 validate(
"[test] line\n");
165 TEST_F(LoggerTest, timestamp)
168 log <<
"line" << endl;
170 ASSERT_EQ(strings->str().size(), 26);
171 ASSERT_EQ(strings->str().substr(20),
" line\n");
174 TEST_F(LoggerTest, multiline)
180 log <<
"m 1" << endl;
181 s <<
"[1] m 1" << endl;
182 log <<
" m 2" << endl;
184 log <<
" m 3" << endl;
186 log <<
" m 4" << endl;
188 log <<
" m 5" << endl;
190 log <<
" m 6" << endl;
191 s <<
"[1] m 6" << endl;
196 TEST_F(LoggerTest, multithread_multi_loggers)
220 for (
int i = 1; i <= 5; i++)
222 tlog <<
"this is log number " << i <<
" from thread number " << n << endl;
228 for (
int n = 1; n <= 5; n++)
230 tv.emplace_back(f, n);
234 for (
int i = 1; i <= 5; i++)
236 log <<
"this is log number " << i <<
" from thread number " << 0 << endl;
249 set<string> ordered_strings;
250 while (getline(*strings, line))
252 ordered_strings.insert(line);
255 stringstream
fs(read_file());
256 set<string> ordered_files;
257 while (getline(
fs, line))
259 ordered_files.insert(line);
262 cout <<
"* ordered stringstream" << endl;
263 for (
auto& s : ordered_strings)
267 cout <<
"* ordered filestream" << endl;
268 for (
auto& s : ordered_strings)
273 ASSERT_EQ(ordered_strings, ordered_files);
278 cout <<
"validate the ordered logs:" << endl;
279 for (
auto& s : ordered_strings)
285 val <<
"[" << cur_thread <<
"] this is log number " << cur_line <<
" from thread number " << cur_thread;
287 ASSERT_EQ(val.str(), s);
301 ASSERT_EQ(cur_thread, 6);
302 ASSERT_EQ(cur_line, 1);
Common file system utilities.
static std::string get_current_dir(std::system_error *=nullptr)
Get working directory.
static void change_dir(const std::string &, std::system_error *=nullptr)
Change working directory.
static void create_dir(const std::string &, std::system_error *=nullptr)
Create a directory.
Thread-safe stream logger.
void multiline(unsigned int max=0)
Set multiline mode.
void add(const std::shared_ptr< std::ostream > &)
Add a shared stream.
void timestamp_iso(bool utc_on=true)
Set iso 8601 timestamp.
void id(const std::string &id="")
Set the id.
void add_cout()
Add std::cout console stream.
Common file system utilities.
TEST(inet_example, client_server_stream_test)
[Inet client server]
TEST_F(LoggerTest, multithread_multi_loggers)