TrueReality  v0.1.1912
LogManager.cpp
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 * @author Matthew W. Campbell
22 * @author Erik Johnson
23 * @author David Guthrie
24 * @author Maxim Serebrennik
25 */
27 
29 
30 #include <algorithm>
31 
32 namespace trUtil::Logging
33 {
36  : mLogWriterConsole(new LogWriterConsole())
37  , mLogWriterFile(new LogWriterFile())
38  , mLogTimeProvider(nullptr)
39  {
40  }
41 
44  {
45  mInstances.clear();
46  mLogWriterConsole = nullptr;
47  mLogWriterFile = nullptr;
48  }
49 
51  bool LogManager::AddInstance(const std::string& name, Log* log)
52  {
53  return mInstances.insert(std::make_pair(name, osg::ref_ptr<Log>(log))).second;
54  }
55 
57  Log* LogManager::GetInstance(const std::string& name)
58  {
60  if (i == mInstances.end())
61  {
62  return nullptr;
63  }
64  return i->second.get();
65  }
66 
68  void LogManager::SetAllLogLevels(const LogLevel& newLevel)
69  {
70  std::for_each(mInstances.begin(), mInstances.end(), [this, &newLevel](trUtil::HashMap<std::string, osg::ref_ptr<Log> >::value_type& value)
71  {
72  Log* log = value.second.get();
73  log->SetLogLevel(newLevel);
74  });
75  }
76 
78  void LogManager::SetAllOutputStreamBits(unsigned int option)
79  {
80  std::for_each(mInstances.begin(), mInstances.end(), [this, option](trUtil::HashMap<std::string, osg::ref_ptr<Log> >::value_type& value)
81  {
82  Log* log = value.second.get();
83  log->SetOutputStreamBit(option);
84  });
85  }
86 
89  {
90  return mLogTimeProviderAsRef.valid() && mLogTimeProvider != nullptr;
91  }
92 
95  {
96  mLogWriterFile->ResetOpenFail();
97  mLogWriterFile->OpenFile();
98  }
99 
102  {
103  mLogWriterFile->LogHorizRule();
104  }
105 
108  {
109  mLogWriterFile->LogMessage(logData);
110  }
111 
114  {
115  mLogWriterConsole->LogMessage(logData);
116  }
117 
120  {
121  mLogTimeProvider = ltp;
122  if (ltp != nullptr)
123  {
125  }
126  else
127  {
128  mLogTimeProviderAsRef = nullptr;
129  }
130  }
131 
134  {
136  }
137 
140  {
141  return mLogTimeProvider->GetDateTime();
142  }
143 
145  OpenThreads::Mutex& LogManager::GetMutex()
146  {
147  return mMutex;
148  }
149 }
Log class which the engine uses for all of its logging needs.
Definition: Log.h:197
virtual unsigned GetFrameNumber()=0
Gets frame number.
OpenThreads::Mutex & GetMutex()
Returns the reference to an internal OpenThreads Mutex.
Definition: LogManager.cpp:145
void LogMessageToConsole(const LogWriter::LogData &logData)
Write out a message to the screen console.
Definition: LogManager.cpp:113
void LogHorizRule()
Writes out a horizontal line to the Log File.
Definition: LogManager.cpp:101
Interface class get the time for the logger.
void LogMessageToFile(const LogWriter::LogData &logData)
Write out a message to the Log file.
Definition: LogManager.cpp:107
Log * GetInstance(const std::string &name)
Retrieves a stored Log instance.
Definition: LogManager.cpp:57
void SetLogLevel(LogLevel logLevel)
Sets the lowest level of logging that will be logged.
Definition: Log.cpp:292
trUtil::HashMap< std::string, osg::ref_ptr< Log > > mInstances
Definition: LogManager.h:200
bool AddInstance(const std::string &name, Log *log)
Stores a new Log instance in the manager.
Definition: LogManager.cpp:51
A specialized LogWriter class that outputs Log messages to a file.
Definition: LogWriterFile.h:48
void SetLogTimeProvider(LogTimeProvider *ltp)
This sets a Log time source.
Definition: LogManager.cpp:119
virtual const trUtil::DateTime & GetDateTime()=0
Gets date time.
bool IsLogTimeProviderValid() const
Check if the Log Time Provider is currently valid in this manager.
Definition: LogManager.cpp:88
LogManager()
Default constructor.
Definition: LogManager.cpp:35
OpenThreads::Mutex mMutex
Definition: LogManager.h:206
LogTimeProvider * mLogTimeProvider
Definition: LogManager.h:205
Specialized LogWriter used to display Log messages in the console window.
osg::ref_ptr< LogWriter > mLogWriterConsole
Definition: LogManager.h:202
osg::ref_ptr< LogWriterFile > mLogWriterFile
writes to console
Definition: LogManager.h:203
void SetOutputStreamBit(unsigned int option)
Tell the Log where to send output messages.
Definition: Log.cpp:259
virtual osg::Referenced * AsReferenced()=0
Converts this object to a referenced.
void ReOpenFile()
Reopens the log file.
Definition: LogManager.cpp:94
osg::observer_ptr< osg::Referenced > mLogTimeProviderAsRef
writes to file
Definition: LogManager.h:204
void SetAllLogLevels(const LogLevel &newLevel)
Set the Log Level for all active Loggers.
Definition: LogManager.cpp:68
unsigned int GetFrameNumber()
Returns the stored frame number.
Definition: LogManager.cpp:133
const trUtil::DateTime & GetDateTime()
Returns the reference to the stored DateTime object.
Definition: LogManager.cpp:139
void SetAllOutputStreamBits(unsigned int option)
Set the OutputStreamOptions bits for all stored Loggers The bits correspond to: NO_OUTPUT = 0x0000000...
Definition: LogManager.cpp:78