Changeset 128:d7a4d9a4379e in rrlib_logging


Ignore:
Timestamp:
09.07.2014 18:30:47 (6 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Children:
129:67028d1b110b, 132:013f568ceb32
Phase:
public
Message:

Updated make.xml and converted "test program" into example

Files:
2 edited
1 copied
4 moved

Legend:

Unmodified
Added
Removed
  • examples/example.cpp

    r116 r128  
    2020// 
    2121//---------------------------------------------------------------------- 
    22 /*!\file    rrlib/logging/test/test_messages.cpp 
     22/*!\file    rrlib/logging/examples/example.cpp 
    2323 * 
    2424 * \author  Tobias Foehst 
     
    3333//---------------------------------------------------------------------- 
    3434#include <cstdlib> 
    35 #include <iostream> 
    3635#include <stdexcept> 
    37 #include <iterator> 
    3836#include <iomanip> 
    3937 
    40 extern "C" 
    41 { 
    4238#include <libgen.h> 
    43 #include <unistd.h> 
    44 } 
    45  
    46 #include "rrlib/util/join.h" 
    4739 
    4840#include "rrlib/logging/configuration.h" 
     
    5244// Internal includes with "" 
    5345//---------------------------------------------------------------------- 
    54 #include "rrlib/logging/test/libA/libA.h" 
    55 #include "rrlib/logging/test/libB/libB.h" 
     46#include "rrlib/logging/examples/lib/A.h" 
     47#include "rrlib/logging/examples/lib/B.h" 
    5648 
    5749//---------------------------------------------------------------------- 
     
    8274  static void function() 
    8375  { 
    84     RRLIB_LOG_PRINT_TO(my_domain, DEBUG_WARNING, "this ", "is a ", "local class test"); 
     76    RRLIB_LOG_PRINT_TO(.custom_domain, DEBUG_WARNING, "this ", "is a ", "concatenated debug warning message from local::Test::function() to custom domain"); 
    8577 
    86     RRLIB_LOG_PRINT(WARNING, "foo"); 
    87     RRLIB_LOG_PRINT_TO(my_domain, ERROR, "foo2"); 
     78    RRLIB_LOG_PRINT(WARNING, "Warning message from local::Test::function()"); 
     79    RRLIB_LOG_PRINT_TO(.custom_domain, ERROR, "Warning message from local::Test::function() to custom domain"); 
    8880    if (true) 
    8981    { 
    90       RRLIB_LOG_PRINTF_TO(my_domain, DEBUG, "%s\n", "FOO"); 
     82      RRLIB_LOG_PRINTF_TO(.custom_domain, DEBUG, "%s\n", "Debug message using printf syntax"); 
    9183    } 
    9284  } 
     
    10496  static void StaticMethod() 
    10597  { 
    106     RRLIB_LOG_PRINT_STATIC(USER, "From static method"); 
     98    RRLIB_LOG_PRINT_STATIC(DEBUG, "Debug message from static context"); 
    10799  } 
    108100 
    109101  void NonStaticMethod() 
    110102  { 
    111     RRLIB_LOG_PRINT(USER, "From non-static method"); 
     103    RRLIB_LOG_PRINT(DEBUG, "Debug message from non-static context"); 
    112104  } 
    113105}; 
     
    116108int main(int argc, char **argv) 
    117109{ 
     110  /*** Setup some needed basic strings ***/ 
    118111  rrlib::logging::default_log_description = basename(argv[0]); 
    119112  rrlib::logging::SetLogFilenamePrefix(basename(argv[0])); 
    120113 
     114  bool domains_configured = false; 
     115 
     116  /*** Add configuring from file if xml support is available ***/ 
    121117#ifdef _LIB_RRLIB_XML_PRESENT_ 
    122   if (!rrlib::logging::ConfigureFromFile("logging_config.xml")) 
     118  if (argc > 2) 
    123119  { 
    124     RRLIB_LOG_PRINT(ERROR, "Loading configuration failed!"); 
     120    RRLIB_LOG_PRINTF(ERROR, "Usage: %s [logging_config.xml]", argv[0]); 
    125121    return EXIT_FAILURE; 
    126122  } 
    127  
    128   rrlib::logging::PrintDomainConfigurations(); 
     123  if (argc == 2) 
     124  { 
     125    try 
     126    { 
     127      domains_configured = rrlib::logging::ConfigureFromFile(argv[1]); 
     128    } 
     129    catch (const rrlib::xml::tException &exception) 
     130    { 
     131      RRLIB_LOG_PRINT(ERROR, "Loading configuration failed: ", exception.what()); 
     132      return EXIT_FAILURE; 
     133    } 
     134  } 
     135#else 
     136  if (argc != 1) 
     137  { 
     138    RRLIB_LOG_PRINTF(ERROR, "Usage: %s", argv[0]); 
     139    return EXIT_FAILURE; 
     140  } 
    129141#endif 
    130142 
    131 //  rrlib::logging::SetDomainPrintsName(".", true); 
    132 //  rrlib::logging::SetDomainPrintsTime(".", true); 
    133 //  rrlib::logging::SetDomainPrintsLevel(".", true); 
    134 //  rrlib::logging::SetDomainPrintsLocation(".", true); 
    135 //  rrlib::logging::SetDomainMaxMessageLevel(".", DEBUG_VERBOSE_3); 
    136 //  rrlib::logging::SetDomainSink(".", rrlib::logging::eLOG_SINK_FILE); 
     143  if (!domains_configured) 
     144  { 
     145    /*** Our custom domain gets a custom configuration if not configured from file ***/ 
     146    RRLIB_LOG_PRINT(USER, "Using exemplary configuration for custom domain"); 
     147    rrlib::logging::SetDomainPrintsName(".custom_domain", true); 
     148    rrlib::logging::SetDomainPrintsTime(".custom_domain", true); 
     149    rrlib::logging::SetDomainPrintsLevel(".custom_domain", true); 
     150    rrlib::logging::SetDomainPrintsLocation(".custom_domain", true); 
     151    rrlib::logging::SetDomainMaxMessageLevel(".custom_domain", rrlib::logging::tLogLevel::DEBUG_VERBOSE_3); 
     152  } 
    137153 
    138   rrlib::logging::SetDomainMaxMessageLevel(".example", rrlib::logging::tLogLevel::DEBUG_VERBOSE_3); 
    139 //  rrlib::logging::SetDomainSink(".example", rrlib::logging::eLOG_SINK_STDOUT, rrlib::logging::eLOG_SINK_COMBINED_FILE); 
     154  /*** Have a look on the configured domains ***/ 
     155  RRLIB_LOG_PRINT(USER, "These are the configured log domains:"); 
     156  rrlib::logging::PrintDomainConfigurations(); 
    140157 
    141   RRLIB_LOG_PRINT(WARNING, "foo"); 
     158  /*** Generate some messages from different contexts and of different type ***/ 
     159  RRLIB_LOG_PRINT(WARNING, "Warning message from main()"); 
    142160 
    143   libA::Test(); 
    144   libB::Test(); 
     161  A::Test(); 
     162  B::Test(); 
    145163 
    146164  local::Test::function(); 
    147165 
    148   std::runtime_error exception("runtime_error"); 
    149   std::exception &e(exception); 
    150   RRLIB_LOG_PRINT(ERROR, e); 
     166  RRLIB_LOG_PRINT(ERROR, std::runtime_error("Forwarding a std::runtime_error directly as error messsage")); 
    151167 
    152   RRLIB_LOG_PRINT(WARNING, "0x", std::setw(20), std::setfill('0'), std::hex, 324); 
     168  RRLIB_LOG_PRINT(WARNING, "Warning message using std::iomanip: 0x", std::setw(20), std::setfill('0'), std::hex, 324); 
    153169 
    154   RRLIB_LOG_PRINT(ERROR, "Das hier ist ein mehrzeiliger\nFehler.\n"); 
    155   RRLIB_LOG_PRINT(USER, "Und das hier ein mehrzeiliger\nText fuer den lieben Benutzer."); 
    156  
    157   const char* texts[] = {"Dies", "ist", "ein", "kleiner", "Text."}; 
    158   RRLIB_LOG_PRINT(DEBUG, rrlib::util::Join(texts, texts + 5, " ")); 
     170  RRLIB_LOG_PRINT(ERROR, "Multiline error message with\npadding and handling of trailing newline.\n"); 
     171  RRLIB_LOG_PRINT(USER, "Multiline user message\nthat should behave accordingly.\n"); 
    159172 
    160173  TestStatic test_static; 
     
    164177  int *a = 0; 
    165178  const int *b = 0; 
    166   RRLIB_LOG_PRINT(DEBUG, "Pointer: ", a); 
    167   RRLIB_LOG_PRINT(DEBUG, "Const-Pointer: ", b); 
    168   RRLIB_LOG_PRINT(DEBUG, "Bool: ", true, false); 
    169   RRLIB_LOG_PRINT(DEBUG, "Mal noch einzelne Zeichen: ", 'a', '\0', 'b'); 
     179  RRLIB_LOG_PRINT(DEBUG, "Handling of some special data types:\n" 
     180                  "- Pointer:\t\t\t", a, "\n", 
     181                  "- Const-Pointer:\t\t", b, "\n", 
     182                  "- Bool:\t\t\t", true, false, "\n", 
     183                  "- Single characters:\t", 'a', '\0', 'b', "\n"); 
    170184 
    171   RRLIB_LOG_PRINT_TO(.speech_log, DEBUG, "This is a speech-log message"); 
     185  /*** In the end, get a list of domains that were configured or used by this program ***/ 
     186  RRLIB_LOG_PRINT(USER, "These are the used and configured log domains:"); 
     187  rrlib::logging::PrintDomainConfigurations(); 
    172188 
    173   std::cout << "Waiting 10 seconds for optional speech synthesis" << std::endl; 
    174   sleep(10); 
    175  
    176   rrlib::logging::PrintDomainConfigurations(); 
    177189  return EXIT_SUCCESS; 
    178190} 
  • examples/lib/A.h

    r94 r128  
    1 #ifndef _LIBA_H_ 
    2 #define _LIBA_H_ 
     1#ifndef _LIB_A_H_ 
     2#define _LIB_A_H_ 
    33 
    44#include "rrlib/logging/messages.h" 
    55 
    6 namespace libA 
     6namespace A 
    77{ 
    88void Test() 
    99{ 
    10   RRLIB_LOG_PRINT(DEBUG, "Test in libA"); 
     10  RRLIB_LOG_PRINT(DEBUG, "Debug message from A::Test()"); 
    1111} 
    1212} 
  • examples/lib/B.h

    r94 r128  
    1 #ifndef _LIBB_H_ 
    2 #define _LIBB_H_ 
     1#ifndef _LIB_B_H_ 
     2#define _LIB_B_H_ 
    33 
    44#include "rrlib/logging/messages.h" 
    55 
    6 namespace libB 
     6namespace B 
    77{ 
    88void Test() 
    99{ 
    10   RRLIB_LOG_PRINT(USER, "Test in libB"); 
     10  RRLIB_LOG_PRINT(USER, "User message from B::Test()"); 
    1111} 
    1212} 
  • examples/logging_config.xml

    r106 r128  
    22<!DOCTYPE rrlib_logging PUBLIC "-//RRLIB//DTD logging 1.0" "http://rrlib.org/xml/1.0/logging.dtd"> 
    33<rrlib_logging> 
     4 
    45  <domain name="." prints_time="true" prints_name="true" prints_level="true" max_level="debug_verbose_3"> 
    56    <sink> 
    67      <stream id="stdout" /> 
    7 <!-- 
    8       <file /> 
    9 --> 
    10     </sink>     
     8    </sink> 
     9    <domain name="custom_domain" prints_time="false" prints_name="true" prints_level="false"> 
     10      <sink> 
     11        <stream id="stderr" /> 
     12      </sink> 
     13    </domain> 
    1114  </domain> 
    12 <!-- 
    13   <domain name=".speech_log" prints_time="false" prints_name="false" prints_level="false" prints_location="false" max_level="debug"> 
    14     <sink> 
    15       <speech_synthesis voice="festival://kal_diphone" /> 
    16     </sink> 
    17   </domain> 
    18 --> 
     15 
    1916</rrlib_logging> 
  • examples/make.xml

    r122 r128  
    11<?xml version="1.0" encoding="UTF-8"?> 
    2 <!DOCTYPE targets PUBLIC "-//RRLIB//DTD make 1.0" "http://rrlib.org/xml/1.0/make.dtd"> 
     2<!DOCTYPE targets PUBLIC "-//RRLIB//DTD make 14.05" "http://finroc.org/xml/14.05/make.dtd"> 
    33<targets> 
    44 
    5   <rrlib> 
     5  <program> 
    66    <sources> 
    7       configuration/* 
    8       messages/* 
    9       sinks/tFile.cpp 
    10       sinks/tStream.cpp 
    11       configuration.cpp 
    12       default_log_description.cpp 
    13       messages.h 
     7      lib/* 
     8      example.cpp 
    149    </sources> 
    15   </rrlib> 
    16  
    17   <rrlib name="log_levels"> 
    18     <sources> 
    19       log_levels.h 
    20     </sources> 
    21   </rrlib> 
    22    
    23   <rrlib name="sinks"> 
    24     <sources> 
    25       sinks/tSink.cpp 
    26     </sources> 
    27   </rrlib> 
    28  
    29   <rrlib name="sinks_speech_synthesis" optional="true"> 
    30     <sources> 
    31       sinks/tSpeechSynthesis.cpp 
    32     </sources> 
    33   </rrlib> 
    34  
    35   <testprogram name="messages"> 
    36     <sources> 
    37       test/test_messages.cpp 
    38       test/libA/* 
    39       test/libB/* 
    40     </sources> 
    41   </testprogram> 
     10  </program> 
    4211 
    4312</targets> 
  • make.xml

    r122 r128  
    11<?xml version="1.0" encoding="UTF-8"?> 
    2 <!DOCTYPE targets PUBLIC "-//RRLIB//DTD make 1.0" "http://rrlib.org/xml/1.0/make.dtd"> 
     2<!DOCTYPE targets PUBLIC "-//RRLIB//DTD make 14.05" "http://finroc.org/xml/14.05/make.dtd"> 
    33<targets> 
    44 
    5   <rrlib> 
     5  <library> 
    66    <sources> 
    77      configuration/* 
     
    1313      messages.h 
    1414    </sources> 
    15   </rrlib> 
     15  </library> 
    1616 
    17   <rrlib name="log_levels"> 
     17  <library name="log_levels"> 
    1818    <sources> 
    1919      log_levels.h 
    2020    </sources> 
    21   </rrlib> 
     21  </library> 
    2222   
    23   <rrlib name="sinks"> 
     23  <library name="sinks"> 
    2424    <sources> 
    2525      sinks/tSink.cpp 
    2626    </sources> 
    27   </rrlib> 
     27  </library> 
    2828 
    29   <rrlib name="sinks_speech_synthesis" optional="true"> 
     29  <library name="sinks_speech_synthesis" optional="true"> 
    3030    <sources> 
    3131      sinks/tSpeechSynthesis.cpp 
    3232    </sources> 
    33   </rrlib> 
    34  
    35   <testprogram name="messages"> 
    36     <sources> 
    37       test/test_messages.cpp 
    38       test/libA/* 
    39       test/libB/* 
    40     </sources> 
    41   </testprogram> 
     33  </library> 
    4234 
    4335</targets> 
  • messages/tStream.h

    r125 r128  
    200200   * \returns A reference to the altered stream (in this case the proxy) 
    201201   */ 
    202   inline tStream &operator << (const rrlib::time::tTimestamp& value) 
    203   { 
    204     this->stream << rrlib::time::ToIsoString(value); 
     202  inline tStream &operator << (const time::tTimestamp& value) 
     203  { 
     204    this->stream << time::ToIsoString(value); 
    205205    return *this; 
    206206  } 
Note: See TracChangeset for help on using the changeset viewer.