Skip to content

Commit 42da4da

Browse files
authored
Merge pull request #26 from AdityaDwiNugroho/feature/logging-system
Add: logging system for debugging and audit trail
2 parents 1363a35 + 0051ed1 commit 42da4da

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ nm-hotspot list
123123

124124
# Show current configuration
125125
nm-hotspot config
126+
127+
# View logs
128+
nm-hotspot log
129+
130+
# View last 50 log entries
131+
nm-hotspot log --tail 50
132+
133+
# Clear log file
134+
nm-hotspot log --clear
126135
```
127136

128137
### Command Options
@@ -143,6 +152,10 @@ nm-hotspot config
143152

144153
**config** - Display saved configuration
145154

155+
**log** - Show log entries
156+
- `--tail N` - Show last N log entries (default: 20)
157+
- `--clear` - Clear the log file
158+
146159
## Examples
147160

148161
### Example 1: Quick Hotspot
@@ -195,8 +208,53 @@ Example configuration file:
195208
SSID="MyHotspot"
196209
PASSWORD="12345678"
197210
BAND="bg"
211+
212+
# Logging settings
213+
ENABLE_LOGGING=true
214+
LOG_PRIVACY=false
215+
```
216+
217+
### Logging
218+
219+
The logging system helps you troubleshoot issues and maintain an audit trail of hotspot operations.
220+
221+
**Enable logging:**
222+
223+
Edit `~/.config/nm-hotspot/hotspot.conf` and add:
224+
```bash
225+
ENABLE_LOGGING=true
198226
```
199227

228+
**Log file location:**
229+
`~/.config/nm-hotspot/nm-hotspot.log`
230+
231+
**Log entries include:**
232+
- Timestamp
233+
- Action performed (create, start, stop)
234+
- SSID used (unless privacy mode is enabled)
235+
- Success/failure status
236+
- Error messages if any
237+
238+
**Example log output:**
239+
```
240+
[2024-10-06 14:30:15] INFO: Creating hotspot - Band: bg - SSID: MyHotspot
241+
[2024-10-06 14:30:16] SUCCESS: Hotspot created successfully - SSID: MyHotspot
242+
[2024-10-06 14:35:22] INFO: Stopping hotspot
243+
[2024-10-06 14:35:23] SUCCESS: Hotspot stopped
244+
[2024-10-06 14:40:10] ERROR: Failed to create hotspot - No wireless device found
245+
```
246+
247+
**Privacy mode:**
248+
249+
To avoid logging sensitive information like SSIDs, enable privacy mode:
250+
```bash
251+
LOG_PRIVACY=true
252+
```
253+
254+
**Log rotation:**
255+
256+
The log file is automatically rotated when it exceeds 1MB. The old log is saved as `nm-hotspot.log.old`.
257+
200258
## Troubleshooting
201259

202260
### Hotspot won't start

src/nm-hotspot

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ DEFAULT_BAND="bg"
1414
CONNECTION_NAME="nm-hotspot"
1515
CONFIG_DIR="$HOME/.config/nm-hotspot"
1616
CONFIG_FILE="$CONFIG_DIR/hotspot.conf"
17+
LOG_FILE="$CONFIG_DIR/nm-hotspot.log"
18+
LOG_MAX_SIZE=1048576 # 1MB in bytes
19+
ENABLE_LOGGING=false
20+
LOG_PRIVACY=false
1721

1822
# Colors for output
1923
RED='\033[0;31m'
@@ -39,6 +43,49 @@ print_warning() {
3943
echo -e "${YELLOW}$1${NC}"
4044
}
4145

