Dragonfly  4.4
A text-based game engine
LogManager.h
1 ///
2 /// The log manager
3 ///
4 
5 #ifndef __LOG_MANAGER_H__
6 #define __LOG_MANAGER_H__
7 
8 // System includes.
9 #include <stdio.h>
10 #include <map>
11 
12 // Engine includes.
13 #include "Manager.h"
14 #include "utility.h"
15 
16 // Two-letter acronym for easier access to manager.
17 #define LM df::LogManager::getInstance()
18 
19 namespace df {
20 
21 /// Default Dragonfly cofiguration file.
22 /// Override with DRAGONFLY_LOG environment variable.
23 const std::string LOGFILE_DEFAULT = "dragonfly.log";
24 
25 class LogManager : public Manager {
26 
27  private:
28  LogManager(); ///< Private since a singleton.
29  LogManager(LogManager const&); ///< Don't allow copy.
30  void operator=(LogManager const&);///< Don't allow assignment.
31  bool m_do_flush; ///< True if flush to disk after write.
32  int m_log_level; ///< Logging level.
33  bool m_log_time_string; ///< True if prepend time.
34  bool m_log_step_count; ///< True if prepend step count.
35  FILE *m_p_f; ///< Pointer to main logfile.
36  std::map <std::string, FILE *> m_file_list; ///< List of custom logfiles.
37 
38  public:
39  /// If logfile is open, close it.
40  ~LogManager();
41 
42  /// Get the one and only instance of the LogManager.
43  static LogManager &getInstance();
44 
45  /// Start up LogManager (open main logfile, usually "dragonfly.log").
46  /// Return 0 if ok, else -1.
47  int startUp();
48 
49  /// Shut down LogManager (close all logfiles).
50  void shutDown();
51 
52  /// Write to logfile.
53  /// Supports printf() formatting of strings.
54  /// Return number of bytes written (excluding prepends), -1 if error.
55  int writeLog(const char *fmt, ...) const;
56 
57  /// Write to logfile.
58  /// Only write if indicated log level >= LogManager log level.
59  /// Supports printf() formatting of strings.
60  /// Return number of bytes written (excluding prepends), -1 if error.
61  int writeLog(int log_level, const char *fmt, ...) const;
62 
63  /// Write to custom logfile, identified by filename.
64  /// Supports printf() formatting of strings.
65  /// Return number of bytes written (excluding prepends), -1 if error.
66  int writeMyLog(std::string filename, const char *fmt, ...);
67 
68  /// Write to custom logfile, identified by filename.
69  /// Only write if indicated log level >= LogManager log level.
70  /// Supports printf() formatting of strings.
71  /// Return number of bytes written (excluding prepends), -1 if error.
72  int writeMyLog(std::string filename, int log_level, const char *fmt, ...);
73 
74  /// Set logging level.
75  void setLogLevel(int new_log_level);
76 
77  /// Get logging level.
78  int getLogLevel() const;
79 
80  /// Set flush of logfile after each write.
81  void setFlush(bool do_flush=true);
82 
83  /// Set prepend time string to log messages.
84  void setLogTimeString(bool log_time_string=true);
85 
86  /// Set prepend step count to log messages.
87  void setLogStepCount(bool log_step_count=true);
88 };
89 
90 } // end of namespace df
91 #endif // __LOG_MANAGER_H__
void operator=(LogManager const &)
Don&#39;t allow assignment.
~LogManager()
If logfile is open, close it.
int writeLog(const char *fmt,...) const
Write to logfile.
bool m_do_flush
True if flush to disk after write.
Definition: LogManager.h:31
void setLogTimeString(bool log_time_string=true)
Set prepend time string to log messages.
int getLogLevel() const
Get logging level.
int writeMyLog(std::string filename, const char *fmt,...)
Write to custom logfile, identified by filename.
static LogManager & getInstance()
Get the one and only instance of the LogManager.
bool m_log_step_count
True if prepend step count.
Definition: LogManager.h:34
FILE * m_p_f
Pointer to main logfile.
Definition: LogManager.h:35
void shutDown()
Shut down LogManager (close all logfiles).
A 2-d bounding box.
Definition: Box.h:10
int m_log_level
Logging level.
Definition: LogManager.h:32
bool m_log_time_string
True if prepend time.
Definition: LogManager.h:33
const std::string LOGFILE_DEFAULT
Default Dragonfly cofiguration file.
Definition: LogManager.h:23
void setLogLevel(int new_log_level)
Set logging level.
Definition: Manager.h:20
int startUp()
Start up LogManager (open main logfile, usually "dragonfly.log").
LogManager()
Private since a singleton.
Definition: LogManager.h:25
std::map< std::string, FILE * > m_file_list
List of custom logfiles.
Definition: LogManager.h:36
void setLogStepCount(bool log_step_count=true)
Set prepend step count to log messages.
void setFlush(bool do_flush=true)
Set flush of logfile after each write.