From 3821a880aece634589ee0a18a0685d259edb7fc4 Mon Sep 17 00:00:00 2001
From: Dustin Gooding <dustin.r.gooding@nasa.gov>
Date: Wed, 18 Jul 2012 11:13:17 -0500
Subject: [PATCH] Applied Peter's patch from 2012/07/11 ML post.  Fixed
 bug in order of header includes in
 ocl/lua/LuaComponent.cpp, based on Stephen's suggestion
 from 2012/07/13 ML post.  Added working RTTLua example
 files to ocl/logging/test/data.

---
 CMakeLists.txt                              |    7 +++
 bin/CMakeLists.txt                          |    1 -
 config/component_rules.cmake                |    3 +-
 logging/tests/data/appenderA_properties.cpf |    8 ++++
 logging/tests/data/appenderB_properties.cpf |    7 +++
 logging/tests/data/logging_properties.cpf   |   11 +++++
 logging/tests/data/setup_logging.lua        |   60 +++++++++++++++++++++++++
 lua/CMakeLists.txt                          |   22 +++++++--
 lua/LuaComponent.cpp                        |   65 +++++++++++++++++++++++++++
 9 files changed, 179 insertions(+), 5 deletions(-)
 create mode 100644 logging/tests/data/appenderA_properties.cpf
 create mode 100644 logging/tests/data/appenderB_properties.cpf
 create mode 100644 logging/tests/data/logging_properties.cpf
 create mode 100644 logging/tests/data/setup_logging.lua

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 06eb56b..17c944b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -193,6 +193,13 @@ IF(NO_GPL)
   ADD_DEFINITIONS( -DNO_GPL )
 ENDIF(NO_GPL)
 
+OPTION( BUILD_RTALLOC "Build with rt-malloc support (requires rt-malloc in RTT to be enabled)." TRUE)
+# RTALLOC is forced to off on win32
+IF(WIN32)
+  SET(BUILD_RTALLOC OFF CACHE BOOL "docstring" FORCE)
+ENDIF()
+
+
 
 ###########################################################
 #                                                         #
diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt
index 148efe7..7a1a1d0 100644
--- a/bin/CMakeLists.txt
+++ b/bin/CMakeLists.txt
@@ -1,6 +1,5 @@
 SET(EXTRA_LIBRARIES)
 SET(EXTRA_DEPS)
-OPTION( BUILD_RTALLOC "Build with rt-malloc support (requires rt-malloc in RTT to be enabled)." TRUE)
 
 # RTALLOC is forced to off on win32
 IF(WIN32)
diff --git a/config/component_rules.cmake b/config/component_rules.cmake
index 03146d0..b956ac7 100644
--- a/config/component_rules.cmake
+++ b/config/component_rules.cmake
@@ -213,7 +213,8 @@ MACRO( TARGET_USE_LIB TARGET_NAME SUBDIRECTORY LIBNAME )
     INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/${SUBDIRECTORY}/dev )
 ENDMACRO( TARGET_USE_LIB DIRECTORY LIBNAME )
 
