4
0

core_messages.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/bin/bash
  2. # LinuxGSM core_messages.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: s-eam
  5. # Website: https://linuxgsm.com
  6. # Description: Defines on-screen messages such as [ OK ] and how script logs look.
  7. # nl: new line: message is following by a new line
  8. # eol: end of line: message is placed at the end of the current line
  9. fn_ansi_loader(){
  10. if [ "${ansi}" != "off" ]; then
  11. # echo colors
  12. default="\e[0m"
  13. red="\e[31m"
  14. lightred="\e[91m"
  15. green="\e[32m"
  16. lightgreen="\e[92m"
  17. yellow="\e[33m"
  18. lightyellow="\e[93m"
  19. blue="\e[34m"
  20. lightblue="\e[94m"
  21. magenta="\e[35m"
  22. lightmagenta="\e[95m"
  23. cyan="\e[36m"
  24. lightcyan="\e[96m"
  25. fi
  26. # carriage return & erase to end of line
  27. creeol="\r\033[K"
  28. }
  29. # Log display
  30. ########################
  31. ## Feb 28 14:56:58 ut99-server: Monitor:
  32. fn_script_log(){
  33. if [ -d "${lgsmlogdir}" ]; then
  34. if [ -n "${commandname}" ]; then
  35. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${commandname}: ${1}" >> "${lgsmlog}"
  36. else
  37. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${1}" >> "${lgsmlog}"
  38. fi
  39. fi
  40. }
  41. ## Feb 28 14:56:58 ut99-server: Monitor: PASS:
  42. fn_script_log_pass(){
  43. if [ -d "${lgsmlogdir}" ]; then
  44. if [ -n "${commandname}" ]; then
  45. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${commandname}: PASS: ${1}" >> "${lgsmlog}"
  46. else
  47. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: PASS: ${1}" >> "${lgsmlog}"
  48. fi
  49. fi
  50. exitcode=0
  51. }
  52. ## Feb 28 14:56:58 ut99-server: Monitor: FATAL:
  53. fn_script_log_fatal(){
  54. if [ -d "${lgsmlogdir}" ]; then
  55. if [ -n "${commandname}" ]; then
  56. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${commandname}: FATAL: ${1}" >> "${lgsmlog}"
  57. else
  58. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: FATAL: ${1}" >> "${lgsmlog}"
  59. fi
  60. fi
  61. exitcode=1
  62. }
  63. ## Feb 28 14:56:58 ut99-server: Monitor: ERROR:
  64. fn_script_log_error(){
  65. if [ -d "${lgsmlogdir}" ]; then
  66. if [ -n "${commandname}" ]; then
  67. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${commandname}: ERROR: ${1}" >> "${lgsmlog}"
  68. else
  69. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ERROR: ${1}" >> "${lgsmlog}"
  70. fi
  71. fi
  72. exitcode=2
  73. }
  74. ## Feb 28 14:56:58 ut99-server: Monitor: WARN:
  75. fn_script_log_warn(){
  76. if [ -d "${lgsmlogdir}" ]; then
  77. if [ -n "${commandname}" ]; then
  78. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${commandname}: WARN: ${1}" >> "${lgsmlog}"
  79. else
  80. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: WARN: ${1}" >> "${lgsmlog}"
  81. fi
  82. fi
  83. exitcode=3
  84. }
  85. ## Feb 28 14:56:58 ut99-server: Monitor: INFO:
  86. fn_script_log_info(){
  87. if [ -d "${lgsmlogdir}" ]; then
  88. if [ -n "${commandname}" ]; then
  89. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: ${commandname}: INFO: ${1}" >> "${lgsmlog}"
  90. else
  91. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${servicename}: INFO: ${1}" >> "${lgsmlog}"
  92. fi
  93. fi
  94. }
  95. # On-Screen - Automated functions
  96. ##################################
  97. # [ .... ]
  98. fn_print_dots(){
  99. if [ -n "${commandaction}" ]; then
  100. echo -en "${creeol}[ .... ] ${commandaction} ${servicename}: $@"
  101. else
  102. echo -en "${creeol}[ .... ] $@"
  103. fi
  104. }
  105. fn_print_dots_nl(){
  106. if [ -n "${commandaction}" ]; then
  107. echo -e "${creeol}[ .... ] ${commandaction} ${servicename}: $@"
  108. else
  109. echo -e "${creeol}[ .... ] $@"
  110. fi
  111. sleep 0.5
  112. echo -en "\n"
  113. }
  114. # [ OK ]
  115. fn_print_ok(){
  116. if [ -n "${commandaction}" ]; then
  117. echo -en "${creeol}[${green} OK ${default}] ${commandaction} ${servicename}: $@"
  118. else
  119. echo -en "${creeol}[${green} OK ${default}] $@"
  120. fi
  121. }
  122. fn_print_ok_nl(){
  123. if [ -n "${commandaction}" ]; then
  124. echo -en "${creeol}[${green} OK ${default}] ${commandaction} ${servicename}: $@"
  125. else
  126. echo -en "${creeol}[${green} OK ${default}] $@"
  127. fi
  128. sleep 0.5
  129. echo -en "\n"
  130. }
  131. # [ FAIL ]
  132. fn_print_fail(){
  133. if [ -n "${commandaction}" ]; then
  134. echo -en "${creeol}[${red} FAIL ${default}] ${commandaction} ${servicename}: $@"
  135. else
  136. echo -en "${creeol}[${red} FAIL ${default}] $@"
  137. fi
  138. }
  139. fn_print_fail_nl(){
  140. if [ -n "${commandaction}" ]; then
  141. echo -en "${creeol}[${red} FAIL ${default}] ${commandaction} ${servicename}: $@"
  142. else
  143. echo -en "${creeol}[${red} FAIL ${default}] $@"
  144. fi
  145. sleep 0.5
  146. echo -en "\n"
  147. }
  148. # [ ERROR ]
  149. fn_print_error(){
  150. if [ -n "${commandaction}" ]; then
  151. echo -en "${creeol}[${red}ERROR ${default}] ${commandaction} ${servicename}: $@"
  152. else
  153. echo -en "${creeol}[${red}ERROR ${default}] $@"
  154. fi
  155. }
  156. fn_print_error_nl(){
  157. if [ -n "${commandaction}" ]; then
  158. echo -en "${creeol}[${red}ERROR ${default}] ${commandaction} ${servicename}: $@"
  159. else
  160. echo -en "${creeol}[${red}ERROR ${default}] $@"
  161. fi
  162. sleep 0.5
  163. echo -en "\n"
  164. }
  165. # [ WARN ]
  166. fn_print_warn(){
  167. if [ -n "${commandaction}" ]; then
  168. echo -en "${creeol}[${yellow} WARN ${default}] ${commandaction} ${servicename}: $@"
  169. else
  170. echo -en "${creeol}[${yellow} WARN ${default}] $@"
  171. fi
  172. }
  173. fn_print_warn_nl(){
  174. if [ -n "${commandaction}" ]; then
  175. echo -en "${creeol}[${yellow} WARN ${default}] ${commandaction} ${servicename}: $@"
  176. else
  177. echo -en "${creeol}[${yellow} WARN ${default}] $@"
  178. fi
  179. sleep 0.5
  180. echo -en "\n"
  181. }
  182. # [ INFO ]
  183. fn_print_info(){
  184. if [ -n "${commandaction}" ]; then
  185. echo -en "${creeol}[${cyan} INFO ${default}] ${commandaction} ${servicename}: $@"
  186. else
  187. echo -en "${creeol}[${cyan} INFO ${default}] $@"
  188. fi
  189. }
  190. fn_print_info_nl(){
  191. if [ -n "${commandaction}" ]; then
  192. echo -en "${creeol}[${cyan} INFO ${default}] ${commandaction} ${servicename}: $@"
  193. else
  194. echo -en "${creeol}[${cyan} INFO ${default}] $@"
  195. fi
  196. sleep 0.5
  197. echo -en "\n"
  198. }
  199. # On-Screen - Interactive messages
  200. ##################################
  201. # No More Room in Hell Debug
  202. # =================================
  203. fn_print_header(){
  204. echo -e ""
  205. echo -e "${gamename} ${commandaction}"
  206. echo -e "=================================${default}"
  207. echo -e ""
  208. }
  209. # Complete!
  210. fn_print_complete(){
  211. echo -en "${green}Complete!${default} $@"
  212. }
  213. fn_print_complete_nl(){
  214. echo -e "${green}Complete!${default} $@"
  215. }
  216. # Failure!
  217. fn_print_failure(){
  218. echo -en "${red}Failure!${default} $@"
  219. }
  220. fn_print_failure_nl(){
  221. echo -e "${red}Failure!${default} $@"
  222. }
  223. # Error!
  224. fn_print_error2(){
  225. echo -en "${red}Error!${default} $@"
  226. }
  227. fn_print_error2_nl(){
  228. echo -e "${red}Error!${default} $@"
  229. }
  230. # Warning!
  231. fn_print_warning(){
  232. echo -en "${yellow}Warning!${default} $@"
  233. }
  234. fn_print_warning_nl(){
  235. echo -e "${yellow}Warning!${default} $@"
  236. }
  237. # Information!
  238. fn_print_information(){
  239. echo -en "${cyan}Information!${default} $@"
  240. }
  241. fn_print_information_nl(){
  242. echo -e "${cyan}Information!${default} $@"
  243. }
  244. # Y/N Prompt
  245. fn_prompt_yn(){
  246. local prompt="$1"
  247. local initial="$2"
  248. if [ "${initial}" == "Y" ]; then
  249. prompt+=" [Y/n] "
  250. elif [ "${initial}" == "N" ]; then
  251. prompt+=" [y/N] "
  252. else
  253. prompt+=" [y/n] "
  254. fi
  255. while true; do
  256. read -e -i "${initial}" -p "${prompt}" -r yn
  257. case "${yn}" in
  258. [Yy]|[Yy][Ee][Ss]) return 0 ;;
  259. [Nn]|[Nn][Oo]) return 1 ;;
  260. *) echo "Please answer yes or no." ;;
  261. esac
  262. done
  263. }
  264. # On-Screen End of Line
  265. ##################################
  266. # OK
  267. fn_print_ok_eol(){
  268. echo -en "${green}OK${default}"
  269. }
  270. fn_print_ok_eol_nl(){
  271. echo -e "${green}OK${default}"
  272. }
  273. # FAIL
  274. fn_print_fail_eol(){
  275. echo -en "${red}FAIL${default}"
  276. }
  277. fn_print_fail_eol_nl(){
  278. echo -e "${red}FAIL${default}"
  279. }
  280. # WARN
  281. fn_print_warn_eol(){
  282. echo -en "${red}WARN${default}"
  283. }
  284. fn_print_warn_eol_nl(){
  285. echo -e "${red}WARN${default}"
  286. }
  287. # INFO
  288. fn_print_info_eol(){
  289. echo -en "${red}INFO${default}"
  290. }
  291. fn_print_info_eol_nl(){
  292. echo -e "${red}INFO${default}"
  293. }
  294. # QUERYING
  295. fn_print_querying_eol(){
  296. echo -en "${cyan}QUERYING${default}"
  297. }
  298. fn_print_querying_eol_nl(){
  299. echo -e "${cyan}QUERYING${default}"
  300. }
  301. # CHECKING
  302. fn_print_checking_eol(){
  303. echo -en "${cyan}CHECKING${default}"
  304. }
  305. fn_print_checking_eol_nl(){
  306. echo -e "${cyan}CHECKING${default}"
  307. }
  308. # CANCELED
  309. fn_print_canceled_eol(){
  310. echo -en "${yellow}CANCELED${default}"
  311. }
  312. fn_print_canceled_eol_nl(){
  313. echo -e "${yellow}CANCELED${default}"
  314. }
  315. # REMOVED
  316. fn_print_removed_eol(){
  317. echo -en "${red}REMOVED${default}"
  318. }
  319. fn_print_removed_eol_nl(){
  320. echo -e "${red}REMOVED${default}"
  321. }
  322. # UPDATE
  323. fn_print_update_eol(){
  324. echo -en "${cyan}UPDATE${default}"
  325. }
  326. fn_print_update_eol_nl(){
  327. echo -e "${cyan}UPDATE${default}"
  328. }