File: Console.hh

package info (click to toggle)
ignition-common 4.5.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,120 kB
  • sloc: cpp: 29,331; ansic: 5,583; javascript: 2,998; sh: 31; makefile: 23
file content (286 lines) | stat: -rw-r--r-- 10,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2016 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/
#ifndef IGNITION_COMMON_CONSOLE_HH_
#define IGNITION_COMMON_CONSOLE_HH_

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include <ignition/common/Util.hh>
#include <ignition/common/Export.hh>
#include <ignition/common/SuppressWarning.hh>

namespace ignition
{
  namespace common
  {
    /// \brief Output an error message, if the verbose level is >= 1
    #define ignerr (ignition::common::Console::err(__FILE__, __LINE__))

    /// \brief Output a warning message, if the verbose level is >= 2
    #define ignwarn (ignition::common::Console::warn(__FILE__, __LINE__))

    /// \brief Output a message, if the verbose level is >= 3
    #define ignmsg (ignition::common::Console::msg())

    /// \brief Output a debug message, if the verbose level is >= 4
    #define igndbg (ignition::common::Console::dbg(__FILE__, __LINE__))

    /// \brief Output a message to a log file, regardless of verbosity level
    #define ignlog (ignition::common::Console::log())

    /// \brief Initialize log file with filename given by _dir/_file.
    /// If called twice, it will close the file currently in use and open a new
    /// log file.
    /// \param[in] _dir Name of directory in which to store the log file. Note
    /// that if _dir is not an absolute path, then _dir will
    /// be relative to your home directory.
    /// \param[in] _file Name of log file for ignlog messages.
    #define ignLogInit(_dir, _file)\
        ignition::common::Console::log.Init(_dir, _file)

    /// \brief Close the file used for logging.
    #define ignLogClose()\
        ignition::common::Console::log.Close()

    /// \brief Get the full path of the directory where the log files are stored
    /// \return Full path of the directory
    #define ignLogDirectory()\
        (ignition::common::Console::log.LogDirectory())

    /// \class FileLogger FileLogger.hh common/common.hh
    /// \brief A logger that outputs messages to a file.
    class IGNITION_COMMON_VISIBLE FileLogger : public std::ostream
    {
      /// \brief Constructor.
      /// \param[in] _filename Filename to write into. If empty,
      /// FileLogger::Init must be called separately.
      public: explicit FileLogger(const std::string &_filename = "");

      /// \brief Destructor.
      public: virtual ~FileLogger();

      /// \brief Initialize the file logger.
      /// \param[in] _directory Name of directory that holds the log file.
      /// \param[in] _filename Name of the log file to write output into.
      public: void Init(const std::string &_directory,
                        const std::string &_filename);

      /// \brief Close the open file handles.
      public: void Close();

      /// \brief Output a filename and line number, then return a reference
      /// to the logger.
      /// \return Reference to this logger.
      public: virtual FileLogger &operator()();

      /// \brief Output a filename and line number, then return a reference
      /// to the logger.
      /// \param[in] _file Filename to output.
      /// \param[in] _line Line number in the _file.
      /// \return Reference to this logger.
      public: virtual FileLogger &operator()(
                  const std::string &_file, int _line);

      /// \brief Get the full path of the directory where all the log files
      /// are stored.
      /// \return Full path of the directory.
      public: std::string LogDirectory() const;

      /// \brief String buffer for the file logger.
      protected: class Buffer : public std::stringbuf
                 {
                   /// \brief Constructor.
                   /// \param[in] _filename Filename to write into.
                   public: explicit Buffer(const std::string &_filename);

                   /// \brief Destructor.
                   public: virtual ~Buffer();

                   /// \brief Sync the stream (output the string buffer
                   /// contents).
                   /// \return Return 0 on success.
                   public: virtual int sync();

                   /// \brief Stream to output information into.
                   public: std::ofstream *stream;
                 };

      IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
      /// \brief Stores the full path of the directory where all the log files
      /// are stored.
      private: std::string logDirectory;
      IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING

      /// \brief True if initialized.
      private: bool initialized;
    };

    /// \class Logger Logger.hh common/common.hh
    /// \brief Terminal logger.
    class IGNITION_COMMON_VISIBLE Logger : public std::ostream
    {
      /// \enum LogType.
      /// \brief Output destination type.
      public: enum LogType
              {
                /// \brief Output to stdout.
                STDOUT,
                /// \brief Output to stderr.
                STDERR
              };

