dnclog.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*********************************************************************
  2. * Copyright 2010, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header$
  5. *********************************************************************/
  6. #include "config.h"
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include "nclog.h"
  13. #define PREFIXLEN 8
  14. #define MAXTAGS 256
  15. #define NCTAGDFALT "Log";
  16. static int ncinitlog = 0;
  17. static int nclogging = 0;
  18. static int ncsystemfile = 0;
  19. static char* nclogfile = NULL;
  20. static FILE* nclogstream = NULL;
  21. static int nctagsize = 0;
  22. static char** nctagset = NULL;
  23. static char* nctagdfalt = NULL;
  24. static char* nctagsetdfalt[] = {"Warning","Error","Note","Debug"};
  25. static char* nctagname(int tag);
  26. void
  27. ncloginit(void)
  28. {
  29. ncinitlog = 1;
  30. ncsetlogging(0);
  31. nclogfile = NULL;
  32. nclogstream = NULL;
  33. /* Use environment variables to preset nclogging state*/
  34. /* I hope this is portable*/
  35. if(getenv(ENVFLAG) != NULL) {
  36. const char* file = getenv(ENVFLAG);
  37. ncsetlogging(1);
  38. nclogopen(file);
  39. }
  40. nctagdfalt = NCTAGDFALT;
  41. nctagset = nctagsetdfalt;
  42. }
  43. void
  44. ncsetlogging(int tf)
  45. {
  46. if(!ncinitlog) ncloginit();
  47. nclogging = tf;
  48. }
  49. void
  50. nclogopen(const char* file)
  51. {
  52. if(!ncinitlog) ncloginit();
  53. nclogclose();
  54. if(file == NULL || strlen(file) == 0) {
  55. /* use stderr*/
  56. nclogstream = stderr;
  57. nclogfile = NULL;
  58. ncsystemfile = 1;
  59. } else if(strcmp(file,"stdout") == 0) {
  60. /* use stdout*/
  61. nclogstream = stdout;
  62. nclogfile = NULL;
  63. ncsystemfile = 1;
  64. } else if(strcmp(file,"stderr") == 0) {
  65. /* use stderr*/
  66. nclogstream = stderr;
  67. nclogfile = NULL;
  68. ncsystemfile = 1;
  69. } else {
  70. int fd;
  71. nclogfile = strdup(file);
  72. nclogstream = NULL;
  73. /* We need to deal with this file carefully
  74. to avoid unauthorized access*/
  75. fd = open(nclogfile,O_WRONLY|O_APPEND|O_CREAT,0600);
  76. if(fd >= 0) {
  77. nclogstream = fdopen(fd,"a");
  78. } else {
  79. free(nclogfile);
  80. nclogfile = NULL;
  81. nclogstream = NULL;
  82. ncsetlogging(0);
  83. }
  84. ncsystemfile = 0;
  85. }
  86. }
  87. void
  88. nclogclose(void)
  89. {
  90. if(nclogstream != NULL && !ncsystemfile) {
  91. fclose(nclogstream);
  92. }
  93. if(nclogfile != NULL) free(nclogfile);
  94. nclogstream = NULL;
  95. nclogfile = NULL;
  96. ncsystemfile = 0;
  97. }
  98. void
  99. nclog(int tag, const char* fmt, ...)
  100. {
  101. va_list args;
  102. char* prefix;
  103. if(!nclogging || nclogstream == NULL) return;
  104. prefix = nctagname(tag);
  105. fprintf(nclogstream,"%s:",prefix);
  106. if(fmt != NULL) {
  107. va_start(args, fmt);
  108. vfprintf(nclogstream, fmt, args);
  109. va_end( args );
  110. }
  111. fprintf(nclogstream, "\n" );
  112. fflush(nclogstream);
  113. }
  114. void
  115. nclogtext(int tag, const char* text)
  116. {
  117. nclogtextn(tag,text,strlen(text));
  118. }
  119. void
  120. nclogtextn(int tag, const char* text, size_t count)
  121. {
  122. if(!nclogging || nclogstream == NULL) return;
  123. fwrite(text,1,count,nclogstream);
  124. fflush(nclogstream);
  125. }
  126. /* The tagset is null terminated */
  127. void
  128. nclogsettags(char** tagset, char* dfalt)
  129. {
  130. nctagdfalt = dfalt;
  131. if(tagset == NULL) {
  132. nctagsize = 0;
  133. } else {
  134. int i;
  135. /* Find end of the tagset */
  136. for(i=0;i<MAXTAGS;i++) {if(tagset[i]==NULL) break;}
  137. nctagsize = i;
  138. }
  139. nctagset = tagset;
  140. }
  141. static char*
  142. nctagname(int tag)
  143. {
  144. if(tag < 0 || tag >= nctagsize) {
  145. return nctagdfalt;
  146. } else {
  147. return nctagset[tag];
  148. }
  149. }