A fast, lightweight and simple STOMP compatible messaging server
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.

log.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2011 Bernhard Froehlich <decke@bluelife.at>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Author's name may not be used endorse or promote products derived
  12. * from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  18. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include "log.h"
  32. char *loglevelnames[] = { "ERROR", "WARN ", "INFO ", "DEBUG" };
  33. int loglevel = LOG_INFO;
  34. FILE *logfile = NULL;
  35. int logopen(char *filename)
  36. {
  37. if(logfile != NULL)
  38. return 1;
  39. logfile = fopen(filename, "a");
  40. if(logfile == NULL)
  41. {
  42. printf("Error %s: Could not open logfile %s\n", strerror(errno), filename);
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. int logclose(void)
  48. {
  49. int retval;
  50. if(logfile == NULL)
  51. return 1;
  52. fflush(logfile);
  53. retval = fclose(logfile);
  54. if(retval != 0)
  55. printf("Error %s on closing logfile\n", strerror(errno));
  56. logfile = NULL;
  57. return retval;
  58. }
  59. int logsetlevel(int loglvl)
  60. {
  61. if(loglvl >= 0 && loglvl < 3)
  62. {
  63. loglevel = loglvl;
  64. return 0;
  65. }
  66. return 1;
  67. }
  68. int logwrite(int loglvl, const char *logfmt, ...)
  69. {
  70. char logmsg[4096];
  71. va_list args;
  72. time_t now;
  73. char timeinfo[32];
  74. if(logfile == NULL)
  75. return 1;
  76. if(loglvl < 0 || loglvl > loglevel)
  77. return 0;
  78. time(&now);
  79. strftime(timeinfo, sizeof(timeinfo), "%c", localtime(&now));
  80. va_start(args, logfmt);
  81. vsprintf(logmsg, logfmt, args);
  82. if(logmsg[strlen(logmsg)-1] == '\n')
  83. logmsg[strlen(logmsg)-1] = '\0';
  84. if(fprintf(logfile, "[%s] %s - %s\n", timeinfo, loglevelnames[loglvl], logmsg) < 0)
  85. {
  86. va_end(args);
  87. return 1;
  88. }
  89. if(fprintf(stdout, "[%s] %s - %s\n", timeinfo, loglevelnames[loglvl], logmsg) < 0)
  90. {
  91. va_end(args);
  92. return 1;
  93. }
  94. va_end(args);
  95. fflush(logfile);
  96. return 0;
  97. }