CFEL - ASG Software Suite  2.5.0
CASS
log.h
Go to the documentation of this file.
1 // Copyright (C) 2012 Lutz Foucar
2 
3 /**
4  * @file log.h contains a logger for cass
5  *
6  * @author Lutz Foucar
7  */
8 
9 #ifndef __LOG_HPP__
10 #define __LOG_HPP__
11 
12 #include <tr1/memory>
13 #include <string>
14 #include <fstream>
15 #include <map>
16 
17 #include <QtCore/QMutex>
18 
19 namespace cass
20 {
21 /** a logger for logging debug and info messages
22  *
23  * class is a singleton so one can call it from everywhere
24  *
25  * @cassttng Log/{MaxLoggingLevel} \n
26  * The maximum Level of output requested.
27  * @cassttng Log/{Directory} \n
28  * The directory where the log file will be written to. Default is
29  * the directory that cass was started in.
30  * @cassttng Log/{Filename} \n
31  * The name of the log file. Default is "casslog_%yyyyMMdd%".
32  *
33  * @author Lutz Foucar
34  */
35 class Log
36 {
37 public:
38  /** the logging levels available */
41 
42  /** retrive a reference to the singleton instance
43  *
44  * @return reference to the log singleton instance
45  */
46  static Log& ref();
47 
48  /** add a string to the log
49  *
50  * @param level the level of importance that the log message has
51  * @param line string containing the line that should be added to the log
52  */
53  static void add(Level level, const std::string& line);
54 
55  /** destructor
56  *
57  * closes the log file
58  */
59  ~Log();
60 
61  /** load the logging settings from the .ini file
62  *
63  * if the instance has not yet been created create the logging instance.
64  */
65  static void loadSettings();
66 
67  /** return the name of the log file
68  *
69  * if the instance has not yet been created create the logging instance.
70  */
71  static std::string filename();
72 
73 private:
74  /** constructor
75  *
76  * open the file that contains the logging output. One can set the directory
77  * via the .ini file. The name is always composed out of "casslog_yyyyMMdd.log"
78  * where yyyy encodes the year, MM the month and dd the day. The log file is
79  * opened such that new entries will always be appended to the file.
80  *
81  * @note private so one can only create the instance from the static
82  * function
83  */
84  Log();
85 
86  /** add a string to the log
87  *
88  * will put the date, time, message level and then the string to the logging
89  *
90  * @param level the level of importance that the log message has
91  * @param line string containing the line that should be added to the log
92  */
93  void addline(Level level, const std::string& line);
94 
95  /** load the logging settings from the .ini file
96  *
97  * Sets the logging message level that will be put into the log file and the
98  * position and name of the file.
99  *
100  * The log file will be created from the current date and the user defined
101  * directory. It will only be opened if the file has not been opened yet. If
102  * the name of the file (including the absolute path) has changed, close it
103  * (if it was open) and open it with the new filename.
104  */
105  void load();
106 
107 private:
108  /** the instance */
109  static std::tr1::shared_ptr<Log> _instance;
110 
111  /** the stream that we write the log messages to */
112  std::ofstream _log;
113 
114  /** a buffer for concatinating the message */
115  std::string _buffer;
116 
117  /** the filename of the log file */
118  std::string _filename;
119 
120  /** mutex to lock the singleton */
121  static QMutex _lock;
122 
123  /** the used logging level*/
125 
126  /** map the level to a string */
127  static const char* _level2string[];
128 };
129 }//end namespace cass
130 
131 #endif
void load()
load the logging settings from the .ini file
Definition: log.cpp:62
static Log & ref()
retrive a reference to the singleton instance
static void loadSettings()
load the logging settings from the .ini file
Definition: log.cpp:41
Log()
constructor
Definition: log.cpp:57
std::string _filename
the filename of the log file
Definition: log.h:118
Level
the logging levels available
Definition: log.h:39
static std::tr1::shared_ptr< Log > _instance
the instance
Definition: log.h:109
static Level _loggingLevel
the used logging level
Definition: log.h:124
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
static const char * _level2string[]
map the level to a string
Definition: log.h:127
static QMutex _lock
mutex to lock the singleton
Definition: log.h:121
a logger for logging debug and info messages
Definition: log.h:35
void addline(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:95
~Log()
destructor
Definition: log.cpp:90
static std::string filename()
return the name of the log file
Definition: log.cpp:49
std::string _buffer
a buffer for concatinating the message
Definition: log.h:115
std::ofstream _log
the stream that we write the log messages to
Definition: log.h:112