This project implements a dynamically configurable logging system that allows real-time log level changes using signals. The logger supports the following log levels:
- DISABLED
- ERROR
- WARNING
- INFO
- DEBUG
gcc -o app main.c logger.c -lrt
gcc -o set_log_level set_log_level.c -lrtRun the application in the background:
./app &This will print a process ID (PID). You can also find the PID using:
pgrep app./set_log_level <PID> <log_level><log_level>:
disableerrorwarninginfodebug
cat application.log[INFO] Info message.
[WARNING] Warning message.
[ERROR] Error message.Bring the process back to the foreground:
fgThen, terminate it using:
Ctrl+C❯ gcc -o app main.c logger.c -lrt
❯ gcc -o set_log_level set_log_level.c -lrt
❯ ./app & ./set_log_level $! info
[3] 29597
Log level set to info for process 29597
❯ cat application.log
[INFO] Info message.
[WARNING] Warning message.
[ERROR] Error message.
❯ ./app & ./set_log_level $! error
[4] 29665
Log level set to error for process 29665
❯ cat application.log
[INFO] Info message.
[WARNING] Warning message.
[ERROR] Error message.
[INFO] Info message.
[WARNING] Warning message.
[ERROR] Error message.
❯ fg
[4] - 29665 running ./app
^C
- Main Application (main.c)
- Logger Module (logger.c & logger.h)
- Log Level Setter (set_log_level.c)
-
Main Application (main.c)
- Purpose: This is the core part of the application. It starts the logging process and continuously logs messages based on the current log level.
- Key Tasks:
- Initializes the logger using
init_logger(). - Logs messages of different levels (e.g., debug, info, warning, error) continuously in an infinite loop.
- Responds to signals that adjust the log level (by calling
handle_signal()).
- Initializes the logger using
- Interaction:
- It includes
logger.hfor function declarations and interacts with the Logger Module for logging. - Receives signals from the Log Level Setter to adjust the log level dynamically.
- It includes
-
Logger Module (logger.c & logger.h)
- Purpose: The Logger Module is responsible for logging messages to a file based on the current log level. It also handles signal-based log level changes.
- Key Tasks:
init_logger(): Initializes the logger and sets up signal handlers for dynamic log level changes.handle_signal(): Receives signals (e.g.,SIGUSR1,SIGUSR2) and updates the log level accordingly.log_message(): Logs a message with a specific log level to a file (application.log).safe_write(): Ensures that log messages are written safely to the log file, even in the case of interruptions.
- Interaction:
- It provides the logging functionality to the Main Application and interacts with it via the
logger.hheader. - It listens for signals from the Log Level Setter and adjusts its logging behavior in real-time.
- It provides the logging functionality to the Main Application and interacts with it via the
-
Log Level Setter (set_log_level.c)
- Purpose: This component allows an external user to change the log level of the running application by sending signals to the process.
- Key Tasks:
- Accepts the process ID (PID) and the desired log level as command-line arguments.
- Maps the desired log level (e.g.,
debug,info,warning) to a specific signal (e.g.,SIGUSR1,SIGUSR2). - Sends the corresponding signal to the running process using
kill()to change the log level.
- Interaction:
- It communicates with the Main Application and Logger Module by sending signals to change the log level dynamically.
- It only sends signals and does not interact with the actual logging behavior beyond that.