core_messages.sh 8.6 KB

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