You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
3.9 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-logging.c,v 1.1 2006/07/09 16:22:52 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_STDIO_H
  28. #include <stdio.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_STDARG_H
  34. #include <stdarg.h>
  35. #endif
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #ifdef HAVE_CTYPE_H
  40. #include <ctype.h>
  41. #endif
  42. #ifdef HAVE_PTHREAD_H
  43. #include <pthread.h>
  44. #endif
  45. #include "nanohttp-logging.h"
  46. #ifdef WIN32
  47. #ifndef __MINGW32__
  48. /* not thread safe!*/
  49. char *
  50. VisualC_funcname(const char *file, int line)
  51. {
  52. static char buffer[256];
  53. int i = strlen(file) - 1;
  54. while (i > 0 && file[i] != '\\')
  55. i--;
  56. sprintf(buffer, "%s:%d", (file[i] != '\\') ? file : (file + i + 1), line);
  57. return buffer;
  58. }
  59. #endif
  60. #endif
  61. static log_level_t loglevel = HLOG_DEBUG;
  62. static char logfile[75] = { '\0' };
  63. static int log_background = 0;
  64. log_level_t
  65. hlog_set_level(log_level_t level)
  66. {
  67. log_level_t old = loglevel;
  68. loglevel = level;
  69. return old;
  70. }
  71. log_level_t
  72. hlog_get_level(void)
  73. {
  74. return loglevel;
  75. }
  76. void
  77. hlog_set_file(const char *filename)
  78. {
  79. if (filename)
  80. strncpy(logfile, filename, 75);
  81. else
  82. logfile[0] = '\0';
  83. }
  84. void
  85. hlog_set_background(int state)
  86. {
  87. log_background = state;
  88. }
  89. char *
  90. hlog_get_file()
  91. {
  92. if (logfile[0] == '\0')
  93. return NULL;
  94. return logfile;
  95. }
  96. static void
  97. _log_write(log_level_t level, const char *prefix,
  98. const char *func, const char *format, va_list ap)
  99. {
  100. char buffer[1054];
  101. char buffer2[1054];
  102. FILE *f;
  103. if (level < loglevel)
  104. return;
  105. if (!log_background || hlog_get_file())
  106. {
  107. #ifdef WIN32
  108. sprintf(buffer, "*%s*: [%s] %s\n", prefix, func, format);
  109. #else
  110. sprintf(buffer, "*%s*:(%ld) [%s] %s\n",
  111. prefix, pthread_self(), func, format);
  112. #endif
  113. vsprintf(buffer2, buffer, ap);
  114. if (!log_background)
  115. {
  116. printf(buffer2);
  117. fflush(stdout);
  118. }
  119. if (hlog_get_file())
  120. {
  121. f = fopen(hlog_get_file(), "a");
  122. if (!f)
  123. f = fopen(hlog_get_file(), "w");
  124. if (f)
  125. {
  126. fprintf(f, buffer2);
  127. fflush(f);
  128. fclose(f);
  129. }
  130. }
  131. }
  132. }
  133. void
  134. hlog_verbose(const char *FUNC, const char *format, ...)
  135. {
  136. va_list ap;
  137. va_start(ap, format);
  138. _log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap);
  139. va_end(ap);
  140. }
  141. void
  142. hlog_debug(const char *FUNC, const char *format, ...)
  143. {
  144. va_list ap;
  145. va_start(ap, format);
  146. _log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap);
  147. va_end(ap);
  148. }
  149. void
  150. hlog_info(const char *FUNC, const char *format, ...)
  151. {
  152. va_list ap;
  153. va_start(ap, format);
  154. _log_write(HLOG_INFO, "INFO", FUNC, format, ap);
  155. va_end(ap);
  156. }
  157. void
  158. hlog_warn(const char *FUNC, const char *format, ...)
  159. {
  160. va_list ap;
  161. va_start(ap, format);
  162. _log_write(HLOG_WARN, "WARN", FUNC, format, ap);
  163. va_end(ap);
  164. }
  165. void
  166. hlog_error(const char *FUNC, const char *format, ...)
  167. {
  168. va_list ap;
  169. va_start(ap, format);
  170. _log_write(HLOG_ERROR, "ERROR", FUNC, format, ap);
  171. va_end(ap);
  172. }