showmsg.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "showmsg.h"
  6. #include "malloc.h"
  7. char tmp_output[1024] = {"\0"};
  8. #undef ShowMessage
  9. int _vShowMessage(enum msg_type flag, const char *string, va_list ap)
  10. { // by MC Cameri
  11. /*
  12. _ShowMessage MUST be used instead of printf as of 10/24/2004.
  13. Return: 0 = Successful, 1 = Failed.
  14. */
  15. // int ret = 0;
  16. char prefix[40];
  17. char *output;
  18. if (strlen(string) <= 0) {
  19. printf("Empty string passed to _ShowMessage().\n");
  20. return 1;
  21. }
  22. switch (flag) {
  23. case MSG_STATUS: //Bright Green (To inform about good things)
  24. strcpy(prefix,CL_GREEN"[Status]"CL_RESET":");
  25. break;
  26. case MSG_SQL: //Bright Violet (For dumping out anything related with SQL)
  27. strcpy(prefix,CL_MAGENTA"[SQL]"CL_RESET":");
  28. break;
  29. case MSG_INFORMATION: //Bright White (Variable information)
  30. strcpy(prefix,CL_WHITE"[Info]"CL_RESET":");
  31. break;
  32. case MSG_NOTICE: //Bright White (Less than a warning)
  33. strcpy(prefix,CL_WHITE"[Notice]"CL_RESET":");
  34. break;
  35. case MSG_WARNING: //Bright Yellow
  36. strcpy(prefix,CL_YELLOW"[Warning]"CL_RESET":");
  37. break;
  38. case MSG_DEBUG: //Bright Cyan, important stuff!
  39. strcpy(prefix,CL_CYAN"[Debug]"CL_RESET":");
  40. break;
  41. case MSG_ERROR: //Bright Red (Regular errors)
  42. strcpy(prefix,CL_RED"[Error]"CL_RESET":");
  43. break;
  44. case MSG_FATALERROR: //Bright Red (Fatal errors, abort(); if possible)
  45. strcpy(prefix,CL_RED"[Fatal Error]"CL_RESET":");
  46. break;
  47. default:
  48. printf("In function _ShowMessage() -> Invalid flag passed.\n");
  49. return 1;
  50. }
  51. if (!(flag == MSG_DEBUG && !SHOW_DEBUG_MSG)) {
  52. output = (char*)aMalloc(sizeof(char)*(strlen(prefix)+strlen(string)+2)); // prefix+string+two chars(space and \0)
  53. if (output == NULL) {
  54. return 1;
  55. // exit(1); // Kill server? Deadly
  56. }
  57. strcpy(output,prefix);
  58. strcat(output," ");
  59. strcat(output,string);
  60. vprintf(output, ap);
  61. fflush(stdout);
  62. aFree(output);
  63. }
  64. va_end(ap);
  65. /*
  66. if ((core_config.debug_output_level > -1) && (flag >= core_config.debug_output_level)) {
  67. FILE *fp;
  68. fp=fopen(OUTPUT_MESSAGES_LOG,"a");
  69. if (fp == NULL) {
  70. printf(CL_RED"[Error]"CL_RESET": Could not open '"CL_WHITE"%s"CL_RESET"', file not found.\n",OUTPUT_MESSAGES_LOG);
  71. fflush(stdout);
  72. return;
  73. }
  74. StripColor(output);
  75. strcpy(output,"\r");
  76. fwrite(output,strlen(output),1,fp);
  77. fclose(fp);
  78. }
  79. */
  80. return 0;
  81. }
  82. int _ShowMessage(enum msg_type flag, const char *string, ...)
  83. {
  84. va_list ap;
  85. va_start(ap, string);
  86. return _vShowMessage(flag, string, ap);
  87. }
  88. int ShowStatus(const char *string, ...) {
  89. va_list ap;
  90. va_start(ap, string);
  91. return _vShowMessage(MSG_STATUS, string, ap);
  92. }
  93. int ShowSQL(const char *string, ...) {
  94. va_list ap;
  95. va_start(ap, string);
  96. return _vShowMessage(MSG_SQL, string, ap);
  97. }
  98. int ShowInfo(const char *string, ...) {
  99. va_list ap;
  100. va_start(ap, string);
  101. return _vShowMessage(MSG_INFORMATION, string, ap);
  102. }
  103. int ShowNotice(const char *string, ...) {
  104. va_list ap;
  105. va_start(ap, string);
  106. return _vShowMessage(MSG_NOTICE, string, ap);
  107. }
  108. int ShowWarning(const char *string, ...) {
  109. va_list ap;
  110. va_start(ap, string);
  111. return _vShowMessage(MSG_WARNING, string, ap);
  112. }
  113. int ShowDebug(const char *string, ...) {
  114. va_list ap;
  115. va_start(ap, string);
  116. return _vShowMessage(MSG_DEBUG, string, ap);
  117. }
  118. int ShowError(const char *string, ...) {
  119. va_list ap;
  120. va_start(ap, string);
  121. return _vShowMessage(MSG_ERROR, string, ap);
  122. }
  123. int ShowFatalError(const char *string, ...) {
  124. va_list ap;
  125. va_start(ap, string);
  126. return _vShowMessage(MSG_FATALERROR, string, ap);
  127. }