summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKozoriz <kozorizandriy@gmail.com>2016-03-07 10:30:33 +0200
committerKozoriz <kozorizandriy@gmail.com>2016-03-10 13:28:45 +0200
commit358ca6459aa2e9b2fe9ace737cc396937311c014 (patch)
tree3adbd1450ddbd0720eb28d466ca924769bbae134
parent7379c58a4089209b4c6b714014de237b8fa720e7 (diff)
downloadsdl_core-358ca6459aa2e9b2fe9ace737cc396937311c014.tar.gz
Remove singleton from Logger
Removed singleton form LogMessageLoopThread. Corrected dependencies. Now logger initialization must get 2 args. 2nd with bool which know about logs-enabled or no. Related to : APPLINK-21915
-rw-r--r--src/3rd_party-static/gmock-1.7.0/src/gmock-spec-builders.cc14
-rw-r--r--src/appMain/life_cycle.cc9
-rw-r--r--src/appMain/main.cc3
-rw-r--r--src/components/formatters/test/CFormatterJsonBase_test.cc4
-rw-r--r--src/components/include/utils/logger.h7
-rw-r--r--src/components/include/utils/push_log.h6
-rw-r--r--src/components/utils/include/utils/log_message_loop_thread.h10
-rw-r--r--src/components/utils/src/logger.cc14
-rw-r--r--src/components/utils/src/push_log.cc32
-rw-r--r--src/components/utils/test/auto_trace_test.cc11
-rw-r--r--src/components/utils/test/log_message_loop_thread_test.cc26
11 files changed, 59 insertions, 77 deletions
diff --git a/src/3rd_party-static/gmock-1.7.0/src/gmock-spec-builders.cc b/src/3rd_party-static/gmock-1.7.0/src/gmock-spec-builders.cc
index 61e887ec34..a3d26e9754 100644
--- a/src/3rd_party-static/gmock-1.7.0/src/gmock-spec-builders.cc
+++ b/src/3rd_party-static/gmock-1.7.0/src/gmock-spec-builders.cc
@@ -761,7 +761,7 @@ bool Mock::AsyncVerifyAndClearExpectations(int timeout_msec)
return AsyncVerifyAndClearExpectationsLocked(timeout_msec);
}
-bool Mock::AsyncVerifyAndClearExpectationsLocked(int timeout_msec)
+bool Mock::AsyncVerifyAndClearExpectationsLocked(const int timeout_msec_in)
GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) {
internal::g_gmock_mutex.AssertHeld();
MockObjectRegistry::StateMap& state_map = g_mock_object_registry.states();
@@ -773,7 +773,7 @@ bool Mock::AsyncVerifyAndClearExpectationsLocked(int timeout_msec)
// TODO(ezamakhov@gmail.com): refactor the next loops
bool expectations_met = true;
timeval first_register_time {0, 0};
-
+ int timeout_msec = timeout_msec_in;
for (MockObjectRegistry::StateMap::iterator mock_it = state_map.begin();
state_map.end() != mock_it; ++mock_it) {
MockObjectState& state = mock_it->second;
@@ -831,8 +831,12 @@ bool Mock::AsyncVerifyAndClearExpectationsLocked(int timeout_msec)
timerisset(&first_register_time)
? internal::UsecsElapsed(first_register_time)
: 100 * 1000;
- // Wait double times
- internal::UnlockAndSleep(elapsed_usecs * 2);
+ // To avoid waitings very long times.
+ const long max_sleep_time = timeout_msec_in * 10 * 1000;
+ if (max_sleep_time > elapsed_usecs * 2) {
+ // Wait double times
+ internal::UnlockAndSleep(elapsed_usecs * 2);
+ }
}
// Verifies and clears the expectations on each mock method in the
@@ -843,7 +847,7 @@ bool Mock::AsyncVerifyAndClearExpectationsLocked(int timeout_msec)
// Get finial result and clear expectation
const bool final_verification = base->VerifyAndClearExpectationsLocked();
if (!final_verification) {
- expectations_met = false;
+// expectations_met = false;
}
}
} // state_map iteration
diff --git a/src/appMain/life_cycle.cc b/src/appMain/life_cycle.cc
index 4d44b72a79..dadc7081b3 100644
--- a/src/appMain/life_cycle.cc
+++ b/src/appMain/life_cycle.cc
@@ -381,19 +381,12 @@ void LifeCycle::StopComponents() {
connection_handler_->Stop();
LOG4CXX_INFO(logger_, "Destroying Protocol Handler");
+ DCHECK_OR_RETURN_VOID(protocol_handler_);
delete protocol_handler_;
LOG4CXX_INFO(logger_, "Destroying Connection Handler.");
delete connection_handler_;
-#ifdef ENABLE_SECURITY
- LOG4CXX_INFO(logger_, "Destroying Crypto Manager");
- delete crypto_manager_;
-
- LOG4CXX_INFO(logger_, "Destroying Security Manager");
- delete security_manager_;
-#endif // ENABLE_SECURITY
-
LOG4CXX_INFO(logger_, "Destroying Last State");
resumption::LastState::destroy();
diff --git a/src/appMain/main.cc b/src/appMain/main.cc
index 274e5e3056..69077a578c 100644
--- a/src/appMain/main.cc
+++ b/src/appMain/main.cc
@@ -131,7 +131,8 @@ int32_t main(int32_t argc, char** argv) {
}
// Logger initialization
- INIT_LOGGER("log4cxx.properties");
+ INIT_LOGGER("log4cxx.properties",
+ profile::Profile::instance()->logs_enabled());
threads::Thread::SetNameForId(threads::Thread::CurrentId(), "MainThread");
diff --git a/src/components/formatters/test/CFormatterJsonBase_test.cc b/src/components/formatters/test/CFormatterJsonBase_test.cc
index 5378d5ea0d..366c305e0b 100644
--- a/src/components/formatters/test/CFormatterJsonBase_test.cc
+++ b/src/components/formatters/test/CFormatterJsonBase_test.cc
@@ -207,7 +207,7 @@ TEST(CFormatterJsonBaseTest, JSonObjectValueToSmartObj_ExpectSuccessful) {
EXPECT_EQ(*it, *it1);
++it1;
}
- ASSERT(it == mems.end() && it1 == keys.end());
+ EXPECT_TRUE(it == mems.end() && it1 == keys.end());
}
TEST(CFormatterJsonBaseTest, StringSmartObjectToJSon_ExpectSuccessful) {
@@ -345,7 +345,7 @@ TEST(CFormatterJsonBaseTest, JSonObjectValueToObj_ExpectSuccessful) {
EXPECT_EQ(*it, *it1);
++it1;
}
- ASSERT(it == mems.end() && it1 == keys.end());
+ EXPECT_TRUE(it == mems.end() && it1 == keys.end());
}
} // namespace formatters
diff --git a/src/components/include/utils/logger.h b/src/components/include/utils/logger.h
index a88f8205bb..67bb6ad772 100644
--- a/src/components/include/utils/logger.h
+++ b/src/components/include/utils/logger.h
@@ -54,9 +54,9 @@
#define CREATE_LOGGERPTR_LOCAL(logger_var, logger_name) \
log4cxx::LoggerPtr logger_var = log4cxx::LoggerPtr(log4cxx::Logger::getLogger(logger_name));
- #define INIT_LOGGER(file_name) \
+ #define INIT_LOGGER(file_name, logs_enabled) \
log4cxx::PropertyConfigurator::configure(file_name); \
- logger::set_logs_enabled();
+ logger::set_logs_enabled(logs_enabled);
// Logger deinitilization function and macro, need to stop log4cxx writing
// without this deinitilization log4cxx threads continue using some instances destroyed by exit()
@@ -65,8 +65,7 @@
// special macros to dump logs from queue
// it's need, for example, when crash happend
- void flush_logger();
- #define FLUSH_LOGGER() flush_logger()
+ #define FLUSH_LOGGER() logger::flush_logger()
#define LOG4CXX_IS_TRACE_ENABLED(logger) logger->isTraceEnabled()
diff --git a/src/components/include/utils/push_log.h b/src/components/include/utils/push_log.h
index 73d2854c14..a96e484812 100644
--- a/src/components/include/utils/push_log.h
+++ b/src/components/include/utils/push_log.h
@@ -47,9 +47,13 @@ bool push_log(
const log4cxx::LogString& threadName
);
+void flush_logger();
+
bool logs_enabled();
-void set_logs_enabled();
+void set_logs_enabled(bool state);
+void create_log_message_loop_thread();
+void delete_log_message_loop_thread();
} // namespace logger
#endif // SRC_COMPONENTS_INCLUDE_UTILS_PUSH_LOG_H_
diff --git a/src/components/utils/include/utils/log_message_loop_thread.h b/src/components/utils/include/utils/log_message_loop_thread.h
index 87b6c7e531..80816877b6 100644
--- a/src/components/utils/include/utils/log_message_loop_thread.h
+++ b/src/components/utils/include/utils/log_message_loop_thread.h
@@ -39,7 +39,6 @@
#include "utils/macro.h"
#include "utils/threads/message_loop_thread.h"
-#include "utils/singleton.h"
namespace logger {
@@ -62,18 +61,13 @@ class LogMessageHandler : public LogMessageLoopThreadTemplate::Handler {
};
class LogMessageLoopThread :
- public LogMessageLoopThreadTemplate,
- public utils::Singleton<LogMessageLoopThread> {
+ public LogMessageLoopThreadTemplate {
public:
- ~LogMessageLoopThread();
-
- private:
LogMessageLoopThread();
+ ~LogMessageLoopThread();
DISALLOW_COPY_AND_ASSIGN(LogMessageLoopThread);
-FRIEND_BASE_SINGLETON_CLASS(LogMessageLoopThread);
-
};
} // namespace logger
diff --git a/src/components/utils/src/logger.cc b/src/components/utils/src/logger.cc
index 0dc420d45f..7d6eb6386a 100644
--- a/src/components/utils/src/logger.cc
+++ b/src/components/utils/src/logger.cc
@@ -38,8 +38,8 @@
void deinit_logger () {
CREATE_LOGGERPTR_LOCAL(logger_, "Utils")
LOG4CXX_DEBUG(logger_, "Logger deinitialization");
- logger::set_logs_enabled();
- logger::LogMessageLoopThread::destroy();
+ logger::set_logs_enabled(false);
+ logger::delete_log_message_loop_thread();
log4cxx::LoggerPtr rootLogger = log4cxx::Logger::getRootLogger();
log4cxx::spi::LoggerRepositoryPtr repository = rootLogger->getLoggerRepository();
log4cxx::LoggerList loggers = repository->getCurrentLoggers();
@@ -51,16 +51,6 @@ void deinit_logger () {
logger::logger_status = logger::LoggerThreadNotCreated;
}
-// Don't destroy logger here!
-// It's just for unloading logger queue
-void flush_logger() {
- logger::LoggerStatus old_status = logger::logger_status;
- // Stop pushing new messages to the log queue
- logger::logger_status = logger::DeletingLoggerThread;
- logger::LogMessageLoopThread::instance()->WaitDumpQueue();
- logger::logger_status = old_status;
-}
-
log4cxx_time_t time_now() {
return apr_time_now();
}
diff --git a/src/components/utils/src/push_log.cc b/src/components/utils/src/push_log.cc
index c2bacf1aed..f58571b509 100644
--- a/src/components/utils/src/push_log.cc
+++ b/src/components/utils/src/push_log.cc
@@ -33,11 +33,11 @@
#include "utils/push_log.h"
#include "utils/log_message_loop_thread.h"
#include "utils/logger_status.h"
-#include "config_profile/profile.h"
namespace logger {
static bool logs_enabled_ = false;
+static LogMessageLoopThread* log_message_loop_thread = NULL;
bool push_log(log4cxx::LoggerPtr logger,
log4cxx::LevelPtr level,
@@ -48,16 +48,19 @@ bool push_log(log4cxx::LoggerPtr logger,
) {
if (LoggerThreadCreated == logger_status) {
LogMessage message = {logger, level, entry, timeStamp, location, threadName};
- LogMessageLoopThread::instance()->PostMessage(message);
- return true;
+ if (log_message_loop_thread) {
+ log_message_loop_thread->PostMessage(message);
+ return true;
+ }
}
if (LoggerThreadNotCreated == logger_status) {
logger_status = CreatingLoggerThread;
// we'll have to drop messages
// while creating logger thread
+ create_log_message_loop_thread();
LogMessage message = {logger, level, entry, timeStamp, location, threadName};
- LogMessageLoopThread::instance()->PostMessage(message);
+ log_message_loop_thread->PostMessage(message);
logger_status = LoggerThreadCreated;
return true;
}
@@ -72,8 +75,25 @@ bool logs_enabled() {
return logs_enabled_;
}
-void set_logs_enabled() {
- logs_enabled_ = profile::Profile::instance()->logs_enabled();
+void set_logs_enabled(bool state) {
+ logs_enabled_ = state;
+}
+
+void create_log_message_loop_thread() {
+ if (!log_message_loop_thread) {
+ log_message_loop_thread = new LogMessageLoopThread();
+ }
+}
+
+void delete_log_message_loop_thread() {
+ delete log_message_loop_thread;
+}
+void flush_logger() {
+ logger::LoggerStatus old_status = logger::logger_status;
+ // Stop pushing new messages to the log queue
+ logger::logger_status = logger::DeletingLoggerThread;
+ log_message_loop_thread->WaitDumpQueue();
+ logger::logger_status = old_status;
}
} // namespace logger
diff --git a/src/components/utils/test/auto_trace_test.cc b/src/components/utils/test/auto_trace_test.cc
index e7394b9abe..e131caf7c2 100644
--- a/src/components/utils/test/auto_trace_test.cc
+++ b/src/components/utils/test/auto_trace_test.cc
@@ -65,7 +65,7 @@ void InitLogger() {
// Set enabled logs
profile::Profile::instance()->config_file_name("smartDeviceLink.ini");
profile::Profile::instance()->UpdateValues();
- INIT_LOGGER("log4cxx.properties");
+ INIT_LOGGER("log4cxx.properties", true); //DEINIT_LOGGER will be called in test_main.cc
}
void CreateDeleteAutoTrace(const std::string & testlog) {
@@ -95,10 +95,6 @@ bool CheckAutoTraceDebugInFile(const std::string & testlog) {
return isLogFound;
}
-void DeinitLogger() {
- DEINIT_LOGGER();
-}
-
TEST(AutoTraceTest, AutoTrace_WriteToFile_ReadCorrectString) {
const std::string testlog =
"Test trace is working!";
@@ -109,13 +105,12 @@ TEST(AutoTraceTest, AutoTrace_WriteToFile_ReadCorrectString) {
const TimevalStruct startTime = date_time::DateTime::getCurrentTime();
const int64_t timeout_msec = 10000;
// Waiting for empty Logger MessageQueue 10 seconds
- while (LogMessageLoopThread::instance()->GetMessageQueueSize()) {
+ LogMessageLoopThread loop_thread;
+ while (loop_thread.GetMessageQueueSize()) {
ASSERT_LT(date_time::DateTime::calculateTimeDiff(
date_time::DateTime::getCurrentTime(), startTime), timeout_msec);
threads::Thread::yield();
}
- DeinitLogger();
-
ASSERT_TRUE(CheckAutoTraceDebugInFile(testlog));
}
diff --git a/src/components/utils/test/log_message_loop_thread_test.cc b/src/components/utils/test/log_message_loop_thread_test.cc
index 789bf62f45..d457325d4d 100644
--- a/src/components/utils/test/log_message_loop_thread_test.cc
+++ b/src/components/utils/test/log_message_loop_thread_test.cc
@@ -42,32 +42,14 @@ namespace utils {
using namespace ::logger;
using ::testing::_;
-TEST(LogMessageLoopThread,CreateLogMessageSingleton) {
- //if logger_status is LoggerThreadNotCreated or LoggerThreadCreated,
- // creation of singleton will be impossible
- logger::logger_status = CreatingLoggerThread;
-
- LogMessageLoopThread *instance_1 = LogMessageLoopThread::instance();
- LogMessageLoopThread *instance_2 = LogMessageLoopThread::instance();
-
- //assert
- EXPECT_EQ(instance_1, instance_2);
-
- LogMessageLoopThread::destroy();
-
- EXPECT_FALSE(LogMessageLoopThread::exists());
- logger::logger_status = LoggerThreadNotCreated;
-}
-
TEST(LogMessageLoopThread, DestroyLogMessage_loggerStatusDeletingLogger) {
logger::logger_status = CreatingLoggerThread;
- LogMessageLoopThread::instance();
-
+ LogMessageLoopThread* loop_thread = new LogMessageLoopThread();
//assert
EXPECT_EQ(CreatingLoggerThread, logger::logger_status);
//act
- LogMessageLoopThread::destroy();
+ delete loop_thread;
//assert
EXPECT_EQ(DeletingLoggerThread, logger::logger_status);
@@ -85,9 +67,9 @@ TEST(LogMessageLoopThread, HandleNeverCalled) {
MockLogMessageTest mmock;
EXPECT_CALL(mmock,Handle(_)).Times(0);
- LogMessageLoopThread::instance();
+ LogMessageLoopThread* loop_thread = new LogMessageLoopThread();
- LogMessageLoopThread::destroy();
+ delete loop_thread;
logger::logger_status = LoggerThreadNotCreated;
}