@@ -14,6 +14,10 @@ DEFAULT_BAND="bg"
1414CONNECTION_NAME=" nm-hotspot"
1515CONFIG_DIR=" $HOME /.config/nm-hotspot"
1616CONFIG_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
1923RED=' \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+
4289check_dependencies () {
4390 if ! command -v nmcli & > /dev/null; then
4491 print_error " NetworkManager (nmcli) is not installed"
@@ -67,6 +114,10 @@ save_config() {
67114SSID="$SSID "
68115PASSWORD="$PASSWORD "
69116BAND="$BAND "
117+
118+ # Logging settings
119+ ENABLE_LOGGING=${ENABLE_LOGGING:- false}
120+ LOG_PRIVACY=${LOG_PRIVACY:- false}
70121EOF
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
94146Options (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+
100156Examples:
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
107173EOF
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
194265start_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
210285stop_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
284417main () {
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