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