46+
# Logging functions
47+
log_message() {
48+
# Only log if logging is enabled
49+
if [ "$ENABLE_LOGGING" != "true" ]; then
50+
return 0
51+
fi
52+
53+
local level="$1"
54+
local message="$2"
55+
local ssid="${3:-}"
56+
57+
# Create log directory if it doesn't exist
58+
mkdir -p "$CONFIG_DIR"
59+
60+
# Check log file size and rotate if needed
61+
if [ -f "$LOG_FILE" ] && [ "$(stat -f%z "$LOG_FILE" 2>/dev/null || stat -c%s "$LOG_FILE" 2>/dev/null)" -ge "$LOG_MAX_SIZE" ]; then
62+
rotate_log
63+
fi
64+
65+
# Format timestamp
66+
local timestamp
67+
timestamp=$(date "+%Y-%m-%d %H:%M:%S")
68+
69+
# Build log entry
70+
local log_entry="[$timestamp] $level: $message"
71+
72+
# Add SSID if provided and privacy mode is disabled
73+
if [ -n "$ssid" ] && [ "$LOG_PRIVACY" != "true" ]; then
74+
log_entry="$log_entry - SSID: $ssid"
75+
fi
76+
77+
# Append to log file
78+
echo "$log_entry" >> "$LOG_FILE"
79+
}
80+
81+
rotate_log() {
82+
if [ -f "$LOG_FILE" ]; then
83+
local backup_file="${LOG_FILE}.old"
84+
mv "$LOG_FILE" "$backup_file"
85+
print_info "Log file rotated to ${backup_file}"
86+
fi
87+
}
88+
4289
check_dependencies() {
4390
if ! command -v nmcli &> /dev/null; then
4491
print_error "NetworkManager (nmcli) is not installed"
@@ -67,6 +114,10 @@ save_config() {
67114
SSID="$SSID"
68115
PASSWORD="$PASSWORD"
69116
BAND="$BAND"
117+
118+
# Logging settings
119+
ENABLE_LOGGING=${ENABLE_LOGGING:-false}
120+
LOG_PRIVACY=${LOG_PRIVACY:-false}
70121
EOF
71122
chmod 600 "$CONFIG_FILE"
72123
print_success "Configuration saved to $CONFIG_FILE"
@@ -89,6 +140,7 @@ Commands:
89140
status Show hotspot status
90141
list List available wireless devices
91142
config Show current configuration
143+
log Show log entries
92144
help Show this help message
93145
94146
Options (for create command):
@@ -97,12 +149,26 @@ Options (for create command):
97149
-b, --band BAND Set band: bg (2.4GHz) or a (5GHz) (default: $DEFAULT_BAND)
98150
--save Save settings as default
99151
152+
Options (for log command):
153+
--tail N Show last N log entries (default: 20)
154+
--clear Clear the log file
155+
100156
Examples:
101157
nm-hotspot create
102158
nm-hotspot create -s MyNetwork -p MyPassword123
103159
nm-hotspot create -s MyNetwork -p MyPassword123 --save
104160
nm-hotspot stop
105161
nm-hotspot status
162+
nm-hotspot log
163+
nm-hotspot log --tail 50
164+
nm-hotspot log --clear
165+
166+
Logging:
167+
Enable logging by adding to $CONFIG_FILE:
168+
ENABLE_LOGGING=true
169+
LOG_PRIVACY=true # Optional: Don't log SSIDs
170+
171+
Log file location: $LOG_FILE
106172
107173
EOF
108174
}
@@ -155,10 +221,12 @@ create_hotspot() {
155221
local DEVICE
156222
DEVICE=$(get_wifi_device)
157223
if [ -z "$DEVICE" ]; then
224+
log_message "ERROR" "Failed to create hotspot - No wireless device found"
158225
print_error "No wireless device found"
159226
exit 1
160227
fi
161228

229+
log_message "INFO" "Creating hotspot - Band: $BAND" "$SSID"
162230
print_info "Creating hotspot on device: $DEVICE"
163231
print_info "SSID: $SSID"
164232
print_info "Band: $BAND ($([ "$BAND" = "bg" ] && echo "2.4GHz" || echo "5GHz"))"
@@ -177,10 +245,13 @@ create_hotspot() {
177245
ssid "$SSID" \
178246
band "$BAND" \
179247
password "$PASSWORD" || {
248+
log_message "ERROR" "Failed to create hotspot - nmcli command failed" "$SSID"
180249
print_error "Failed to create hotspot"
181250
exit 1
182251
}
183252

253+
log_message "SUCCESS" "Hotspot created successfully" "$SSID"
254+
184255
# Save configuration if requested
185256
if [ "$SAVE_CONFIG" = true ]; then
186257
save_config
@@ -193,32 +264,40 @@ create_hotspot() {
193264

194265
start_hotspot() {
195266
if ! nmcli connection show "$CONNECTION_NAME" &>/dev/null; then
267+
log_message "ERROR" "Failed to start hotspot - Connection not found"
196268
print_error "Hotspot connection not found. Create it first with 'nm-hotspot create'"
197269
exit 1
198270
fi
199271

272+
log_message "INFO" "Starting hotspot"
200273
print_info "Starting hotspot..."
201274
nmcli connection up "$CONNECTION_NAME" || {
275+
log_message "ERROR" "Failed to start hotspot - nmcli command failed"
202276
print_error "Failed to start hotspot"
203277
exit 1
204278
}
205279

280+
log_message "SUCCESS" "Hotspot started successfully"
206281
print_success "Hotspot started successfully"
207282
show_status
208283
}
209284

210285
stop_hotspot() {
211286
if ! nmcli connection show --active "$CONNECTION_NAME" &>/dev/null; then
287+
log_message "INFO" "Stop requested but hotspot is not active"
212288
print_warning "Hotspot is not active"
213289
exit 0
214290
fi
215291

292+
log_message "INFO" "Stopping hotspot"
216293
print_info "Stopping hotspot..."
217294
nmcli connection down "$CONNECTION_NAME" || {
295+
log_message "ERROR" "Failed to stop hotspot - nmcli command failed"
218296
print_error "Failed to stop hotspot"
219297
exit 1
220298
}
221299

300+
log_message "SUCCESS" "Hotspot stopped"
222301
print_success "Hotspot stopped successfully"
223302
}
224303

@@ -280,6 +359,60 @@ show_config() {
280359
echo ""
281360
}
282361

362+
show_log() {
363+
local tail_count=20
364+
local clear_log=false
365+
366+
# Parse arguments
367+
while [[ $# -gt 0 ]]; do
368+
case $1 in
369+
--tail)
370+
tail_count="$2"
371+
shift 2
372+
;;
373+
--clear)
374+
clear_log=true
375+
shift
376+
;;
377+
*)
378+
print_error "Unknown option: $1"
379+
echo "Usage: nm-hotspot log [--tail N] [--clear]"
380+
exit 1
381+
;;
382+
esac
383+
done
384+
385+
# Clear log if requested
386+
if [ "$clear_log" = true ]; then
387+
if [ -f "$LOG_FILE" ]; then
388+
true > "$LOG_FILE"
389+
print_success "Log file cleared"
390+
log_message "INFO" "Log file cleared by user"
391+
else
392+
print_warning "No log file found"
393+
fi
394+
return 0
395+
fi
396+
397+
# Show log entries
398+
if [ ! -f "$LOG_FILE" ]; then
399+
print_warning "No log file found"
400+
echo "Logging can be enabled in $CONFIG_FILE"
401+
echo "Set ENABLE_LOGGING=true"
402+
return 0
403+
fi
404+
405+
if [ ! -s "$LOG_FILE" ]; then
406+
print_info "Log file is empty"
407+
return 0
408+
fi
409+
410+
print_info "=== Last $tail_count Log Entries ==="
411+
echo ""
412+
tail -n "$tail_count" "$LOG_FILE"
413+
echo ""
414+
}
415+
283416
# Main script
284417
main() {
285418
check_dependencies
@@ -312,6 +445,9 @@ main() {
312445
config)
313446
show_config
314447
;;
448+
log)
449+
show_log "$@"
450+
;;
315451
help|--help|-h)
316452
show_usage
317453
;;

0 commit comments

Comments
 (0)