LogByThread
is a multi-threaded logging system implemented in C++ using the Qt framework. The project features asynchronous log handling, custom log message formatting, and thread-safe operations. The logging system consists of two main components: Logger
and LogWorker
.
LogByThread
│
├── build # Build directory
│ └── demo
│
├── demo # Demo application
│ ├── CMakeLists.txt
│ └── main.cpp
│
├── include # Library source files
│ ├── CMakeLists.txt
│ ├── logger.cpp
│ ├── logger.h
│ ├── logworker.cpp
│ └── logworker.h
│
└── CMakeLists.txt # Root CMake configuration file
- Qt (Core, Concurrent)
- CMake (version 3.14 or higher)
-
Clone the Repository:
git clone https://github.com/7osssam/LogByThread.git cd LogByThread
-
Create Build Directory:
mkdir build cd build
-
Run CMake:
cmake ..
-
Build the Project:
cmake --build .
After building the project, an executable named demo
will be created in the build/demo
directory. You can run this executable to see the logging system in action.
./demo/demo
#========================= LogByThread =========================
# Add the include directory for the LogByThread library
add_subdirectory(LogByThread)
# optional: Build the demo application (default: OFF)
option(LOG_BY_THREAD_BUILD_DEMO "Build the demo" OFF)
# Include directories
include_directories(LogByThread/include)
# Link the LogByThread library
target_link_libraries(${PROJECT_NAME} PRIVATE LogByThread)
#==================================================================
-
Include the Library Headers:
#include "logger.h"
-
Install the Message Handler:
qInstallMessageHandler(Logger::myMessageOutput);
-
Start Logging:
Logger::instance()->startLogging();
- To suspend logging, use
Logger::instance()->suspendLogging()
.
- To suspend logging, use
The Logger
class handles the main logging functionality. It provides methods to start and suspend logging, set the log flush size, and customize log message output.
Key Methods:
startLogging()
:- Starts the logging process.
suspendLogging()
:- Suspends the logging process.
setLogFlushSize(quint64 size)
:- Sets the size of the log flush.
myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
:- Custom message handler for Qt logging.
The LogWorker
class handles writing log messages to a file. It runs in a separate thread and ensures thread-safe file operations.
Key Slots:
onWriteLog(const QString &logStr)
:- Slot to write a log string to the log file.
onSetLogFlushSize(quint64 size)
:- Slot to set the log flush size.
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
This library is licensed under the MIT License. Feel free to use it in your projects.