-# Link a program with a component library                                                                                                                                                                                                   # Usage: PROGRAM_ADD_DEPS( taskbrowser-test orocos-reporting )                                                                                                                                                                             M
+# Link a program with a component library
+# Usage: PROGRAM_ADD_DEPS( taskbrowser-test orocos-reporting )
 MACRO( PROGRAM_ADD_DEPS PROGRAM_NAME )
   TARGET_LINK_LIBRARIES( ${PROGRAM_NAME} ${ARGN} )
   SET_TARGET_PROPERTIES( ${PROGRAM_NAME} PROPERTIES
diff --git a/logging/tests/data/appenderA_properties.cpf b/logging/tests/data/appenderA_properties.cpf
new file mode 100644
index 0000000..b179dff
--- /dev/null
+++ b/logging/tests/data/appenderA_properties.cpf
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE properties SYSTEM "cpf.dtd">
+<properties>
+    <simple name="LayoutName" type="string"><description>Layout name (e.g. 'simple', 'pattern')</description><value>pattern</value></simple>
+    <simple name="LayoutPattern" type="string"><description>Layout conversion pattern (for those layouts that use a pattern)</description><value>%d [%-5p] [%c] %m%n</value></simple>
+    <simple name="Filename" type="string"><description>Name of file to log to</description><value>orocos-log4cpp.log</value></simple>
+    <simple name="MaxEventsPerCycle" type="long"><description>Maximum number of log events to pop per cycle</description><value>100</value></simple>
+</properties>
diff --git a/logging/tests/data/appenderB_properties.cpf b/logging/tests/data/appenderB_properties.cpf
new file mode 100644
index 0000000..e9010db
--- /dev/null
+++ b/logging/tests/data/appenderB_properties.cpf
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE properties SYSTEM "cpf.dtd">
+<properties>
+    <simple name="LayoutName" type="string"><description>Layout name (e.g. 'simple', 'pattern')</description><value>pattern</value></simple>
+    <simple name="LayoutPattern" type="string"><description>Layout conversion pattern (for those layouts that use a pattern)</description><value>[%-5p] [%c{1}] %m%n</value></simple>
+    <simple name="MaxEventsPerCycle" type="long"><description>Maximum number of log events to pop per cycle</description><value>100</value></simple>
+</properties>
diff --git a/logging/tests/data/logging_properties.cpf b/logging/tests/data/logging_properties.cpf
new file mode 100644
index 0000000..9e39ae7
--- /dev/null
+++ b/logging/tests/data/logging_properties.cpf
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE properties SYSTEM "cpf.dtd">
+<properties>
+    <struct name="Levels" type="PropertyBag">
+        <simple name="org.orocos.ocl.logging.tests.TestComponent" type="string"><value>info</value></simple>
+    </struct>
+    <struct name="Appenders" type="PropertyBag">
+        <simple name="org.orocos.ocl.logging.tests.TestComponent" type="string"><value>AppenderA</value></simple>
+        <simple name="org.orocos.ocl.logging.tests.TestComponent" type="string"><value>AppenderB</value></simple>
+    </struct>
+</properties>
diff --git a/logging/tests/data/setup_logging.lua b/logging/tests/data/setup_logging.lua
new file mode 100644
index 0000000..1b905a3
--- /dev/null
+++ b/logging/tests/data/setup_logging.lua
@@ -0,0 +1,60 @@
+require("rttlib")
+
+rtt.setLogLevel("Info")
+
+-- Set this to true to write the property files the first time.
+write_props=false
+
+tc = rtt.getTC()
+depl = tc:getPeer("deployer")
+
+-- Create components. Enable BUILD_LOGGING and BUILD_TESTS for this to
+-- work.
+depl:loadComponent("AppenderA", "OCL::logging::FileAppender")
+depl:setActivity("AppenderA", 0.5, 0, 0)
+
+depl:loadComponent("AppenderB", "OCL::logging::OstreamAppender")
+depl:setActivity("AppenderB", 0.5, 0, 0)
+
+depl:loadComponent("LoggingService", "OCL::logging::LoggingService")
+depl:setActivity("LoggingService", 0.5, 0, 0)
+
+depl:loadComponent("TestComponent","OCL::logging::test::Component")
+depl:setActivity("TestComponent", 0.5, 0, 0)
+
+aa = depl:getPeer("AppenderA")
+ab = depl:getPeer("AppenderB")
+ls = depl:getPeer("LoggingService")
+test = depl:getPeer("TestComponent")
+
+depl:addPeer("LoggingService","AppenderA")
+depl:addPeer("LoggingService","AppenderB")
+
+-- Load marshalling service to read/write components
+depl:loadService("LoggingService","marshalling")
+depl:loadService("AppenderA","marshalling")
+depl:loadService("AppenderB","marshalling")
+
+if write_props then
+	ls:provides("marshalling"):writeProperties("logging_properties.cpf")
+	aa:provides("marshalling"):writeProperties("appenderA_properties.cpf")
+	ab:provides("marshalling"):writeProperties("appenderB_properties.cpf")
+	print("Wrote property files. Edit them and set write_props=false")
+	os.exit(0)
+else
+	ls:provides("marshalling"):loadProperties("logging_properties.cpf")
+	aa:provides("marshalling"):loadProperties("appenderA_properties.cpf")
+	ab:provides("marshalling"):loadProperties("appenderB_properties.cpf")
+end
+
+ls:configure()
+ls:start()
+ls:logCategories()
+
+aa:configure()
+ab:configure()
+test:configure()
+
+aa:start()
+ab:start()
+test:start()
diff --git a/lua/CMakeLists.txt b/lua/CMakeLists.txt
index abebece..88e4dd8 100644
--- a/lua/CMakeLists.txt
+++ b/lua/CMakeLists.txt
@@ -14,6 +14,22 @@ if(BUILD_LUA_RTT)
 
   else(NOT LUA51_FOUND)
 
+    if ( BUILD_RTALLOC )
+      # default to 512 kilobytes - see minimum size warning in deployer-funcs.cpp
+      set(ORO_DEFAULT_RTALLOC_SIZE 524288 CACHE STRING "Real-time memory pool size (bytes) for operations and logging.")
+      add_definitions("-DORO_BUILD_RTALLOC -DORO_DEFAULT_RTALLOC_SIZE=${ORO_DEFAULT_RTALLOC_SIZE}")
+    else ( BUILD_RTALLOC )
+      add_definitions("-DORO_DEFAULT_RTALLOC_SIZE=0")
+    endif ( BUILD_RTALLOC )
+
+    if ( BUILD_LOGGING )
+      include_directories( "${LOG4CPP_INCLUDE_DIRS}" )
+      link_directories( "${LOG4CPP_LIBRARY_DIRS}" )
+      list(APPEND EXTRA_LIBRARIES "${LOG4CPP_LIBRARY}")
+      list(APPEND EXTRA_DEPS "orocos-ocl-log4cpp")
+      add_definitions("-DORO_BUILD_LOGGING")
+    endif ( BUILD_LOGGING )
+
     # compiling lua component and rttlua interpreter
     include_directories(${LUA_INCLUDE_DIR})
 
@@ -23,8 +39,8 @@ if(BUILD_LUA_RTT)
 
     orocos_component(orocos-ocl-lua rtt.cpp LuaComponent.cpp )
     orocos_executable(rttlua rtt.cpp LuaComponent.cpp)
-    program_add_deps(rttlua lua-repl orocos-ocl-deployment ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES})
-    target_link_libraries(orocos-ocl-lua ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES})
+    program_add_deps(rttlua lua-repl orocos-ocl-deployment ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES} ${EXTRA_DEPS} ${EXTRA_LIBRARIES} )
+    target_link_libraries(orocos-ocl-lua ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES} )
     set_target_properties(orocos-ocl-lua PROPERTIES COMPILE_FLAGS "-DOCL_COMPONENT_ONLY")
     install(TARGETS rttlua RUNTIME DESTINATION bin)
     install(PROGRAMS ../bin/rttlua DESTINATION bin) # The shell script
