test_icl_core_logging.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include <icl_core/BaseTypes.h>
00024 #include <icl_core/internal_raw_debug.h>
00025 #include <icl_core/os_lxrt.h>
00026 #include <icl_core_config/Config.h>
00027 #include <icl_core_logging/Logging.h>
00028 #include <icl_core_logging/ScopedTimer.h>
00029 #include <icl_core_thread/Thread.h>
00030 
00031 DECLARE_LOG_STREAM(Main);
00032 DECLARE_LOG_STREAM(ThreadLog);
00033 
00034 REGISTER_LOG_STREAM(Main)
00035 REGISTER_LOG_STREAM(ThreadLog)
00036 
00037 using icl_core::logging::endl;
00038 
00039 class LoggingThread : public icl_core::thread::Thread
00040 {
00041 public:
00042   LoggingThread(icl_core::ThreadPriority priority,
00043                 size_t message_count,
00044                 uint32_t sleep_time_us)
00045     : icl_core::thread::Thread("LoggingThread", priority),
00046       m_message_count(message_count),
00047       m_sleep_time_us(sleep_time_us)
00048   { }
00049 
00050   virtual void run()
00051   {
00052 #ifdef _SYSTEM_LXRT_
00053     PRINTF("test_icl_core_logging(LoggingThread): If you see this on the console then I am not hard real-time!\n");
00054 #endif
00055 
00056     LOGGING_ERROR_C(ThreadLog, LoggingThread, "Thread Error" << endl);
00057     LOGGING_WARNING_C(ThreadLog, LoggingThread, "Thread Warning" << endl);
00058     LOGGING_INFO_C(ThreadLog, LoggingThread, "Thread Info" << endl);
00059     LOGGING_DEBUG_C(ThreadLog, LoggingThread, "Thread Debug" << endl);
00060     LOGGING_TRACE_C(ThreadLog, LoggingThread, "Thread Trace" << endl);
00061 
00062     for (size_t i = 0; i < m_message_count; ++i)
00063     {
00064       LOGGING_INFO_C(ThreadLog, LoggingThread, "Thread Loop " << i << endl);
00065       if (m_sleep_time_us > 0)
00066       {
00067         icl_core::os::usleep(m_sleep_time_us);
00068       }
00069     }
00070   }
00071 
00072 private:
00073   size_t m_message_count;
00074   uint32_t m_sleep_time_us;
00075 };
00076 
00077 int main(int argc, char *argv[])
00078 {
00079   icl_core::os::lxrtStartup();
00080 
00081   icl_core::config::addParameter(icl_core::config::ConfigParameter("priority:", "p", "/TestLogging/ThreadPriority", "Priority of the logging thread."));
00082   icl_core::config::addParameter(icl_core::config::ConfigParameter("message-count:", "c", "/TestLogging/MessageCount", "Number of messages to be logged."));
00083   icl_core::config::addParameter(icl_core::config::ConfigParameter("sleep-time:", "s", "/TestLogging/SleepTimeUS", "Sleep time (us) between two log messages."));
00084 
00085   icl_core::logging::initialize(argc, argv);
00086 
00087   icl_core::ThreadPriority priority = icl_core::config::getDefault<icl_core::ThreadPriority>("/TestLogging/ThreadPriority", 0);
00088   size_t message_count = icl_core::config::getDefault<size_t>("/TestLogging/MessageCount", 10);
00089   uint32_t sleep_time_us = icl_core::config::getDefault<uint32_t>("/TestLogging/SleepTimeUS", 1000);
00090 
00091   LOGGING_INFO(Main, "Creating logging thread." << endl);
00092   LoggingThread *thread = new LoggingThread(priority, message_count, sleep_time_us);
00093   LOGGING_INFO(Main, "Starting logging thread." << endl);
00094   thread->start();
00095 
00096   {
00097     LOGGING_SCOPED_TIMER_INFO(Main, "Main Loop");
00098     for (size_t i = 0; i < message_count; ++i)
00099     {
00100       LOGGING_INFO(Main, "Main Loop " << i << endl);
00101       if (sleep_time_us > 0)
00102       {
00103         icl_core::os::usleep(sleep_time_us);
00104       }
00105     }
00106   }
00107 
00108   LOGGING_INFO(Main, "Waiting for logging thread to finish." << endl);
00109   thread->join();
00110   LOGGING_INFO(Main, "Logging thread finished." << endl);
00111   delete thread;
00112 
00113   icl_core::logging::LoggingManager::instance().shutdown();
00114   icl_core::os::lxrtShutdown();
00115 
00116   return 0;
00117 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04