Skip to content

Commit f7e80e9

Browse files
author
Saurabh Garg
committed
Revised README.md.
Added User Guide.
1 parent c35a034 commit f7e80e9

File tree

5 files changed

+103
-30
lines changed

5 files changed

+103
-30
lines changed

Doc/SLogLibArchitecture.jpg

-606 KB
Binary file not shown.

Doc/SLogLibArchitecture.png

33 KB
Loading

README.md

Lines changed: 100 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,138 @@ SLogLib is easy to use, fully customizable and extensible, cross-platform loggin
88
## Getting Started
99
Following is the minimum code required to start using SLogLib:
1010

11-
~~~c++
11+
```c++
1212
#include "SLogLib/SLogLib"
1313
void main()
1414
{
1515
// Add these lines at the beginning of your program.
1616
// The devices and formatters are automatically deleted by SLogLib.
1717
using namespace SLogLib;
1818
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+
2121
// The following line writes the message to both console and file.
2222
int a = 10;
2323
double b = 15.3;
2424
const char* c = "Success";
2525
SLOGLIB_LOG_MSG_INFO("a = " << a << " b = " << b);
2626
SLOGLIB_LOG_MSG_INFO(c);
2727
}
28-
~~~
28+
```
2929

3030
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:
3131
> a = 10 b = 15.3Success
3232
3333
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.
3434

3535
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+
```
4548

4649
### Few important points
4750
* SLogLib automatically manage devices and formatters, so don't delete them once you pass them to SLogLib.
4851
* Formatters can be assigned to devices only at the time of construction and cannot be changed after that.
4952
* 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.
5153
* 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+
#define SLOGLIB_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+
#define SLOGLIB_LOG_MESSAGE(level, msg)
66+
```
67+
Write a message at a specified level to all enabled logging devices.
68+
69+
```c++
70+
#define SLOGLIB_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` and and can be written to logging devices.
82+
83+
### Functions
84+
All of the following functions are included in the SLogLib namespace.
85+
86+
```c++
87+
void SLogLib::addLoggingDevice(AbstractLoggingDevice* device);
88+
```
89+
Add a specified logging device to the list of logging devices.
90+
91+
```c++
92+
void SLogLib::removeLoggingDevice(AbstractLoggingDevice* device);
93+
void SLogLib::removeLoggingDevice(const std::string& name);
94+
```
95+
Remove a logging device from the list of logging devices.
96+
97+
```c++
98+
AbstractLoggingDevice* SLogLib::queryLoggingDevice(const std::string& name);
99+
```
100+
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().
101+
102+
```c++
103+
void SLogLib::writeMessage(const std::string& fileName,
104+
const std::string& funcName,
105+
unsigned int lineNo,
106+
unsigned int level,
107+
const std::string& msg);
108+
```
109+
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+
void SLogLib::disableLogging();
113+
void SLogLib::enableLogging();
114+
bool SLogLib::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.
54120

55121
## 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:
57125

58126
* `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).
62130

63131
SLogLib has been tested on Windows with Visual Studio 2010 and 2013, on Linux with g++, and on OSX with XCode 6 and g++.
64132

65133
## 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:
67135

68-
![SLogLib Architecture](/doc/SLogLib-Architecture.jpg)
136+
![SLogLib Architecture](/Doc/SLogLibArchitecture.png)
69137

70138
### Message is the core data
71139
The `Message` structure is the core data which is formatted to a string and written to devices. It has the following fields:
72140

73141
* **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.
75143
* **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:
76144
* `MESSAGE_LEVEL_INFO`
77145
* `MESSAGE_LEVEL_WARNING`
@@ -82,10 +150,15 @@ The `Message` structure is the core data which is formatted to a string and writ
82150
* **mProcessID**: The ID of the process which logged the message.
83151
* **mThreadID**: The ID of the thread which logged the message.
84152

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-
87153
### 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.
89162

90163
### Logging devices write strings to devices
91164
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.
@@ -97,4 +170,4 @@ Saurabh Garg (saurabhgarg@mysoc.net)
97170
Bugs should be reported on GitHub at https://github.com/saurabhg017/SLogLib/issues
98171

99172
## License
100-
SLogLib is release under the MIT License.
173+
SLogLib is release under the MIT License.

SLogLib/SLogLib.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636

3737
#if !defined(SLOGLIB_DISABLE_LOGGING)
3838
// The macro to write the message to all logging devices.
39-
// traceLevel is the message level.
39+
// level is the message level.
4040
// msg is the message to write to logging devices.
41-
#define SLOGLIB_LOG_MESSAGE(traceLevel, msg) \
41+
#define SLOGLIB_LOG_MESSAGE(level, msg) \
4242
{ \
4343
unsigned int _lineNo = __LINE__; \
4444
std::ostringstream __stream__unique__; \
@@ -50,7 +50,7 @@
5050
__stream__unique__.str()); \
5151
}
5252

53-
// Convenience macros to write the message at various trace levels.
53+
// Convenience macros to write the message at various levels.
5454
#define SLOGLIB_LOG_MSG_INFO(msg) SLOGLIB_LOG_MESSAGE(SLogLib::MESSAGE_LEVEL_INFO , msg);
5555
#define SLOGLIB_LOG_MSG_WARN(msg) SLOGLIB_LOG_MESSAGE(SLogLib::MESSAGE_LEVEL_WARNING, msg);
5656
#define SLOGLIB_LOG_MSG_ERROR(msg) SLOGLIB_LOG_MESSAGE(SLogLib::MESSAGE_LEVEL_ERROR , msg);

User Guide.pdf

141 KB
Binary file not shown.

0 commit comments

Comments
 (0)