      /// \brief Constructor.
      /// \param[in] _prefix String to use as prefix when logging to file.
      /// \param[in] _color Color of the output stream.
      /// \param[in] _type Output destination type (STDOUT, or STDERR)
      /// \param[in] _verbosity Verbosity level.
      public: Logger(const std::string &_prefix, const int _color,
                     const LogType _type, const int _verbosity);

      /// \brief Destructor.
      public: virtual ~Logger();

      /// \brief Access operator.
      /// \return Reference to this logger.
      public: virtual Logger &operator()();

      /// \brief Output a filename and line number, then return a reference
      /// to the logger.
      /// \param[in] _file Filename to output.
      /// \param[in] _line Line number in the _file.
      /// \return Reference to this logger.
      public: virtual Logger &operator()(
                  const std::string &_file, int _line);

      /// \brief String buffer for the base logger.
      protected: class Buffer : public std::stringbuf
                 {
                   /// \brief Constructor.
                   /// \param[in] _type Output destination type
                   /// (STDOUT, or STDERR)
                   /// \param[in] _color Color of the output stream.
                   /// \param[in] _verbosity Verbosity level.
                   public: Buffer(LogType _type, const int _color,
                                  const int _verbosity);

                   /// \brief Destructor.
                   public: virtual ~Buffer();

                   /// \brief Writes _count characters to the string buffer
                   /// \param[in] _char Input rharacter array.
                   /// \param[in] _count Number of characters in array.
                   /// \return The number of characters successfully written.
                   public: std::streamsize xsputn(
                        const char *_char, std::streamsize _count) override;

                   /// \brief Sync the stream (output the string buffer
                   /// contents).
                   /// \return Return 0 on success.
                   public: int sync() override;

                   /// \brief Destination type for the messages.
                   public: LogType type;

                   /// \brief ANSI color code using Select Graphic Rendition
                   /// parameters (SGR). See
                   /// http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
                   public: int color;

                   /// \brief Level of verbosity
                   public: int verbosity;

                   /// \brief Mutex to synchronize writes to the string buffer
                   /// and the output stream.
                   /// \todo(nkoenig) Put this back in for ign-common5, and
                   /// remove the corresponding static version.
                   // public: std::mutex syncMutex;
                 };

      IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
      /// \brief Prefix to use when logging to file.
      private: std::string prefix;
      IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
    };

    /// \class Console Console.hh common/common.hh
    /// \brief Container for loggers, and global logging options
    /// (such as verbose vs. quiet output).
    class IGNITION_COMMON_VISIBLE Console
    {
      /// \brief Set verbosity, where
      /// <= 0: No output,
      /// 1: Error messages,
      /// 2: Error and warning messages,
      /// 3: Error, warning, and info messages,
      /// >= 4: Error, warning, info, and debug messages.
      /// \param[in] _level The new verbose level.
      public: static void SetVerbosity(const int _level);

      /// \brief Get the verbose level.
      /// \return The level of verbosity.
      /// \sa SetVerbosity(const int _level)
      public: static int Verbosity();

      /// \brief Add a custom prefix in front of the default prefixes.
      ///
      /// By default, the custom prefix is an empty string, so the messages
      /// start as:
      ///
      /// [Err], [Wrn], [Msg], [Dbg]
      ///
      /// If you set the prefix to "-my-", for example, they become:
      ///
      /// -my-[Err], -my-[Wrn], -my-[Msg], -my-[Dbg]
      ///
      /// \param[in] _customPrefix Prefix string.
      /// \sa std::string Prefix() const
      public: static void SetPrefix(const std::string &_customPrefix);

      /// \brief Get custom prefix. This is empty by default.
      /// \return The custom prefix.
      /// \sa void SetPrefix(const std::string &_customPrefix)
      public: static std::string Prefix();

      /// \brief Global instance of the message logger.
      public: static Logger msg;

      /// \brief Global instance of the error logger.
      public: static Logger err;

      /// \brief Global instance of the debug logger.
      public: static Logger dbg;

      /// \brief Global instance of the warning logger.
      public: static Logger warn;

      /// \brief Global instance of the file logger.
      public: static FileLogger log;

      /// \brief The level of verbosity, the default level is 1.
      private: static int verbosity;

      IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
      /// \brief A custom prefix. See SetPrefix().
      private: static std::string customPrefix;
      IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
    };
  }
}
#endif