TrueReality  v0.1.1912
Log.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author Matthew W. Campbell
25 * @author Erik Johnson
26 * @author David Guthrie
27 * @author Maxim Serebrennik
28 */
29 #pragma once
30 #include <trUtil/Export.h>
31 
32 #include <trUtil/DateTime.h>
33 #include <trUtil/Logging/LogFile.h>
35 
36 #include <string>
37 #include <cstdarg>
38 #include <vector>
39 #include <sstream>
40 
41 #include <osg/Referenced>
42 #include <osg/ref_ptr>
43 
44 
45 namespace trUtil::Logging
46 {
47  // Fwd declaration
48  class LogWriter;
49  class LogTimeProvider;
50  class LogManager;
51  class LogImpl;
52 
59 #define TR_LOG_SOURCE __FILE__, __FUNCTION__, __LINE__
60 
70 #define LOG_FULL(level, name, msg) \
71  {\
72  trUtil::Logging::Log& logger = trUtil::Logging::Log::GetInstance(name); \
73  if (logger.IsLevelEnabled(level)) \
74  {\
75  std::ostringstream st;\
76  st << msg;\
77  logger.LogMessage(TR_LOG_SOURCE, st.str(), level); \
78  }\
79  }\
80 
81 
89 #define LOGN_D(name, msg) LOG_FULL(trUtil::Logging::LogLevel::LOG_DEBUG, name, msg)
90 
99 #define LOGN_I(name, msg) LOG_FULL(trUtil::Logging::LogLevel::LOG_INFO, name, msg)
100 
109 #define LOGN_W(name, msg) LOG_FULL(trUtil::Logging::LogLevel::LOG_WARNING, name, msg)
110 
119 #define LOGN_E(name, msg) LOG_FULL(trUtil::Logging::LogLevel::LOG_ERROR, name, msg)
120 
129 #define LOGN_A(name, msg) LOG_FULL(trUtil::Logging::LogLevel::LOG_ALWAYS, name, msg)
130 
138 #define LOG_D(msg) LOGN_D(trUtil::Logging::Log::LOG_DEFAULT_NAME, msg)
139 
147 #define LOG_I(msg) LOGN_I(trUtil::Logging::Log::LOG_DEFAULT_NAME, msg)
148 
156 #define LOG_W(msg) LOGN_W(trUtil::Logging::Log::LOG_DEFAULT_NAME, msg)
157 
165 #define LOG_E(msg) LOGN_E(trUtil::Logging::Log::LOG_DEFAULT_NAME, msg)
166 
174 #define LOG_A(msg) LOGN_A(trUtil::Logging::Log::LOG_DEFAULT_NAME, msg)
175 
183 #define LOG_PRINT_TEST \
184  LOG_D("Test Message") \
185  LOG_I("Test Message") \
186  LOG_W("Test Message") \
187  LOG_E("Test Message") \
188  LOG_A("Test Message")
189 
197  class TR_UTIL_EXPORT Log : public osg::Referenced
198  {
199  public:
200 
206  struct LogTestData
207  {
208  trUtil::Logging::LogLevel logLevel;
210  unsigned frameNumber;
211  std::string logName;
212  std::string file;
213  std::string method;
214  int line;
215  std::string msg;
216  };
217 
218  static const std::string LOG_DEFAULT_NAME;
219 
231  void LogMessage(const std::string& cppFile, const std::string& method, int line, const std::string& msg, LogLevel logLevel) const;
232 
247  void LogMessage(LogLevel logLevel, const std::string& source, int line, const char* msg, ...) const;
248 
262  void LogMessage(LogLevel logLevel, const std::string& source, int line, const std::string& msg) const;
263 
278  void LogMessage(LogLevel logLevel, const std::string& source, int line, const char* msg, va_list list) const;
279 
293  void LogMessage(LogLevel logLevel, const std::string &source, const char *msg, ...) const;
294 
300  void LogHorizRule();
301 
311  bool IsLevelEnabled(LogLevel logLevel) const;
312 
321  void SetLogLevel(LogLevel logLevel);
322 
330  LogLevel GetLogLevel() const;
331 
341  static const std::string GetLogLevelString(LogLevel logLevel);
342 
352  static LogLevel GetLogLevelForString(const std::string& levelString);
353 
366  static Log& GetInstance(const std::string& name = Log::LOG_DEFAULT_NAME);
367 
375  static void SetDefaultLogLevel(LogLevel newLevel);
376 
386  static void SetAllLogLevels(LogLevel newLevel);
387 
397  static void SetLogTimeProvider(LogTimeProvider* ltp);
398 
411  void AddWriter(LogWriter& writer);
412 
422  void RemoveWriter(LogWriter& writer);
423 
425  using LogWriterContainer = std::vector<osg::ref_ptr<LogWriter>>;
426 
434  const LogWriterContainer& GetWriters() const;
435 
443  LogWriterContainer& GetWriters();
444 
451  {
452  NO_OUTPUT = 0x00000000, /* Log messages don't get written to any device*/
453  TO_FILE = 0x00000001, /* Log messages get sent to the output file*/
454  TO_CONSOLE = 0x00000002,/* Log messages get sent to the console*/
455  TO_WRITER = 0x00000004,/* Log messages get sent to all registered writers*/
456  STANDARD = TO_FILE | TO_CONSOLE | TO_WRITER /* The default setting*/
457  };
458 
472  void SetOutputStreamBit(unsigned int option);
473 
481  static void SetAllOutputStreamBits(unsigned int option);
482 
490  unsigned int GetOutputStreamBit() const;
491 
499  const std::string& GetName() const;
500 
508  LogManager& GetLogManagerRef();
509 
517  void SetTestMode(bool state);
518 
526  LogTestData* GetLastLogData() const;
527 
528  protected:
529 
538  Log(const std::string& name);
539 
545  ~Log();
546 
547  private:
549 
550  bool mTestingMode = false;
552  };
553 
561  {
562  public:
563 
571  LoggingOff(const std::string& name = Log::LOG_DEFAULT_NAME);
572 
578  ~LoggingOff();
579  private:
581  LogLevel mOldLevel;
582  };
583 }
Log class which the engine uses for all of its logging needs.
Definition: Log.h:197
trUtil::Logging::LogLevel logLevel
Definition: Log.h:208
Interface class get the time for the logger.
std::string file
The name of the Log instance (could be empty)
Definition: Log.h:212
int line
The calling method of the message.
Definition: Log.h:214
Scoped variable to turn off logging (except for always) for a block of code.
Definition: Log.h:560
LogImpl * mImpl
Definition: Log.h:548
std::string msg
The line number of the source code of the message.
Definition: Log.h:215
std::vector< osg::ref_ptr< LogWriter > > LogWriterContainer
The log writer container.
Definition: Log.h:425
Manager for logs.
Definition: LogManager.h:56
A data structure that is used for Unit Tests.
Definition: Log.h:206
OutputStreamOptions
Values that represent output stream options.
Definition: Log.h:450
Interface class to receive messages from the Log.
Definition: LogWriter.h:51
std::string logName
The frame number.
Definition: Log.h:211
A class that represents date time utility.
trUtil::DateTime time
Log level.
Definition: Log.h:209
static const std::string LOG_DEFAULT_NAME
Definition: Log.h:218
unsigned frameNumber
Time of message.
Definition: Log.h:210
std::string method
The source file of the message.
Definition: Log.h:213
trUtil::Logging::Log & mLog
Definition: Log.h:580
LogTestData mLogTestData
Definition: Log.h:551