Gazebo Common

API Reference

5.5.1
gz/common/Console.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Open Source Robotics Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16*/
17#ifndef GZ_COMMON_CONSOLE_HH_
18#define GZ_COMMON_CONSOLE_HH_
19
20#include <iostream>
21#include <fstream>
22#include <memory>
23#include <mutex>
24#include <sstream>
25#include <string>
26
27#include <gz/common/Export.hh>
28#include <gz/utils/SuppressWarning.hh>
29#include <gz/common/Util.hh>
30
31namespace gz
32{
33 namespace common
34 {
35 // TODO(CH3): Deprecated. Remove all the ign macros on tock.
37 #define gzerr (gz::common::Console::err(__FILE__, __LINE__))
38 #define ignerr gzerr
39
41 #define gzwarn (gz::common::Console::warn(__FILE__, __LINE__))
42 #define ignwarn gzwarn
43
45 #define gzmsg (gz::common::Console::msg())
46 #define ignmsg gzmsg
47
49 #define gzdbg (gz::common::Console::dbg(__FILE__, __LINE__))
50 #define igndbg gzdbg
51
53 #define gzlog (gz::common::Console::log())
54 #define ignlog gzlog
55
63 #define gzLogInit(_dir, _file)\
64 gz::common::Console::log.Init(_dir, _file)
65 #define ignLogInit(_dir, _file) gzLogInit(_dir, _file)
66
68 #define gzLogClose()\
69 gz::common::Console::log.Close()
70 #define ignLogClose() gzLogClose()
71
74 #define gzLogDirectory()\
75 (gz::common::Console::log.LogDirectory())
76 #define ignLogDirectory() gzLogDirectory()
77
80 class GZ_COMMON_VISIBLE FileLogger : public std::ostream
81 {
85 public: explicit FileLogger(const std::string &_filename = "");
86
88 public: virtual ~FileLogger();
89
93 public: void Init(const std::string &_directory,
94 const std::string &_filename);
95
97 public: void Close();
98
102 public: virtual FileLogger &operator()();
103
109 public: virtual FileLogger &operator()(
110 const std::string &_file, int _line);
111
115 public: std::string LogDirectory() const;
116
118 protected: class Buffer : public std::stringbuf
119 {
122 public: explicit Buffer(const std::string &_filename);
123
125 public: virtual ~Buffer();
126
132 const char *_char, std::streamsize _count) override;
133
137 public: int sync() override;
138
141
145 };
146
147 GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
150 private: std::string logDirectory;
151 GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
152
154 private: bool initialized;
155 };
156
159 class GZ_COMMON_VISIBLE Logger : public std::ostream
160 {
163 public: enum LogType
164 {
168 STDERR
169 };
170
176 public: Logger(const std::string &_prefix, const int _color,
177 const LogType _type, const int _verbosity);
178
180 public: virtual ~Logger();
181
184 public: virtual Logger &operator()();
185
191 public: virtual Logger &operator()(
192 const std::string &_file, int _line);
193
195 protected: class Buffer : public std::stringbuf
196 {
202 public: Buffer(LogType _type, const int _color,
203 const int _verbosity);
204
206 public: virtual ~Buffer();
207
213 const char *_char, std::streamsize _count) override;
214
218 public: int sync() override;
219
221 public: LogType type;
222
226 public: int color;
227
229 public: int verbosity;
230
234 };
235
236 GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
238 private: std::string prefix;
239 GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
240 };
241
245 class GZ_COMMON_VISIBLE Console
246 {
254 public: static void SetVerbosity(const int _level);
255
259 public: static int Verbosity();
260
274 public: static void SetPrefix(const std::string &_customPrefix);
275
279 public: static std::string Prefix();
280
282 public: static Logger msg;
283
285 public: static Logger err;
286
288 public: static Logger dbg;
289
291 public: static Logger warn;
292
294 public: static FileLogger log;
295
297 private: static int verbosity;
298
299 GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
301 private: static std::string customPrefix;
302 GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
303 };
304 }
305}
306#endif