You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -8,70 +8,138 @@ SLogLib is easy to use, fully customizable and extensible, cross-platform loggin
8
8
## Getting Started
9
9
Following is the minimum code required to start using SLogLib:
10
10
11
-
~~~c++
11
+
```c++
12
12
#include"SLogLib/SLogLib"
13
13
voidmain()
14
14
{
15
15
// Add these lines at the beginning of your program.
16
16
// The devices and formatters are automatically deleted by SLogLib.
17
17
using namespace SLogLib;
18
18
addLoggingDevice(new ConsoleLogger(new NullFormatter)); // LINE 1
19
-
addLoggingDevice(new FileLogger("foo.log", new DetailedFormatter)); // LINE 2
20
-
19
+
addLoggingDevice(new FileLogger("foo.log", new DetailedFormatter)); // LINE 2
20
+
21
21
// The following line writes the message to both console and file.
22
22
int a = 10;
23
23
double b = 15.3;
24
24
const char* c = "Success";
25
25
SLOGLIB_LOG_MSG_INFO("a = " << a << " b = " << b);
26
26
SLOGLIB_LOG_MSG_INFO(c);
27
27
}
28
-
~~~
28
+
```
29
29
30
30
The first line adds a console logging device to SLogLib. Logging devices use formatters to format the messages before writing them to the underlying device. Here, a `NullFormatter` is used which does nothing. Output on the console looks like this:
31
31
> a = 10 b = 15.3Success
32
32
33
33
Note that both messages are written to the same line. That's because the `NullFormatter` doesn't add a new line after every message. You can either add new line to each message yourself or pass `AppendNewLine` argument to `NullFormatter's` constructor to automatically add new line after every message.
34
34
35
35
SLogLib can write a message to multiple logging devices. The second line adds a file logging device and uses a detailed formatter to write the message. Here is the first message written to the file:
36
-
> Msg Level : 1
37
-
> Time : 2015-2-12 17:57:11:151
38
-
> Process ID : 4188
39
-
> Thread ID : 7760
40
-
> FileName : LoggingDemo.cpp
41
-
> FuncName : wmain
42
-
> Line No. : 15
43
-
> CallStack : LoggingDemo.cpp : wmain [15]
44
-
> Message : a = 10 b = 15.3
36
+
37
+
```
38
+
Msg Level : 1
39
+
Time : 2015-2-12 17:57:11:151
40
+
Process ID : 4188
41
+
Thread ID : 7760
42
+
FileName : LoggingDemo.cpp
43
+
FuncName : wmain
44
+
Line No. : 15
45
+
CallStack : LoggingDemo.cpp : wmain [15]
46
+
Message : a = 10 b = 15.3
47
+
```
45
48
46
49
### Few important points
47
50
* SLogLib automatically manage devices and formatters, so don't delete them once you pass them to SLogLib.
48
51
* Formatters can be assigned to devices only at the time of construction and cannot be changed after that.
49
52
* Formatters cannot be shared among devices. Each device must have a unique formatter.
50
-
* Logging can be disabled for all devices using `disableLogging()` and be enabled again using `enableLogging()`. Use `isLoggingEnabled()` to check the logging status.
51
53
* It is possible to temporarily disable a logging device using `Disable()`. To enable again use `Enable()`.
52
-
* An unused device can be deleted by calling removeLoggingDevice().
53
-
* Logging can be disabled at the compile time by adding `SLOGLIB_DISABLE_LOGGING` macro to the compiler options.
54
+
55
+
## SLogLib API
56
+
57
+
### Macros
58
+
59
+
```c++
60
+
#defineSLOGLIB_DISABLE_LOGGING
61
+
```
62
+
Disables all logging code at the compile time. This can be used to disable logging completely from the production builds. If is best to define this using the compiler arguments. If you wish to disable logging at the runtime use disableLogging() and enableLogging().
63
+
64
+
```c++
65
+
#defineSLOGLIB_LOG_MESSAGE(level, msg)
66
+
```
67
+
Write a message at a specified level to all enabled logging devices.
68
+
69
+
```c++
70
+
#defineSLOGLIB_LOG_MSG_INFO(m) SLOGLIB_LOG_MESSAGE(MESSAGE_LEVEL_INFO , m)
71
+
#define SLOGLIB_LOG_MSG_WARN(m) SLOGLIB_LOG_MESSAGE(MESSAGE_LEVEL_WARNING, m)
72
+
#define SLOGLIB_LOG_MSG_ERROR(m) SLOGLIB_LOG_MESSAGE(MESSAGE_LEVEL_ERROR , m)
73
+
#define SLOGLIB_LOG_MSG_DEBUG(m) SLOGLIB_LOG_MESSAGE(MESSAGE_LEVEL_DEBUG , m)
74
+
#define SLOGLIB_LOG_MSG_DETAIL(m) SLOGLIB_LOG_MESSAGE(MESSAGE_LEVEL_DEBUG , m)
75
+
```
76
+
The above convenience macros write the message at a predefined level.
77
+
78
+
```c++
79
+
#define SLOGLIB_ADD_TO_CALLSTACK
80
+
```
81
+
Add the function from which this macro is called to the current call stack. It can be used to build a call stack for only the important functions at the time of debugging. The call stack is included in `Message` andand can be written to logging devices.
82
+
83
+
### Functions
84
+
All of the following functions are included in the SLogLib namespace.
Get a pointer to the logging device. Note that the returned pointer must not be deleted or changed. If you wish to delete an existing logging device use removeLoggingDevice().
Write a message to all active logging devices. The first three parameters must the file name, function name, and line number from which the message was logged from. The last two parameters are the level and the message to be written. You don't need to call this function directly; `SLOGLIB_LOG_MESSAGE` calls this function internally after automatically adding the file name, function name, and line number.
110
+
111
+
```c++
112
+
voidSLogLib::disableLogging();
113
+
voidSLogLib::enableLogging();
114
+
boolSLogLib::isLoggingEnabled();
115
+
```
116
+
Disable and enable logging at runtime. While logging is disabled all messages are ignored and they will not be written to logging devices once logging is enabled again.
117
+
118
+
### Notes
119
+
You should also read comments in AbstractLoggingDevice.h and AbstractFormatter.h to learn their API. They are well commented and should be easy to understand.
54
120
55
121
## Building SLogLib
56
-
SLogLib uses cmake (http://www.cmake.org) to generate the files needed by the build tools such as GNU Make, Visual Studio, XCode, etc. If you are new to cmake, you should read Running CMake tutorial (http://www.cmake.org/runningcmake/). You can configure the following variables:
122
+
SLogLib uses cmake (http://www.cmake.org) to generate the files needed by the build tools such as GNU Make, Visual Studio, XCode, etc. If you are new to cmake, you should read Running CMake tutorial (http://www.cmake.org/runningcmake/).
123
+
124
+
You can configure following variables in cmake:
57
125
58
126
*`SLOGLIB_DEBUG_POSTFIX` (string): The suffix to add to the library name for debug builds (default is d).
59
-
*`SLOGLIB_BUILD_EXAMPLES` (bool): Build Examples. Defaults to true.
60
-
*`SLOGLIB_BUILD_QT_EXAMPLES` (bool): Build Qt examples. Defaults to false.
61
-
*`SLOGLIB_USE_QT_VERSION` (list): Select the Qt version: Qt4 or Qt5. Default is Qt5.
127
+
*`SLOGLIB_BUILD_EXAMPLES` (bool): Build examples (Default is true).
128
+
*`SLOGLIB_BUILD_QT_EXAMPLES` (bool): Build Qt examples (Defaults is false).
129
+
*`SLOGLIB_USE_QT_VERSION` (list): Select the Qt version: Qt4 or Qt5 (Default is Qt5).
62
130
63
131
SLogLib has been tested on Windows with Visual Studio 2010 and 2013, on Linux with g++, and on OSX with XCode 6 and g++.
64
132
65
133
## SLogLib Internals
66
-
SLogLib has three main components: a `Message` structure, formatters which convert Messages to std::strings, and logging devices which write std::strings to the underlying devices. Below is the overall architecture of SLogLib:
134
+
If you wish to extend SLogLib by creating your own logging devices or formatters you should read this section to understand how SLogLib works. SLogLib has three main components: a `Message` structure, formatters which convert Messages to std::strings, and logging devices which write std::strings to the underlying devices. Below is the overall architecture of SLogLib:
The `Message` structure is the core data which is formatted to a string and written to devices. It has the following fields:
72
140
73
141
***mUserMessage**: The message logged by the user.
74
-
***mDateTime**: The local date and the time at which the message was logged.
142
+
***mDateTime**: The local date and the time at which the message was logged.
75
143
***mLevel**: It is used to categorize messages into various levels. A formatter can use message levels for filtering by formatting only certain types of messages and ignore the rest. For example, you may want to write only the error messages to console but all types of messages to a file. Following message-levels are defined as:
76
144
*`MESSAGE_LEVEL_INFO`
77
145
*`MESSAGE_LEVEL_WARNING`
@@ -82,10 +150,15 @@ The `Message` structure is the core data which is formatted to a string and writ
82
150
***mProcessID**: The ID of the process which logged the message.
83
151
***mThreadID**: The ID of the thread which logged the message.
84
152
85
-
The example in the previous section used `SLOGLIB_LOG_MSG_INFO` which sets the message level to eLevel_Info. There is a separate macro for each message level: `SLOGLIB_LOG_MSG_WARN`, `SLOGLIB_LOG_MSG_ERROR`, `SLOGLIB_LOG_MSG_DEBUG`, and `SLOGLIB_LOG_MSG_DETAIL`. There is also a more general macro `SLOGLIB_LOG_MESSAGE` which accepts two parameters: the message level, and the user message.
86
-
87
153
### Formatters format messages to strings
88
-
All formatters are derived from `AbstractFormatter` class. If you wish to write a new formatter you should inherit from `AbstractFormatter` and override `FormatMessage()` function. There are four predefined formatters: `NullFormatter`, `InfoFormatter`, `ErrorFormatter`, and `DetailedFormatter`. `NullFormatter` simply outputs the user message. `InfoFormatter`, `ErrorFormatter`, and `DetailedFormatter` formats only messages which are at or below their messages levels.
154
+
All formatters are derived from `AbstractFormatter` class. If you wish to write a new formatter you should inherit from `AbstractFormatter` and override `FormatMessage()` function. There are four predefined formatters:
155
+
156
+
*`NullFormatter`
157
+
*`InfoFormatter`
158
+
*`ErrorFormatter`
159
+
*`DetailedFormatter`
160
+
161
+
`NullFormatter` simply outputs the user message. `InfoFormatter`, `ErrorFormatter`, and `DetailedFormatter` formats only messages which are at or below their messages levels.
89
162
90
163
### Logging devices write strings to devices
91
164
All logging devices inherit from `AbstractLoggingDevice`. SLogLib comes with a `ConsoleLogger` (std::cout) and `FileLogger` (std::ofstream). To create a new logging device simply inherit from the `AbstractLoggingDevice` and override `_WriteMessage()`. For more details see AbstractLoggingDevice.h.
0 commit comments