source: rrlib_logging/examples/example.cpp @ 128:d7a4d9a4379e

Last change on this file since 128:d7a4d9a4379e was 128:d7a4d9a4379e, checked in by Tobias Föhst <foehst@…>, 6 years ago

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

File size: 6.1 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/logging/examples/example.cpp
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-06-16
27 *
28 */
29//----------------------------------------------------------------------
30
31//----------------------------------------------------------------------
32// External includes (system with <>, local with "")
33//----------------------------------------------------------------------
34#include <cstdlib>
35#include <stdexcept>
36#include <iomanip>
37
38#include <libgen.h>
39
40#include "rrlib/logging/configuration.h"
41#include "rrlib/logging/messages.h"
42
43//----------------------------------------------------------------------
44// Internal includes with ""
45//----------------------------------------------------------------------
46#include "rrlib/logging/examples/lib/A.h"
47#include "rrlib/logging/examples/lib/B.h"
48
49//----------------------------------------------------------------------
50// Debugging
51//----------------------------------------------------------------------
52
53//----------------------------------------------------------------------
54// Namespace usage
55//----------------------------------------------------------------------
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Const values
63//----------------------------------------------------------------------
64
65//----------------------------------------------------------------------
66// Implementation
67//----------------------------------------------------------------------
68
69namespace local
70{
71
72struct Test
73{
74  static void function()
75  {
76    RRLIB_LOG_PRINT_TO(.custom_domain, DEBUG_WARNING, "this ", "is a ", "concatenated debug warning message from local::Test::function() to custom domain");
77
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");
80    if (true)
81    {
82      RRLIB_LOG_PRINTF_TO(.custom_domain, DEBUG, "%s\n", "Debug message using printf syntax");
83    }
84  }
85};
86
87}
88
89struct TestStatic
90{
91  const char *GetLogDescription()
92  {
93    return "TestStatic";
94  }
95
96  static void StaticMethod()
97  {
98    RRLIB_LOG_PRINT_STATIC(DEBUG, "Debug message from static context");
99  }
100
101  void NonStaticMethod()
102  {
103    RRLIB_LOG_PRINT(DEBUG, "Debug message from non-static context");
104  }
105};
106
107
108int main(int argc, char **argv)
109{
110  /*** Setup some needed basic strings ***/
111  rrlib::logging::default_log_description = basename(argv[0]);
112  rrlib::logging::SetLogFilenamePrefix(basename(argv[0]));
113
114  bool domains_configured = false;
115
116  /*** Add configuring from file if xml support is available ***/
117#ifdef _LIB_RRLIB_XML_PRESENT_
118  if (argc > 2)
119  {
120    RRLIB_LOG_PRINTF(ERROR, "Usage: %s [logging_config.xml]", argv[0]);
121    return EXIT_FAILURE;
122  }
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  }
141#endif
142
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  }
153
154  /*** Have a look on the configured domains ***/
155  RRLIB_LOG_PRINT(USER, "These are the configured log domains:");
156  rrlib::logging::PrintDomainConfigurations();
157
158  /*** Generate some messages from different contexts and of different type ***/
159  RRLIB_LOG_PRINT(WARNING, "Warning message from main()");
160
161  A::Test();
162  B::Test();
163
164  local::Test::function();
165
166  RRLIB_LOG_PRINT(ERROR, std::runtime_error("Forwarding a std::runtime_error directly as error messsage"));
167
168  RRLIB_LOG_PRINT(WARNING, "Warning message using std::iomanip: 0x", std::setw(20), std::setfill('0'), std::hex, 324);
169
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");
172
173  TestStatic test_static;
174  test_static.StaticMethod();
175  test_static.NonStaticMethod();
176
177  int *a = 0;
178  const int *b = 0;
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");
184
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();
188
189  return EXIT_SUCCESS;
190}
Note: See TracBrowser for help on using the repository browser.