core_messages.sh 7.9 KB

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