@@ -57,7 +73,7 @@ if(BUILD_LUA_RTT)
       set_target_properties(orocos-ocl-lua-tlsf PROPERTIES COMPILE_FLAGS "-DOCL_COMPONENT_ONLY -DLUA_RTT_TLSF")
       set_target_properties(rttlua-tlsf PROPERTIES COMPILE_FLAGS "-DLUA_RTT_TLSF")
 
-      program_add_deps(rttlua-tlsf tlsf_rtt lua-repl-tlsf orocos-ocl-deployment ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES})
+      program_add_deps(rttlua-tlsf tlsf_rtt lua-repl-tlsf orocos-ocl-deployment ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES} ${EXTRA_DEPS} ${EXTRA_LIBRARIES} )
       target_link_libraries(orocos-ocl-lua-tlsf tlsf_rtt ${LUA_LIBRARIES} ${OROCOS-RTT_LIBRARIES})
 
       install(TARGETS rttlua-tlsf RUNTIME DESTINATION bin)
diff --git a/lua/LuaComponent.cpp b/lua/LuaComponent.cpp
index 6586c70..78e59af 100644
--- a/lua/LuaComponent.cpp
+++ b/lua/LuaComponent.cpp
@@ -33,6 +33,24 @@
  */
 
 #ifndef OCL_COMPONENT_ONLY
