#!/bin/bash # file: gsl__log_manager # Folder: /var/lib/gsl/scripts # By echolib (XMPP: im@echolib.re) # License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 #====================================================================== # Get arguments from COMMAND (log) # $2: arguments list #====================================================================== log__OPTIONS() { while test "$2" do case "$2" in clean|-C) shift if [[ "$2" == "all" ]];then rm -f "$gsl_dir_logs/"*".gsl.log" echo "! Clean all saved logs" exit fi [[ -z `grep '[^[:space:]]' "$gsl_file_logs" 2>/dev/null` ]] \ && echo "# No logs to show. File is empty." \ && exit gsl_date_logs=`date +%F-%H-%M-%S` mv "$gsl_file_logs" "$gsl_dir_logs/$gsl_date_logs.gsl.log" rm -f "$gsl_file_logs" printf '%s %s %s\n' \ "# Logs saved to" \ "$gsl_dir_logs/$gsl_date_logs.gsl.log" \ "and cleaned" exit ;; *) [[ -z `grep '[^[:space:]]' "$gsl_file_logs" 2>/dev/null` ]] \ && echo "# No logs to show. File is empty." \ && exit case "$2" in -s) gsl_logs_last_session=true ;; -e) gsl_logs_err=true ;; -i) gsl_logs_inf=true ;; -w) gsl_logs_war=true ;; *) gsl_logs_search="$2" ;; esac ;; esac shift done gsl__logs_show exit } #====================================================================== # Print in log starting process # $1; process #====================================================================== log__process_begin() { gsl__logs_print -i -B -P \ "$1" \ "$PWD" } #====================================================================== # Print in log ending process # $1; oricess #====================================================================== log__process_end() { gsl__logs_print -i -E -P \ "$1" \ "$PWD" } #---------------------------------------------------------------------- # Print in log file #---------------------------------------------------------------------- gsl__logs_print() { #1: Level #2: Type #3: Process #4: Infos #5: File case "$1" in -i) log_f1="$gsl_log_i";CL=${NC} ;; -w) log_f1="$gsl_log_w";CL=${CY};gsl_checker_war=true ;; -e) log_f1="$gsl_log_e";CL=${CR};gsl_checker_err=true ;; *) log_f1="$1";; esac case "$2" in -h) log_f2="Header" ;; -c) log_f2="Content" ;; -g) log_f2="GSL" ;; -B) log_f2="Starting" ;; -E) log_f2="Stopping" ;; -A) log_f2="Article" ;; -srv) log_f2="Server" ;; -st) log_f2="Stats" ;; -R) log_f2="Removed" ;; -D) log_f2="Database" ;; -sdb) log_f2="Sidebar" ;; *) log_f2="$2" ;; esac case "$3" in -c) log_f3="Created" ;; -f) log_f3="file" ;; -e) log_f3="Edit" ;; -h) log_f3="Hash" ;; -C) log_f3="Check" ;; -M) log_f3="Make" ;; -P) log_f3="Process" ;; -wip) log_f3="wip" ;; -www) log_f3="www" ;; -src) log_f3="Source" ;; -sk) log_f3="Skip" ;; -inv) log_f3="Invalid" ;; *) log_f3="$3" ;; esac printf '%b%s %s %-7s\t%-7s\t%-38s\t%s\t%s\t%s%b\n' \ "${CL}" \ "`date +%F' '%T`" \ "$log_f1" \ "$log_f2" \ "$log_f3" \ "$4" \ "$gsl_post" \ "$domain_name" \ "$5" \ "${NC}" \ >> "$gsl_file_logs" } #----------------------------------------------------------------------- # Show logs from file with filters #----------------------------------------------------------------------- gsl__logs_show() { # Choices are important, but let's give priority [[ "$gsl_logs_inf" ]] \ && gsl_logs_filter="$gsl_log_i" [[ "$gsl_logs_war" ]] \ && gsl_logs_filter="$gsl_log_w" [[ "$gsl_logs_err" ]] \ && gsl_logs_filter="$gsl_log_e" # Last session show or not if [[ "$gsl_logs_last_session" ]];then gsl_log_start_line=` grep -n "Starting" "$gsl_file_logs" \ | tail -1 \ | awk -F: '{print $1}'` awk -v sl="$gsl_log_start_line" \ 'NR >= sl' \ "$gsl_file_logs" \ | grep "$gsl_logs_filter" \ | grep -Ei "$gsl_logs_search" \ | column -t -s$'\t' -o' ' else while read -r "gsl_log_content" do # Let's read clear sessions [[ "$gsl_log_content" =~ "Starting" ]] \ && echo echo "$gsl_log_content" done < <(cat "$gsl_file_logs" \ | grep "$gsl_logs_filter" \ | grep -Ei "$gsl_logs_search" \ | column -t -s$'\t' -o' ') fi }