/[pdpsoft]/trunk/grid-mw-security/cgul/log_to_file/log_to_file.c
ViewVC logotype

Contents of /trunk/grid-mw-security/cgul/log_to_file/log_to_file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1782 - (show annotations) (download) (as text)
Wed Jun 16 09:22:25 2010 UTC (11 years, 7 months ago) by aramv
File MIME type: text/x-chdr
File size: 5326 byte(s)
Not logging debug messages if debug flag was not set
1 #include "log_to_file.h"
2
3 /* example
4 int main(void){
5 cgul_openlog("/tmp/foo");
6 cgul_set_ident("test");
7 cgul_set_log_options(CGUL_SYSLOG | CGUL_LOGFILE | CGUL_STDERR);
8 cgul_log(LOG_ERR, "Foo %s\n", "bar");
9 cgul_closelog();
10 return 0;
11 }
12 */
13
14 /**
15 * Internal function - Opens log file
16 */
17 int cgul_openlog(char* file){
18 pid_t _pid = getpid();
19 openlog(file, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
20 my_asprintf(&pid_str, "%llu", (long long unsigned int) _pid);
21 if((log_file_fp = fopen(file, "a"))){
22 return 0;
23 } else {
24 return -1;
25 }
26 }
27
28 void cgul_set_ident(char* ident){
29 ident_str = ident;
30 }
31
32 /**
33 * Internal function - Flush log file
34 */
35 int cgul_flushlog(){
36 return fflush(log_file_fp);
37 }
38
39 /**
40 * Internal function - Closes log file
41 */
42 int cgul_closelog(void){
43 free(pid_str);
44 return fclose(log_file_fp);
45 }
46
47 /**
48 * Sets the current logging options as a bitmask
49 */
50 void cgul_set_log_options(CGUL_LOGMASK mask){
51 log_options |= mask;
52 }
53
54 /**
55 * Returns the current logging options as a bitmask
56 */
57 CGUL_LOGMASK cgul_get_log_options(){
58 return log_options;
59 }
60
61 /**
62 * Main log function. Follows syslog() prototype. Depending on which log flags are set through cgul_set_log_options will log to either stderr, syslog and/or a logfile
63 */
64 void cgul_log(int priority, const char* format, ...){
65 va_list args;
66 char * _format = append_newline(format);
67
68 /* error logging */
69 if(log_options & CGUL_STDERR){
70 va_start(args, format);
71 if((priority == LOG_ERR) || (priority == LOG_WARNING)){
72 vfprintf(stderr, _format, args);
73 }
74 va_end(args);
75 }
76
77 /* debug logging */
78 if(log_options & CGUL_STDDEBUG){
79 va_start(args, format);
80 if(priority == LOG_DEBUG){
81 vfprintf(stderr, _format, args);
82 }
83 va_end(args);
84 } else {
85 /* if running in non-debug mode */
86 if(priority > LOG_INFO){
87 /* and priority is debug or info */
88 goto end;
89 }
90
91 }
92
93 /* Default output */
94 if(log_options & CGUL_SYSLOG){
95 va_start(args, format);
96 vsyslog(priority, format, args);
97 va_end(args);
98 }
99
100 if(log_options & CGUL_LOGFILE){
101 va_start(args, format);
102 log_to_file(priority, format, args);
103 va_end(args);
104 }
105
106 end:
107 /* if the format string was changed by appending, it needs to be free'd */
108 if(format != _format){
109 free(_format);
110 }
111
112 /* if the format string was changed by stripping, it needs to be free'd */
113 return;
114 }
115
116 /**
117 * Log to file function. Follows vsyslog() prototype
118 */
119 void log_to_file(int priority, const char* format, va_list ap){
120 time_t _time = 0;
121 struct tm * _time_s = NULL;
122 char * _output = NULL;
123 char _strf_fmt[MAX_TIME_STRING_SIZE];
124 char * _format = append_newline(format);
125 char _hostname[1024];
126 int _max_format_string_length;
127
128 _time = time(NULL);
129 _time_s = localtime(&_time);
130 strftime(_strf_fmt, MAX_TIME_STRING_SIZE-1, "%b %d %H:%M:%S", _time_s);
131 gethostname(_hostname, 1023);
132
133 /* concat timestamp and format string */
134 if((_output = calloc((strlen(_strf_fmt)+1+strlen(_hostname)+1+strlen(ident_str)+1+strlen(pid_str)+3+strlen(_format)+1),sizeof(char)))){
135 strncpy(_output, _strf_fmt, strlen(_strf_fmt));
136 strncat(_output, " ", 1);
137 strncat(_output, _hostname, strlen(_hostname));
138 strncat(_output, " ", 1);
139 strncat(_output, ident_str, strlen(ident_str));
140 strncat(_output, "[", 1);
141 strncat(_output, pid_str, strlen(pid_str));
142 strncat(_output, "]: ", 3);
143 _max_format_string_length = (MAX_LOG_MESSAGE_SIZE - strlen(_strf_fmt) - 1 - strlen(_hostname) - 1 - strlen(ident_str) - 1 - strlen(pid_str) - 3);
144 strncat(_output, _format, _max_format_string_length);
145 vfprintf(log_file_fp, _output, ap);
146 cgul_flushlog();
147 free(_output);
148 }
149 if(format != _format){
150 free(_format);
151 }
152 }
153
154 /**
155 * Returns a new string which is a copy of the supplied string with newline appended
156 */
157 char* append_newline(const char* string){
158 char* mutable_string = NULL;
159 size_t string_length = strlen(string);
160
161 if(string[string_length-1] != '\n'){
162 if((mutable_string = calloc((string_length+2),sizeof(char)))){
163 strncpy(mutable_string, string, string_length);
164 strncat(mutable_string, "\n", 1);
165 }
166 } else {
167 mutable_string = strdup(string);
168 }
169 return mutable_string;
170 }
171
172 /**
173 * Returns a new string which is a copy of the supplied string with the newline stripped off
174 */
175 char* strip_newline(const char* string){
176 char* mutable_string = NULL;
177 size_t string_length = strlen(string);
178
179 if((sscanf(string, "\n")) > 0){
180 /*if(string[strlen(string)-1] == '\n'){*/
181 if((mutable_string = calloc((string_length),sizeof(char)))){
182 strncpy(mutable_string, string, string_length);
183 }
184 } else {
185 mutable_string = strdup(string);
186 }
187 return mutable_string;
188 }
189
190 int my_asprintf(char **strp, const char* fmt, ...){
191 size_t _str_len = 0;
192 va_list args;
193 va_start(args, fmt);
194 if(args){
195 _str_len = vsnprintf(NULL, 0, fmt, args);
196 } else {
197 _str_len = snprintf(NULL, 0, fmt);
198 }
199 if((*strp = calloc(1, sizeof(char) * (_str_len+1)))){
200 if(args){
201 vsnprintf(*strp, _str_len+1, fmt, args);
202 } else {
203 snprintf(*strp, _str_len+1, fmt);
204 }
205 } else {
206 va_end(args);
207 return -1;
208 }
209 va_end(args);
210 return 0;
211 }
212
213

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28