core_messages.sh 8.4 KB

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