move doc from wiki

master
Chunting Gu 5 years ago
parent 1e3c2b2604
commit b72db3a048

@ -0,0 +1,52 @@
# Build Instructions
## Dependencies
* [Boost](https://www.boost.org/) (asio, system, filesystem, date_time)
* [OpenSSL](https://www.openssl.org/) (HTTPS)
* [Zlib](https://www.zlib.net/) (GZIP compression)
* [Googletest/gtest](https://github.com/google/googletest) (automation and unit tests)
* [CMake](https://cmake.org/)
Googletest is included in `third_party/src`. No need to install.
OpenSSL and Zlib are **optional** since they could be disabled. See the build options below.
## Build Options
The following CMake options determine how you build the projects. They are quite self-explanatory.
```cmake
option(WEBCC_ENABLE_AUTOTEST "Build automation test?" OFF)
option(WEBCC_ENABLE_UNITTEST "Build unit test?" OFF)
option(WEBCC_ENABLE_EXAMPLES "Build examples?" OFF)
set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)")
set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO)")
set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)")
set(WEBCC_ENABLE_GZIP 0 CACHE STRING "Enable gzip compression (need Zlib)? (1:Yes, 0:No)")
```
### `WEBCC_ENABLE_AUTOTEST`
Automation test based on real servers (mostly [httpbin.org](http://httpbin.org/)).
### `WEBCC_ENABLE_LOG` and `WEBCC_LOG_LEVEL`
These two options define how logging behaves.
See [Logging](Logging.md) for more details.
### `WEBCC_ENABLE_SSL`
For HTTPS support (need OpenSSL).
### `WEBCC_ENABLE_GZIP`
For GZIP compression support (need Zlib).
## Platforms
- [Build on Windows](Build-on-Windows.md)
- [Build on Linux](Build-on-Linux.md)
- [Build on Mac](Build-on-Mac.md)

@ -0,0 +1,110 @@
# Build on Linux
_For any Ubuntu based Linux distributions._
## Install Zlib & OpenSSL
```
sudo apt install zlib1g-dev libssl-dev
```
## Install Boost
Download the .tar.bz2 or .tar.gz from [here](https://www.boost.org/users/download/#live). The current version is 1.70.0.
Unpack and go into the directory:
```
tar -xzf boost_1_70_0.tar.bz2
cd boost_1_70_0
```
Run `bootstrap.sh` to generate `b2`:
```
./bootstrap.sh
```
You can change install prefix with `--prefix` (default is `/usr/local`, need `sudo`), but I don't recommend.
Build and install:
```
sudo ./b2 --with-system --with-filesystem --with-date_time variant=debug link=static threading=multi -j4 install
```
Notes:
- Only build the specified libraries. `Asio` itself is header only so doesnt have to be built.
- Only build static libraries (`link=static`)
- The number after `-j` depends on the number of CPU cores you have.
- If you want to build release version libraries, set `variant=release`. The `debug` and `release` libraries have exactly the same name, so you cannot build them both at the same time.
- Dont forget the `sudo` since the install prefix is `/usr/local`.
To clean the build, run `b2` with target "clean":
```
./b2 clean
```
The libraries are installed to `/usr/local/lib`. E.g.,
```
$ ls -l /usr/local/lib/libboost*
-rw-r--r-- 1 adam admin 540288 Apr 21 11:01 /usr/local/lib/libboost_date_time.a
-rw-r--r-- 1 adam admin 1717736 Apr 21 11:01 /usr/local/lib/libboost_filesystem.a
-rw-r--r-- 1 root admin 2976 Apr 21 11:01 /usr/local/lib/libboost_system.a
```
The headers are installed to `/usr/local/include/boost`.
## Build Webcc
Create a build folder under the root (or any other) directory, and `cd` to it:
```
mkdir build
cd build
```
Generate Makefiles with the following command:
```
cmake -G"Unix Makefiles" \
-DCMAKE_INSTALL_PREFIX=~ \
-DWEBCC_ENABLE_LOG=1 \
-DWEBCC_LOG_LEVEL=0 \
-DWEBCC_ENABLE_SSL=1 \
-DWEBCC_ENABLE_GZIP=1 \
-DWEBCC_ENABLE_AUTOTEST=OFF \
-DWEBCC_ENABLE_UNITTEST=OFF \
-DWEBCC_ENABLE_EXAMPLES=OFF \
..
```
_NOTE: You can create a script (e.g., `gen.sh`) with the above command to avoid typing again and again whenever you want to change an option._
Feel free to change the build options according to your need.
CMake variable `CMAKE_INSTALL_PREFIX` defines where to install the output library and header files. The default is `/usr/local`. But this feature needs rework. I should come back to this later.
If everything is OK, you can then build with `make`:
```
make -j4
```
The number after `-j` depends on how many CPU cores you have. You can just ignore `-j` option.
Install the libraries:
```bash
make install
```
If you `WEBCC_ENABLE_AUTOTEST` was `ON`, you can run the automation test:
```
cd autotest
./webcc_autotest
```

@ -0,0 +1,148 @@
# Build on Mac
## Install Xcode
You should install Xcode as the step 0.
## Install CMake
Download the .dmg from [here](https://cmake.org/download/). Just double click and follow the instructions.
## Install Boost
Download the .tar.bz2 or .tar.gz from [here](https://www.boost.org/users/download/#live). The current version is 1.70.0.
Unpack and go into the directory:
```
tar -xzf boost_1_70_0.tar.bz2
cd boost_1_70_0
```
Run `bootstrap.sh` to generate `b2`:
```
./bootstrap.sh
```
You can change install prefix with `--prefix` option (default is `/usr/local`, need `sudo`), but I don't recommend.
Build and install:
```
sudo ./b2 --with-system --with-filesystem --with-date_time variant=debug link=static threading=multi -j4 install
```
Notes:
- Only build the specified libraries. `Asio` itself is header only so doesnt have to be built.
Only build static libraries (`link=static`)
- The number after `-j` depends on the number of CPU cores you have.
- If you want to build release version libraries, set `variant=release`. The `debug` and `release` libraries have exactly the same name, so you cannot build them both at the same time.
- Dont forget the `sudo` since the install prefix is `/usr/local`.
To clean the build, run `b2` with target "clean":
```
./b2 clean
```
The libraries are installed to `/usr/local/lib`. E.g.,
```
$ ls -l /usr/local/lib/libboost*
-rw-r--r-- 1 adam admin 540288 Apr 21 11:01 /usr/local/lib/libboost_date_time.a
-rw-r--r-- 1 adam admin 1717736 Apr 21 11:01 /usr/local/lib/libboost_filesystem.a
-rw-r--r-- 1 root admin 2976 Apr 21 11:01 /usr/local/lib/libboost_system.a
```
The headers are installed to `/usr/local/include/boost`.
**IMPORTANT**
Remove the following directory otherwise CMake couldnt find Boost.
```
sudo rm -r /usr/local/lib/cmake
```
This directory was installed by `b2`. I cant see why its necessary.
## Install OpenSSL
Install with [Homebrew](https://brew.sh/) (the usage of Homebrew itself is out of the scope, please Google and learn it by yourself):
```
brew install openssl
```
Please note that it will be installed to `/usr/local/opt/openssl` instead of `/usr/local`.
## Install Zlib
Zlib should have already been provided by the system (`/usr/lib/libz.dylib`). If not, try to install with Homebrew.
## Build Webcc
Open CMake, set **Where is the source code** to Webcc root directory (e.g., `/Users/adam/github/webcc`), set **Where to build the binaries** to any directory (e.g., `/Users/adam/github/webcc_build`).
Check _**Grouped**_ and _**Advanced**_ two check boxes.
Click _**Configure**_ button. Select "Unix Makefiles" (or "Xcode") as the generator in the popup dialog.
In the center of CMake, you can see a lot of configure options which are grouped and might also be highlighted in RED.
![CMake config](screenshots/mac_cmake_config.png)
Now change the options according to your need. E.g.,
![CMake config](screenshots/mac_cmake_config_2.png)
Click _**Configure**_ button again, a message would say that OpenSSL couldnt be found.
> Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
Dont try to set the `OPENSSL_ROOT_DIR` system variable, it wont work.
As aforementioned, the OpenSSL was installed by Homebrew, and its located in `/usr/local/opt/openssl`. Lets tell CMake where it is.
![CMake config OpenSSL](screenshots/mac_cmake_config_openssl.png)
Click _**Configure**_ button again. If everything is OK, click _**Generate**_ button.
Open Terminal, go to the build directory, run `make`. E.g.,
```
$ cd github/webcc_build
$ make -j4
```
There might be some warnings, just ignore them.
Suppose you have checked `WEBCC_ENABLE_AUTOTEST` during the configuration, you can run it now.
```
$ cd autotest
$ ./webcc_autotest
[==========] Running 7 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 7 tests from ClientTest
[ RUN ] ClientTest.Get_RequestFunc
[ OK ] ClientTest.Get_RequestFunc (1066 ms)
[ RUN ] ClientTest.Get_Shortcut
[ OK ] ClientTest.Get_Shortcut (1844 ms)
[ RUN ] ClientTest.Get_SSL
[ OK ] ClientTest.Get_SSL (2158 ms)
[ RUN ] ClientTest.Compression_Gzip
[ OK ] ClientTest.Compression_Gzip (913 ms)
[ RUN ] ClientTest.Compression_Deflate
[ OK ] ClientTest.Compression_Deflate (921 ms)
[ RUN ] ClientTest.KeepAlive
[ OK ] ClientTest.KeepAlive (5121 ms)
[ RUN ] ClientTest.GetImageJpeg
[ OK ] ClientTest.GetImageJpeg (1244 ms)
[----------] 7 tests from ClientTest (13267 ms total)
[----------] Global test environment tear-down
[==========] 7 tests from 1 test case ran. (13267 ms total)
[ PASSED ] 7 tests.
```

@ -0,0 +1,70 @@
# Build on Windows
VS 2013 and above is required for building `Webcc` on Windows.
## Install Boost
Download the .7z or .zip from [here](https://www.boost.org/users/download/#live). Unpack it.
Open `VS Native Tools Command Prompt` from Windows start menu.
![VS Cmd Prompts Win7](screenshots/vs_cmd_prompts_win7.png)
E.g., I choose `VS2015 x64 Native Tools Command Prompt` to build a 64-bit Boost.
In the prompt, `cd` to the Boost root directory. Run `bootstrap.bat` to generate `b2.exe`:
Run `b2.exe` to start the build:
```
b2 --with-system --with-filesystem --with-date_time variant=debug variant=release link=static threading=multi stage
```
Only `system`, `filesystem` and `date_time` are built. `Asio` is a header-only library.
We don't install Boost to another place (e.g., `C:\Boost`) by specifying `stage` instead of `install` at the end of the command. In order to let CMake find Boost, please add an environment variable named `Boost_ROOT` pointing to the root directory of Boost.
## Install OpenSSL
Download from [here](http://slproweb.com/products/Win32OpenSSL.html).
The following installers are recommended for development:
- Win64 OpenSSL v1.1.0k
- Win32 OpenSSL v1.1.0k
During the installation, you will be asked to copy OpenSSL DLLs (`libcrypto-1_1-x64.dll` and `libssl-1_1-x64.dll`) to "The Windows system directory" or "The OpenSSL libraries (/bin) directory". If you choose the later, remember to add the path (e.g., `C:\OpenSSL-Win64\bin`) to the `PATH` environment variable.
![OpenSSL Installation](screenshots/win_openssl_install.png)
OpenSSL can also be statically linked (see `C:\OpenSSL-Win64\lib\VC\static`), but it's not recommended. Because the static libraries might not match the version of your VS.
The only drawback of dynamic link is that you must distribute the OpenSSL DLLs together with your program.
## Install Zlib
Zlib has been included in `third_party\src`. Maybe it's not a good idea.
In order to integrate `webcc` into your project, you have to integrate this zlib, too. This makes it complicated. I will come back to this later.
## Generate VS Solution
Open CMake, set **Where is the source code** to Webcc root directory (e.g., `D:/github/webcc`), set **Where to build the binaries** to any directory (e.g., `D:/github/webcc/build_vs2015_64`).
Check _**Grouped**_ and _**Advanced**_ two check boxes.
Click _**Configure**_ button, select the generator and platform (win32 or x64) from the popup dialog.
![CMake generator](screenshots/win_cmake_generator.png)
In the center of CMake, you can see a lot of configure options which are grouped. Change them according to your need. E.g., set `WEBCC_ENABLE_SSL` to `1` to enable OpenSSL.
![CMake config](screenshots/win_cmake_config.png)
Click _**Configure**_ button again. OpenSSL should be found.
![CMake config OpenSSL](screenshots/win_cmake_config_openssl.png)
Click _**Configure**_ button again. If everything is OK, click _**Generate**_ button to generate the VS solution.
Click _**Open Project**_ button to open VS.

@ -0,0 +1,29 @@
# Integrate Into Your Project
## Integrate with CMake
If your project also uses CMake as the build tool, it's highly recommended to integrate *Webcc* with source code. Just copy the `webcc` sub-folder to the side of your project, add the configure options to your top `CMakeLists.txt`, then include it by the following statement:
```cmake
add_subdirectory(webcc)
```
One of the difficulties is the dependency of third-party libraries, like `zlib` and `openssl`. It's not a big deal for Linux since they can be installed easily using the system package manager (`apt-get` for Ubuntu).
For Windows, you should copy `third_pary\src\zlib`, which is the source code of zlib with the official `CMakeLists.txt`, to some place of your project. Download OpenSSL and install it. See [Build Instructions](Build-Instructions.md) for more details.
## Integrate with Visual Studio
If you don't want to use CMake, you can copy the `webcc` sub-folder directly into your project to integrate.
But the following changes will be necessary:
- Create a file `config.h` by copying `config.h.example`, change the macros.
- Now create a static library project named _webcc_ in _Visual Studio_ with all the C++ files.
- Add the include path of _Boost_ in _Visual Studio_ for this project.
- Add webcc project's parent directory to the _Additional Including Directories_ of your solution. This is for including webcc headers with a prefix, e.g., `#include "webcc/http_client_session.h"`.
- Add the following definitions to the project properties page: **C/C++ -> Preprocessor -> Preprocessor Definitions**.
```
_WIN32_WINNT=0x0601
BOOST_ASIO_NO_DEPRECATED
```
The first one is needed by Asio. `0x0601` means Win7, should be OK even you develop with Win10.

@ -0,0 +1,359 @@
# Logging
## Log Control
Webcc's logging module was designed for "best performance".
By defining macro `WEBCC_ENABLE_LOG` as `1` or `0`, you can enable or disable the logging globally at compile-time. And by setting `WEBCC_LOG_LEVEL` to `0` to `4`, you can control above which level the logs will be logged.
There are five log levels defined in webcc:
```cpp
// Log levels.
// VERB is similar to DEBUG commonly used by other projects.
// USER is for the users who want to log their own logs but don't want any
// VERB or INFO.
#define WEBCC_VERB 0
#define WEBCC_INFO 1
#define WEBCC_USER 2
#define WEBCC_WARN 3
#define WEBCC_ERRO 4
```
One important difference from other logging libraries is that the level control is at compile-time. For example, when you define `WEBCC_LOG_LEVEL` to `2` (`USER`), the logs of `VERB` and `INFO` levels will be totally eliminated during preprocessing of the compiler.
### Configure With CMake
If you are using _CMake_, you can define the macros in your _CMakeLists.txt_. Take webcc's own CMake files as example, they are defined in the _CMakeLists.txt_ of the project root directory. And in order to configure dynamically, they are determined by corresponding CMake variables:
```cmake
set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (0:OFF, 1:ON)")
set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO)")
```
These two CMake variables determine the related macros defined in `config.h`. The file `config.h` will be generated automatically during CMake configure step from `webcc/config.in`.
```cmake
# webcc/CMakeLists.txt
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/config.h"
)
```
### Configure With Visual Studio
If you manage projects with Visual Studio directly, you just create `config.h` by copying `config.h.example` then change the values accordingly.
### Set Log Level Properly
During development, set log level to `VERB` to log as many as possible so that you can find more issues. But before release, please switch log level to at least `USER` for better performance. Verbose logs consume a lot of memory, IO and CPU. Never enable `VERB` in a release!
## Log Format
For simplicity, the format of the logs is not configurable. It consists of the following fields:
```plain
Timestamp, Level, Thread ID, File Name, Line Number, Message
```
Each field is well aligned and formatted to achieve a beautiful display. See the following examples.
### Example: Server Side
The following logs are from [book_server](../examples/book_server) example.
```csv
2019-07-11 15:25:35.620, INFO, main, server.cc, 60, Server is going to run...
2019-07-11 15:25:35.635, INFO, 4160, request_handler.cc, 89, Worker is running.
2019-07-11 15:25:35.635, INFO, 2116, request_handler.cc, 89, Worker is running.
2019-07-11 15:25:53.148, INFO, main, server.cc, 95, Accepted a connection.
2019-07-11 15:25:53.148, VERB, main, connection_pool.cc, 8, Starting connection...
2019-07-11 15:25:53.149, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.150, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.150, VERB, main, connection.cc, 94, HTTP request:
> GET /books HTTP/1.1
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
2019-07-11 15:25:53.151, INFO, 2116, request_handler.cc, 114, Request URL path: /books
2019-07-11 15:25:53.153, VERB, 2116, connection.cc, 102, HTTP response:
> HTTP/1.1 200 OK
> Content-Type: application/json; charset=utf-8
> Content-Length: 2
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> []
2019-07-11 15:25:53.155, INFO, main, connection.cc, 146, Response has been sent back.
2019-07-11 15:25:53.155, INFO, main, connection.cc, 149, The client asked for a keep-alive connection.
2019-07-11 15:25:53.156, INFO, main, connection.cc, 150, Continue to read the next request...
2019-07-11 15:25:53.212, INFO, main, parser.cc, 156, Content length: 52.
2019-07-11 15:25:53.214, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.214, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.215, VERB, main, connection.cc, 94, HTTP request:
> POST /books HTTP/1.1
> Content-Type: application/json
> Content-Length: 52
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
> {
> "price" : 12.300000000000001,
> "title" : "1984"
> }
2019-07-11 15:25:53.222, INFO, 2116, request_handler.cc, 114, Request URL path: /books
2019-07-11 15:25:53.225, VERB, 2116, connection.cc, 102, HTTP response:
> HTTP/1.1 201 Created
> Content-Type: application/json; charset=utf-8
> Content-Length: 15
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> {
> "id" : "1"
> }
2019-07-11 15:25:53.241, INFO, main, connection.cc, 146, Response has been sent back.
2019-07-11 15:25:53.245, INFO, main, connection.cc, 149, The client asked for a keep-alive connection.
2019-07-11 15:25:53.253, INFO, main, connection.cc, 150, Continue to read the next request...
2019-07-11 15:25:53.358, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.360, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.362, VERB, main, connection.cc, 94, HTTP request:
> GET /books HTTP/1.1
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
2019-07-11 15:25:53.372, INFO, 2116, request_handler.cc, 114, Request URL path: /books
2019-07-11 15:25:53.379, VERB, 2116, connection.cc, 102, HTTP response:
> HTTP/1.1 200 OK
> Content-Type: application/json; charset=utf-8
> Content-Length: 74
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> [
> {
> "id" : "1",
> "price" : 12.300000000000001,
> "title" : "1984"
> }
> ]
2019-07-11 15:25:53.399, INFO, main, connection.cc, 146, Response has been sent back.
2019-07-11 15:25:53.400, INFO, main, connection.cc, 149, The client asked for a keep-alive connection.
2019-07-11 15:25:53.407, INFO, main, connection.cc, 150, Continue to read the next request...
...
```
As you can see, the HTTP requests and responses are logged at level `VERB`, and they are indented by 4 spaces and a `"> "` prefix to improve the readability. The thread ID will be replaced by "main" if it's logged from main thread.
### Example: Client Side
The following logs are from [book_client](../examples/book_client) example.
```csv
2019-07-11 15:25:53.052, VERB, main, client.cc, 80, Resize buffer: 0 -> 1024.
2019-07-11 15:25:53.146, VERB, main, client.cc, 117, Connect to server...
2019-07-11 15:25:53.147, VERB, main, client.cc, 128, Socket connected.
2019-07-11 15:25:53.148, VERB, main, client.cc, 132, HTTP request:
> GET /books HTTP/1.1
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
2019-07-11 15:25:53.149, INFO, main, client.cc, 161, Request sent.
2019-07-11 15:25:53.149, VERB, main, client.cc, 165, Read response (timeout: 30s)...
2019-07-11 15:25:53.150, VERB, main, client.cc, 238, Wait timer asynchronously.
2019-07-11 15:25:53.154, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.155, INFO, main, client.cc, 274, Cancel timer...
2019-07-11 15:25:53.156, INFO, main, client.cc, 197, Read data, length: 115.
2019-07-11 15:25:53.157, INFO, main, common.cc, 127, Content-type charset: utf-8.
2019-07-11 15:25:53.158, INFO, main, parser.cc, 156, Content length: 2.
2019-07-11 15:25:53.159, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.161, VERB, main, client.cc, 243, On timer.
2019-07-11 15:25:53.162, VERB, main, client.cc, 247, Timer canceled.
2019-07-11 15:25:53.163, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.164, INFO, main, client.cc, 197, Read data, length: 17.
2019-07-11 15:25:53.178, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.179, INFO, main, client.cc, 213, Keep the socket connection alive.
2019-07-11 15:25:53.180, INFO, main, client.cc, 218, Finished to read and parse HTTP response.
2019-07-11 15:25:53.181, VERB, main, client.cc, 173, HTTP response:
> HTTP/1.1 200 OK
> Content-Type: application/json; charset=utf-8
> Content-Length: 2
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> []
2019-07-11 15:25:53.183, INFO, main, client_pool.cc, 32, Added connection to pool (http, localhost, 8080).
2019-07-11 15:25:53.206, VERB, main, client_session.cc, 191, Reuse an existing connection.
2019-07-11 15:25:53.207, VERB, main, client.cc, 132, HTTP request:
> POST /books HTTP/1.1
> Content-Type: application/json
> Content-Length: 52
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
> {
> "price" : 12.300000000000001,
> "title" : "1984"
> }
2019-07-11 15:25:53.212, INFO, main, client.cc, 161, Request sent.
2019-07-11 15:25:53.219, VERB, main, client.cc, 165, Read response (timeout: 30s)...
2019-07-11 15:25:53.220, VERB, main, client.cc, 238, Wait timer asynchronously.
2019-07-11 15:25:53.240, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.241, INFO, main, client.cc, 274, Cancel timer...
2019-07-11 15:25:53.246, INFO, main, client.cc, 197, Read data, length: 121.
2019-07-11 15:25:53.253, INFO, main, common.cc, 127, Content-type charset: utf-8.
2019-07-11 15:25:53.262, INFO, main, parser.cc, 156, Content length: 15.
2019-07-11 15:25:53.271, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.278, VERB, main, client.cc, 243, On timer.
2019-07-11 15:25:53.280, VERB, main, client.cc, 247, Timer canceled.
2019-07-11 15:25:53.284, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.288, INFO, main, client.cc, 197, Read data, length: 30.
2019-07-11 15:25:53.296, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.298, INFO, main, client.cc, 213, Keep the socket connection alive.
2019-07-11 15:25:53.312, INFO, main, client.cc, 218, Finished to read and parse HTTP response.
2019-07-11 15:25:53.314, VERB, main, client.cc, 173, HTTP response:
> HTTP/1.1 201 Created
> Content-Type: application/json; charset=utf-8
> Content-Length: 15
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> {
> "id" : "1"
> }
2019-07-11 15:25:53.344, VERB, main, client_session.cc, 191, Reuse an existing connection.
2019-07-11 15:25:53.344, VERB, main, client.cc, 132, HTTP request:
> GET /books HTTP/1.1
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
2019-07-11 15:25:53.357, INFO, main, client.cc, 161, Request sent.
2019-07-11 15:25:53.362, VERB, main, client.cc, 165, Read response (timeout: 30s)...
2019-07-11 15:25:53.369, VERB, main, client.cc, 238, Wait timer asynchronously.
2019-07-11 15:25:53.398, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.400, INFO, main, client.cc, 274, Cancel timer...
2019-07-11 15:25:53.407, INFO, main, client.cc, 197, Read data, length: 116.
2019-07-11 15:25:53.412, INFO, main, common.cc, 127, Content-type charset: utf-8.
2019-07-11 15:25:53.415, INFO, main, parser.cc, 156, Content length: 74.
2019-07-11 15:25:53.418, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.421, VERB, main, client.cc, 243, On timer.
2019-07-11 15:25:53.423, VERB, main, client.cc, 247, Timer canceled.
2019-07-11 15:25:53.431, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.433, INFO, main, client.cc, 197, Read data, length: 89.
2019-07-11 15:25:53.440, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.441, INFO, main, client.cc, 213, Keep the socket connection alive.
2019-07-11 15:25:53.445, INFO, main, client.cc, 218, Finished to read and parse HTTP response.
2019-07-11 15:25:53.448, VERB, main, client.cc, 173, HTTP response:
> HTTP/1.1 200 OK
> Content-Type: application/json; charset=utf-8
> Content-Length: 74
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> [
> {
> "id" : "1",
> "price" : 12.300000000000001,
> "title" : "1984"
> }
> ]
2019-07-11 15:25:53.473, VERB, main, client_session.cc, 191, Reuse an existing connection.
2019-07-11 15:25:53.474, VERB, main, client.cc, 132, HTTP request:
> GET /books/1 HTTP/1.1
> User-Agent: Webcc/0.1.0
> Accept-Encoding: gzip, deflate
> Accept: */*
> Connection: Keep-Alive
> Host: localhost:8080
>
2019-07-11 15:25:53.481, INFO, main, client.cc, 161, Request sent.
2019-07-11 15:25:53.486, VERB, main, client.cc, 165, Read response (timeout: 30s)...
2019-07-11 15:25:53.488, VERB, main, client.cc, 238, Wait timer asynchronously.
2019-07-11 15:25:53.498, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.508, INFO, main, client.cc, 274, Cancel timer...
2019-07-11 15:25:53.510, INFO, main, client.cc, 197, Read data, length: 116.
2019-07-11 15:25:53.512, INFO, main, common.cc, 127, Content-type charset: utf-8.
2019-07-11 15:25:53.515, INFO, main, parser.cc, 156, Content length: 65.
2019-07-11 15:25:53.517, INFO, main, parser.cc, 61, HTTP headers will continue in next read.
2019-07-11 15:25:53.522, VERB, main, client.cc, 243, On timer.
2019-07-11 15:25:53.524, VERB, main, client.cc, 247, Timer canceled.
2019-07-11 15:25:53.526, VERB, main, client.cc, 184, Socket async read handler.
2019-07-11 15:25:53.527, INFO, main, client.cc, 197, Read data, length: 80.
2019-07-11 15:25:53.529, INFO, main, parser.cc, 65, HTTP headers just ended.
2019-07-11 15:25:53.540, INFO, main, client.cc, 213, Keep the socket connection alive.
2019-07-11 15:25:53.550, INFO, main, client.cc, 218, Finished to read and parse HTTP response.
2019-07-11 15:25:53.553, VERB, main, client.cc, 173, HTTP response:
> HTTP/1.1 200 OK
> Content-Type: application/json; charset=utf-8
> Content-Length: 65
> Connection: Keep-Alive
> Server: Webcc/0.1.0
>
> {
> "id" : "1",
> "price" : 12.300000000000001,
> "title" : "1984"
> }
...
```
### Colorful Console Output
If the terminal supports colors, some levels of the logs will be highlighted.
![Logger colors](screenshots/logger_colors.png)
## Initialize Logging
In your program, call macro `WEBCC_LOG_INIT()` to initialize the logging. E.g., the following example initializes logging to write to both console and file. The file path is the current directory (indicated by passing empty string to the first parameter), and the file will be overwritten instead of appended.
```cpp
#include "webcc/logger.h"
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE_FILE_OVERWRITE);
```
Constant `LOG_CONSOLE_FILE_OVERWRITE` is actually a combination of several mode flags:
```cpp
const int LOG_CONSOLE_FILE_OVERWRITE = LOG_CONSOLE | LOG_FILE | LOG_OVERWRITE;
```
And the flags are defined as `enum` values:
```cpp
enum LogMode {
LOG_FILE = 1, // Log to file.
LOG_CONSOLE = 2, // Log to console.
LOG_FLUSH = 4, // Flush on each log.
LOG_OVERWRITE = 8, // Overwrite any existing log file.
};
```
Loading…
Cancel
Save