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 
153 TEST(TestAWSLogSystem, TestLogMethod)
154 {
155  // Create a logger object with "Debug" configured level.
156  // This means, log at all the levels above "Debug" will be emitted.
157  ClassUnderTest * logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Debug);
158 
159  // Test if info log function is correctly getting called with correct log message.
160  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
161  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
162  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
163  "[1] fake info log message");
164  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
165 
166  // Test if debug log function is correctly getting called with correct log message.
167  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 2);
168  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "debug_tag");
169  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
170  "[2] fake debug log message");
171  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 1);
172 
173  // Test if warn log function is correctly getting called with correct log message.
174  logger->Log(Aws::Utils::Logging::LogLevel::Warn, "warn_tag", "[%d] fake warn log message", 3);
175  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "warn_tag");
176  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
177  "[3] fake warn log message");
178  EXPECT_EQ(ClassUnderTest::GetLogWarnCount(logger), 1);
179 
180  // Test if error log function is correctly getting called with correct log message.
181  logger->Log(Aws::Utils::Logging::LogLevel::Error, "error_tag", "[%d] fake error log message", 4);
182  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
183  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
184  "[4] fake error log message");
185  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
186 
187  // Test if fatal log function is correctly getting called with correct log message.
188  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 5);
189  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
190  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
191  "[5] fake fatal log message");
192  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
193 
194  // Test if debug log function is correctly getting called second time with appropriate log
195  // message.
196  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 6);
197  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "debug_tag");
198  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
199  "[6] fake debug log message");
200  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 2);
201 
202  delete logger;
203 
204  // Create a logger object with "Error" configured level.
205  // This means, logs only above "Error" level will be emitted.
206  logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Error);
207 
208  // info log function should not be called since info_log_level < error_log_level
209  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
210  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
211  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
212  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 0);
213 
214  // debug log function should not be called since debug_log_level < error_log_level
215  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 2);
216  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
217  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
218  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 0);
219 
220  // Test if error log function is correctly getting called with correct log message.
221  logger->Log(Aws::Utils::Logging::LogLevel::Error, "error_tag", "[%d] fake error log message", 3);
222  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
223  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
224  "[3] fake error log message");
225  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
226 
227  // Test if fatal log function is correctly getting called with correct log message.
228  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 4);
229  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
230  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
231  "[4] fake fatal log message");
232  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
233 
234  delete logger;
235 
236  // Create a logger object with "Trace" configured level.
237  // This means, logs of all levels will be emitted.
238  logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Trace);
239 
240  // Test if info log function is correctly getting called with correct log message.
241  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
242  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
243  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
244  "[1] fake info log message");
245  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
246 
247  // Test if debug log function is correctly getting called with correct log message.
248  logger->Log(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", "[%d] fake debug log message", 2);
249  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "debug_tag");
250  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
251  "[2] fake debug log message");
252  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 1);
253 
254  // Test if error log function is correctly getting called with correct log message.
255  logger->Log(Aws::Utils::Logging::LogLevel::Error, "error_tag", "[%d] fake error log message", 3);
256  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
257  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
258  "[3] fake error log message");
259  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
260 
261  // Test if fatal log function is correctly getting called with correct log message.
262  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 4);
263  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
264  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
265  "[4] fake fatal log message");
266  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
267 
268  // Test if trace log function is correctly getting called with correct log message.
269  logger->Log(Aws::Utils::Logging::LogLevel::Trace, "trace_tag", "[%d] fake trace log message", 5);
270  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "trace_tag");
271  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
272  "[5] fake trace log message");
273  EXPECT_EQ(ClassUnderTest::GetLogTraceCount(logger), 1);
274 }
275 
276 TEST(TestAWSLogSystem, TestLogStreamMethod)
277 {
278  Aws::OStringStream message_stream;
279 
280  // Create a logger object with "Debug" configured level.
281  // This means, log at all the levels above "Debug" will be emitted.
282  ClassUnderTest * logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Debug);
283 
284  // Test if info log function is correctly getting called with correct log message.
285  message_stream << "This is a first info log message.";
286  logger->LogStream(Aws::Utils::Logging::LogLevel::Info, "info_tag", message_stream);
287  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
288  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
289  "This is a first info log message.");
290  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
291 
292  message_stream << " This is a second info log message.";
293  logger->LogStream(Aws::Utils::Logging::LogLevel::Info, "info_tag", message_stream);
294  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
295  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
296  "This is a first info log message. This is a second info log message.");
297  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 2);
298 
299  delete logger;
300  message_stream.str("");
301 
302  // Create a logger object with "Error" configured level.
303  // This means, logs only above "Error" level will be emitted.
304  logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Error);
305 
306  // info log function should not be called since info_log_level < error_log_level
307  message_stream << "This is a first info log message.";
308  logger->LogStream(Aws::Utils::Logging::LogLevel::Info, "info_tag", message_stream);
309  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
310  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
311  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 0);
312 
313  // debug log function should not be called since debug_log_level < error_log_level
314  logger->LogStream(Aws::Utils::Logging::LogLevel::Debug, "debug_tag", message_stream);
315  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
316  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
317  EXPECT_EQ(ClassUnderTest::GetLogDebugCount(logger), 0);
318 
319  // clear string buffer and expected log message string.
320  message_stream.str("");
322 
323  // Test if error log function is correctly getting called with correct log message.
324  message_stream << "This is a first error log message.";
325  logger->LogStream(Aws::Utils::Logging::LogLevel::Error, "error_tag", message_stream);
326  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "error_tag");
327  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
328  "This is a first error log message.");
329  EXPECT_EQ(ClassUnderTest::GetLogErrorCount(logger), 1);
330 
331  delete logger;
332 }
333 
334 TEST(TestAWSLogSystem, TestChangingLogLevels)
335 {
336  // Create a logger object with "Debug" configured level.
337  // This means, log at all the levels above "Debug" will be emitted.
338  ClassUnderTest * logger = new ClassUnderTest(Aws::Utils::Logging::LogLevel::Debug);
339 
340  // check if log level is correctly set.
341  EXPECT_EQ(logger->GetLogLevel(), Aws::Utils::Logging::LogLevel::Debug);
342 
343  // Test if info log function is correctly getting called with correct log message.
344  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
345  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
346  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
347  "[1] fake info log message");
348  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
349 
350  // reset test variables
354 
355  // change log level at runtime
356  logger->SetLogLevel(Aws::Utils::Logging::LogLevel::Error);
357 
358  // check if log level is correctly set.
359  EXPECT_EQ(logger->GetLogLevel(), Aws::Utils::Logging::LogLevel::Error);
360 
361  // info log function should not be called since info_log_level < error_log_level
362  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 1);
363  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "");
364  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(), "");
365  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 0);
366 
367  // Test if fatal log function is correctly getting called with correct log message.
368  logger->Log(Aws::Utils::Logging::LogLevel::Fatal, "fatal_tag", "[%d] fake fatal log message", 1);
369  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "fatal_tag");
370  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
371  "[1] fake fatal log message");
372  EXPECT_EQ(ClassUnderTest::GetLogFatalCount(logger), 1);
373 
374  // reset test variables
378 
379  // now change log level to Info.
380  logger->SetLogLevel(Aws::Utils::Logging::LogLevel::Info);
381 
382  // check if log level is correctly set.
383  EXPECT_EQ(logger->GetLogLevel(), Aws::Utils::Logging::LogLevel::Info);
384 
385  // Test if info log function is correctly getting called with correct log message.
386  logger->Log(Aws::Utils::Logging::LogLevel::Info, "info_tag", "[%d] fake info log message", 3);
387  EXPECT_STREQ(ClassUnderTest::GetExpectedTag(logger).c_str(), "info_tag");
388  EXPECT_STREQ(ClassUnderTest::GetExpectedLogMessageString(logger).c_str(),
389  "[3] fake info log message");
390  EXPECT_EQ(ClassUnderTest::GetLogInfoCount(logger), 1);
391 
392  delete logger;
393 }
394 
395 int main(int argc, char ** argv)
396 {
397  testing::InitGoogleTest(&argc, argv);
398  return RUN_ALL_TESTS();
399 }
TEST(TestAWSLogSystem, TestLogMethod)
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 Sat Mar 6 2021 03:11:38