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