OC Updated for version 2.0
|
00001 /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc. 00002 See the COPYRIGHT file for more information. */ 00003 00004 #include "config.h" 00005 00006 #include "ocinternal.h" 00007 #include <stdio.h> 00008 #include <fcntl.h> 00009 00010 #define PREFIXLEN 8 00011 00012 #define ENVFLAG "OCLOGFILE" 00013 00014 static int ocloginit = 0; 00015 static int oclogging = 0; 00016 static char* oclogfile = NULL; 00017 static FILE* oclogstream = NULL; 00018 00025 void 00026 oc_loginit(void) 00027 { 00028 ocloginit = 1; 00029 oc_setlogging(0); 00030 oclogfile = NULL; 00031 oclogstream = NULL; 00032 /* Use environment variables to preset oclogging state*/ 00033 /* I hope this is portable*/ 00034 if(getenv(ENVFLAG) != NULL) { 00035 const char* file = getenv(ENVFLAG); 00036 oc_setlogging(1); 00037 oc_logopen(file); 00038 } 00039 } 00040 00049 int 00050 oc_setlogging(int tf) 00051 { 00052 if(!ocloginit) oc_loginit(); 00053 int was = oclogging; 00054 oclogging = tf; 00055 return was; 00056 } 00057 00068 int 00069 oc_logopen(const char* file) 00070 { 00071 if(!ocloginit) oc_loginit(); 00072 if(oclogfile != NULL) { 00073 fclose(oclogstream); 00074 free(oclogfile); 00075 oclogfile = NULL; 00076 return 0; 00077 } 00078 if(file == NULL || strlen(file) == 0) { 00079 /* use stderr*/ 00080 oclogstream = stderr; 00081 oclogfile = NULL; 00082 } else { 00083 int fd; 00084 oclogfile = (char*)malloc(strlen(file)+1); 00085 strcpy(oclogfile,file); 00086 oclogstream = NULL; 00087 /* We need to deal with this file carefully 00088 to avoid unauthorized access*/ 00089 fd = open(oclogfile,O_WRONLY|O_APPEND|O_CREAT,0600); 00090 if(fd >= 0) { 00091 oclogstream = fdopen(fd,"a"); 00092 } else { 00093 free(oclogfile); 00094 oclogfile = NULL; 00095 oc_setlogging(0); 00096 return 0; 00097 } 00098 } 00099 return 1; 00100 } 00101 00107 void 00108 oc_logclose(void) 00109 { 00110 if(oclogfile != NULL && oclogstream != NULL) { 00111 fclose(oclogstream); 00112 oclogstream = NULL; 00113 if(oclogfile != NULL) free(oclogfile); 00114 oclogfile = NULL; 00115 } 00116 } 00117 00127 void 00128 oc_log(int tag, const char* format, ...) 00129 { 00130 va_list args; 00131 char* prefix; 00132 if(!oclogging || oclogstream == NULL) return; 00133 00134 switch (tag) { 00135 case LOGWARN: prefix = "Warning:"; break; 00136 case LOGERR: prefix = "Error: "; break; 00137 case LOGNOTE: prefix = "Note: "; break; 00138 case LOGDBG: prefix = "Debug: "; break; 00139 default: 00140 fprintf(oclogstream,"Error: Bad log prefix: %d\n",tag); 00141 prefix = "Error: "; 00142 break; 00143 } 00144 fprintf(oclogstream,"%s:",prefix); 00145 00146 if(format != NULL) { 00147 va_start(args, format); 00148 vfprintf(oclogstream, format, args); 00149 va_end( args ); 00150 } 00151 fprintf(oclogstream, "\n" ); 00152 fflush(oclogstream); 00153 } 00154 00163 void 00164 oc_logtext(int tag, const char* text) 00165 { 00166 char line[1024]; 00167 size_t delta = 0; 00168 const char* eol = text; 00169 00170 if(!oclogging || oclogstream == NULL) return; 00171 00172 while(*text) { 00173 eol = strchr(text,'\n'); 00174 if(eol == NULL) 00175 delta = strlen(text); 00176 else 00177 delta = (eol - text); 00178 if(delta > 0) memcpy(line,text,delta); 00179 line[delta] = '\0'; 00180 fprintf(oclogstream," %s\n",line); 00181 text = eol+1; 00182 } 00183 } 00184