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