oclog.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #include "ocinternal.h"
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #define PREFIXLEN 8
  8. #define ENVFLAG "OCLOGFILE"
  9. static int ocloginit = 0;
  10. static int oclogging = 0;
  11. static char* oclogfile = NULL;
  12. static FILE* oclogstream = NULL;
  13. void
  14. oc_loginit(void)
  15. {
  16. ocloginit = 1;
  17. oc_setlogging(0);
  18. oclogfile = NULL;
  19. oclogstream = NULL;
  20. /* Use environment variables to preset oclogging state*/
  21. /* I hope this is portable*/
  22. if(getenv(ENVFLAG) != NULL) {
  23. const char* file = getenv(ENVFLAG);
  24. oc_setlogging(1);
  25. oc_logopen(file);
  26. }
  27. }
  28. void
  29. oc_setlogging(int tf)
  30. {
  31. if(!ocloginit) oc_loginit();
  32. oclogging = tf;
  33. }
  34. void
  35. oc_logopen(const char* file)
  36. {
  37. if(!ocloginit) oc_loginit();
  38. if(oclogfile != NULL) {
  39. fclose(oclogstream);
  40. free(oclogfile);
  41. oclogfile = NULL;
  42. }
  43. if(file == NULL || strlen(file) == 0) {
  44. /* use stderr*/
  45. oclogstream = stderr;
  46. oclogfile = NULL;
  47. } else {
  48. int fd;
  49. oclogfile = (char*)malloc(strlen(file)+1);
  50. strcpy(oclogfile,file);
  51. oclogstream = NULL;
  52. /* We need to deal with this file carefully
  53. to avoid unauthorized access*/
  54. fd = open(oclogfile,O_WRONLY|O_APPEND|O_CREAT,0600);
  55. if(fd >= 0) {
  56. oclogstream = fdopen(fd,"a");
  57. } else {
  58. free(oclogfile);
  59. oclogfile = NULL;
  60. oc_setlogging(0);
  61. }
  62. }
  63. }
  64. void
  65. oc_logclose(void)
  66. {
  67. if(oclogfile != NULL && oclogstream != NULL) {
  68. fclose(oclogstream);
  69. oclogstream = NULL;
  70. if(oclogfile != NULL) free(oclogfile);
  71. oclogfile = NULL;
  72. }
  73. }
  74. void
  75. oc_log(int tag, const char* fmt, ...)
  76. {
  77. va_list args;
  78. char* prefix;
  79. if(!oclogging || oclogstream == NULL) return;
  80. switch (tag) {
  81. case LOGWARN: prefix = "Warning:"; break;
  82. case LOGERR: prefix = "Error: "; break;
  83. case LOGNOTE: prefix = "Note: "; break;
  84. case LOGDBG: prefix = "Debug: "; break;
  85. default:
  86. fprintf(oclogstream,"Error: Bad log prefix: %d\n",tag);
  87. prefix = "Error: ";
  88. break;
  89. }
  90. fprintf(oclogstream,"%s:",prefix);
  91. if(fmt != NULL) {
  92. va_start(args, fmt);
  93. vfprintf(oclogstream, fmt, args);
  94. va_end( args );
  95. }
  96. fprintf(oclogstream, "\n" );
  97. fflush(oclogstream);
  98. }
  99. void
  100. oc_logtext(int tag, const char* text)
  101. {
  102. char line[1024];
  103. size_t delta = 0;
  104. const char* eol = text;
  105. if(!oclogging || oclogstream == NULL) return;
  106. while(*text) {
  107. eol = strchr(text,'\n');
  108. if(eol == NULL)
  109. delta = strlen(text);
  110. else
  111. delta = (eol - text);
  112. if(delta > 0) memcpy(line,text,delta);
  113. line[delta] = '\0';
  114. fprintf(oclogstream," %s\n",line);
  115. text = eol+1;
  116. }
  117. }