+
+#include <rtt/rtt-config.h>
+#ifdef OS_RT_MALLOC
+// need access to all TLSF functions embedded in RTT
+#define ORO_MEMORY_POOL
+#include <rtt/os/tlsf/tlsf.h>
+#endif
+#include <rtt/os/main.h>
+#include <rtt/RTT.hpp>
+#include <rtt/Logger.hpp>
+#ifdef  ORO_BUILD_LOGGING
+#   ifndef OS_RT_MALLOC
+#   warning "Logging needs rtalloc!"
+#   endif
+#include <log4cpp/HierarchyMaintainer.hh>
+#include "logging/Category.hpp"
+#endif
+
 extern "C" {
 #include "lua-repl.h"
 void dotty (lua_State *L);
@@ -249,6 +267,33 @@ int ORO_main(int argc, char** argv)
 	struct stat stb;
 	wordexp_t init_exp;
 
+#ifdef  ORO_BUILD_RTALLOC
+    size_t                  memSize     = ORO_DEFAULT_RTALLOC_SIZE;
+    void*                   rtMem       = 0;
+    size_t                  freeMem     = 0;
+    if (0 < memSize)
+    {
+        // don't calloc() as is first thing TLSF does.
+        rtMem = malloc(memSize);
+        assert(0 != rtMem);
+        freeMem = init_memory_pool(memSize, rtMem);
+        if ((size_t)-1 == freeMem)
+        {
+            cerr << "Invalid memory pool size of " << memSize
+                          << " bytes (TLSF has a several kilobyte overhead)." << endl;
+            free(rtMem);
+            return -1;
+        }
+        cout << "Real-time memory: " << freeMem << " bytes free of "
+                  << memSize << " allocated." << endl;
+    }
+#endif  // ORO_BUILD_RTALLOC
+
+#ifdef  ORO_BUILD_LOGGING
+    log4cpp::HierarchyMaintainer::set_category_factory(
+        OCL::logging::Category::createOCLCategory);
+#endif
+
 	LuaComponent lua("lua");
 	DeploymentComponent dc("deployer");
 	lua.connectPeers(&dc);
@@ -264,6 +309,26 @@ int ORO_main(int argc, char** argv)
 	wordfree(&init_exp);
 
 	lua.lua_repl(argc, argv);
+
+#ifdef  ORO_BUILD_LOGGING
+    log4cpp::HierarchyMaintainer::getDefaultMaintainer().shutdown();
+    log4cpp::HierarchyMaintainer::getDefaultMaintainer().deleteAllCategories();
+#endif
+
+#ifdef  ORO_BUILD_RTALLOC
+    if (0 != rtMem)
+    {
+        std::cout << "TLSF bytes allocated=" << memSize
+                  << " overhead=" << (memSize - freeMem)
+                  << " max-used=" << get_max_size(rtMem)
+                  << " currently-used=" << get_used_size(rtMem)
+                  << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
+                  << "\n";
+
+        destroy_memory_pool(rtMem);
+        free(rtMem);
+    }
+#endif
 	return 0;
 }
 
-- 
1.7.9.5

