aws_log_system_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
17 #include <gtest/gtest.h>
18 
19 #include <iostream>
20 
21 using namespace Aws::Utils::Logging;
22 
24 {
25 private:
26  int log_info_count_ = 0;
27  int log_debug_count_ = 0;
28  int log_trace_count_ = 0;
29  int log_warn_count_ = 0;
30  int log_error_count_ = 0;
31  int log_fatal_count_ = 0;
32 
34  std::string expected_tag_;
35 
36 public:
37  explicit ClassUnderTest(Aws::Utils::Logging::LogLevel log_level) : AWSLogSystem(log_level) {}
38 
39  ~ClassUnderTest() = default;
40 
41  /*********************************
42  * Overidden log methods to test *
43  *********************************/
44 
45  void LogInfo(const char * tag, const std::string & message) override
46  {
47  log_info_count_++;
48  expected_log_message_string_ = message;
49  expected_tag_ = tag;
50  }
51 
52  void LogTrace(const char * tag, const std::string & message) override
53  {
54  log_trace_count_++;
55  expected_log_message_string_ = message;
56  expected_tag_ = tag;
57  }
58 
59  void LogDebug(const char * tag, const std::string & message) override
60  {
61  log_debug_count_++;
62  expected_log_message_string_ = message;
63  expected_tag_ = tag;
64  }
65 
66  void LogWarn(const char * tag, const std::string & message) override
67  {
68  log_warn_count_++;
69  expected_log_message_string_ = message;
70  expected_tag_ = tag;
71  }
72 
73  void LogError(const char * tag, const std::string & message) override
74  {
75  log_error_count_++;
76  expected_log_message_string_ = message;
77  expected_tag_ = tag;
78  }
79 
80  void LogFatal(const char * tag, const std::string & message) override
81  {
82  log_fatal_count_++;
83  expected_log_message_string_ = message;
84  expected_tag_ = tag;
85  }
86 
87  /*********************************
88  * static helper methods *
89  *********************************/
90 
91  static const std::string & GetExpectedLogMessageString(const ClassUnderTest * class_under_test)
92  {
93  return class_under_test->expected_log_message_string_;
94  }
95 
96  static const std::string & GetExpectedTag(const ClassUnderTest * class_under_test)
97  {
98  return class_under_test->expected_tag_;
99  }
100 
101  static int GetLogInfoCount(const ClassUnderTest * class_under_test)
102  {
103  return class_under_test->log_info_count_;
104  }
105 
106  static int GetLogDebugCount(const ClassUnderTest * class_under_test)
107  {
108  return class_under_test->log_debug_count_;
109  }
110 
111  static int GetLogTraceCount(const ClassUnderTest * class_under_test)
112  {
113  return class_under_test->log_trace_count_;
114  }
115 
116  static int GetLogWarnCount(const ClassUnderTest * class_under_test)
117  {
118  return class_under_test->log_warn_count_;
119  }
120 
121  static int GetLogErrorCount(const ClassUnderTest * class_under_test)
122  {
123  return class_under_test->log_error_count_;
124  }
125 
126  static int GetLogFatalCount(const ClassUnderTest * class_under_test)
127  {
128  return class_under_test->log_fatal_count_;
129  }
130 
131  static void SetExpectedLogMessageString(ClassUnderTest * class_under_test,
132  const std::string & message)
133  {
134  class_under_test->expected_log_message_string_ = message;
135  }
136 
137  static void SetExpectedTagString(ClassUnderTest * class_under_test, const std::string & tag)
138  {
139  class_under_test->expected_tag_ = tag;
140  }
141 
142  static void ResetLogCounts(ClassUnderTest * class_under_test)
143  {
144  class_under_test->log_info_count_ = 0;
145  class_under_test->log_debug_count_ = 0;
146  class_under_test->log_trace_count_ = 0;
147  class_under_test->log_warn_count_ = 0;
148  class_under_test->log_error_count_ = 0;
149  class_under_test->log_fatal_count_ = 0;
150  }
151 
152  void Flush() override {
153  }
154 };
155 
156 TEST(TestAWSLogSystem, TestLogMethod)
157 {
158  // Create a logger object with "Debug" configured level.
159  // This means, log at all the levels above "Debug" will be emitted.
160  ClassUnderTest * logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Debug);
161 
162  // Test if info log function is correctly getting called with correct log message.
163  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
164  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
165  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
166  "[1] fake info log message");
167  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
168 
169  // Test if debug log function is correctly getting called with correct log message.
170  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 2);
171  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "debug_tag");
172  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
173  "[2] fake debug log message");
174  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 1);
175 
176  // Test if warn log function is correctly getting called with correct log message.
177  logger->Log(Aws::Utils::Logging::LogLevel::Warn, "warn_tag", "[%d] fake warn log message", 3);
178  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "warn_tag");
179  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
180  "[3] fake warn log message");
181  EXPECT_EQ(ClassUnderTest::GetLogWarnCount(logger), 1);
182 
183  // Test if error log function is correctly getting called with correct log message.
184  logger->Log(Aws::Utils::Logging::LogLevel::Error, "error_tag", "[%d] fake error log message", 4);
185  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
186  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
187  "[4] fake error log message");
188  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
189 
190  // Test if fatal log function is correctly getting called with correct log message.
191  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 5);
192  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
193  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
194  "[5] fake fatal log message");
195  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
196 
197  // Test if debug log function is correctly getting called second time with appropriate log
198  // message.
199  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 6);
200  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "debug_tag");
201  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
202  "[6] fake debug log message");
203  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 2);
204 
205  delete logger;
206 
207  // Create a logger object with "Error" configured level.
208  // This means, logs only above "Error" level will be emitted.
209  logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Error);
210 
211  // info log function should not be called since info_log_level < error_log_level
212  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
213  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
214  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
215  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 0);
216 
217  // debug log function should not be called since debug_log_level < error_log_level
218  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 2);
219  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
220  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
221  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 0);
222 
223  // Test if error log function is correctly getting called with correct log message.
224  logger->Log(Aws::Utils::Logging::LogLevel::Error, "error_tag", "[%d] fake error log message", 3);
225  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
226  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
227  "[3] fake error log message");
228  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
229 
230  // Test if fatal log function is correctly getting called with correct log message.
231  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 4);
232  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
233  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
234  "[4] fake fatal log message");
235  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
236 
237  delete logger;
238 
239  // Create a logger object with "Trace" configured level.
240  // This means, logs of all levels will be emitted.
241  logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Trace);
242 
243  // Test if info log function is correctly getting called with correct log message.
244  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
245  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
246  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
247  "[1] fake info log message");
248  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
249 
250  // Test if debug log function is correctly getting called with correct log message.
251  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 2);
252  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "debug_tag");
253  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
254  "[2] fake debug log message");
255  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 1);
256 
257  // Test if error log function is correctly getting called with correct log message.
258  logger->Log(Aws::Utils::Logging::LogLevel::Error, "error_tag", "[%d] fake error log message", 3);
259  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
260  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
261  "[3] fake error log message");
262  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
263 
264  // Test if fatal log function is correctly getting called with correct log message.
265  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 4);
266  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
267  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
268  "[4] fake fatal log message");
269  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
270 
271  // Test if trace log function is correctly getting called with correct log message.
272  logger->Log(Aws::Utils::Logging::LogLevel::Trace, "trace_tag", "[%d] fake trace log message", 5);
273  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "trace_tag");
274  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
275  "[5] fake trace log message");
276  EXPECT_EQ(ClassUnderTest::GetLogTraceCount(logger), 1);
277 }
278 
279 TEST(TestAWSLogSystem, TestLogStreamMethod)
280 {
281  Aws::OStringStream message_stream;
282 
283  // Create a logger object with "Debug" configured level.
284  // This means, log at all the levels above "Debug" will be emitted.
285  ClassUnderTest * logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Debug);
286 
287  // Test if info log function is correctly getting called with correct log message.
288  message_stream << "This is a first info log message.";
289  logger->LogStream(Aws::Utils::Logging::LogLevel::Info, "info_tag", message_stream);
290  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
291  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
292  "This is a first info log message.");
293  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
294 
295  message_stream << " This is a second info log message.";
296  logger->LogStream(Aws::Utils::Logging::LogLevel::Info, "info_tag", message_stream);
297  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
298  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
299  "This is a first info log message. This is a second info log message.");
300  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 2);
301 
302  delete logger;
303  message_stream.str("");
304 
305  // Create a logger object with "Error" configured level.
306  // This means, logs only above "Error" level will be emitted.
307  logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Error);
308 
309  // info log function should not be called since info_log_level < error_log_level
310  message_stream << "This is a first info log message.";
311  logger->LogStream(Aws::Utils::Logging::LogLevel::Info, "info_tag", message_stream);
312  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
313  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
314  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 0);
315 
316  // debug log function should not be called since debug_log_level < error_log_level
317  logger->LogStream(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", message_stream);
318  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
319  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
320  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 0);
321 
322  // clear string buffer and expected log message string.
323  message_stream.str("");
325 
326  // Test if error log function is correctly getting called with correct log message.
327  message_stream << "This is a first error log message.";
328  logger->LogStream(Aws::Utils::Logging::LogLevel::Error, "error_tag", message_stream);
329  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
330  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
331  "This is a first error log message.");
332  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
333 
334  delete logger;
335 }
336 
337 TEST(TestAWSLogSystem, TestChangingLogLevels)
338 {
339  // Create a logger object with "Debug" configured level.
340  // This means, log at all the levels above "Debug" will be emitted.
341  ClassUnderTest * logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Debug);
342 
343  // check if log level is correctly set.
344  EXPECT_EQ(logger->GetLogLevel(), Aws::Utils::Logging::LogLevel::Debug);
345 
346  // Test if info log function is correctly getting called with correct log message.
347  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
348  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
349  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
350  "[1] fake info log message");
351  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
352 
353  // reset test variables
357 
358  // change log level at runtime
359  logger->SetLogLevel(Aws::Utils::Logging::LogLevel::Error);
360 
361  // check if log level is correctly set.
362  EXPECT_EQ(logger->GetLogLevel(), Aws::Utils::Logging::LogLevel::Error);
363 
364  // info log function should not be called since info_log_level < error_log_level
365  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
366  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
367  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
368  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 0);
369 
370  // Test if fatal log function is correctly getting called with correct log message.
371  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 1);
372  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
373  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
374  "[1] fake fatal log message");
375  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
376 
377  // reset test variables
381 
382  // now change log level to Info.
383  logger->SetLogLevel(Aws::Utils::Logging::LogLevel::Info);
384 
385  // check if log level is correctly set.
386  EXPECT_EQ(logger->GetLogLevel(), Aws::Utils::Logging::LogLevel::Info);
387 
388  // Test if info log function is correctly getting called with correct log message.
389  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 3);
390  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
391  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
392  "[3] fake info log message");
393  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
394 
395  delete logger;
396 }
397 
398 int main(int argc, char ** argv)
399 {
400  testing::InitGoogleTest(&argc, argv);
401  return RUN_ALL_TESTS();
402 }
TEST(TestAWSLogSystem, TestLogMethod)
void Flush() override
void LogDebug(const char *tag, const std::string &message) override
void LogTrace(const char *tag, const std::string &message) override
void LogInfo(const char *tag, const std::string &message) override
void LogWarn(const char *tag, const std::string &message) override
static int GetLogInfoCount(const ClassUnderTest *class_under_test)
void Log(Aws::Utils::Logging::LogLevel log_level, const char *tag, const char *format,...) override
static int GetLogDebugCount(const ClassUnderTest *class_under_test)
static const std::string & GetExpectedTag(const ClassUnderTest *class_under_test)
Aws::Utils::Logging::LogLevel GetLogLevel() const override
static int GetLogTraceCount(const ClassUnderTest *class_under_test)
static int GetLogErrorCount(const ClassUnderTest *class_under_test)
static int GetLogFatalCount(const ClassUnderTest *class_under_test)
std::string expected_tag_
static const std::string & GetExpectedLogMessageString(const ClassUnderTest *class_under_test)
std::string expected_log_message_string_
ClassUnderTest(Aws::Utils::Logging::LogLevel log_level)
static int GetLogWarnCount(const ClassUnderTest *class_under_test)
static void ResetLogCounts(ClassUnderTest *class_under_test)
static void SetExpectedTagString(ClassUnderTest *class_under_test, const std::string &tag)
void LogFatal(const char *tag, const std::string &message) override
void LogStream(Aws::Utils::Logging::LogLevel log_level, const char *tag, const Aws::OStringStream &message_stream) override
static void SetExpectedLogMessageString(ClassUnderTest *class_under_test, const std::string &message)
void LogError(const char *tag, const std::string &message) override
void SetLogLevel(Aws::Utils::Logging::LogLevel log_level)
int main(int argc, char **argv)


aws_common
Author(s): AWS RoboMaker
autogenerated on Mon Feb 28 2022 21:58:58